From 39b8765961d560ac7ea961f8405a327c7f277e49 Mon Sep 17 00:00:00 2001 From: Prateeksha Singh Date: Mon, 12 Nov 2018 17:19:56 +0530 Subject: [PATCH] [setup][init] setup as document records --- erpnext/regional/india/setup.py | 9 +- .../operations/install_fixtures.py | 257 +++++++++++++----- erpnext/setup/setup_wizard/setup_wizard.py | 83 +++--- 3 files changed, 241 insertions(+), 108 deletions(-) diff --git a/erpnext/regional/india/setup.py b/erpnext/regional/india/setup.py index 5fc06eabb7..79e414104e 100644 --- a/erpnext/regional/india/setup.py +++ b/erpnext/regional/india/setup.py @@ -11,14 +11,17 @@ from erpnext.accounts.utils import get_fiscal_year from frappe.utils import today def setup(company=None, patch=True): + if not patch: + update_address_template() + make_fixtures() + +# TODO: for all countries +def setup_company_independent_fixtures(): make_custom_fields() add_permissions() add_custom_roles_for_reports() frappe.enqueue('erpnext.regional.india.setup.add_hsn_sac_codes', now=frappe.flags.in_test) add_print_formats() - if not patch: - update_address_template() - make_fixtures(company) def update_address_template(): with open(os.path.join(os.path.dirname(__file__), 'address_template.html'), 'r') as f: diff --git a/erpnext/setup/setup_wizard/operations/install_fixtures.py b/erpnext/setup/setup_wizard/operations/install_fixtures.py index f7e2906da7..325b353e8e 100644 --- a/erpnext/setup/setup_wizard/operations/install_fixtures.py +++ b/erpnext/setup/setup_wizard/operations/install_fixtures.py @@ -6,6 +6,10 @@ from __future__ import unicode_literals import frappe, os, json from frappe import _ +from frappe.desk.page.setup_wizard.setup_wizard import make_records +from frappe.utils import cstr, getdate +from frappe.utils.nestedset import get_root_of +from erpnext.accounts.doctype.account.account import RootNotEditable default_lead_sources = ["Existing Customer", "Reference", "Advertisement", "Cold Calling", "Exhibition", "Supplier Reference", "Mass Mailing", @@ -15,6 +19,8 @@ default_sales_partner_type = ["Channel Partner", "Distributor", "Dealer", "Agent "Retailer", "Implementation Partner", "Reseller"] def install(country=None): + root_territory = get_root_of("Territory") + records = [ # domains { 'doctype': 'Domain', 'domain': 'Distribution'}, @@ -26,34 +32,6 @@ def install(country=None): { 'doctype': 'Domain', 'domain': 'Agriculture'}, { 'doctype': 'Domain', 'domain': 'Non Profit'}, - # Setup Progress - {'doctype': "Setup Progress", "actions": [ - {"action_name": "Add Company", "action_doctype": "Company", "min_doc_count": 1, "is_completed": 1, - "domains": '[]' }, - {"action_name": "Set Sales Target", "action_doctype": "Company", "min_doc_count": 99, - "action_document": frappe.defaults.get_defaults().get("company") or '', - "action_field": "monthly_sales_target", "is_completed": 0, - "domains": '["Manufacturing", "Services", "Retail", "Distribution"]' }, - {"action_name": "Add Customers", "action_doctype": "Customer", "min_doc_count": 1, "is_completed": 0, - "domains": '["Manufacturing", "Services", "Retail", "Distribution"]' }, - {"action_name": "Add Suppliers", "action_doctype": "Supplier", "min_doc_count": 1, "is_completed": 0, - "domains": '["Manufacturing", "Services", "Retail", "Distribution"]' }, - {"action_name": "Add Products", "action_doctype": "Item", "min_doc_count": 1, "is_completed": 0, - "domains": '["Manufacturing", "Services", "Retail", "Distribution"]' }, - {"action_name": "Add Programs", "action_doctype": "Program", "min_doc_count": 1, "is_completed": 0, - "domains": '["Education"]' }, - {"action_name": "Add Instructors", "action_doctype": "Instructor", "min_doc_count": 1, "is_completed": 0, - "domains": '["Education"]' }, - {"action_name": "Add Courses", "action_doctype": "Course", "min_doc_count": 1, "is_completed": 0, - "domains": '["Education"]' }, - {"action_name": "Add Rooms", "action_doctype": "Room", "min_doc_count": 1, "is_completed": 0, - "domains": '["Education"]' }, - {"action_name": "Add Users", "action_doctype": "User", "min_doc_count": 4, "is_completed": 0, - "domains": '[]' }, - {"action_name": "Add Letterhead", "action_doctype": "Letter Head", "min_doc_count": 1, "is_completed": 0, - "domains": '[]' } - ]}, - # address template {'doctype':"Address Template", "country": country}, @@ -124,8 +102,10 @@ def install(country=None): {'doctype': 'Designation', 'designation_name': _('Designer')}, {'doctype': 'Designation', 'designation_name': _('Researcher')}, - # territory + # territory: with two default territories, one for home country and one named Rest of the World {'doctype': 'Territory', 'territory_name': _('All Territories'), 'is_group': 1, 'name': _('All Territories'), 'parent_territory': ''}, + {'doctype': 'Territory', 'territory_name': country.replace("'", ""), 'is_group': 0, 'parent_territory': root_territory}, + {'doctype': 'Territory', 'territory_name': _("Rest Of The World"), 'is_group': 0, 'parent_territory': root_territory}, # customer group {'doctype': 'Customer Group', 'customer_group_name': _('All Customer Groups'), 'is_group': 1, 'name': _('All Customer Groups'), 'parent_customer_group': ''}, @@ -231,7 +211,7 @@ def install(country=None): # Share Management {"doctype": "Share Type", "title": _("Equity")}, - {"doctype": "Share Type", "title": _("Preference")} + {"doctype": "Share Type", "title": _("Preference")}, ] from erpnext.setup.setup_wizard.data.industry_type import get_industry_types @@ -260,7 +240,17 @@ def install(country=None): from erpnext.buying.doctype.supplier_scorecard.supplier_scorecard import make_default_records make_default_records() - make_fixture_records(records) + make_records(records, True) + + set_more_defaults() + + path = frappe.get_app_path('erpnext', 'regional', frappe.scrub(country)) + if os.path.exists(path.encode("utf-8")): + frappe.get_attr("erpnext.regional.{0}.setup.setup_company_independent_fixtures".format(frappe.scrub(country)))() + + +def set_more_defaults(): + # Do more setup stuff that can be done here with no dependencies # set default customer group and territory selling_settings = frappe.get_doc("Selling Settings") @@ -269,6 +259,33 @@ def install(country=None): add_uom_data() + # set no copy fields of an item doctype to item variant settings + doc = frappe.get_doc('Item Variant Settings') + doc.set_default_fields() + doc.save() + + selling_settings = frappe.get_doc("Selling Settings") + selling_settings.cust_master_name = "Customer Name" + selling_settings.so_required = "No" + selling_settings.dn_required = "No" + selling_settings.allow_multiple_items = 1 + selling_settings.sales_update_frequency = "Each Transaction" + selling_settings.save() + + buying_settings = frappe.get_doc("Buying Settings") + buying_settings.supp_master_name = "Supplier Name" + buying_settings.po_required = "No" + buying_settings.pr_required = "No" + buying_settings.maintain_same_rate = 1 + buying_settings.allow_multiple_items = 1 + buying_settings.save() + + hr_settings = frappe.get_doc("HR Settings") + hr_settings.emp_created_by = "Naming Series" + hr_settings.leave_approval_notification_template = _("Leave Approval Notification") + hr_settings.leave_status_notification_template = _("Leave Status Notification") + hr_settings.save() + def add_uom_data(): # add UOMs uoms = json.loads(open(frappe.get_app_path("erpnext", "setup", "setup_wizard", "data", "uom_data.json")).read()) @@ -306,7 +323,7 @@ def add_market_segments(): {"doctype": "Market Segment", "market_segment": _("Upper Income")} ] - make_fixture_records(records) + make_records(records) def add_sale_stages(): # Sale Stages @@ -320,46 +337,154 @@ def add_sale_stages(): {"doctype": "Sales Stage", "stage_name": _("Proposal/Price Quote")}, {"doctype": "Sales Stage", "stage_name": _("Negotiation/Review")} ] - make_fixture_records(records) + make_records(records) -def make_fixture_records(records): - from frappe.modules import scrub - for r in records: - doc = frappe.new_doc(r.get("doctype")) - doc.update(r) - # ignore mandatory for root - parent_link_field = ("parent_" + scrub(doc.doctype)) - if doc.meta.get_field(parent_link_field) and not doc.get(parent_link_field): - doc.flags.ignore_mandatory = True +def install_company(args): + records = [ + # Fiscal Year + { "doctype": "Fiscal Year", 'year': get_fy_details(args.fy_start_date, args.fy_end_date), 'year_start_date': args.fy_start_date, 'year_end_date': args.fy_end_date }, - try: - doc.insert(ignore_permissions=True) - except frappe.DuplicateEntryError as e: - # pass DuplicateEntryError and continue - if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: - # make sure DuplicateEntryError is for the exact same doc and not a related doc - pass - else: - raise + # Company + { + "doctype":"Company", + 'company_name': args.company_name, 'enable_perpetual_inventory': 1, + 'abbr': args.company_abbr, 'default_currency': args.currency, 'country': args.country, + 'create_chart_of_accounts_based_on': 'Standard Template', + 'domain': args.domain + } + ] -def install_post_company_fixtures(company=None): + make_records(records, True) + + +def install_post_company_fixtures(args=None): records = [ # Department {'doctype': 'Department', 'department_name': _('All Departments'), 'is_group': 1, 'parent_department': ''}, - {'doctype': 'Department', 'department_name': _('Accounts'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Marketing'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Sales'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Purchase'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Operations'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Production'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Dispatch'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Customer Service'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Human Resources'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Management'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Quality Management'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Research & Development'), 'parent_department': _('All Departments'), 'company': company}, - {'doctype': 'Department', 'department_name': _('Legal'), 'parent_department': _('All Departments'), 'company': company}, + {'doctype': 'Department', 'department_name': _('Accounts'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Marketing'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Sales'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Purchase'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Operations'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Production'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Dispatch'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Customer Service'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Human Resources'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Management'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Quality Management'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Research & Development'), 'parent_department': _('All Departments'), 'company': args.company_name}, + {'doctype': 'Department', 'department_name': _('Legal'), 'parent_department': _('All Departments'), 'company': args.company_name}, + + # Price Lists + { "doctype": "Price List", "price_list_name": _("Standard Buying"), "enabled": 1, "buying": 1, "selling": 0, "currency": args.currency }, + { "doctype": "Price List", "price_list_name": _("Standard Selling"), "enabled": 1, "buying": 0, "selling": 1, "currency": args.currency }, + ] - make_fixture_records(records) \ No newline at end of file + make_records(records) + + # enable default currency + frappe.db.set_value("Currency", args.get("currency"), "enabled", 1) + + global_defaults = frappe.get_doc("Global Defaults", "Global Defaults") + global_defaults.update({ + 'current_fiscal_year': get_fy_details(args.get('fy_start_date'), args.get('fy_end_date')), + 'default_currency': args.get('currency'), + 'default_company':args.get('company_name') , + "country": args.get("country"), + }) + + global_defaults.save() + + system_settings = frappe.get_doc("System Settings") + system_settings.email_footer_address = args.get("company_name") + system_settings.save() + + domain_settings = frappe.get_single('Domain Settings') + domain_settings.set_active_domains([args.domain]) + + stock_settings = frappe.get_doc("Stock Settings") + stock_settings.item_naming_by = "Item Code" + stock_settings.valuation_method = "FIFO" + stock_settings.default_warehouse = frappe.db.get_value('Warehouse', {'warehouse_name': _('Stores')}) + stock_settings.stock_uom = _("Nos") + stock_settings.auto_indent = 1 + stock_settings.auto_insert_price_list_rate_if_missing = 1 + stock_settings.automatically_set_serial_nos_based_on_fifo = 1 + stock_settings.set_qty_in_transactions_based_on_serial_no_input = 1 + stock_settings.save() + + + if args.bank_account: + company_name = args.company_name + bank_account_group = frappe.db.get_value("Account", + {"account_type": "Bank", "is_group": 1, "root_type": "Asset", + "company": company_name}) + if bank_account_group: + bank_account = frappe.get_doc({ + "doctype": "Account", + 'account_name': args.bank_account, + 'parent_account': bank_account_group, + 'is_group':0, + 'company': company_name, + "account_type": "Bank", + }) + try: + return bank_account.insert() + except RootNotEditable: + frappe.throw(_("Bank account cannot be named as {0}").format(args.bank_account)) + except frappe.DuplicateEntryError: + # bank account same as a CoA entry + pass + + + # Now, with fixtures out of the way, onto concrete stuff + records = [ + # # Bank Account + # { + # "doctype": "Account", + # "__condition": lambda: c.bank_account and frappe.db.get_value("Account", + # {"account_type": "Bank", "is_group": 1, "root_type": "Asset", + # "company": c.company_name}), + + # "__exception": { + # "exception": RootNotEditable, + # "handler": lambda: frappe.throw(_("Bank account cannot be named as {0}").format(config.get("bank_account"))) + # }, + # 'account_name': c.bank_account, + + # 'parent_account': lambda: frappe.db.get_value("Account", + # {"account_type": "Bank", "is_group": 1, "root_type": "Asset", + # "company": c.company_name}), + + # 'is_group':0, + # 'company': c.company_name, + # "account_type": "Bank", + # }, + + # # Shopping cart: needs price lists + { + "doctype": "Shopping Cart Settings", + "enabled": 1, + 'company': args.company_name, + # uh oh + 'price_list': frappe.db.get_value("Price List", {"selling": 1}), + 'default_customer_group': _("Individual"), + 'quotation_series': "QTN-", + }, + + # # TODO: Email digest, logo, website + # {} + ] + + make_records(records, True) + + +def get_fy_details(fy_start_date, fy_end_date): + start_year = getdate(fy_start_date).year + if start_year == getdate(fy_end_date).year: + fy = cstr(start_year) + else: + fy = cstr(start_year) + '-' + cstr(start_year + 1) + return fy diff --git a/erpnext/setup/setup_wizard/setup_wizard.py b/erpnext/setup/setup_wizard/setup_wizard.py index af28e4f586..d19dd185e9 100644 --- a/erpnext/setup/setup_wizard/setup_wizard.py +++ b/erpnext/setup/setup_wizard/setup_wizard.py @@ -4,8 +4,15 @@ from __future__ import unicode_literals import frappe +import json from frappe import _ -from .operations import install_fixtures, taxes_setup, defaults_setup, company_setup, sample_data + +from .operations import install_fixtures as fixtures, taxes_setup, company_setup, sample_data + +from frappe import _, _dict +from frappe.utils import cstr, getdate +from argparse import Namespace +from frappe.desk.page.setup_wizard.setup_wizard import update_global_settings, run_post_setup_complete, make_records def get_setup_stages(args=None): if frappe.db.sql("select name from tabCompany"): @@ -59,22 +66,6 @@ def get_setup_stages(args=None): 'fn': setup_post_company_fixtures, 'args': args, 'fail_msg': _("Failed to setup post company fixtures") - }, - { - 'fn': stage_three, - 'args': args, - 'fail_msg': _("Failed to set defaults") - } - ] - }, - { - 'status': _('Making website'), - 'fail_msg': _('Failed to create website'), - 'tasks': [ - { - 'fn': stage_four, - 'args': args, - 'fail_msg': _("Failed to create website") } ] }, @@ -98,38 +89,19 @@ def setup_complete(args=None): setup_company(args) setup_taxes(args) setup_post_company_fixtures(args) - stage_three(args) - stage_four(args) fin(args) def stage_fixtures(args): - install_fixtures.install(args.get("country")) - install_fixtures.add_market_segments() - install_fixtures.add_sale_stages() + fixtures.install(_dict(frappe.local.conf.setup).country) def setup_company(args): - defaults_setup.create_price_lists(args) - company_setup.create_fiscal_year_and_company(args) - company_setup.enable_shopping_cart(args) - company_setup.create_bank_account(args) + fixtures.install_company(args) def setup_taxes(args): taxes_setup.create_sales_tax(args) def setup_post_company_fixtures(args): - install_fixtures.install_post_company_fixtures(args.get("company_name")) - -def stage_three(args): - defaults_setup.create_employee_for_self(args) - defaults_setup.set_default_settings(args) - defaults_setup.create_territories() - defaults_setup.create_feed_and_todo() - defaults_setup.set_no_copy_fields_in_variant_settings() - -def stage_four(args): - company_setup.create_website(args) - company_setup.create_email_digest() - company_setup.create_logo(args) + fixtures.install_post_company_fixtures(args) def fin(args): frappe.local.message_log = [] @@ -149,3 +121,36 @@ def make_sample_data(domains): def login_as_first_user(args): if args.get("email") and hasattr(frappe.local, "login_manager"): frappe.local.login_manager.login_as(args.get("email")) + +@frappe.whitelist() +def install_fixtures(config=None): + if not config: + config = frappe.local.conf.setup or {} + + update_global_settings(_dict(config)) + + fixtures.install(_dict(config).country) + +@frappe.whitelist() +def make_setup_docs(args, config=None): + # TODO: get from not site conf + if not config: + config = frappe.local.conf.setup or {} + + args = json.loads(args) + + args.update(config) + + fixtures.install_company(_dict(args)) + fixtures.install_post_company_fixtures(_dict(args)) + + run_post_setup_complete(args) + + +def get_fy_details(fy_start_date, fy_end_date): + start_year = getdate(fy_start_date).year + if start_year == getdate(fy_end_date).year: + fy = cstr(start_year) + else: + fy = cstr(start_year) + '-' + cstr(start_year + 1) + return fy