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