Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 4 | import webnotes, os, datetime |
| 5 | import webnotes.utils |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 6 | from webnotes.utils import random_string |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 7 | from webnotes.widgets import query_report |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 8 | import random |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 9 | import json |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 11 | webnotes.session = webnotes._dict({"user":"Administrator"}) |
| 12 | from core.page.data_import_tool.data_import_tool import upload |
| 13 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 14 | # fix price list |
| 15 | # fix fiscal year |
| 16 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 17 | company = "Wind Power LLC" |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 18 | company_abbr = "WP" |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 19 | country = "United States" |
| 20 | currency = "USD" |
| 21 | time_zone = "America/New York" |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 22 | start_date = '2010-01-01' |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 23 | bank_name = "Citibank" |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 24 | runs_for = 20 |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 25 | prob = { |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 26 | "default": { "make": 0.6, "qty": (1,5) }, |
| 27 | "Purchase Order": { "make": 0.7, "qty": (1,15) }, |
| 28 | "Purchase Receipt": { "make": 0.7, "qty": (1,15) }, |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 29 | } |
| 30 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 31 | def make(reset=False): |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 32 | webnotes.connect() |
Rushabh Mehta | 2b76a5e | 2013-08-02 15:16:59 +0530 | [diff] [blame] | 33 | webnotes.print_messages = True |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 34 | webnotes.mute_emails = True |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 35 | webnotes.rollback_on_exception = True |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 36 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 37 | if reset: |
| 38 | setup() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 39 | simulate() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 40 | |
| 41 | def setup(): |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 42 | install() |
| 43 | complete_setup() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 44 | make_customers_suppliers_contacts() |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 45 | make_items() |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 46 | make_users_and_employees() |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 47 | make_bank_account() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 48 | # make_opening_stock() |
| 49 | # make_opening_accounts() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 50 | |
| 51 | def simulate(): |
| 52 | current_date = None |
| 53 | for i in xrange(runs_for): |
| 54 | if not current_date: |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 55 | # get last stock ledger posting date or use default |
| 56 | last_posting = webnotes.conn.sql("""select max(posting_date) from `tabStock Ledger Entry`""") |
| 57 | if last_posting[0][0]: |
| 58 | current_date = webnotes.utils.add_days(last_posting[0][0], 1) |
| 59 | else: |
| 60 | current_date = webnotes.utils.getdate(start_date) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 61 | else: |
| 62 | current_date = webnotes.utils.add_days(current_date, 1) |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 63 | |
| 64 | print current_date.strftime("%Y-%m-%d") |
| 65 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 66 | if current_date.weekday() in (5, 6): |
| 67 | continue |
| 68 | |
| 69 | run_sales(current_date) |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 70 | run_purchase(current_date) |
| 71 | run_manufacturing(current_date) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 72 | run_stock(current_date) |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 73 | run_accounts(current_date) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 74 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 75 | def run_sales(current_date): |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 76 | if can_make("Quotation"): |
| 77 | for i in xrange(how_many("Quotation")): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 78 | make_quotation(current_date) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 79 | |
| 80 | if can_make("Sales Order"): |
| 81 | for i in xrange(how_many("Sales Order")): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 82 | make_sales_order(current_date) |
| 83 | |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 84 | def run_accounts(current_date): |
| 85 | if can_make("Sales Invoice"): |
| 86 | from selling.doctype.sales_order.sales_order import make_sales_invoice |
| 87 | report = "Ordered Items to be Billed" |
| 88 | for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Sales Invoice")]: |
| 89 | si = webnotes.bean(make_sales_invoice(so)) |
| 90 | si.doc.posting_date = current_date |
| 91 | si.insert() |
| 92 | si.submit() |
| 93 | webnotes.conn.commit() |
| 94 | |
| 95 | if can_make("Purchase Invoice"): |
| 96 | from stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice |
| 97 | report = "Received Items to be Billed" |
| 98 | for pr in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Invoice")]: |
| 99 | pi = webnotes.bean(make_purchase_invoice(pr)) |
| 100 | pi.doc.posting_date = current_date |
| 101 | pi.doc.bill_no = random_string(6) |
| 102 | pi.insert() |
| 103 | pi.submit() |
| 104 | webnotes.conn.commit() |
| 105 | |
| 106 | if can_make("Payment Received"): |
| 107 | from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_sales_invoice |
| 108 | report = "Accounts Receivable" |
| 109 | for si in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Sales Invoice"]))[:how_many("Payment Received")]: |
| 110 | jv = webnotes.bean(get_payment_entry_from_sales_invoice(si)) |
| 111 | jv.doc.posting_date = current_date |
| 112 | jv.doc.cheque_no = random_string(6) |
| 113 | jv.doc.cheque_date = current_date |
| 114 | jv.insert() |
| 115 | jv.submit() |
| 116 | webnotes.conn.commit() |
| 117 | |
| 118 | if can_make("Payment Made"): |
| 119 | from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_purchase_invoice |
| 120 | report = "Accounts Payable" |
| 121 | for pi in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Purchase Invoice"]))[:how_many("Payment Made")]: |
| 122 | jv = webnotes.bean(get_payment_entry_from_purchase_invoice(pi)) |
| 123 | jv.doc.posting_date = current_date |
| 124 | jv.doc.cheque_no = random_string(6) |
| 125 | jv.doc.cheque_date = current_date |
| 126 | jv.insert() |
| 127 | jv.submit() |
| 128 | webnotes.conn.commit() |
| 129 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 130 | def run_stock(current_date): |
| 131 | # make purchase requests |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 132 | if can_make("Purchase Receipt"): |
| 133 | from buying.doctype.purchase_order.purchase_order import make_purchase_receipt |
| 134 | report = "Purchase Order Items To Be Received" |
| 135 | for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]: |
| 136 | pr = webnotes.bean(make_purchase_receipt(po)) |
| 137 | pr.doc.posting_date = current_date |
| 138 | pr.doc.fiscal_year = "2010" |
| 139 | pr.insert() |
| 140 | pr.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 141 | webnotes.conn.commit() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 142 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 143 | # make delivery notes (if possible) |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 144 | if can_make("Delivery Note"): |
| 145 | from selling.doctype.sales_order.sales_order import make_delivery_note |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 146 | from stock.stock_ledger import NegativeStockError |
| 147 | from stock.doctype.stock_ledger_entry.stock_ledger_entry import SerialNoRequiredError, SerialNoQtyError |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 148 | report = "Ordered Items To Be Delivered" |
| 149 | for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]: |
| 150 | dn = webnotes.bean(make_delivery_note(so)) |
| 151 | dn.doc.posting_date = current_date |
| 152 | dn.doc.fiscal_year = "2010" |
| 153 | dn.insert() |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 154 | try: |
| 155 | dn.submit() |
| 156 | webnotes.conn.commit() |
| 157 | except NegativeStockError: pass |
| 158 | except SerialNoRequiredError: pass |
| 159 | except SerialNoQtyError: pass |
| 160 | |
| 161 | # try submitting existing |
| 162 | for dn in webnotes.conn.get_values("Delivery Note", {"docstatus": 0}, "name"): |
| 163 | b = webnotes.bean("Delivery Note", dn[0]) |
| 164 | b.submit() |
| 165 | webnotes.conn.commit() |
| 166 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 167 | def run_purchase(current_date): |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 168 | # make material requests for purchase items that have negative projected qtys |
| 169 | if can_make("Material Request"): |
| 170 | report = "Items To Be Requested" |
| 171 | for row in query_report.run(report)["result"][:how_many("Material Request")]: |
| 172 | mr = webnotes.new_bean("Material Request") |
| 173 | mr.doc.material_request_type = "Purchase" |
| 174 | mr.doc.transaction_date = current_date |
| 175 | mr.doc.fiscal_year = "2010" |
| 176 | mr.doclist.append({ |
| 177 | "doctype": "Material Request Item", |
| 178 | "parentfield": "indent_details", |
| 179 | "schedule_date": webnotes.utils.add_days(current_date, 7), |
| 180 | "item_code": row[0], |
| 181 | "qty": -row[-1] |
| 182 | }) |
| 183 | mr.insert() |
| 184 | mr.submit() |
| 185 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 186 | # make supplier quotations |
| 187 | if can_make("Supplier Quotation"): |
| 188 | from stock.doctype.material_request.material_request import make_supplier_quotation |
| 189 | report = "Material Requests for which Supplier Quotations are not created" |
| 190 | for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]: |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 191 | if row[0] != "Total": |
| 192 | sq = webnotes.bean(make_supplier_quotation(row[0])) |
| 193 | sq.doc.transaction_date = current_date |
| 194 | sq.doc.fiscal_year = "2010" |
| 195 | sq.insert() |
| 196 | sq.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 197 | webnotes.conn.commit() |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 198 | |
| 199 | # make purchase orders |
| 200 | if can_make("Purchase Order"): |
| 201 | from stock.doctype.material_request.material_request import make_purchase_order |
| 202 | report = "Requested Items To Be Ordered" |
| 203 | for row in query_report.run(report)["result"][:how_many("Purchase Order")]: |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 204 | if row[0] != "Total": |
| 205 | po = webnotes.bean(make_purchase_order(row[0])) |
| 206 | po.doc.transaction_date = current_date |
| 207 | po.doc.fiscal_year = "2010" |
| 208 | po.insert() |
| 209 | po.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 210 | webnotes.conn.commit() |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 211 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 212 | def run_manufacturing(current_date): |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 213 | from stock.stock_ledger import NegativeStockError |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 214 | from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 215 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 216 | ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool") |
| 217 | ppt.doc.company = company |
| 218 | ppt.doc.use_multi_level_bom = 1 |
| 219 | ppt.doc.purchase_request_for_warehouse = "Stores - WP" |
| 220 | ppt.run_method("get_open_sales_orders") |
| 221 | ppt.run_method("get_items_from_so") |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 222 | ppt.run_method("raise_production_order") |
| 223 | ppt.run_method("raise_purchase_request") |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 224 | webnotes.conn.commit() |
Rushabh Mehta | 2c968ce | 2013-08-08 14:16:39 +0530 | [diff] [blame] | 225 | |
| 226 | # submit production orders |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 227 | for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}, "name"): |
Rushabh Mehta | 2c968ce | 2013-08-08 14:16:39 +0530 | [diff] [blame] | 228 | b = webnotes.bean("Production Order", pro[0]) |
| 229 | b.doc.wip_warehouse = "Work in Progress - WP" |
| 230 | b.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 231 | webnotes.conn.commit() |
Rushabh Mehta | 75c3171 | 2013-08-08 16:00:40 +0530 | [diff] [blame] | 232 | |
| 233 | # submit material requests |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 234 | for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}, "name"): |
Rushabh Mehta | 75c3171 | 2013-08-08 16:00:40 +0530 | [diff] [blame] | 235 | b = webnotes.bean("Material Request", pro[0]) |
| 236 | b.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 237 | webnotes.conn.commit() |
Rushabh Mehta | 2c968ce | 2013-08-08 14:16:39 +0530 | [diff] [blame] | 238 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 239 | # stores -> wip |
| 240 | if can_make("Stock Entry for WIP"): |
| 241 | for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]: |
| 242 | make_stock_entry_from_pro(pro[0], "Material Transfer", current_date) |
| 243 | |
| 244 | # wip -> fg |
| 245 | if can_make("Stock Entry for FG"): |
| 246 | for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]: |
| 247 | make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date) |
| 248 | |
| 249 | # try posting older drafts (if exists) |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 250 | for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}, "name"): |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 251 | try: |
| 252 | webnotes.bean("Stock Entry", st[0]).submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 253 | webnotes.conn.commit() |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 254 | except NegativeStockError: pass |
| 255 | except IncorrectValuationRateError: pass |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 256 | except DuplicateEntryForProductionOrderError: pass |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 257 | |
| 258 | def make_stock_entry_from_pro(pro_id, purpose, current_date): |
| 259 | from manufacturing.doctype.production_order.production_order import make_stock_entry |
| 260 | from stock.stock_ledger import NegativeStockError |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 261 | from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 262 | |
| 263 | st = webnotes.bean(make_stock_entry(pro_id, purpose)) |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 264 | st.doc.posting_date = current_date |
| 265 | st.doc.fiscal_year = "2010" |
| 266 | st.doc.expense_adjustment_account = "Stock in Hand - WP" |
| 267 | try: |
| 268 | st.insert() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 269 | webnotes.conn.commit() |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 270 | st.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 271 | webnotes.conn.commit() |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 272 | except NegativeStockError: pass |
| 273 | except IncorrectValuationRateError: pass |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 274 | except DuplicateEntryForProductionOrderError: pass |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 275 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 276 | def make_quotation(current_date): |
| 277 | b = webnotes.bean([{ |
| 278 | "creation": current_date, |
| 279 | "doctype": "Quotation", |
| 280 | "quotation_to": "Customer", |
| 281 | "customer": get_random("Customer"), |
| 282 | "order_type": "Sales", |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 283 | "transaction_date": current_date, |
| 284 | "fiscal_year": "2010" |
| 285 | }]) |
| 286 | |
| 287 | add_random_children(b, { |
| 288 | "doctype": "Quotation Item", |
| 289 | "parentfield": "quotation_details", |
| 290 | }, rows=3, randomize = { |
| 291 | "qty": (1, 5), |
| 292 | "item_code": ("Item", {"is_sales_item": "Yes"}) |
| 293 | }, unique="item_code") |
| 294 | |
| 295 | b.insert() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 296 | webnotes.conn.commit() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 297 | b.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 298 | webnotes.conn.commit() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 299 | |
| 300 | def make_sales_order(current_date): |
| 301 | q = get_random("Quotation", {"status": "Submitted"}) |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 302 | if q: |
| 303 | from selling.doctype.quotation.quotation import make_sales_order |
| 304 | so = webnotes.bean(make_sales_order(q)) |
| 305 | so.doc.transaction_date = current_date |
| 306 | so.doc.delivery_date = webnotes.utils.add_days(current_date, 10) |
| 307 | so.insert() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 308 | webnotes.conn.commit() |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 309 | so.submit() |
Rushabh Mehta | 6ca903f | 2013-08-13 14:31:15 +0530 | [diff] [blame] | 310 | webnotes.conn.commit() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 311 | |
| 312 | def add_random_children(bean, template, rows, randomize, unique=None): |
| 313 | for i in xrange(random.randrange(1, rows)): |
| 314 | d = template.copy() |
| 315 | for key, val in randomize.items(): |
| 316 | if isinstance(val[0], basestring): |
| 317 | d[key] = get_random(*val) |
| 318 | else: |
| 319 | d[key] = random.randrange(*val) |
| 320 | |
| 321 | if unique: |
| 322 | if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}): |
| 323 | bean.doclist.append(d) |
| 324 | else: |
| 325 | bean.doclist.append(d) |
| 326 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 327 | def get_random(doctype, filters=None): |
| 328 | condition = [] |
| 329 | if filters: |
| 330 | for key, val in filters.items(): |
| 331 | condition.append("%s='%s'" % (key, val)) |
| 332 | if condition: |
| 333 | condition = " where " + " and ".join(condition) |
| 334 | else: |
| 335 | condition = "" |
| 336 | |
| 337 | out = webnotes.conn.sql("""select name from `tab%s` %s |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 338 | order by RAND() limit 0,1""" % (doctype, condition)) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 339 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 340 | return out and out[0][0] or None |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 341 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 342 | def can_make(doctype): |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 343 | return random.random() < prob.get(doctype, prob["default"])["make"] |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 344 | |
| 345 | def how_many(doctype): |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 346 | return random.randrange(*prob.get(doctype, prob["default"])["qty"]) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 347 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 348 | def install(): |
| 349 | print "Creating Fresh Database..." |
| 350 | from webnotes.install_lib.install import Installer |
| 351 | inst = Installer('root') |
| 352 | inst.import_from_db("demo", verbose = 1) |
| 353 | |
| 354 | def complete_setup(): |
| 355 | print "Complete Setup..." |
| 356 | webnotes.get_obj("Setup Control").setup_account({ |
| 357 | "first_name": "Test", |
| 358 | "last_name": "User", |
| 359 | "fy_start": "1st Jan", |
| 360 | "industry": "Manufacturing", |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 361 | "company_name": company, |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 362 | "company_abbr": company_abbr, |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 363 | "currency": currency, |
| 364 | "timezone": time_zone, |
| 365 | "country": country |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 366 | }) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 367 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 368 | import_data("Fiscal_Year") |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 369 | |
| 370 | def make_items(): |
Rushabh Mehta | 20a1094 | 2013-08-20 15:18:42 +0530 | [diff] [blame] | 371 | import_data("Item") |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 372 | import_data("BOM", submit=True) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 373 | |
| 374 | def make_customers_suppliers_contacts(): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 375 | import_data(["Customer", "Supplier", "Contact", "Address", "Lead"]) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 376 | |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 377 | def make_users_and_employees(): |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 378 | webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series") |
| 379 | webnotes.conn.commit() |
| 380 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 381 | import_data(["Profile", "Employee", "Salary_Structure"]) |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 382 | |
| 383 | def make_bank_account(): |
| 384 | ba = webnotes.bean({ |
| 385 | "doctype": "Account", |
| 386 | "account_name": bank_name, |
| 387 | "account_type": "Bank or Cash", |
| 388 | "group_or_ledger": "Ledger", |
| 389 | "parent_account": "Bank Accounts - " + company_abbr, |
| 390 | "company": company |
| 391 | }).insert() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 392 | |
Rushabh Mehta | 622c98d | 2013-08-22 15:17:58 +0530 | [diff] [blame] | 393 | webnotes.set_value("Company", company, "default_bank_account", ba.doc.name) |
| 394 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 395 | def import_data(dt, submit=False): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 396 | if not isinstance(dt, (tuple, list)): |
| 397 | dt = [dt] |
| 398 | |
| 399 | for doctype in dt: |
| 400 | print "Importing", doctype.replace("_", " "), "..." |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 401 | webnotes.form_dict = {} |
| 402 | if submit: |
| 403 | webnotes.form_dict["params"] = json.dumps({"_submit": 1}) |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 404 | webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv") |
| 405 | upload() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 406 | |
| 407 | if __name__=="__main__": |
| 408 | make() |