Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 4 | import webnotes, os, datetime |
| 5 | import webnotes.utils |
| 6 | import random |
| 7 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 8 | webnotes.session = webnotes._dict({"user":"Administrator"}) |
| 9 | from core.page.data_import_tool.data_import_tool import upload |
| 10 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 11 | company = "Wind Power LLC" |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 12 | start_date = '2010-01-01' |
| 13 | runs_for = 100 |
| 14 | prob = { |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 15 | "Quotation": { "make": 0.5, "qty": (1,3) }, |
| 16 | "Sales Order": { "make": 0.5, "qty": (1,2) } |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 17 | } |
| 18 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 19 | def make(): |
| 20 | webnotes.connect() |
Rushabh Mehta | 2b76a5e | 2013-08-02 15:16:59 +0530 | [diff] [blame] | 21 | webnotes.print_messages = True |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 22 | webnotes.mute_emails = True |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 23 | |
| 24 | # setup() |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 25 | simulate() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 26 | |
| 27 | def setup(): |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 28 | install() |
| 29 | complete_setup() |
| 30 | make_items() |
| 31 | make_customers_suppliers_contacts() |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 32 | make_users_and_employees() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 33 | # make_opening_stock() |
| 34 | # make_opening_accounts() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 35 | |
| 36 | def simulate(): |
| 37 | current_date = None |
| 38 | for i in xrange(runs_for): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 39 | print i |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 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) |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 49 | run_purchase(current_date) |
| 50 | run_manufacturing(current_date) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 52 | webnotes.conn.commit() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 53 | |
| 54 | def run_sales(current_date): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 55 | 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 | |
| 63 | def run_purchase(current_date): |
| 64 | pass |
| 65 | |
| 66 | def 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 Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 77 | def 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 Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 98 | b.submit() |
| 99 | |
| 100 | def 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 Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 108 | |
| 109 | def 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 Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 124 | def 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 Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 138 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 139 | def 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 | |
| 145 | def 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 Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 152 | "company_name": company, |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 153 | "company_abbr": "WP", |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 154 | "currency": "USD", |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 155 | "timezone": "America/New York", |
| 156 | "country": "United States" |
| 157 | }) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 158 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 159 | import_data("Fiscal_Year") |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 160 | |
| 161 | def make_items(): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 162 | import_data(["Item", "Item_Price", "BOM"]) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 163 | |
| 164 | def make_customers_suppliers_contacts(): |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 165 | import_data(["Customer", "Supplier", "Contact", "Address", "Lead"]) |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 166 | |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 167 | def make_users_and_employees(): |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 168 | webnotes.conn.set_value("HR Settings", None, "emp_created_by", "Naming Series") |
| 169 | webnotes.conn.commit() |
| 170 | |
Rushabh Mehta | 7cfefbc | 2013-08-07 17:46:35 +0530 | [diff] [blame] | 171 | import_data(["Profile", "Employee", "Salary_Structure"]) |
| 172 | |
| 173 | def 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 Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 181 | |
| 182 | if __name__=="__main__": |
| 183 | make() |