blob: eaa3f41999ab55b89af056902e83df4ea428e9fb [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")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053073 ppt.run_method("raise_production_order")
74 ppt.run_method("raise_purchase_request")
Rushabh Mehta2c968ce2013-08-08 14:16:39 +053075
76 # submit production orders
77 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}):
78 b = webnotes.bean("Production Order", pro[0])
79 b.doc.wip_warehouse = "Work in Progress - WP"
80 b.submit()
Rushabh Mehta75c31712013-08-08 16:00:40 +053081
82 # submit material requests
83 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}):
84 b = webnotes.bean("Material Request", pro[0])
85 b.submit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +053086
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053087def make_quotation(current_date):
88 b = webnotes.bean([{
89 "creation": current_date,
90 "doctype": "Quotation",
91 "quotation_to": "Customer",
92 "customer": get_random("Customer"),
93 "order_type": "Sales",
94 "price_list_name": "Standard Selling",
95 "transaction_date": current_date,
96 "fiscal_year": "2010"
97 }])
98
99 add_random_children(b, {
100 "doctype": "Quotation Item",
101 "parentfield": "quotation_details",
102 }, rows=3, randomize = {
103 "qty": (1, 5),
104 "item_code": ("Item", {"is_sales_item": "Yes"})
105 }, unique="item_code")
106
107 b.insert()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530108 b.submit()
109
110def make_sales_order(current_date):
111 q = get_random("Quotation", {"status": "Submitted"})
112 from selling.doctype.quotation.quotation import make_sales_order
113 so = webnotes.bean(make_sales_order(q))
114 so.doc.transaction_date = current_date
115 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
116 so.insert()
117 so.submit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530118
119def add_random_children(bean, template, rows, randomize, unique=None):
120 for i in xrange(random.randrange(1, rows)):
121 d = template.copy()
122 for key, val in randomize.items():
123 if isinstance(val[0], basestring):
124 d[key] = get_random(*val)
125 else:
126 d[key] = random.randrange(*val)
127
128 if unique:
129 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
130 bean.doclist.append(d)
131 else:
132 bean.doclist.append(d)
133
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530134def get_random(doctype, filters=None):
135 condition = []
136 if filters:
137 for key, val in filters.items():
138 condition.append("%s='%s'" % (key, val))
139 if condition:
140 condition = " where " + " and ".join(condition)
141 else:
142 condition = ""
143
144 out = webnotes.conn.sql("""select name from `tab%s` %s
145 order by RAND() limit 0,1""" % (doctype, condition))[0][0]
146
147 return out
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530148
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530149def install():
150 print "Creating Fresh Database..."
151 from webnotes.install_lib.install import Installer
152 inst = Installer('root')
153 inst.import_from_db("demo", verbose = 1)
154
155def complete_setup():
156 print "Complete Setup..."
157 webnotes.get_obj("Setup Control").setup_account({
158 "first_name": "Test",
159 "last_name": "User",
160 "fy_start": "1st Jan",
161 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530162 "company_name": company,
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530163 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530164 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530165 "timezone": "America/New York",
166 "country": "United States"
167 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530168
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530169 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530170
171def make_items():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530172 import_data(["Item", "Item_Price", "BOM"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530173
174def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530175 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530176
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530177def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530178 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
179 webnotes.conn.commit()
180
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530181 import_data(["Profile", "Employee", "Salary_Structure"])
182
183def import_data(dt):
184 if not isinstance(dt, (tuple, list)):
185 dt = [dt]
186
187 for doctype in dt:
188 print "Importing", doctype.replace("_", " "), "..."
189 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
190 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530191
192if __name__=="__main__":
193 make()