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