blob: 124259fe4e17aa70ac0c121d026637dffbde97e8 [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 Mehta0b995402013-08-09 15:29:59 +053022 "Supplier Quotation": { "make": 0.5, "qty": (1, 3) }
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053023}
24
Rushabh Mehtaed186b72013-07-30 16:19:40 +053025def make():
26 webnotes.connect()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +053027 webnotes.print_messages = True
Rushabh Mehtaed186b72013-07-30 16:19:40 +053028 webnotes.mute_emails = True
Rushabh Mehta4a404e92013-08-09 18:11:35 +053029
30 #setup()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053031 simulate()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053032
33def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053034 install()
35 complete_setup()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053036 make_customers_suppliers_contacts()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053037 make_items()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053038 make_users_and_employees()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053039 # make_opening_stock()
40 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053041
42def simulate():
43 current_date = None
44 for i in xrange(runs_for):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053045 print i
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053046 if not current_date:
47 current_date = webnotes.utils.getdate(start_date)
48 else:
49 current_date = webnotes.utils.add_days(current_date, 1)
50
51 if current_date.weekday() in (5, 6):
52 continue
53
54 run_sales(current_date)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053055 run_purchase(current_date)
56 run_manufacturing(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053057 run_stock(current_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053058
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053059 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053060
61def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053062 if can_make("Quotation"):
63 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053064 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053065
66 if can_make("Sales Order"):
67 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053068 make_sales_order(current_date)
69
Rushabh Mehta0b995402013-08-09 15:29:59 +053070def run_stock(current_date):
Rushabh Mehta4a404e92013-08-09 18:11:35 +053071 pass
Rushabh Mehta0b995402013-08-09 15:29:59 +053072 # make purchase requests
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053073
Rushabh Mehta0b995402013-08-09 15:29:59 +053074 # make delivery notes (if possible)
75
76 # make stock entry (from production order)
77
78def run_purchase(current_date):
79 # make supplier quotations
80 if can_make("Supplier Quotation"):
81 from stock.doctype.material_request.material_request import make_supplier_quotation
82 report = "Material Requests for which Supplier Quotations are not created"
83 for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +053084 if row[0] != "Total":
85 sq = webnotes.bean(make_supplier_quotation(row[0]))
86 sq.doc.transaction_date = current_date
87 sq.doc.fiscal_year = "2010"
88 sq.insert()
89 sq.submit()
Rushabh Mehta0b995402013-08-09 15:29:59 +053090
91 # make purchase orders
92 if can_make("Purchase Order"):
93 from stock.doctype.material_request.material_request import make_purchase_order
94 report = "Requested Items To Be Ordered"
95 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +053096 if row[0] != "Total":
97 po = webnotes.bean(make_purchase_order(row[0]))
98 po.doc.transaction_date = current_date
99 po.doc.fiscal_year = "2010"
100 po.insert()
101 po.submit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530102
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530103def run_manufacturing(current_date):
104 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
105 ppt.doc.company = company
106 ppt.doc.use_multi_level_bom = 1
107 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
108 ppt.run_method("get_open_sales_orders")
109 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530110 ppt.run_method("raise_production_order")
111 ppt.run_method("raise_purchase_request")
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530112
113 # submit production orders
114 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}):
115 b = webnotes.bean("Production Order", pro[0])
116 b.doc.wip_warehouse = "Work in Progress - WP"
117 b.submit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530118
119 # submit material requests
120 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}):
121 b = webnotes.bean("Material Request", pro[0])
122 b.submit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530123
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530124def make_quotation(current_date):
125 b = webnotes.bean([{
126 "creation": current_date,
127 "doctype": "Quotation",
128 "quotation_to": "Customer",
129 "customer": get_random("Customer"),
130 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530131 "transaction_date": current_date,
132 "fiscal_year": "2010"
133 }])
134
135 add_random_children(b, {
136 "doctype": "Quotation Item",
137 "parentfield": "quotation_details",
138 }, rows=3, randomize = {
139 "qty": (1, 5),
140 "item_code": ("Item", {"is_sales_item": "Yes"})
141 }, unique="item_code")
142
143 b.insert()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530144 b.submit()
145
146def make_sales_order(current_date):
147 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530148 if q:
149 from selling.doctype.quotation.quotation import make_sales_order
150 so = webnotes.bean(make_sales_order(q))
151 so.doc.transaction_date = current_date
152 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
153 so.insert()
154 so.submit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530155
156def add_random_children(bean, template, rows, randomize, unique=None):
157 for i in xrange(random.randrange(1, rows)):
158 d = template.copy()
159 for key, val in randomize.items():
160 if isinstance(val[0], basestring):
161 d[key] = get_random(*val)
162 else:
163 d[key] = random.randrange(*val)
164
165 if unique:
166 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
167 bean.doclist.append(d)
168 else:
169 bean.doclist.append(d)
170
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530171def get_random(doctype, filters=None):
172 condition = []
173 if filters:
174 for key, val in filters.items():
175 condition.append("%s='%s'" % (key, val))
176 if condition:
177 condition = " where " + " and ".join(condition)
178 else:
179 condition = ""
180
181 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530182 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530183
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530184 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530185
Rushabh Mehta0b995402013-08-09 15:29:59 +0530186def can_make(doctype):
187 return random.random() < prob.get(doctype, {"make": 0.5})["make"]
188
189def how_many(doctype):
190 return random.randrange(*prob.get(doctype, {"qty": (1, 3)})["qty"])
191
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530192def install():
193 print "Creating Fresh Database..."
194 from webnotes.install_lib.install import Installer
195 inst = Installer('root')
196 inst.import_from_db("demo", verbose = 1)
197
198def complete_setup():
199 print "Complete Setup..."
200 webnotes.get_obj("Setup Control").setup_account({
201 "first_name": "Test",
202 "last_name": "User",
203 "fy_start": "1st Jan",
204 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530205 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530206 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530207 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530208 "timezone": "America/New York",
209 "country": "United States"
210 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530211
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530212 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530213
214def make_items():
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530215 import_data(["Item", "Item_Price"])
216 import_data("BOM", submit=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530217
218def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530219 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530220
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530221def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530222 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
223 webnotes.conn.commit()
224
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530225 import_data(["Profile", "Employee", "Salary_Structure"])
226
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530227def import_data(dt, submit=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530228 if not isinstance(dt, (tuple, list)):
229 dt = [dt]
230
231 for doctype in dt:
232 print "Importing", doctype.replace("_", " "), "..."
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530233 webnotes.form_dict = {}
234 if submit:
235 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530236 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
237 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530238
239if __name__=="__main__":
240 make()