blob: 61a971bdbd3953f66038917978e47afc5e6d8be4 [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 Mehta7c2a2e22013-08-07 15:08:11 +053011start_date = '2010-01-01'
12runs_for = 100
13prob = {
14 "Quotation": 0.5
15}
16
Rushabh Mehtaed186b72013-07-30 16:19:40 +053017def make():
18 webnotes.connect()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +053019 webnotes.print_messages = True
Rushabh Mehtaed186b72013-07-30 16:19:40 +053020 webnotes.mute_emails = True
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053021
22 # setup()
23 # simulate()
24 make_quotation("2010-01-01")
25 webnotes.conn.commit()
26
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_bom()
34 # make_opening_stock()
35 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053036
37def simulate():
38 current_date = None
39 for i in xrange(runs_for):
40 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)
49
50 webnotes.conn.commit()
51
52
53def run_sales(current_date):
54 if random.random() < prob["Quotation"]:
55 make_quotation(current_date)
56
57def make_quotation(current_date):
58 b = webnotes.bean([{
59 "creation": current_date,
60 "doctype": "Quotation",
61 "quotation_to": "Customer",
62 "customer": get_random("Customer"),
63 "order_type": "Sales",
64 "price_list_name": "Standard Selling",
65 "transaction_date": current_date,
66 "fiscal_year": "2010"
67 }])
68
69 add_random_children(b, {
70 "doctype": "Quotation Item",
71 "parentfield": "quotation_details",
72 }, rows=3, randomize = {
73 "qty": (1, 5),
74 "item_code": ("Item", {"is_sales_item": "Yes"})
75 }, unique="item_code")
76
77 b.insert()
78 print b.doc.name
79
80def add_random_children(bean, template, rows, randomize, unique=None):
81 for i in xrange(random.randrange(1, rows)):
82 d = template.copy()
83 for key, val in randomize.items():
84 if isinstance(val[0], basestring):
85 d[key] = get_random(*val)
86 else:
87 d[key] = random.randrange(*val)
88
89 if unique:
90 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
91 bean.doclist.append(d)
92 else:
93 bean.doclist.append(d)
94
95
96
97def get_random(doctype, filters=None):
98 condition = []
99 if filters:
100 for key, val in filters.items():
101 condition.append("%s='%s'" % (key, val))
102 if condition:
103 condition = " where " + " and ".join(condition)
104 else:
105 condition = ""
106
107 out = webnotes.conn.sql("""select name from `tab%s` %s
108 order by RAND() limit 0,1""" % (doctype, condition))[0][0]
109
110 return out
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530111
112def install():
113 print "Creating Fresh Database..."
114 from webnotes.install_lib.install import Installer
115 inst = Installer('root')
116 inst.import_from_db("demo", verbose = 1)
117
118def complete_setup():
119 print "Complete Setup..."
120 webnotes.get_obj("Setup Control").setup_account({
121 "first_name": "Test",
122 "last_name": "User",
123 "fy_start": "1st Jan",
124 "industry": "Manufacturing",
125 "company_name": "Wind Power LLC",
126 "company_abbr": "WP",
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530127 "currency": "USD",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530128 "timezone": "America/New York",
129 "country": "United States"
130 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530131
132 print "Importing Fiscal Years..."
133 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Fiscal_Year.csv")
134 upload()
135
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530136
137def make_items():
138 print "Importing Items..."
139 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Item.csv")
140 upload()
Rushabh Mehta2b76a5e2013-08-02 15:16:59 +0530141 print "Importing Item Prices..."
142 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Item_Price.csv")
143 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530144
145def make_customers_suppliers_contacts():
146 print "Importing Customers..."
147 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Customer.csv")
148 upload()
149 print "Importing Suppliers..."
150 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Supplier.csv")
151 upload()
152 print "Importing Contacts..."
153 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Contact.csv")
154 upload()
155 print "Importing Address..."
156 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Address.csv")
157 upload()
158 print "Importing Lead..."
159 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Lead.csv")
160 upload()
161
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530162def make_users_and_employees():
163 print "Importing Profile..."
164 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Profile.csv")
165 upload()
166 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
167 webnotes.conn.commit()
168
169 print "Importing Employee..."
170 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Employee.csv")
171 upload()
172
173 print "Importing Salary Structure..."
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530174 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Salary_Structure.csv")
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530175 upload()
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530176
177if __name__=="__main__":
178 make()