blob: d1758a034e8910e7a53ed05175d220f5f224282f [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 Mehta20a10942013-08-20 15:18:42 +053017country = "United States"
18currency = "USD"
19time_zone = "America/New York"
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053020start_date = '2010-01-01'
Rushabh Mehta4a404e92013-08-09 18:11:35 +053021runs_for = 20
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053022prob = {
Rushabh Mehta20a10942013-08-20 15:18:42 +053023 "default": { "make": 0.6, "qty": (1,5) },
24 "Purchase Order": { "make": 0.7, "qty": (1,15) },
25 "Purchase Receipt": { "make": 0.7, "qty": (1,15) },
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053026}
27
Rushabh Mehta4c17f942013-08-12 14:18:09 +053028def make(reset=False):
Rushabh Mehtaed186b72013-07-30 16:19:40 +053029 webnotes.connect()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +053030 webnotes.print_messages = True
Rushabh Mehtaed186b72013-07-30 16:19:40 +053031 webnotes.mute_emails = True
Rushabh Mehta62030e02013-08-14 18:37:28 +053032 webnotes.rollback_on_exception = True
Rushabh Mehta4a404e92013-08-09 18:11:35 +053033
Rushabh Mehta4c17f942013-08-12 14:18:09 +053034 if reset:
35 setup()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053036 simulate()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053037
38def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053039 install()
40 complete_setup()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053041 make_customers_suppliers_contacts()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053042 make_items()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053043 make_users_and_employees()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053044 # make_opening_stock()
45 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053046
47def simulate():
48 current_date = None
49 for i in xrange(runs_for):
50 if not current_date:
Rushabh Mehta20a10942013-08-20 15:18:42 +053051 # get last stock ledger posting date or use default
52 last_posting = webnotes.conn.sql("""select max(posting_date) from `tabStock Ledger Entry`""")
53 if last_posting[0][0]:
54 current_date = webnotes.utils.add_days(last_posting[0][0], 1)
55 else:
56 current_date = webnotes.utils.getdate(start_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053057 else:
58 current_date = webnotes.utils.add_days(current_date, 1)
Rushabh Mehta20a10942013-08-20 15:18:42 +053059
60 print current_date.strftime("%Y-%m-%d")
61
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053062 if current_date.weekday() in (5, 6):
63 continue
64
65 run_sales(current_date)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053066 run_purchase(current_date)
67 run_manufacturing(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053068 run_stock(current_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053069
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053070def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053071 if can_make("Quotation"):
72 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053073 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053074
75 if can_make("Sales Order"):
76 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053077 make_sales_order(current_date)
78
Rushabh Mehta0b995402013-08-09 15:29:59 +053079def run_stock(current_date):
80 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +053081 if can_make("Purchase Receipt"):
82 from buying.doctype.purchase_order.purchase_order import make_purchase_receipt
83 report = "Purchase Order Items To Be Received"
84 for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]:
85 pr = webnotes.bean(make_purchase_receipt(po))
86 pr.doc.posting_date = current_date
87 pr.doc.fiscal_year = "2010"
88 pr.insert()
89 pr.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +053090 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053091
Rushabh Mehta0b995402013-08-09 15:29:59 +053092 # make delivery notes (if possible)
Rushabh Mehta4c17f942013-08-12 14:18:09 +053093 if can_make("Delivery Note"):
94 from selling.doctype.sales_order.sales_order import make_delivery_note
Rushabh Mehta20a10942013-08-20 15:18:42 +053095 from stock.stock_ledger import NegativeStockError
96 from stock.doctype.stock_ledger_entry.stock_ledger_entry import SerialNoRequiredError, SerialNoQtyError
Rushabh Mehta4c17f942013-08-12 14:18:09 +053097 report = "Ordered Items To Be Delivered"
98 for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]:
99 dn = webnotes.bean(make_delivery_note(so))
100 dn.doc.posting_date = current_date
101 dn.doc.fiscal_year = "2010"
102 dn.insert()
Rushabh Mehta20a10942013-08-20 15:18:42 +0530103 try:
104 dn.submit()
105 webnotes.conn.commit()
106 except NegativeStockError: pass
107 except SerialNoRequiredError: pass
108 except SerialNoQtyError: pass
109
110 # try submitting existing
111 for dn in webnotes.conn.get_values("Delivery Note", {"docstatus": 0}, "name"):
112 b = webnotes.bean("Delivery Note", dn[0])
113 b.submit()
114 webnotes.conn.commit()
115
Rushabh Mehta0b995402013-08-09 15:29:59 +0530116
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530117
Rushabh Mehta0b995402013-08-09 15:29:59 +0530118def run_purchase(current_date):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530119 # make material requests for purchase items that have negative projected qtys
120 if can_make("Material Request"):
121 report = "Items To Be Requested"
122 for row in query_report.run(report)["result"][:how_many("Material Request")]:
123 mr = webnotes.new_bean("Material Request")
124 mr.doc.material_request_type = "Purchase"
125 mr.doc.transaction_date = current_date
126 mr.doc.fiscal_year = "2010"
127 mr.doclist.append({
128 "doctype": "Material Request Item",
129 "parentfield": "indent_details",
130 "schedule_date": webnotes.utils.add_days(current_date, 7),
131 "item_code": row[0],
132 "qty": -row[-1]
133 })
134 mr.insert()
135 mr.submit()
136
Rushabh Mehta0b995402013-08-09 15:29:59 +0530137 # make supplier quotations
138 if can_make("Supplier Quotation"):
139 from stock.doctype.material_request.material_request import make_supplier_quotation
140 report = "Material Requests for which Supplier Quotations are not created"
141 for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530142 if row[0] != "Total":
143 sq = webnotes.bean(make_supplier_quotation(row[0]))
144 sq.doc.transaction_date = current_date
145 sq.doc.fiscal_year = "2010"
146 sq.insert()
147 sq.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530148 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530149
150 # make purchase orders
151 if can_make("Purchase Order"):
152 from stock.doctype.material_request.material_request import make_purchase_order
153 report = "Requested Items To Be Ordered"
154 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530155 if row[0] != "Total":
156 po = webnotes.bean(make_purchase_order(row[0]))
157 po.doc.transaction_date = current_date
158 po.doc.fiscal_year = "2010"
159 po.insert()
160 po.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530161 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530162
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530163def run_manufacturing(current_date):
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530164 from stock.stock_ledger import NegativeStockError
Rushabh Mehta20a10942013-08-20 15:18:42 +0530165 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530166
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530167 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
168 ppt.doc.company = company
169 ppt.doc.use_multi_level_bom = 1
170 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
171 ppt.run_method("get_open_sales_orders")
172 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530173 ppt.run_method("raise_production_order")
174 ppt.run_method("raise_purchase_request")
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530175 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530176
177 # submit production orders
Rushabh Mehta20a10942013-08-20 15:18:42 +0530178 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}, "name"):
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530179 b = webnotes.bean("Production Order", pro[0])
180 b.doc.wip_warehouse = "Work in Progress - WP"
181 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530182 webnotes.conn.commit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530183
184 # submit material requests
Rushabh Mehta20a10942013-08-20 15:18:42 +0530185 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}, "name"):
Rushabh Mehta75c31712013-08-08 16:00:40 +0530186 b = webnotes.bean("Material Request", pro[0])
187 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530188 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530189
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530190 # stores -> wip
191 if can_make("Stock Entry for WIP"):
192 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
193 make_stock_entry_from_pro(pro[0], "Material Transfer", current_date)
194
195 # wip -> fg
196 if can_make("Stock Entry for FG"):
197 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
198 make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date)
199
200 # try posting older drafts (if exists)
Rushabh Mehta20a10942013-08-20 15:18:42 +0530201 for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}, "name"):
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530202 try:
203 webnotes.bean("Stock Entry", st[0]).submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530204 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530205 except NegativeStockError: pass
206 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530207 except DuplicateEntryForProductionOrderError: pass
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530208
209def make_stock_entry_from_pro(pro_id, purpose, current_date):
210 from manufacturing.doctype.production_order.production_order import make_stock_entry
211 from stock.stock_ledger import NegativeStockError
Rushabh Mehta20a10942013-08-20 15:18:42 +0530212 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530213
214 st = webnotes.bean(make_stock_entry(pro_id, purpose))
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530215 st.doc.posting_date = current_date
216 st.doc.fiscal_year = "2010"
217 st.doc.expense_adjustment_account = "Stock in Hand - WP"
218 try:
219 st.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530220 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530221 st.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530222 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530223 except NegativeStockError: pass
224 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530225 except DuplicateEntryForProductionOrderError: pass
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530226
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530227def make_quotation(current_date):
228 b = webnotes.bean([{
229 "creation": current_date,
230 "doctype": "Quotation",
231 "quotation_to": "Customer",
232 "customer": get_random("Customer"),
233 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530234 "transaction_date": current_date,
235 "fiscal_year": "2010"
236 }])
237
238 add_random_children(b, {
239 "doctype": "Quotation Item",
240 "parentfield": "quotation_details",
241 }, rows=3, randomize = {
242 "qty": (1, 5),
243 "item_code": ("Item", {"is_sales_item": "Yes"})
244 }, unique="item_code")
245
246 b.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530247 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530248 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530249 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530250
251def make_sales_order(current_date):
252 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530253 if q:
254 from selling.doctype.quotation.quotation import make_sales_order
255 so = webnotes.bean(make_sales_order(q))
256 so.doc.transaction_date = current_date
257 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
258 so.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530259 webnotes.conn.commit()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530260 so.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530261 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530262
263def add_random_children(bean, template, rows, randomize, unique=None):
264 for i in xrange(random.randrange(1, rows)):
265 d = template.copy()
266 for key, val in randomize.items():
267 if isinstance(val[0], basestring):
268 d[key] = get_random(*val)
269 else:
270 d[key] = random.randrange(*val)
271
272 if unique:
273 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
274 bean.doclist.append(d)
275 else:
276 bean.doclist.append(d)
277
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530278def get_random(doctype, filters=None):
279 condition = []
280 if filters:
281 for key, val in filters.items():
282 condition.append("%s='%s'" % (key, val))
283 if condition:
284 condition = " where " + " and ".join(condition)
285 else:
286 condition = ""
287
288 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530289 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530290
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530291 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530292
Rushabh Mehta0b995402013-08-09 15:29:59 +0530293def can_make(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530294 return random.random() < prob.get(doctype, prob["default"])["make"]
Rushabh Mehta0b995402013-08-09 15:29:59 +0530295
296def how_many(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530297 return random.randrange(*prob.get(doctype, prob["default"])["qty"])
Rushabh Mehta0b995402013-08-09 15:29:59 +0530298
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530299def install():
300 print "Creating Fresh Database..."
301 from webnotes.install_lib.install import Installer
302 inst = Installer('root')
303 inst.import_from_db("demo", verbose = 1)
304
305def complete_setup():
306 print "Complete Setup..."
307 webnotes.get_obj("Setup Control").setup_account({
308 "first_name": "Test",
309 "last_name": "User",
310 "fy_start": "1st Jan",
311 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530312 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530313 "company_abbr": "WP",
Rushabh Mehta20a10942013-08-20 15:18:42 +0530314 "currency": currency,
315 "timezone": time_zone,
316 "country": country
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530317 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530318
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530319 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530320
321def make_items():
Rushabh Mehta20a10942013-08-20 15:18:42 +0530322 import_data("Item")
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530323 import_data("BOM", submit=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530324
325def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530326 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530327
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530328def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530329 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
330 webnotes.conn.commit()
331
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530332 import_data(["Profile", "Employee", "Salary_Structure"])
333
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530334def import_data(dt, submit=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530335 if not isinstance(dt, (tuple, list)):
336 dt = [dt]
337
338 for doctype in dt:
339 print "Importing", doctype.replace("_", " "), "..."
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530340 webnotes.form_dict = {}
341 if submit:
342 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530343 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
344 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530345
346if __name__=="__main__":
347 make()