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