blob: a5228620f255cd17e02d689027c1588c9cfd61e0 [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 Mehta7cfefbc2013-08-07 17:46:35 +053062 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053063
64def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053065 if can_make("Quotation"):
66 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053067 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053068
69 if can_make("Sales Order"):
70 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053071 make_sales_order(current_date)
72
Rushabh Mehta0b995402013-08-09 15:29:59 +053073def run_stock(current_date):
74 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +053075 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 Mehta7cfefbc2013-08-07 17:46:35 +053084
Rushabh Mehta0b995402013-08-09 15:29:59 +053085 # make delivery notes (if possible)
Rushabh Mehta4c17f942013-08-12 14:18:09 +053086 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 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 Mehta0b995402013-08-09 15:29:59 +0530109
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 Mehta4a404e92013-08-09 18:11:35 +0530115 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 Mehta0b995402013-08-09 15:29:59 +0530121
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530122def 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 Mehta7cfefbc2013-08-07 17:46:35 +0530129 ppt.run_method("raise_production_order")
130 ppt.run_method("raise_purchase_request")
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530131
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 Mehta75c31712013-08-08 16:00:40 +0530137
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 Mehta2c968ce2013-08-08 14:16:39 +0530142
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530143 # 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
161def 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 Mehta7c2a2e22013-08-07 15:08:11 +0530177def 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 Mehta7c2a2e22013-08-07 15:08:11 +0530184 "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 Mehta7cfefbc2013-08-07 17:46:35 +0530197 b.submit()
198
199def make_sales_order(current_date):
200 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530201 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 Mehta7c2a2e22013-08-07 15:08:11 +0530208
209def 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 Mehta7c2a2e22013-08-07 15:08:11 +0530224def 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 Mehta4a404e92013-08-09 18:11:35 +0530235 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530236
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530237 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530238
Rushabh Mehta0b995402013-08-09 15:29:59 +0530239def can_make(doctype):
240 return random.random() < prob.get(doctype, {"make": 0.5})["make"]
241
242def how_many(doctype):
243 return random.randrange(*prob.get(doctype, {"qty": (1, 3)})["qty"])
244
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530245def 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
251def 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 Mehta7cfefbc2013-08-07 17:46:35 +0530258 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530259 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530260 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530261 "timezone": "America/New York",
262 "country": "United States"
263 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530264
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530265 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530266
267def make_items():
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530268 import_data(["Item", "Item_Price"])
269 import_data("BOM", submit=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530270
271def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530272 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530273
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530274def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530275 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
276 webnotes.conn.commit()
277
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530278 import_data(["Profile", "Employee", "Salary_Structure"])
279
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530280def import_data(dt, submit=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530281 if not isinstance(dt, (tuple, list)):
282 dt = [dt]
283
284 for doctype in dt:
285 print "Importing", doctype.replace("_", " "), "..."
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530286 webnotes.form_dict = {}
287 if submit:
288 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530289 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
290 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530291
292if __name__=="__main__":
293 make()