blob: 025c83a833997551ba1b83e27ccb41887ab322d0 [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
6import random
7
Rushabh Mehtaed186b72013-07-30 16:19:40 +05308webnotes.session = webnotes._dict({"user":"Administrator"})
9from core.page.data_import_tool.data_import_tool import upload
10
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053011company = "Wind Power LLC"
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053012start_date = '2010-01-01'
13runs_for = 100
14prob = {
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053015 "Quotation": { "make": 0.5, "qty": (1,3) },
16 "Sales Order": { "make": 0.5, "qty": (1,2) }
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053017}
18
Rushabh Mehtaed186b72013-07-30 16:19:40 +053019def make():
20 webnotes.connect()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +053021 webnotes.print_messages = True
Rushabh Mehtaed186b72013-07-30 16:19:40 +053022 webnotes.mute_emails = True
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053023
24 # setup()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053025 simulate()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053026
27def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053028 install()
29 complete_setup()
30 make_items()
31 make_customers_suppliers_contacts()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053032 make_users_and_employees()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053033 # make_opening_stock()
34 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053035
36def simulate():
37 current_date = None
38 for i in xrange(runs_for):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053039 print i
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053040 if not current_date:
41 current_date = webnotes.utils.getdate(start_date)
42 else:
43 current_date = webnotes.utils.add_days(current_date, 1)
44
45 if current_date.weekday() in (5, 6):
46 continue
47
48 run_sales(current_date)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053049 run_purchase(current_date)
50 run_manufacturing(current_date)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053051
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053052 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053053
54def run_sales(current_date):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053055 if random.random() < prob["Quotation"]["make"]:
56 for i in xrange(random.randrange(*prob["Quotation"]["qty"])):
57 make_quotation(current_date)
58
59 if random.random() < prob["Sales Order"]["make"]:
60 for i in xrange(random.randrange(*prob["Sales Order"]["qty"])):
61 make_sales_order(current_date)
62
63def run_purchase(current_date):
64 pass
65
66def run_manufacturing(current_date):
67 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
68 ppt.doc.company = company
69 ppt.doc.use_multi_level_bom = 1
70 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
71 ppt.run_method("get_open_sales_orders")
72 ppt.run_method("get_items_from_so")
73 ppt.run_method("get_items_from_so")
74 ppt.run_method("raise_production_order")
75 ppt.run_method("raise_purchase_request")
76
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053077def make_quotation(current_date):
78 b = webnotes.bean([{
79 "creation": current_date,
80 "doctype": "Quotation",
81 "quotation_to": "Customer",
82 "customer": get_random("Customer"),
83 "order_type": "Sales",
84 "price_list_name": "Standard Selling",
85 "transaction_date": current_date,
86 "fiscal_year": "2010"
87 }])
88
89 add_random_children(b, {
90 "doctype": "Quotation Item",
91 "parentfield": "quotation_details",
92 }, rows=3, randomize = {
93 "qty": (1, 5),
94 "item_code": ("Item", {"is_sales_item": "Yes"})
95 }, unique="item_code")
96
97 b.insert()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053098 b.submit()
99
100def make_sales_order(current_date):
101 q = get_random("Quotation", {"status": "Submitted"})
102 from selling.doctype.quotation.quotation import make_sales_order
103 so = webnotes.bean(make_sales_order(q))
104 so.doc.transaction_date = current_date
105 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
106 so.insert()
107 so.submit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530108
109def add_random_children(bean, template, rows, randomize, unique=None):
110 for i in xrange(random.randrange(1, rows)):
111 d = template.copy()
112 for key, val in randomize.items():
113 if isinstance(val[0], basestring):
114 d[key] = get_random(*val)
115 else:
116 d[key] = random.randrange(*val)
117
118 if unique:
119 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
120 bean.doclist.append(d)
121 else:
122 bean.doclist.append(d)
123
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530124def get_random(doctype, filters=None):
125 condition = []
126 if filters:
127 for key, val in filters.items():
128 condition.append("%s='%s'" % (key, val))
129 if condition:
130 condition = " where " + " and ".join(condition)
131 else:
132 condition = ""
133
134 out = webnotes.conn.sql("""select name from `tab%s` %s
135 order by RAND() limit 0,1""" % (doctype, condition))[0][0]
136
137 return out
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530138
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530139def install():
140 print "Creating Fresh Database..."
141 from webnotes.install_lib.install import Installer
142 inst = Installer('root')
143 inst.import_from_db("demo", verbose = 1)
144
145def complete_setup():
146 print "Complete Setup..."
147 webnotes.get_obj("Setup Control").setup_account({
148 "first_name": "Test",
149 "last_name": "User",
150 "fy_start": "1st Jan",
151 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530152 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530153 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530154 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530155 "timezone": "America/New York",
156 "country": "United States"
157 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530158
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530159 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530160
161def make_items():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530162 import_data(["Item", "Item_Price", "BOM"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530163
164def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530165 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530166
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530167def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530168 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
169 webnotes.conn.commit()
170
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530171 import_data(["Profile", "Employee", "Salary_Structure"])
172
173def import_data(dt):
174 if not isinstance(dt, (tuple, list)):
175 dt = [dt]
176
177 for doctype in dt:
178 print "Importing", doctype.replace("_", " "), "..."
179 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
180 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530181
182if __name__=="__main__":
183 make()