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 | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 11 | start_date = '2010-01-01' |
| 12 | runs_for = 100 |
| 13 | prob = { |
| 14 | "Quotation": 0.5 |
| 15 | } |
| 16 | |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 17 | def make(): |
| 18 | webnotes.connect() |
Rushabh Mehta | 2b76a5e | 2013-08-02 15:16:59 +0530 | [diff] [blame] | 19 | webnotes.print_messages = True |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 20 | webnotes.mute_emails = True |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 21 | |
| 22 | # setup() |
| 23 | # simulate() |
| 24 | make_quotation("2010-01-01") |
| 25 | webnotes.conn.commit() |
| 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_bom() |
| 34 | # make_opening_stock() |
| 35 | # make_opening_accounts() |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 36 | |
| 37 | def 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 | |
| 53 | def run_sales(current_date): |
| 54 | if random.random() < prob["Quotation"]: |
| 55 | make_quotation(current_date) |
| 56 | |
| 57 | def 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 | |
| 80 | def 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 | |
| 97 | def 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 Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 111 | |
| 112 | def 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 | |
| 118 | def 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 Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 127 | "currency": "USD", |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 128 | "timezone": "America/New York", |
| 129 | "country": "United States" |
| 130 | }) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 131 | |
| 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 Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 136 | |
| 137 | def make_items(): |
| 138 | print "Importing Items..." |
| 139 | webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Item.csv") |
| 140 | upload() |
Rushabh Mehta | 2b76a5e | 2013-08-02 15:16:59 +0530 | [diff] [blame] | 141 | print "Importing Item Prices..." |
| 142 | webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Item_Price.csv") |
| 143 | upload() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 144 | |
| 145 | def 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 Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 162 | def 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 Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 174 | webnotes.uploaded_file = os.path.join(os.path.dirname(__file__), "demo_docs", "Salary_Structure.csv") |
Rushabh Mehta | 0b50b48 | 2013-08-06 17:53:41 +0530 | [diff] [blame] | 175 | upload() |
Rushabh Mehta | ed186b7 | 2013-07-30 16:19:40 +0530 | [diff] [blame] | 176 | |
| 177 | if __name__=="__main__": |
| 178 | make() |