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 | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 6 | from webnotes.widgets import query_report |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 7 | import random |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 8 | import json |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 10 | webnotes.session = webnotes._dict({"user":"Administrator"}) |
| 11 | from core.page.data_import_tool.data_import_tool import upload |
| 12 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 13 | # fix price list |
| 14 | # fix fiscal year |
| 15 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 16 | company = "Wind Power LLC" |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 17 | start_date = '2010-01-01' |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 18 | runs_for = 20 |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 19 | prob = { |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 20 | "Quotation": { "make": 0.5, "qty": (1,5) }, |
| 21 | "Sales Order": { "make": 0.5, "qty": (1,4) }, |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 22 | "Purchase Order": { "make": 0.7, "qty": (1,4) }, |
| 23 | "Purchase Receipt": { "make": 0.7, "qty": (1,4) }, |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 24 | "Supplier Quotation": { "make": 0.5, "qty": (1, 3) } |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 25 | } |
| 26 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 27 | def make(reset=False): |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 28 | webnotes.connect() |
Rushabh Mehta | 2b76a5e | 2013-08-02 15:16:59 +0530 | [diff] [blame] | 29 | webnotes.print_messages = True |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 30 | webnotes.mute_emails = True |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 31 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 32 | if reset: |
| 33 | setup() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 34 | simulate() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 35 | |
| 36 | def setup(): |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 37 | install() |
| 38 | complete_setup() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 39 | make_customers_suppliers_contacts() |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 40 | make_items() |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 41 | make_users_and_employees() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 42 | # make_opening_stock() |
| 43 | # make_opening_accounts() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 44 | |
| 45 | def simulate(): |
| 46 | current_date = None |
| 47 | for i in xrange(runs_for): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 48 | print i |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 49 | if not current_date: |
| 50 | current_date = webnotes.utils.getdate(start_date) |
| 51 | else: |
| 52 | current_date = webnotes.utils.add_days(current_date, 1) |
| 53 | |
| 54 | if current_date.weekday() in (5, 6): |
| 55 | continue |
| 56 | |
| 57 | run_sales(current_date) |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 58 | run_purchase(current_date) |
| 59 | run_manufacturing(current_date) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 60 | run_stock(current_date) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 61 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 62 | webnotes.conn.commit() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 63 | |
| 64 | def run_sales(current_date): |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 65 | if can_make("Quotation"): |
| 66 | for i in xrange(how_many("Quotation")): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 67 | make_quotation(current_date) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 68 | |
| 69 | if can_make("Sales Order"): |
| 70 | for i in xrange(how_many("Sales Order")): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 71 | make_sales_order(current_date) |
| 72 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 73 | def run_stock(current_date): |
| 74 | # make purchase requests |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 75 | if can_make("Purchase Receipt"): |
| 76 | from buying.doctype.purchase_order.purchase_order import make_purchase_receipt |
| 77 | report = "Purchase Order Items To Be Received" |
| 78 | for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]: |
| 79 | pr = webnotes.bean(make_purchase_receipt(po)) |
| 80 | pr.doc.posting_date = current_date |
| 81 | pr.doc.fiscal_year = "2010" |
| 82 | pr.insert() |
| 83 | pr.submit() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 84 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 85 | # make delivery notes (if possible) |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 86 | if can_make("Delivery Note"): |
| 87 | from selling.doctype.sales_order.sales_order import make_delivery_note |
| 88 | report = "Ordered Items To Be Delivered" |
| 89 | for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]: |
| 90 | dn = webnotes.bean(make_delivery_note(so)) |
| 91 | dn.doc.posting_date = current_date |
| 92 | dn.doc.fiscal_year = "2010" |
| 93 | dn.insert() |
| 94 | dn.submit() |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 95 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 96 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 97 | def run_purchase(current_date): |
| 98 | # make supplier quotations |
| 99 | if can_make("Supplier Quotation"): |
| 100 | from stock.doctype.material_request.material_request import make_supplier_quotation |
| 101 | report = "Material Requests for which Supplier Quotations are not created" |
| 102 | for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]: |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 103 | if row[0] != "Total": |
| 104 | sq = webnotes.bean(make_supplier_quotation(row[0])) |
| 105 | sq.doc.transaction_date = current_date |
| 106 | sq.doc.fiscal_year = "2010" |
| 107 | sq.insert() |
| 108 | sq.submit() |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 109 | |
| 110 | # make purchase orders |
| 111 | if can_make("Purchase Order"): |
| 112 | from stock.doctype.material_request.material_request import make_purchase_order |
| 113 | report = "Requested Items To Be Ordered" |
| 114 | for row in query_report.run(report)["result"][:how_many("Purchase Order")]: |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 115 | if row[0] != "Total": |
| 116 | po = webnotes.bean(make_purchase_order(row[0])) |
| 117 | po.doc.transaction_date = current_date |
| 118 | po.doc.fiscal_year = "2010" |
| 119 | po.insert() |
| 120 | po.submit() |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 121 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 122 | def run_manufacturing(current_date): |
| 123 | ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool") |
| 124 | ppt.doc.company = company |
| 125 | ppt.doc.use_multi_level_bom = 1 |
| 126 | ppt.doc.purchase_request_for_warehouse = "Stores - WP" |
| 127 | ppt.run_method("get_open_sales_orders") |
| 128 | ppt.run_method("get_items_from_so") |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 129 | ppt.run_method("raise_production_order") |
| 130 | ppt.run_method("raise_purchase_request") |
Rushabh Mehta | 2c968ce | 2013-08-08 14:16:39 +0530 | [diff] [blame] | 131 | |
| 132 | # submit production orders |
| 133 | for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}): |
| 134 | b = webnotes.bean("Production Order", pro[0]) |
| 135 | b.doc.wip_warehouse = "Work in Progress - WP" |
| 136 | b.submit() |
Rushabh Mehta | 75c3171 | 2013-08-08 16:00:40 +0530 | [diff] [blame] | 137 | |
| 138 | # submit material requests |
| 139 | for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}): |
| 140 | b = webnotes.bean("Material Request", pro[0]) |
| 141 | b.submit() |
Rushabh Mehta | 2c968ce | 2013-08-08 14:16:39 +0530 | [diff] [blame] | 142 | |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 143 | # stores -> wip |
| 144 | if can_make("Stock Entry for WIP"): |
| 145 | for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]: |
| 146 | make_stock_entry_from_pro(pro[0], "Material Transfer", current_date) |
| 147 | |
| 148 | # wip -> fg |
| 149 | if can_make("Stock Entry for FG"): |
| 150 | for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]: |
| 151 | make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date) |
| 152 | |
| 153 | # try posting older drafts (if exists) |
| 154 | for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}): |
| 155 | try: |
| 156 | webnotes.bean("Stock Entry", st[0]).submit() |
| 157 | except NegativeStockError: pass |
| 158 | except IncorrectValuationRateError: pass |
| 159 | |
| 160 | |
| 161 | def make_stock_entry_from_pro(pro_id, purpose, current_date): |
| 162 | from manufacturing.doctype.production_order.production_order import make_stock_entry |
| 163 | from stock.stock_ledger import NegativeStockError |
| 164 | from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError |
| 165 | |
| 166 | st = webnotes.bean(make_stock_entry(pro_id, purpose)) |
| 167 | st.run_method("get_items") |
| 168 | st.doc.posting_date = current_date |
| 169 | st.doc.fiscal_year = "2010" |
| 170 | st.doc.expense_adjustment_account = "Stock in Hand - WP" |
| 171 | try: |
| 172 | st.insert() |
| 173 | st.submit() |
| 174 | except NegativeStockError: pass |
| 175 | except IncorrectValuationRateError: pass |
| 176 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 177 | def make_quotation(current_date): |
| 178 | b = webnotes.bean([{ |
| 179 | "creation": current_date, |
| 180 | "doctype": "Quotation", |
| 181 | "quotation_to": "Customer", |
| 182 | "customer": get_random("Customer"), |
| 183 | "order_type": "Sales", |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 184 | "transaction_date": current_date, |
| 185 | "fiscal_year": "2010" |
| 186 | }]) |
| 187 | |
| 188 | add_random_children(b, { |
| 189 | "doctype": "Quotation Item", |
| 190 | "parentfield": "quotation_details", |
| 191 | }, rows=3, randomize = { |
| 192 | "qty": (1, 5), |
| 193 | "item_code": ("Item", {"is_sales_item": "Yes"}) |
| 194 | }, unique="item_code") |
| 195 | |
| 196 | b.insert() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 197 | b.submit() |
| 198 | |
| 199 | def make_sales_order(current_date): |
| 200 | q = get_random("Quotation", {"status": "Submitted"}) |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 201 | if q: |
| 202 | from selling.doctype.quotation.quotation import make_sales_order |
| 203 | so = webnotes.bean(make_sales_order(q)) |
| 204 | so.doc.transaction_date = current_date |
| 205 | so.doc.delivery_date = webnotes.utils.add_days(current_date, 10) |
| 206 | so.insert() |
| 207 | so.submit() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 208 | |
| 209 | def add_random_children(bean, template, rows, randomize, unique=None): |
| 210 | for i in xrange(random.randrange(1, rows)): |
| 211 | d = template.copy() |
| 212 | for key, val in randomize.items(): |
| 213 | if isinstance(val[0], basestring): |
| 214 | d[key] = get_random(*val) |
| 215 | else: |
| 216 | d[key] = random.randrange(*val) |
| 217 | |
| 218 | if unique: |
| 219 | if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}): |
| 220 | bean.doclist.append(d) |
| 221 | else: |
| 222 | bean.doclist.append(d) |
| 223 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 224 | def get_random(doctype, filters=None): |
| 225 | condition = [] |
| 226 | if filters: |
| 227 | for key, val in filters.items(): |
| 228 | condition.append("%s='%s'" % (key, val)) |
| 229 | if condition: |
| 230 | condition = " where " + " and ".join(condition) |
| 231 | else: |
| 232 | condition = "" |
| 233 | |
| 234 | out = webnotes.conn.sql("""select name from `tab%s` %s |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 235 | order by RAND() limit 0,1""" % (doctype, condition)) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 236 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 237 | return out and out[0][0] or None |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 238 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 239 | def can_make(doctype): |
| 240 | return random.random() < prob.get(doctype, {"make": 0.5})["make"] |
| 241 | |
| 242 | def how_many(doctype): |
| 243 | return random.randrange(*prob.get(doctype, {"qty": (1, 3)})["qty"]) |
| 244 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 245 | def install(): |
| 246 | print "Creating Fresh Database..." |
| 247 | from webnotes.install_lib.install import Installer |
| 248 | inst = Installer('root') |
| 249 | inst.import_from_db("demo", verbose = 1) |
| 250 | |
| 251 | def complete_setup(): |
| 252 | print "Complete Setup..." |
| 253 | webnotes.get_obj("Setup Control").setup_account({ |
| 254 | "first_name": "Test", |
| 255 | "last_name": "User", |
| 256 | "fy_start": "1st Jan", |
| 257 | "industry": "Manufacturing", |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 258 | "company_name": company, |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 259 | "company_abbr": "WP", |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 260 | "currency": "USD", |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 261 | "timezone": "America/New York", |
| 262 | "country": "United States" |
| 263 | }) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 264 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 265 | import_data("Fiscal_Year") |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 266 | |
| 267 | def make_items(): |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 268 | import_data(["Item", "Item_Price"]) |
| 269 | import_data("BOM", submit=True) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 270 | |
| 271 | def make_customers_suppliers_contacts(): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 272 | import_data(["Customer", "Supplier", "Contact", "Address", "Lead"]) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 273 | |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 274 | def make_users_and_employees(): |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 275 | webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series") |
| 276 | webnotes.conn.commit() |
| 277 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 278 | import_data(["Profile", "Employee", "Salary_Structure"]) |
| 279 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 280 | def import_data(dt, submit=False): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 281 | if not isinstance(dt, (tuple, list)): |
| 282 | dt = [dt] |
| 283 | |
| 284 | for doctype in dt: |
| 285 | print "Importing", doctype.replace("_", " "), "..." |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 286 | webnotes.form_dict = {} |
| 287 | if submit: |
| 288 | webnotes.form_dict["params"] = json.dumps({"_submit": 1}) |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 289 | webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv") |
| 290 | upload() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 291 | |
| 292 | if __name__=="__main__": |
| 293 | make() |