blob: 461bbefb6ef4935d2f4739faa7e9b28a36d32e2e [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
3
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +05304import webnotes, os, datetime
5import webnotes.utils
Rushabh Mehta0b995402013-08-09 15:29:59 +05306from webnotes.widgets import query_report
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +05307import random
Rushabh Mehta4a404e92013-08-09 18:11:35 +05308import json
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +05309
Rushabh Mehtaed186b72013-07-30 16:19:40 +053010webnotes.session = webnotes._dict({"user":"Administrator"})
11from core.page.data_import_tool.data_import_tool import upload
12
Rushabh Mehta0b995402013-08-09 15:29:59 +053013# fix price list
14# fix fiscal year
15
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053016company = "Wind Power LLC"
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053017start_date = '2010-01-01'
Rushabh Mehta4a404e92013-08-09 18:11:35 +053018runs_for = 20
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053019prob = {
Rushabh Mehta4a404e92013-08-09 18:11:35 +053020 "Quotation": { "make": 0.5, "qty": (1,5) },
21 "Sales Order": { "make": 0.5, "qty": (1,4) },
Rushabh Mehta4c17f942013-08-12 14:18:09 +053022 "Purchase Order": { "make": 0.7, "qty": (1,4) },
23 "Purchase Receipt": { "make": 0.7, "qty": (1,4) },
Rushabh Mehta0b995402013-08-09 15:29:59 +053024 "Supplier Quotation": { "make": 0.5, "qty": (1, 3) }
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053025}
26
Rushabh Mehta4c17f942013-08-12 14:18:09 +053027def make(reset=False):
Rushabh Mehtaed186b72013-07-30 16:19:40 +053028 webnotes.connect()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +053029 webnotes.print_messages = True
Rushabh Mehtaed186b72013-07-30 16:19:40 +053030 webnotes.mute_emails = True
Rushabh Mehta4a404e92013-08-09 18:11:35 +053031
Rushabh Mehta4c17f942013-08-12 14:18:09 +053032 if reset:
33 setup()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053034 simulate()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053035
36def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053037 install()
38 complete_setup()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053039 make_customers_suppliers_contacts()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053040 make_items()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053041 make_users_and_employees()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053042 # make_opening_stock()
43 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053044
45def simulate():
46 current_date = None
47 for i in xrange(runs_for):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053048 print i
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053049 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 Mehta7cfefbc2013-08-07 17:46:35 +053058 run_purchase(current_date)
59 run_manufacturing(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053060 run_stock(current_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053061
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053062def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053063 if can_make("Quotation"):
64 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053065 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053066
67 if can_make("Sales Order"):
68 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053069 make_sales_order(current_date)
70
Rushabh Mehta0b995402013-08-09 15:29:59 +053071def run_stock(current_date):
72 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +053073 if can_make("Purchase Receipt"):
74 from buying.doctype.purchase_order.purchase_order import make_purchase_receipt
75 report = "Purchase Order Items To Be Received"
76 for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]:
77 pr = webnotes.bean(make_purchase_receipt(po))
78 pr.doc.posting_date = current_date
79 pr.doc.fiscal_year = "2010"
80 pr.insert()
81 pr.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +053082 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053083
Rushabh Mehta0b995402013-08-09 15:29:59 +053084 # make delivery notes (if possible)
Rushabh Mehta4c17f942013-08-12 14:18:09 +053085 if can_make("Delivery Note"):
86 from selling.doctype.sales_order.sales_order import make_delivery_note
87 report = "Ordered Items To Be Delivered"
88 for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]:
89 dn = webnotes.bean(make_delivery_note(so))
90 dn.doc.posting_date = current_date
91 dn.doc.fiscal_year = "2010"
92 dn.insert()
93 dn.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +053094 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +053095
Rushabh Mehta4c17f942013-08-12 14:18:09 +053096
Rushabh Mehta0b995402013-08-09 15:29:59 +053097def 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 Mehta4a404e92013-08-09 18:11:35 +0530103 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 Mehta6ca903f2013-08-13 14:31:15 +0530109 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530110
111 # make purchase orders
112 if can_make("Purchase Order"):
113 from stock.doctype.material_request.material_request import make_purchase_order
114 report = "Requested Items To Be Ordered"
115 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530116 if row[0] != "Total":
117 po = webnotes.bean(make_purchase_order(row[0]))
118 po.doc.transaction_date = current_date
119 po.doc.fiscal_year = "2010"
120 po.insert()
121 po.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530122 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530123
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530124def run_manufacturing(current_date):
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530125 from stock.stock_ledger import NegativeStockError
126 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError
127
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530128 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
129 ppt.doc.company = company
130 ppt.doc.use_multi_level_bom = 1
131 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
132 ppt.run_method("get_open_sales_orders")
133 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530134 ppt.run_method("raise_production_order")
135 ppt.run_method("raise_purchase_request")
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530136 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530137
138 # submit production orders
139 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}):
140 b = webnotes.bean("Production Order", pro[0])
141 b.doc.wip_warehouse = "Work in Progress - WP"
142 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530143 webnotes.conn.commit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530144
145 # submit material requests
146 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}):
147 b = webnotes.bean("Material Request", pro[0])
148 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530149 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530150
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530151 # stores -> wip
152 if can_make("Stock Entry for WIP"):
153 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
154 make_stock_entry_from_pro(pro[0], "Material Transfer", current_date)
155
156 # wip -> fg
157 if can_make("Stock Entry for FG"):
158 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
159 make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date)
160
161 # try posting older drafts (if exists)
162 for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}):
163 try:
164 webnotes.bean("Stock Entry", st[0]).submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530165 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530166 except NegativeStockError: pass
167 except IncorrectValuationRateError: pass
168
169
170def make_stock_entry_from_pro(pro_id, purpose, current_date):
171 from manufacturing.doctype.production_order.production_order import make_stock_entry
172 from stock.stock_ledger import NegativeStockError
173 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError
174
175 st = webnotes.bean(make_stock_entry(pro_id, purpose))
176 st.run_method("get_items")
177 st.doc.posting_date = current_date
178 st.doc.fiscal_year = "2010"
179 st.doc.expense_adjustment_account = "Stock in Hand - WP"
180 try:
181 st.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530182 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530183 st.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530184 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530185 except NegativeStockError: pass
186 except IncorrectValuationRateError: pass
187
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530188def make_quotation(current_date):
189 b = webnotes.bean([{
190 "creation": current_date,
191 "doctype": "Quotation",
192 "quotation_to": "Customer",
193 "customer": get_random("Customer"),
194 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530195 "transaction_date": current_date,
196 "fiscal_year": "2010"
197 }])
198
199 add_random_children(b, {
200 "doctype": "Quotation Item",
201 "parentfield": "quotation_details",
202 }, rows=3, randomize = {
203 "qty": (1, 5),
204 "item_code": ("Item", {"is_sales_item": "Yes"})
205 }, unique="item_code")
206
207 b.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530208 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530209 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530210 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530211
212def make_sales_order(current_date):
213 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530214 if q:
215 from selling.doctype.quotation.quotation import make_sales_order
216 so = webnotes.bean(make_sales_order(q))
217 so.doc.transaction_date = current_date
218 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
219 so.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530220 webnotes.conn.commit()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530221 so.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530222 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530223
224def add_random_children(bean, template, rows, randomize, unique=None):
225 for i in xrange(random.randrange(1, rows)):
226 d = template.copy()
227 for key, val in randomize.items():
228 if isinstance(val[0], basestring):
229 d[key] = get_random(*val)
230 else:
231 d[key] = random.randrange(*val)
232
233 if unique:
234 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
235 bean.doclist.append(d)
236 else:
237 bean.doclist.append(d)
238
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530239def get_random(doctype, filters=None):
240 condition = []
241 if filters:
242 for key, val in filters.items():
243 condition.append("%s='%s'" % (key, val))
244 if condition:
245 condition = " where " + " and ".join(condition)
246 else:
247 condition = ""
248
249 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530250 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530251
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530252 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530253
Rushabh Mehta0b995402013-08-09 15:29:59 +0530254def can_make(doctype):
255 return random.random() < prob.get(doctype, {"make": 0.5})["make"]
256
257def how_many(doctype):
258 return random.randrange(*prob.get(doctype, {"qty": (1, 3)})["qty"])
259
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530260def install():
261 print "Creating Fresh Database..."
262 from webnotes.install_lib.install import Installer
263 inst = Installer('root')
264 inst.import_from_db("demo", verbose = 1)
265
266def complete_setup():
267 print "Complete Setup..."
268 webnotes.get_obj("Setup Control").setup_account({
269 "first_name": "Test",
270 "last_name": "User",
271 "fy_start": "1st Jan",
272 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530273 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530274 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530275 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530276 "timezone": "America/New York",
277 "country": "United States"
278 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530279
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530280 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530281
282def make_items():
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530283 import_data(["Item", "Item_Price"])
284 import_data("BOM", submit=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530285
286def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530287 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530288
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530289def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530290 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
291 webnotes.conn.commit()
292
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530293 import_data(["Profile", "Employee", "Salary_Structure"])
294
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530295def import_data(dt, submit=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530296 if not isinstance(dt, (tuple, list)):
297 dt = [dt]
298
299 for doctype in dt:
300 print "Importing", doctype.replace("_", " "), "..."
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530301 webnotes.form_dict = {}
302 if submit:
303 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530304 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
305 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530306
307if __name__=="__main__":
308 make()