Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | |
| 3 | import random, json |
| 4 | from erpnext.demo.domains import data |
| 5 | import frappe, erpnext |
| 6 | import frappe.utils |
| 7 | |
| 8 | def setup_data(): |
| 9 | domain = frappe.flags.domain |
| 10 | complete_setup(domain) |
| 11 | setup_demo_page() |
| 12 | setup_fiscal_year() |
| 13 | setup_holiday_list() |
| 14 | setup_customer() |
| 15 | setup_supplier() |
| 16 | setup_item() |
| 17 | import_json('Address') |
| 18 | import_json('Contact') |
| 19 | setup_workstation() |
| 20 | import_json('Operation') |
| 21 | import_json('Lead') |
| 22 | setup_item_price() |
| 23 | show_item_groups_in_website() |
| 24 | setup_currency_exchange() |
| 25 | import_json('BOM', submit=True) |
| 26 | setup_user() |
| 27 | setup_employee() |
| 28 | setup_salary_structure() |
| 29 | setup_user_roles() |
| 30 | |
| 31 | def complete_setup(domain='Manufacturing'): |
| 32 | print "Complete Setup..." |
| 33 | from frappe.desk.page.setup_wizard.setup_wizard import setup_complete |
| 34 | |
| 35 | if not frappe.get_all('Company', limit=1): |
| 36 | setup_complete({ |
| 37 | "first_name": "Test", |
| 38 | "last_name": "User", |
| 39 | "email": "test_demo@erpnext.com", |
| 40 | "company_tagline": 'Awesome Products and Services', |
| 41 | "password": "test", |
| 42 | "fy_start_date": "2015-01-01", |
| 43 | "fy_end_date": "2015-12-31", |
| 44 | "bank_account": "National Bank", |
| 45 | "industry": domain, |
| 46 | "company_name": data.get(domain).get('company_name'), |
| 47 | "chart_of_accounts": "Standard", |
| 48 | "company_abbr": ''.join([d[0] for d in data.get(domain).get('company_name').split()]).upper(), |
| 49 | "currency": 'USD', |
| 50 | "timezone": 'America/New_York', |
| 51 | "country": 'United States', |
| 52 | "language": "english" |
| 53 | }) |
| 54 | |
| 55 | def setup_demo_page(): |
| 56 | # home page should always be "start" |
| 57 | website_settings = frappe.get_doc("Website Settings", "Website Settings") |
| 58 | website_settings.home_page = "demo" |
| 59 | website_settings.save() |
| 60 | |
| 61 | def setup_fiscal_year(): |
| 62 | fiscal_year = None |
| 63 | for year in xrange(2014, frappe.utils.now_datetime().year + 1, 1): |
| 64 | try: |
| 65 | fiscal_year = frappe.get_doc({ |
| 66 | "doctype": "Fiscal Year", |
| 67 | "year": frappe.utils.cstr(year), |
| 68 | "year_start_date": "{0}-01-01".format(year), |
| 69 | "year_end_date": "{0}-12-31".format(year) |
| 70 | }).insert() |
| 71 | except frappe.DuplicateEntryError: |
| 72 | pass |
| 73 | |
| 74 | # set the last fiscal year (current year) as default |
| 75 | fiscal_year.set_as_default() |
| 76 | |
| 77 | def setup_holiday_list(): |
| 78 | """Setup Holiday List for the current year""" |
| 79 | year = frappe.utils.now_datetime().year |
| 80 | holiday_list = frappe.get_doc({ |
| 81 | "doctype": "Holiday List", |
| 82 | "holiday_list_name": str(year), |
| 83 | "from_date": "{0}-01-01".format(year), |
| 84 | "to_date": "{0}-12-31".format(year), |
| 85 | }) |
| 86 | holiday_list.insert() |
| 87 | holiday_list.weekly_off = "Saturday" |
| 88 | holiday_list.get_weekly_off_dates() |
| 89 | holiday_list.weekly_off = "Sunday" |
| 90 | holiday_list.get_weekly_off_dates() |
| 91 | holiday_list.save() |
| 92 | |
| 93 | frappe.set_value("Company", erpnext.get_default_company(), "default_holiday_list", holiday_list.name) |
| 94 | |
| 95 | def setup_customer(): |
| 96 | customers = [u'Asian Junction', u'Life Plan Counselling', u'Two Pesos', u'Mr Fables', u'Intelacard', u'Big D Supermarkets', u'Adaptas', u'Nelson Brothers', u'Landskip Yard Care', u'Buttrey Food & Drug', u'Fayva', u'Asian Fusion', u'Crafts Canada', u'Consumers and Consumers Express', u'Netobill', u'Choices', u'Chi-Chis', u'Red Food', u'Endicott Shoes', u'Hind Enterprises'] |
| 97 | for c in customers: |
| 98 | frappe.get_doc({ |
| 99 | "doctype": "Customer", |
| 100 | "customer_name": c, |
| 101 | "customer_group": "Commercial", |
| 102 | "customer_type": random.choice(["Company", "Individual"]), |
| 103 | "territory": "Rest Of The World" |
| 104 | }).insert() |
| 105 | |
| 106 | def setup_supplier(): |
| 107 | suppliers = [u'Helios Air', u'Ks Merchandise', u'HomeBase', u'Scott Ties', u'Reliable Investments', u'Nan Duskin', u'Rainbow Records', u'New World Realty', u'Asiatic Solutions', u'Eagle Hardware', u'Modern Electricals'] |
| 108 | for s in suppliers: |
| 109 | frappe.get_doc({ |
| 110 | "doctype": "Supplier", |
| 111 | "supplier_name": s, |
| 112 | "supplier_type": random.choice(["Services", "Raw Material"]), |
| 113 | }).insert() |
| 114 | |
| 115 | def setup_workstation(): |
| 116 | workstations = [u'Drilling Machine 1', u'Lathe 1', u'Assembly Station 1', u'Assembly Station 2', u'Packing and Testing Station'] |
| 117 | for w in workstations: |
| 118 | frappe.get_doc({ |
| 119 | "doctype": "Workstation", |
| 120 | "workstation_name": w, |
| 121 | "holiday_list": frappe.get_all("Holiday List")[0].name, |
| 122 | "hour_rate_consumable": int(random.random() * 20), |
| 123 | "hour_rate_electricity": int(random.random() * 10), |
| 124 | "hour_rate_labour": int(random.random() * 40), |
| 125 | "hour_rate_rent": int(random.random() * 10), |
| 126 | "working_hours": [ |
| 127 | { |
| 128 | "enabled": 1, |
| 129 | "start_time": "8:00:00", |
| 130 | "end_time": "15:00:00" |
| 131 | } |
| 132 | ] |
| 133 | }).insert() |
| 134 | |
| 135 | def show_item_groups_in_website(): |
| 136 | """set show_in_website=1 for Item Groups""" |
| 137 | products = frappe.get_doc("Item Group", "Products") |
| 138 | products.show_in_website = 1 |
| 139 | products.route = 'products' |
| 140 | products.save() |
| 141 | |
| 142 | def setup_item(): |
| 143 | items = json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', 'item.json')).read()) |
| 144 | for i in items: |
| 145 | item = frappe.new_doc('Item') |
| 146 | item.update(i) |
| 147 | item.default_warehouse = frappe.get_all('Warehouse', filters={'warehouse_name': item.default_warehouse}, limit=1)[0].name |
| 148 | item.insert() |
| 149 | |
| 150 | def setup_currency_exchange(): |
| 151 | frappe.get_doc({ |
| 152 | 'doctype': 'Currency Exchange', |
| 153 | 'from_currency': 'EUR', |
| 154 | 'to_currency': 'USD', |
| 155 | 'exchange_rate': 1.13 |
| 156 | }).insert() |
| 157 | |
| 158 | frappe.get_doc({ |
| 159 | 'doctype': 'Currency Exchange', |
| 160 | 'from_currency': 'CNY', |
| 161 | 'to_currency': 'USD', |
| 162 | 'exchange_rate': 0.16 |
| 163 | }).insert() |
| 164 | |
| 165 | def setup_product_bundle(): |
| 166 | frappe.get_doc({ |
| 167 | 'doctype': 'Product Bundle', |
| 168 | 'new_item_code': 'Wind Mill A Series with Spare Bearing', |
| 169 | 'items': [ |
| 170 | {'item_code': 'Wind Mill A Series', 'qty': 1}, |
| 171 | {'item_code': 'Bearing Collar', 'qty': 1}, |
| 172 | {'item_code': 'Bearing Assembly', 'qty': 1}, |
| 173 | ] |
| 174 | }).insert() |
| 175 | |
| 176 | def setup_user(): |
| 177 | frappe.db.sql('delete from tabUser where name not in ("Guest", "Administrator")') |
| 178 | for u in json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', 'user.json')).read()): |
| 179 | user = frappe.new_doc("User") |
| 180 | user.update(u) |
| 181 | user.flags.no_welcome_mail |
| 182 | user.password = 'demo' |
| 183 | user.insert() |
| 184 | |
| 185 | def import_json(doctype, submit=False): |
| 186 | frappe.flags.in_import = True |
| 187 | data = json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', |
| 188 | frappe.scrub(doctype) + '.json')).read()) |
| 189 | for d in data: |
| 190 | doc = frappe.new_doc(doctype) |
| 191 | doc.update(d) |
| 192 | doc.insert() |
| 193 | if submit: |
| 194 | doc.submit() |
| 195 | |
| 196 | def setup_employee(): |
| 197 | frappe.db.set_value("HR Settings", None, "emp_created_by", "Naming Series") |
| 198 | frappe.db.commit() |
| 199 | |
| 200 | import_json('Employee') |
| 201 | |
| 202 | def setup_item_price(): |
| 203 | frappe.db.sql("delete from `tabItem Price`") |
| 204 | |
| 205 | standard_selling = { |
| 206 | "Base Bearing Plate": 28, |
| 207 | "Base Plate": 21, |
| 208 | "Bearing Assembly": 300, |
| 209 | "Bearing Block": 14, |
| 210 | "Bearing Collar": 103.6, |
| 211 | "Bearing Pipe": 63, |
| 212 | "Blade Rib": 46.2, |
| 213 | "Disc Collars": 42, |
| 214 | "External Disc": 56, |
| 215 | "Internal Disc": 70, |
| 216 | "Shaft": 340, |
| 217 | "Stand": 400, |
| 218 | "Upper Bearing Plate": 300, |
| 219 | "Wind Mill A Series": 320, |
| 220 | "Wind Mill A Series with Spare Bearing": 750, |
| 221 | "Wind MIll C Series": 400, |
| 222 | "Wind Turbine": 400, |
| 223 | "Wing Sheet": 30.8 |
| 224 | } |
| 225 | |
| 226 | standard_buying = { |
| 227 | "Base Bearing Plate": 20, |
| 228 | "Base Plate": 28, |
| 229 | "Base Plate Un Painted": 16, |
| 230 | "Bearing Block": 13, |
| 231 | "Bearing Collar": 96.4, |
| 232 | "Bearing Pipe": 55, |
| 233 | "Blade Rib": 38, |
| 234 | "Disc Collars": 34, |
| 235 | "External Disc": 50, |
| 236 | "Internal Disc": 60, |
| 237 | "Shaft": 250, |
| 238 | "Stand": 300, |
| 239 | "Upper Bearing Plate": 200, |
| 240 | "Wing Sheet": 25 |
| 241 | } |
| 242 | |
| 243 | for price_list in ("standard_buying", "standard_selling"): |
| 244 | for item, rate in locals().get(price_list).iteritems(): |
| 245 | frappe.get_doc({ |
| 246 | "doctype": "Item Price", |
| 247 | "price_list": price_list.replace("_", " ").title(), |
| 248 | "item_code": item, |
| 249 | "selling": 1 if price_list=="standard_selling" else 0, |
| 250 | "buying": 1 if price_list=="standard_buying" else 0, |
| 251 | "price_list_rate": rate, |
| 252 | "currency": "USD" |
| 253 | }).insert() |
| 254 | |
| 255 | def setup_salary_structure(): |
| 256 | f = frappe.get_doc('Fiscal Year', frappe.defaults.get_global_default('fiscal_year')) |
| 257 | |
| 258 | for e in frappe.get_all('Employee', fields=['name', 'date_of_joining']): |
| 259 | ss = frappe.new_doc('Salary Structure') |
| 260 | ss.employee = e.name |
| 261 | |
| 262 | if not e.date_of_joining: |
| 263 | continue |
| 264 | |
| 265 | ss.from_date = e.date_of_joining if (e.date_of_joining |
| 266 | and e.date_of_joining > f.year_start_date) else f.year_start_date |
| 267 | ss.to_date = f.year_end_date |
| 268 | ss.append('earnings', { |
| 269 | 'earning_type': 'Basic', |
| 270 | 'modified_value': random.random() * 10000 |
| 271 | }) |
| 272 | ss.append('deductions', { |
| 273 | 'deduction_type': 'Income Tax', |
| 274 | 'd_modified_amt': random.random() * 1000 |
| 275 | }) |
| 276 | ss.insert() |
| 277 | |
| 278 | def setup_account(): |
| 279 | frappe.flags.in_import = True |
| 280 | data = json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', |
| 281 | 'account.json')).read()) |
| 282 | for d in data: |
| 283 | doc = frappe.new_doc('Account') |
| 284 | doc.update(d) |
| 285 | doc.parent_account = frappe.db.get_value('Account', {'account_name': doc.parent_account}) |
| 286 | doc.insert() |
| 287 | |
| 288 | def setup_user_roles(): |
| 289 | if not frappe.db.get_global('demo_hr_user'): |
| 290 | user = frappe.get_doc('User', 'CharmaineGaudreau@example.com') |
| 291 | user.add_roles('HR User', 'HR Manager', 'Accounts User') |
| 292 | frappe.db.set_global('demo_hr_user', user.name) |
| 293 | |
| 294 | if not frappe.db.get_global('demo_sales_user_1'): |
| 295 | user = frappe.get_doc('User', 'VakhitaRyzaev@example.com') |
| 296 | user.add_roles('Sales User') |
| 297 | frappe.db.set_global('demo_sales_user_1', user.name) |
| 298 | |
| 299 | if not frappe.db.get_global('demo_sales_user_2'): |
| 300 | user = frappe.get_doc('User', 'GabrielleLoftus@example.com') |
| 301 | user.add_roles('Sales User', 'Sales Manager', 'Accounts User') |
| 302 | frappe.db.set_global('demo_sales_user_2', user.name) |