blob: 600faaa220f6a252f70ae5c854270990163b5e2b [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 Mehta62030e02013-08-14 18:37:28 +053031 webnotes.rollback_on_exception = True
Rushabh Mehta4a404e92013-08-09 18:11:35 +053032
Rushabh Mehta4c17f942013-08-12 14:18:09 +053033 if reset:
34 setup()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053035 simulate()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053036
37def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053038 install()
39 complete_setup()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053040 make_customers_suppliers_contacts()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053041 make_items()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053042 make_users_and_employees()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053043 # make_opening_stock()
44 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053045
46def simulate():
47 current_date = None
48 for i in xrange(runs_for):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053049 print i
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053050 if not current_date:
51 current_date = webnotes.utils.getdate(start_date)
52 else:
53 current_date = webnotes.utils.add_days(current_date, 1)
54
55 if current_date.weekday() in (5, 6):
56 continue
57
58 run_sales(current_date)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053059 run_purchase(current_date)
60 run_manufacturing(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053061 run_stock(current_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053062
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053063def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053064 if can_make("Quotation"):
65 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053066 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053067
68 if can_make("Sales Order"):
69 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053070 make_sales_order(current_date)
71
Rushabh Mehta0b995402013-08-09 15:29:59 +053072def run_stock(current_date):
73 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +053074 if can_make("Purchase Receipt"):
75 from buying.doctype.purchase_order.purchase_order import make_purchase_receipt
76 report = "Purchase Order Items To Be Received"
77 for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]:
78 pr = webnotes.bean(make_purchase_receipt(po))
79 pr.doc.posting_date = current_date
80 pr.doc.fiscal_year = "2010"
81 pr.insert()
82 pr.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +053083 webnotes.conn.commit()
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 Mehta6ca903f2013-08-13 14:31:15 +053095 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +053096
Rushabh Mehta4c17f942013-08-12 14:18:09 +053097
Rushabh Mehta0b995402013-08-09 15:29:59 +053098def run_purchase(current_date):
99 # make supplier quotations
100 if can_make("Supplier Quotation"):
101 from stock.doctype.material_request.material_request import make_supplier_quotation
102 report = "Material Requests for which Supplier Quotations are not created"
103 for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530104 if row[0] != "Total":
105 sq = webnotes.bean(make_supplier_quotation(row[0]))
106 sq.doc.transaction_date = current_date
107 sq.doc.fiscal_year = "2010"
108 sq.insert()
109 sq.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530110 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530111
112 # make purchase orders
113 if can_make("Purchase Order"):
114 from stock.doctype.material_request.material_request import make_purchase_order
115 report = "Requested Items To Be Ordered"
116 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530117 if row[0] != "Total":
118 po = webnotes.bean(make_purchase_order(row[0]))
119 po.doc.transaction_date = current_date
120 po.doc.fiscal_year = "2010"
121 po.insert()
122 po.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530123 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530124
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530125def run_manufacturing(current_date):
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530126 from stock.stock_ledger import NegativeStockError
127 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError
128
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530129 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
130 ppt.doc.company = company
131 ppt.doc.use_multi_level_bom = 1
132 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
133 ppt.run_method("get_open_sales_orders")
134 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530135 ppt.run_method("raise_production_order")
136 ppt.run_method("raise_purchase_request")
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530137 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530138
139 # submit production orders
140 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}):
141 b = webnotes.bean("Production Order", pro[0])
142 b.doc.wip_warehouse = "Work in Progress - WP"
143 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530144 webnotes.conn.commit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530145
146 # submit material requests
147 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}):
148 b = webnotes.bean("Material Request", pro[0])
149 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530150 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530151
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530152 # stores -> wip
153 if can_make("Stock Entry for WIP"):
154 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
155 make_stock_entry_from_pro(pro[0], "Material Transfer", current_date)
156
157 # wip -> fg
158 if can_make("Stock Entry for FG"):
159 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
160 make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date)
161
162 # try posting older drafts (if exists)
163 for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}):
164 try:
165 webnotes.bean("Stock Entry", st[0]).submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530166 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530167 except NegativeStockError: pass
168 except IncorrectValuationRateError: pass
169
170
171def make_stock_entry_from_pro(pro_id, purpose, current_date):
172 from manufacturing.doctype.production_order.production_order import make_stock_entry
173 from stock.stock_ledger import NegativeStockError
174 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError
175
176 st = webnotes.bean(make_stock_entry(pro_id, purpose))
177 st.run_method("get_items")
178 st.doc.posting_date = current_date
179 st.doc.fiscal_year = "2010"
180 st.doc.expense_adjustment_account = "Stock in Hand - WP"
181 try:
182 st.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530183 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530184 st.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530185 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530186 except NegativeStockError: pass
187 except IncorrectValuationRateError: pass
188
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530189def make_quotation(current_date):
190 b = webnotes.bean([{
191 "creation": current_date,
192 "doctype": "Quotation",
193 "quotation_to": "Customer",
194 "customer": get_random("Customer"),
195 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530196 "transaction_date": current_date,
197 "fiscal_year": "2010"
198 }])
199
200 add_random_children(b, {
201 "doctype": "Quotation Item",
202 "parentfield": "quotation_details",
203 }, rows=3, randomize = {
204 "qty": (1, 5),
205 "item_code": ("Item", {"is_sales_item": "Yes"})
206 }, unique="item_code")
207
208 b.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530209 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530210 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530211 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530212
213def make_sales_order(current_date):
214 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530215 if q:
216 from selling.doctype.quotation.quotation import make_sales_order
217 so = webnotes.bean(make_sales_order(q))
218 so.doc.transaction_date = current_date
219 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
220 so.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530221 webnotes.conn.commit()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530222 so.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530223 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530224
225def add_random_children(bean, template, rows, randomize, unique=None):
226 for i in xrange(random.randrange(1, rows)):
227 d = template.copy()
228 for key, val in randomize.items():
229 if isinstance(val[0], basestring):
230 d[key] = get_random(*val)
231 else:
232 d[key] = random.randrange(*val)
233
234 if unique:
235 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
236 bean.doclist.append(d)
237 else:
238 bean.doclist.append(d)
239
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530240def get_random(doctype, filters=None):
241 condition = []
242 if filters:
243 for key, val in filters.items():
244 condition.append("%s='%s'" % (key, val))
245 if condition:
246 condition = " where " + " and ".join(condition)
247 else:
248 condition = ""
249
250 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530251 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530252
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530253 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530254
Rushabh Mehta0b995402013-08-09 15:29:59 +0530255def can_make(doctype):
256 return random.random() < prob.get(doctype, {"make": 0.5})["make"]
257
258def how_many(doctype):
259 return random.randrange(*prob.get(doctype, {"qty": (1, 3)})["qty"])
260
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530261def install():
262 print "Creating Fresh Database..."
263 from webnotes.install_lib.install import Installer
264 inst = Installer('root')
265 inst.import_from_db("demo", verbose = 1)
266
267def complete_setup():
268 print "Complete Setup..."
269 webnotes.get_obj("Setup Control").setup_account({
270 "first_name": "Test",
271 "last_name": "User",
272 "fy_start": "1st Jan",
273 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530274 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530275 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530276 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530277 "timezone": "America/New York",
278 "country": "United States"
279 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530280
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530281 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530282
283def make_items():
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530284 import_data(["Item", "Item_Price"])
285 import_data("BOM", submit=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530286
287def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530288 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530289
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530290def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530291 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
292 webnotes.conn.commit()
293
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530294 import_data(["Profile", "Employee", "Salary_Structure"])
295
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530296def import_data(dt, submit=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530297 if not isinstance(dt, (tuple, list)):
298 dt = [dt]
299
300 for doctype in dt:
301 print "Importing", doctype.replace("_", " "), "..."
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530302 webnotes.form_dict = {}
303 if submit:
304 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530305 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
306 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530307
308if __name__=="__main__":
309 make()