blob: 99c3e8b7e8f46c75eeb79be5f29dbba478f2e269 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# 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 Mehta622c98d2013-08-22 15:17:58 +05306from webnotes.utils import random_string
Rushabh Mehta0b995402013-08-09 15:29:59 +05307from webnotes.widgets import query_report
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +05308import random
Rushabh Mehta4a404e92013-08-09 18:11:35 +05309import json
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053010
Rushabh Mehtaed186b72013-07-30 16:19:40 +053011webnotes.session = webnotes._dict({"user":"Administrator"})
12from core.page.data_import_tool.data_import_tool import upload
13
Rushabh Mehta0b995402013-08-09 15:29:59 +053014# fix price list
15# fix fiscal year
16
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053017company = "Wind Power LLC"
Rushabh Mehta622c98d2013-08-22 15:17:58 +053018company_abbr = "WP"
Rushabh Mehta20a10942013-08-20 15:18:42 +053019country = "United States"
20currency = "USD"
Anand Doshi427935f2013-11-07 15:53:09 +053021time_zone = "America/New_York"
Anand Doshifc13b872014-01-24 18:54:50 +053022start_date = '2014-01-01'
Rushabh Mehta622c98d2013-08-22 15:17:58 +053023bank_name = "Citibank"
Rushabh Mehtaacd33672013-08-26 16:17:50 +053024runs_for = None
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053025prob = {
Rushabh Mehta20a10942013-08-20 15:18:42 +053026 "default": { "make": 0.6, "qty": (1,5) },
Rushabh Mehta16a62fa2013-08-23 13:16:22 +053027 "Sales Order": { "make": 0.4, "qty": (1,3) },
Rushabh Mehta20a10942013-08-20 15:18:42 +053028 "Purchase Order": { "make": 0.7, "qty": (1,15) },
29 "Purchase Receipt": { "make": 0.7, "qty": (1,15) },
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053030}
31
Rushabh Mehtae499a482013-09-01 11:14:32 +053032def make(reset=False, simulate=True):
Rushabh Mehta5d455572013-10-08 18:23:05 +053033 #webnotes.flags.print_messages = True
34 webnotes.flags.mute_emails = True
35 webnotes.flags.rollback_on_exception = True
Rushabh Mehta4a404e92013-08-09 18:11:35 +053036
Anand Doshif879a9d2013-09-26 15:56:54 +053037 if not webnotes.conf.demo_db_name:
38 raise Exception("conf.py does not have demo_db_name")
39
Rushabh Mehta4c17f942013-08-12 14:18:09 +053040 if reset:
41 setup()
Anand Doshide8b6aa2013-09-24 17:17:39 +053042 else:
Anand Doshifd3adda2013-10-03 13:19:51 +053043 if webnotes.conn:
44 webnotes.conn.close()
45
Anand Doshif879a9d2013-09-26 15:56:54 +053046 webnotes.connect(db_name=webnotes.conf.demo_db_name)
Anand Doshide8b6aa2013-09-24 17:17:39 +053047
Rushabh Mehtae499a482013-09-01 11:14:32 +053048 if simulate:
Anand Doshi9283b6c2013-09-02 14:58:45 +053049 _simulate()
Anand Doshide8b6aa2013-09-24 17:17:39 +053050
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053051def setup():
Rushabh Mehtaed186b72013-07-30 16:19:40 +053052 install()
Anand Doshif879a9d2013-09-26 15:56:54 +053053 webnotes.connect(db_name=webnotes.conf.demo_db_name)
Rushabh Mehtaed186b72013-07-30 16:19:40 +053054 complete_setup()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053055 make_customers_suppliers_contacts()
Rushabh Mehta4a404e92013-08-09 18:11:35 +053056 make_items()
Akhilesh Darjeec96c13a2013-09-12 19:19:46 +053057 make_price_lists()
Rushabh Mehta0b50b482013-08-06 17:53:41 +053058 make_users_and_employees()
Rushabh Mehta622c98d2013-08-22 15:17:58 +053059 make_bank_account()
Rushabh Mehtaed186b72013-07-30 16:19:40 +053060 # make_opening_stock()
61 # make_opening_accounts()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053062
Anand Doshi9283b6c2013-09-02 14:58:45 +053063def _simulate():
Rushabh Mehtaacd33672013-08-26 16:17:50 +053064 global runs_for
65 current_date = webnotes.utils.getdate(start_date)
66
67 # continue?
68 last_posting = webnotes.conn.sql("""select max(posting_date) from `tabStock Ledger Entry`""")
69 if last_posting[0][0]:
70 current_date = webnotes.utils.add_days(last_posting[0][0], 1)
71
72 # run till today
73 if not runs_for:
74 runs_for = webnotes.utils.date_diff(webnotes.utils.nowdate(), current_date)
75
76 for i in xrange(runs_for):
Rushabh Mehta20a10942013-08-20 15:18:42 +053077 print current_date.strftime("%Y-%m-%d")
Anand Doshi77989342013-10-10 12:03:11 +053078 webnotes.local.current_date = current_date
Rushabh Mehta20a10942013-08-20 15:18:42 +053079
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053080 if current_date.weekday() in (5, 6):
Rushabh Mehtaacd33672013-08-26 16:17:50 +053081 current_date = webnotes.utils.add_days(current_date, 1)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053082 continue
83
84 run_sales(current_date)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053085 run_purchase(current_date)
86 run_manufacturing(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053087 run_stock(current_date)
Rushabh Mehta622c98d2013-08-22 15:17:58 +053088 run_accounts(current_date)
Rushabh Mehtaacd33672013-08-26 16:17:50 +053089
90 current_date = webnotes.utils.add_days(current_date, 1)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053091
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053092def run_sales(current_date):
Rushabh Mehta0b995402013-08-09 15:29:59 +053093 if can_make("Quotation"):
94 for i in xrange(how_many("Quotation")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053095 make_quotation(current_date)
Rushabh Mehta0b995402013-08-09 15:29:59 +053096
97 if can_make("Sales Order"):
98 for i in xrange(how_many("Sales Order")):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +053099 make_sales_order(current_date)
100
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530101def run_accounts(current_date):
102 if can_make("Sales Invoice"):
103 from selling.doctype.sales_order.sales_order import make_sales_invoice
104 report = "Ordered Items to be Billed"
105 for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Sales Invoice")]:
106 si = webnotes.bean(make_sales_invoice(so))
107 si.doc.posting_date = current_date
Anand Doshifc13b872014-01-24 18:54:50 +0530108 for d in si.doclist.get({"parentfield": "entries"}):
109 if not d.income_account:
110 d.income_account = "Sales - {}".format(company_abbr)
111
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530112 si.insert()
113 si.submit()
114 webnotes.conn.commit()
115
116 if can_make("Purchase Invoice"):
117 from stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
118 report = "Received Items to be Billed"
119 for pr in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Invoice")]:
120 pi = webnotes.bean(make_purchase_invoice(pr))
121 pi.doc.posting_date = current_date
122 pi.doc.bill_no = random_string(6)
123 pi.insert()
124 pi.submit()
125 webnotes.conn.commit()
126
127 if can_make("Payment Received"):
128 from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_sales_invoice
129 report = "Accounts Receivable"
130 for si in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Sales Invoice"]))[:how_many("Payment Received")]:
131 jv = webnotes.bean(get_payment_entry_from_sales_invoice(si))
132 jv.doc.posting_date = current_date
133 jv.doc.cheque_no = random_string(6)
134 jv.doc.cheque_date = current_date
135 jv.insert()
136 jv.submit()
137 webnotes.conn.commit()
138
139 if can_make("Payment Made"):
140 from accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_purchase_invoice
141 report = "Accounts Payable"
142 for pi in list(set([r[4] for r in query_report.run(report, {"report_date": current_date })["result"] if r[3]=="Purchase Invoice"]))[:how_many("Payment Made")]:
143 jv = webnotes.bean(get_payment_entry_from_purchase_invoice(pi))
144 jv.doc.posting_date = current_date
145 jv.doc.cheque_no = random_string(6)
146 jv.doc.cheque_date = current_date
147 jv.insert()
148 jv.submit()
149 webnotes.conn.commit()
150
Rushabh Mehta0b995402013-08-09 15:29:59 +0530151def run_stock(current_date):
152 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530153 if can_make("Purchase Receipt"):
154 from buying.doctype.purchase_order.purchase_order import make_purchase_receipt
Anand Doshif879a9d2013-09-26 15:56:54 +0530155 from stock.stock_ledger import NegativeStockError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530156 report = "Purchase Order Items To Be Received"
157 for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]:
158 pr = webnotes.bean(make_purchase_receipt(po))
159 pr.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530160 pr.doc.fiscal_year = current_date.year
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530161 pr.insert()
Anand Doshif879a9d2013-09-26 15:56:54 +0530162 try:
163 pr.submit()
164 webnotes.conn.commit()
165 except NegativeStockError: pass
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530166
Rushabh Mehta0b995402013-08-09 15:29:59 +0530167 # make delivery notes (if possible)
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530168 if can_make("Delivery Note"):
169 from selling.doctype.sales_order.sales_order import make_delivery_note
Rushabh Mehta20a10942013-08-20 15:18:42 +0530170 from stock.stock_ledger import NegativeStockError
Anand Doshi35107642013-10-14 18:59:16 +0530171 from stock.doctype.serial_no.serial_no import SerialNoRequiredError, SerialNoQtyError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530172 report = "Ordered Items To Be Delivered"
173 for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]:
174 dn = webnotes.bean(make_delivery_note(so))
175 dn.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530176 dn.doc.fiscal_year = current_date.year
Anand Doshifc13b872014-01-24 18:54:50 +0530177 for d in dn.doclist.get({"parentfield": "delivery_note_details"}):
178 d.expense_account = "Cost of Goods Sold - {}".format(company_abbr)
179
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530180 dn.insert()
Rushabh Mehta20a10942013-08-20 15:18:42 +0530181 try:
182 dn.submit()
183 webnotes.conn.commit()
184 except NegativeStockError: pass
185 except SerialNoRequiredError: pass
186 except SerialNoQtyError: pass
187
188 # try submitting existing
189 for dn in webnotes.conn.get_values("Delivery Note", {"docstatus": 0}, "name"):
190 b = webnotes.bean("Delivery Note", dn[0])
191 b.submit()
192 webnotes.conn.commit()
193
Rushabh Mehta0b995402013-08-09 15:29:59 +0530194def run_purchase(current_date):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530195 # make material requests for purchase items that have negative projected qtys
196 if can_make("Material Request"):
197 report = "Items To Be Requested"
198 for row in query_report.run(report)["result"][:how_many("Material Request")]:
199 mr = webnotes.new_bean("Material Request")
200 mr.doc.material_request_type = "Purchase"
201 mr.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530202 mr.doc.fiscal_year = current_date.year
Rushabh Mehta20a10942013-08-20 15:18:42 +0530203 mr.doclist.append({
204 "doctype": "Material Request Item",
205 "parentfield": "indent_details",
206 "schedule_date": webnotes.utils.add_days(current_date, 7),
207 "item_code": row[0],
208 "qty": -row[-1]
209 })
210 mr.insert()
211 mr.submit()
212
Rushabh Mehta0b995402013-08-09 15:29:59 +0530213 # make supplier quotations
214 if can_make("Supplier Quotation"):
215 from stock.doctype.material_request.material_request import make_supplier_quotation
216 report = "Material Requests for which Supplier Quotations are not created"
217 for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530218 if row[0] != "Total":
219 sq = webnotes.bean(make_supplier_quotation(row[0]))
220 sq.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530221 sq.doc.fiscal_year = current_date.year
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530222 sq.insert()
223 sq.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530224 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530225
226 # make purchase orders
227 if can_make("Purchase Order"):
228 from stock.doctype.material_request.material_request import make_purchase_order
229 report = "Requested Items To Be Ordered"
230 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530231 if row[0] != "Total":
232 po = webnotes.bean(make_purchase_order(row[0]))
233 po.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530234 po.doc.fiscal_year = current_date.year
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530235 po.insert()
236 po.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530237 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530238
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530239def run_manufacturing(current_date):
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530240 from stock.stock_ledger import NegativeStockError
Rushabh Mehta20a10942013-08-20 15:18:42 +0530241 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530242
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530243 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
244 ppt.doc.company = company
245 ppt.doc.use_multi_level_bom = 1
Anand Doshifc13b872014-01-24 18:54:50 +0530246 ppt.doc.purchase_request_for_warehouse = "Stores - {}".format(company_abbr)
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530247 ppt.run_method("get_open_sales_orders")
248 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530249 ppt.run_method("raise_production_order")
250 ppt.run_method("raise_purchase_request")
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530251 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530252
253 # submit production orders
Rushabh Mehta20a10942013-08-20 15:18:42 +0530254 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}, "name"):
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530255 b = webnotes.bean("Production Order", pro[0])
256 b.doc.wip_warehouse = "Work in Progress - WP"
257 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530258 webnotes.conn.commit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530259
260 # submit material requests
Rushabh Mehta20a10942013-08-20 15:18:42 +0530261 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}, "name"):
Rushabh Mehta75c31712013-08-08 16:00:40 +0530262 b = webnotes.bean("Material Request", pro[0])
263 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530264 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530265
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530266 # stores -> wip
267 if can_make("Stock Entry for WIP"):
268 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
269 make_stock_entry_from_pro(pro[0], "Material Transfer", current_date)
270
271 # wip -> fg
272 if can_make("Stock Entry for FG"):
273 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
274 make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date)
275
276 # try posting older drafts (if exists)
Rushabh Mehta20a10942013-08-20 15:18:42 +0530277 for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}, "name"):
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530278 try:
279 webnotes.bean("Stock Entry", st[0]).submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530280 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530281 except NegativeStockError: pass
282 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530283 except DuplicateEntryForProductionOrderError: pass
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530284
285def make_stock_entry_from_pro(pro_id, purpose, current_date):
286 from manufacturing.doctype.production_order.production_order import make_stock_entry
287 from stock.stock_ledger import NegativeStockError
Rushabh Mehta20a10942013-08-20 15:18:42 +0530288 from stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530289
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530290 try:
Anand Doshic753d312013-08-26 17:33:50 +0530291 st = webnotes.bean(make_stock_entry(pro_id, purpose))
292 st.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530293 st.doc.fiscal_year = current_date.year
Nabin Hait142007a2013-09-17 15:15:16 +0530294 for d in st.doclist.get({"parentfield": "mtn_details"}):
295 d.expense_account = "Stock Adjustment - " + company_abbr
296 d.cost_center = "Main - " + company_abbr
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530297 st.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530298 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530299 st.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530300 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530301 except NegativeStockError: pass
302 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530303 except DuplicateEntryForProductionOrderError: pass
Anand Doshic753d312013-08-26 17:33:50 +0530304
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530305def make_quotation(current_date):
306 b = webnotes.bean([{
307 "creation": current_date,
308 "doctype": "Quotation",
309 "quotation_to": "Customer",
310 "customer": get_random("Customer"),
311 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530312 "transaction_date": current_date,
Anand Doshida07ed02013-11-29 14:53:56 +0530313 "fiscal_year": current_date.year
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530314 }])
315
316 add_random_children(b, {
317 "doctype": "Quotation Item",
318 "parentfield": "quotation_details",
319 }, rows=3, randomize = {
320 "qty": (1, 5),
321 "item_code": ("Item", {"is_sales_item": "Yes"})
322 }, unique="item_code")
323
324 b.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530325 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530326 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530327 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530328
329def make_sales_order(current_date):
330 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530331 if q:
332 from selling.doctype.quotation.quotation import make_sales_order
333 so = webnotes.bean(make_sales_order(q))
334 so.doc.transaction_date = current_date
335 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
336 so.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530337 webnotes.conn.commit()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530338 so.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530339 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530340
341def add_random_children(bean, template, rows, randomize, unique=None):
342 for i in xrange(random.randrange(1, rows)):
343 d = template.copy()
344 for key, val in randomize.items():
345 if isinstance(val[0], basestring):
346 d[key] = get_random(*val)
347 else:
348 d[key] = random.randrange(*val)
349
350 if unique:
351 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
352 bean.doclist.append(d)
353 else:
354 bean.doclist.append(d)
355
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530356def get_random(doctype, filters=None):
357 condition = []
358 if filters:
359 for key, val in filters.items():
360 condition.append("%s='%s'" % (key, val))
361 if condition:
362 condition = " where " + " and ".join(condition)
363 else:
364 condition = ""
365
366 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530367 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530368
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530369 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530370
Rushabh Mehta0b995402013-08-09 15:29:59 +0530371def can_make(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530372 return random.random() < prob.get(doctype, prob["default"])["make"]
Rushabh Mehta0b995402013-08-09 15:29:59 +0530373
374def how_many(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530375 return random.randrange(*prob.get(doctype, prob["default"])["qty"])
Rushabh Mehta0b995402013-08-09 15:29:59 +0530376
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530377def install():
378 print "Creating Fresh Database..."
379 from webnotes.install_lib.install import Installer
Pratik Vyascfed8c42013-09-21 15:16:47 +0530380 from webnotes import conf
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530381 inst = Installer('root')
Anand Doshif879a9d2013-09-26 15:56:54 +0530382 inst.install(conf.demo_db_name, verbose=1, force=1)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530383
384def complete_setup():
385 print "Complete Setup..."
Rushabh Mehta8c5a5f12013-10-08 17:59:11 +0530386 from setup.page.setup_wizard.setup_wizard import setup_account
387 setup_account({
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530388 "first_name": "Test",
389 "last_name": "User",
Anand Doshifc13b872014-01-24 18:54:50 +0530390 "fy_start_date": "2014-01-01",
391 "fy_end_date": "2014-12-31",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530392 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530393 "company_name": company,
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530394 "company_abbr": company_abbr,
Rushabh Mehta20a10942013-08-20 15:18:42 +0530395 "currency": currency,
396 "timezone": time_zone,
397 "country": country
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530398 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530399
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530400 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530401
402def make_items():
Rushabh Mehta20a10942013-08-20 15:18:42 +0530403 import_data("Item")
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530404 import_data("BOM", submit=True)
Akhilesh Darjeec96c13a2013-09-12 19:19:46 +0530405
406def make_price_lists():
Anand Doshifec52b52013-10-28 19:59:09 +0530407 import_data("Item_Price", overwrite=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530408
409def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530410 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530411
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530412def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530413 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
414 webnotes.conn.commit()
415
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530416 import_data(["Profile", "Employee", "Salary_Structure"])
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530417
418def make_bank_account():
419 ba = webnotes.bean({
420 "doctype": "Account",
421 "account_name": bank_name,
422 "account_type": "Bank or Cash",
423 "group_or_ledger": "Ledger",
424 "parent_account": "Bank Accounts - " + company_abbr,
425 "company": company
426 }).insert()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530427
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530428 webnotes.set_value("Company", company, "default_bank_account", ba.doc.name)
Rushabh Mehtaacd33672013-08-26 16:17:50 +0530429 webnotes.conn.commit()
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530430
Akhilesh Darjee2e1f94f2013-09-13 17:37:23 +0530431def import_data(dt, submit=False, overwrite=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530432 if not isinstance(dt, (tuple, list)):
433 dt = [dt]
434
435 for doctype in dt:
436 print "Importing", doctype.replace("_", " "), "..."
Anand Doshia295bf52013-10-15 19:52:29 +0530437 webnotes.local.form_dict = webnotes._dict()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530438 if submit:
439 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530440 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
Pratik Vyascfed8c42013-09-21 15:16:47 +0530441 upload(overwrite=overwrite)