blob: f4691395137700ec479d21d54bb6672ab55a6074 [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"})
Rushabh Mehta39bb8e22013-12-11 15:32:14 +053012from webnotes.core.page.data_import_tool.data_import_tool import upload
Rushabh Mehtaed186b72013-07-30 16:19:40 +053013
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"
Rushabh Mehtaacd33672013-08-26 16:17:50 +053022start_date = '2013-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"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530103 from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530104 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
108 si.insert()
109 si.submit()
110 webnotes.conn.commit()
111
112 if can_make("Purchase Invoice"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530113 from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530114 report = "Received Items to be Billed"
115 for pr in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Invoice")]:
116 pi = webnotes.bean(make_purchase_invoice(pr))
117 pi.doc.posting_date = current_date
118 pi.doc.bill_no = random_string(6)
119 pi.insert()
120 pi.submit()
121 webnotes.conn.commit()
122
123 if can_make("Payment Received"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530124 from erpnext.accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_sales_invoice
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530125 report = "Accounts Receivable"
126 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")]:
127 jv = webnotes.bean(get_payment_entry_from_sales_invoice(si))
128 jv.doc.posting_date = current_date
129 jv.doc.cheque_no = random_string(6)
130 jv.doc.cheque_date = current_date
131 jv.insert()
132 jv.submit()
133 webnotes.conn.commit()
134
135 if can_make("Payment Made"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530136 from erpnext.accounts.doctype.journal_voucher.journal_voucher import get_payment_entry_from_purchase_invoice
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530137 report = "Accounts Payable"
138 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")]:
139 jv = webnotes.bean(get_payment_entry_from_purchase_invoice(pi))
140 jv.doc.posting_date = current_date
141 jv.doc.cheque_no = random_string(6)
142 jv.doc.cheque_date = current_date
143 jv.insert()
144 jv.submit()
145 webnotes.conn.commit()
146
Rushabh Mehta0b995402013-08-09 15:29:59 +0530147def run_stock(current_date):
148 # make purchase requests
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530149 if can_make("Purchase Receipt"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530150 from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_receipt
151 from erpnext.stock.stock_ledger import NegativeStockError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530152 report = "Purchase Order Items To Be Received"
153 for po in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Purchase Receipt")]:
154 pr = webnotes.bean(make_purchase_receipt(po))
155 pr.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530156 pr.doc.fiscal_year = current_date.year
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530157 pr.insert()
Anand Doshif879a9d2013-09-26 15:56:54 +0530158 try:
159 pr.submit()
160 webnotes.conn.commit()
161 except NegativeStockError: pass
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530162
Rushabh Mehta0b995402013-08-09 15:29:59 +0530163 # make delivery notes (if possible)
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530164 if can_make("Delivery Note"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530165 from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note
166 from erpnext.stock.stock_ledger import NegativeStockError
167 from erpnext.stock.doctype.serial_no.serial_no import SerialNoRequiredError, SerialNoQtyError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530168 report = "Ordered Items To Be Delivered"
169 for so in list(set([r[0] for r in query_report.run(report)["result"] if r[0]!="Total"]))[:how_many("Delivery Note")]:
170 dn = webnotes.bean(make_delivery_note(so))
171 dn.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530172 dn.doc.fiscal_year = current_date.year
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530173 dn.insert()
Rushabh Mehta20a10942013-08-20 15:18:42 +0530174 try:
175 dn.submit()
176 webnotes.conn.commit()
177 except NegativeStockError: pass
178 except SerialNoRequiredError: pass
179 except SerialNoQtyError: pass
180
181 # try submitting existing
182 for dn in webnotes.conn.get_values("Delivery Note", {"docstatus": 0}, "name"):
183 b = webnotes.bean("Delivery Note", dn[0])
184 b.submit()
185 webnotes.conn.commit()
186
Rushabh Mehta0b995402013-08-09 15:29:59 +0530187def run_purchase(current_date):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530188 # make material requests for purchase items that have negative projected qtys
189 if can_make("Material Request"):
190 report = "Items To Be Requested"
191 for row in query_report.run(report)["result"][:how_many("Material Request")]:
192 mr = webnotes.new_bean("Material Request")
193 mr.doc.material_request_type = "Purchase"
194 mr.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530195 mr.doc.fiscal_year = current_date.year
Rushabh Mehta20a10942013-08-20 15:18:42 +0530196 mr.doclist.append({
197 "doctype": "Material Request Item",
198 "parentfield": "indent_details",
199 "schedule_date": webnotes.utils.add_days(current_date, 7),
200 "item_code": row[0],
201 "qty": -row[-1]
202 })
203 mr.insert()
204 mr.submit()
205
Rushabh Mehta0b995402013-08-09 15:29:59 +0530206 # make supplier quotations
207 if can_make("Supplier Quotation"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530208 from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
Rushabh Mehta0b995402013-08-09 15:29:59 +0530209 report = "Material Requests for which Supplier Quotations are not created"
210 for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530211 if row[0] != "Total":
212 sq = webnotes.bean(make_supplier_quotation(row[0]))
213 sq.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530214 sq.doc.fiscal_year = current_date.year
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530215 sq.insert()
216 sq.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530217 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530218
219 # make purchase orders
220 if can_make("Purchase Order"):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530221 from erpnext.stock.doctype.material_request.material_request import make_purchase_order
Rushabh Mehta0b995402013-08-09 15:29:59 +0530222 report = "Requested Items To Be Ordered"
223 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530224 if row[0] != "Total":
225 po = webnotes.bean(make_purchase_order(row[0]))
226 po.doc.transaction_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530227 po.doc.fiscal_year = current_date.year
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530228 po.insert()
229 po.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530230 webnotes.conn.commit()
Rushabh Mehta0b995402013-08-09 15:29:59 +0530231
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530232def run_manufacturing(current_date):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530233 from erpnext.stock.stock_ledger import NegativeStockError
234 from erpnext.stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530235
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530236 ppt = webnotes.bean("Production Planning Tool", "Production Planning Tool")
237 ppt.doc.company = company
238 ppt.doc.use_multi_level_bom = 1
239 ppt.doc.purchase_request_for_warehouse = "Stores - WP"
240 ppt.run_method("get_open_sales_orders")
241 ppt.run_method("get_items_from_so")
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530242 ppt.run_method("raise_production_order")
243 ppt.run_method("raise_purchase_request")
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530244 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530245
246 # submit production orders
Rushabh Mehta20a10942013-08-20 15:18:42 +0530247 for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}, "name"):
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530248 b = webnotes.bean("Production Order", pro[0])
249 b.doc.wip_warehouse = "Work in Progress - WP"
250 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530251 webnotes.conn.commit()
Rushabh Mehta75c31712013-08-08 16:00:40 +0530252
253 # submit material requests
Rushabh Mehta20a10942013-08-20 15:18:42 +0530254 for pro in webnotes.conn.get_values("Material Request", {"docstatus": 0}, "name"):
Rushabh Mehta75c31712013-08-08 16:00:40 +0530255 b = webnotes.bean("Material Request", pro[0])
256 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530257 webnotes.conn.commit()
Rushabh Mehta2c968ce2013-08-08 14:16:39 +0530258
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530259 # stores -> wip
260 if can_make("Stock Entry for WIP"):
261 for pro in query_report.run("Open Production Orders")["result"][:how_many("Stock Entry for WIP")]:
262 make_stock_entry_from_pro(pro[0], "Material Transfer", current_date)
263
264 # wip -> fg
265 if can_make("Stock Entry for FG"):
266 for pro in query_report.run("Production Orders in Progress")["result"][:how_many("Stock Entry for FG")]:
267 make_stock_entry_from_pro(pro[0], "Manufacture/Repack", current_date)
268
269 # try posting older drafts (if exists)
Rushabh Mehta20a10942013-08-20 15:18:42 +0530270 for st in webnotes.conn.get_values("Stock Entry", {"docstatus":0}, "name"):
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530271 try:
272 webnotes.bean("Stock Entry", st[0]).submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530273 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530274 except NegativeStockError: pass
275 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530276 except DuplicateEntryForProductionOrderError: pass
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530277
278def make_stock_entry_from_pro(pro_id, purpose, current_date):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530279 from erpnext.manufacturing.doctype.production_order.production_order import make_stock_entry
280 from erpnext.stock.stock_ledger import NegativeStockError
281 from erpnext.stock.doctype.stock_entry.stock_entry import IncorrectValuationRateError, DuplicateEntryForProductionOrderError
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530282
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530283 try:
Anand Doshic753d312013-08-26 17:33:50 +0530284 st = webnotes.bean(make_stock_entry(pro_id, purpose))
285 st.doc.posting_date = current_date
Anand Doshida07ed02013-11-29 14:53:56 +0530286 st.doc.fiscal_year = current_date.year
Nabin Hait142007a2013-09-17 15:15:16 +0530287 for d in st.doclist.get({"parentfield": "mtn_details"}):
288 d.expense_account = "Stock Adjustment - " + company_abbr
289 d.cost_center = "Main - " + company_abbr
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530290 st.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530291 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530292 st.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530293 webnotes.conn.commit()
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530294 except NegativeStockError: pass
295 except IncorrectValuationRateError: pass
Rushabh Mehta20a10942013-08-20 15:18:42 +0530296 except DuplicateEntryForProductionOrderError: pass
Anand Doshic753d312013-08-26 17:33:50 +0530297
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530298def make_quotation(current_date):
299 b = webnotes.bean([{
300 "creation": current_date,
301 "doctype": "Quotation",
302 "quotation_to": "Customer",
303 "customer": get_random("Customer"),
304 "order_type": "Sales",
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530305 "transaction_date": current_date,
Anand Doshida07ed02013-11-29 14:53:56 +0530306 "fiscal_year": current_date.year
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530307 }])
308
309 add_random_children(b, {
310 "doctype": "Quotation Item",
311 "parentfield": "quotation_details",
312 }, rows=3, randomize = {
313 "qty": (1, 5),
314 "item_code": ("Item", {"is_sales_item": "Yes"})
315 }, unique="item_code")
316
317 b.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530318 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530319 b.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530320 webnotes.conn.commit()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530321
322def make_sales_order(current_date):
323 q = get_random("Quotation", {"status": "Submitted"})
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530324 if q:
Rushabh Mehta1f847992013-12-12 19:12:19 +0530325 from erpnext.selling.doctype.quotation.quotation import make_sales_order
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530326 so = webnotes.bean(make_sales_order(q))
327 so.doc.transaction_date = current_date
328 so.doc.delivery_date = webnotes.utils.add_days(current_date, 10)
329 so.insert()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530330 webnotes.conn.commit()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530331 so.submit()
Rushabh Mehta6ca903f2013-08-13 14:31:15 +0530332 webnotes.conn.commit()
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530333
334def add_random_children(bean, template, rows, randomize, unique=None):
335 for i in xrange(random.randrange(1, rows)):
336 d = template.copy()
337 for key, val in randomize.items():
338 if isinstance(val[0], basestring):
339 d[key] = get_random(*val)
340 else:
341 d[key] = random.randrange(*val)
342
343 if unique:
344 if not bean.doclist.get({"doctype": d["doctype"], unique:d[unique]}):
345 bean.doclist.append(d)
346 else:
347 bean.doclist.append(d)
348
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530349def get_random(doctype, filters=None):
350 condition = []
351 if filters:
352 for key, val in filters.items():
353 condition.append("%s='%s'" % (key, val))
354 if condition:
355 condition = " where " + " and ".join(condition)
356 else:
357 condition = ""
358
359 out = webnotes.conn.sql("""select name from `tab%s` %s
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530360 order by RAND() limit 0,1""" % (doctype, condition))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530361
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530362 return out and out[0][0] or None
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530363
Rushabh Mehta0b995402013-08-09 15:29:59 +0530364def can_make(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530365 return random.random() < prob.get(doctype, prob["default"])["make"]
Rushabh Mehta0b995402013-08-09 15:29:59 +0530366
367def how_many(doctype):
Rushabh Mehta20a10942013-08-20 15:18:42 +0530368 return random.randrange(*prob.get(doctype, prob["default"])["qty"])
Rushabh Mehta0b995402013-08-09 15:29:59 +0530369
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530370def install():
371 print "Creating Fresh Database..."
372 from webnotes.install_lib.install import Installer
Pratik Vyascfed8c42013-09-21 15:16:47 +0530373 from webnotes import conf
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530374 inst = Installer('root')
Anand Doshif879a9d2013-09-26 15:56:54 +0530375 inst.install(conf.demo_db_name, verbose=1, force=1)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530376
377def complete_setup():
378 print "Complete Setup..."
Rushabh Mehta1f847992013-12-12 19:12:19 +0530379 from erpnext.setup.page.setup_wizard.setup_wizard import setup_account
Rushabh Mehta8c5a5f12013-10-08 17:59:11 +0530380 setup_account({
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530381 "first_name": "Test",
382 "last_name": "User",
Akhilesh Darjeea8e704d2013-11-27 16:16:32 +0530383 "fy_start_date": "2013-01-01",
384 "fy_end_date": "2013-12-31",
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530385 "industry": "Manufacturing",
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530386 "company_name": company,
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530387 "company_abbr": company_abbr,
Rushabh Mehta20a10942013-08-20 15:18:42 +0530388 "currency": currency,
389 "timezone": time_zone,
390 "country": country
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530391 })
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530392
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530393 import_data("Fiscal_Year")
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530394
395def make_items():
Rushabh Mehta20a10942013-08-20 15:18:42 +0530396 import_data("Item")
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530397 import_data("BOM", submit=True)
Akhilesh Darjeec96c13a2013-09-12 19:19:46 +0530398
399def make_price_lists():
Anand Doshifec52b52013-10-28 19:59:09 +0530400 import_data("Item_Price", overwrite=True)
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530401
402def make_customers_suppliers_contacts():
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530403 import_data(["Customer", "Supplier", "Contact", "Address", "Lead"])
Rushabh Mehtaed186b72013-07-30 16:19:40 +0530404
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530405def make_users_and_employees():
Rushabh Mehta0b50b482013-08-06 17:53:41 +0530406 webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series")
407 webnotes.conn.commit()
408
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530409 import_data(["Profile", "Employee", "Salary_Structure"])
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530410
411def make_bank_account():
412 ba = webnotes.bean({
413 "doctype": "Account",
414 "account_name": bank_name,
415 "account_type": "Bank or Cash",
416 "group_or_ledger": "Ledger",
417 "parent_account": "Bank Accounts - " + company_abbr,
418 "company": company
419 }).insert()
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530420
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530421 webnotes.set_value("Company", company, "default_bank_account", ba.doc.name)
Rushabh Mehtaacd33672013-08-26 16:17:50 +0530422 webnotes.conn.commit()
Rushabh Mehta622c98d2013-08-22 15:17:58 +0530423
Akhilesh Darjee2e1f94f2013-09-13 17:37:23 +0530424def import_data(dt, submit=False, overwrite=False):
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530425 if not isinstance(dt, (tuple, list)):
426 dt = [dt]
427
428 for doctype in dt:
429 print "Importing", doctype.replace("_", " "), "..."
Anand Doshia295bf52013-10-15 19:52:29 +0530430 webnotes.local.form_dict = webnotes._dict()
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530431 if submit:
432 webnotes.form_dict["params"] = json.dumps({"_submit": 1})
Rushabh Mehta7cfefbc2013-08-07 17:46:35 +0530433 webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", doctype+".csv")
Pratik Vyascfed8c42013-09-21 15:16:47 +0530434 upload(overwrite=overwrite)