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