brotherton-erpnext/erpnext/setup/page/setup_wizard/setup_wizard.py

395 lines
12 KiB
Python
Raw Normal View History

2013-11-20 07:29:58 +00:00
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
2013-10-08 12:29:11 +00:00
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe, json
2013-10-08 12:29:11 +00:00
from frappe.utils import cstr, getdate
2014-02-14 10:17:51 +00:00
from frappe.utils.file_manager import save_file
from frappe.translate import set_default_language, get_dict, get_lang_dict
from frappe.country_info import get_country_info
2013-10-08 12:29:11 +00:00
2014-02-14 10:17:51 +00:00
@frappe.whitelist()
2013-10-08 12:29:11 +00:00
def setup_account(args=None):
2014-02-26 07:05:33 +00:00
# if frappe.db.sql("select name from tabCompany"):
2014-02-14 10:17:51 +00:00
# frappe.throw(_("Setup Already Complete!!"))
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
if not args:
2014-02-14 10:17:51 +00:00
args = frappe.local.form_dict
2013-10-08 12:29:11 +00:00
if isinstance(args, basestring):
args = json.loads(args)
2014-02-14 10:17:51 +00:00
args = frappe._dict(args)
2014-04-07 09:39:09 +00:00
if args.language != "english":
set_default_language(args.language)
update_user_name(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_fiscal_year_and_company(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
set_defaults(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_territories()
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_price_lists(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_feed_and_todo()
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_email_digest()
frappe.local.message_log = []
create_letter_head(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_taxes(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_items(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_customers(args)
frappe.local.message_log = []
2013-10-08 12:29:11 +00:00
create_suppliers(args)
frappe.local.message_log = []
2014-04-04 06:30:36 +00:00
frappe.db.set_default('desktop:home_page', 'desktop')
2013-10-08 12:29:11 +00:00
2014-02-14 10:17:51 +00:00
frappe.clear_cache()
2014-02-26 07:05:33 +00:00
frappe.db.commit()
2014-04-07 09:39:09 +00:00
def update_user_name(args):
2013-10-08 12:53:05 +00:00
if args.get("email"):
2013-10-08 12:29:11 +00:00
args['name'] = args.get("email")
2014-02-14 10:17:51 +00:00
frappe.flags.mute_emails = True
2014-03-28 11:14:37 +00:00
frappe.get_doc({
"doctype":"User",
2013-10-08 12:29:11 +00:00
"email": args.get("email"),
"first_name": args.get("first_name"),
"last_name": args.get("last_name")
}).insert()
2014-02-14 10:17:51 +00:00
frappe.flags.mute_emails = False
from frappe.auth import _update_password
2013-10-08 12:29:11 +00:00
_update_password(args.get("email"), args.get("password"))
else:
2014-02-14 10:17:51 +00:00
args['name'] = frappe.session.user
2013-10-08 12:29:11 +00:00
# Update User
2014-04-07 09:39:09 +00:00
if not args.get('last_name') or args.get('last_name')=='None':
2013-10-08 12:29:11 +00:00
args['last_name'] = None
frappe.db.sql("""update `tabUser` SET first_name=%(first_name)s,
2013-10-08 12:29:11 +00:00
last_name=%(last_name)s WHERE name=%(name)s""", args)
2014-04-07 09:39:09 +00:00
if args.get("attach_user"):
filename, filetype, content = args.get("attach_user").split(",")
fileurl = save_file(filename, content, "User", args.get("name"), decode=True).file_name
frappe.db.set_value("User", args.get("name"), "user_image", fileurl)
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
add_all_roles_to(args.get("name"))
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def create_fiscal_year_and_company(args):
curr_fiscal_year = get_fy_details(args.get('fy_start_date'), args.get('fy_end_date'))
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Fiscal Year",
'year': curr_fiscal_year,
'year_start_date': args.get('fy_start_date'),
'year_end_date': args.get('fy_end_date'),
}).insert()
2013-10-08 12:29:11 +00:00
# Company
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Company",
'domain': args.get("industry"),
'company_name':args.get('company_name'),
'abbr':args.get('company_abbr'),
'default_currency':args.get('currency'),
'country': args.get('country'),
'chart_of_accounts': args.get(('chart_of_accounts')),
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
args["curr_fiscal_year"] = curr_fiscal_year
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def create_price_lists(args):
for pl_type in ["Selling", "Buying"]:
frappe.get_doc({
"doctype": "Price List",
"price_list_name": "Standard " + pl_type,
"enabled": 1,
"buying": 1 if pl_type == "Buying" else 0,
"selling": 1 if pl_type == "Selling" else 0,
"currency": args["currency"],
"valid_for_territories": [{
"territory": "All Territories"
}]
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def set_defaults(args):
# enable default currency
2014-02-26 07:05:33 +00:00
frappe.db.set_value("Currency", args.get("currency"), "enabled", 1)
2014-04-07 09:39:09 +00:00
2014-03-28 11:14:37 +00:00
global_defaults = frappe.get_doc("Global Defaults", "Global Defaults")
global_defaults.update({
2013-10-08 12:29:11 +00:00
'current_fiscal_year': args.curr_fiscal_year,
'default_currency': args.get('currency'),
'default_company':args.get('company_name'),
"country": args.get("country"),
2013-10-08 12:29:11 +00:00
})
2013-10-08 12:29:11 +00:00
global_defaults.save()
2014-04-07 09:39:09 +00:00
system_settings = frappe.get_doc("System Settings", "System Settings")
system_settings.update({
"language": args.get("language"),
"time_zone": args.get("timezone"),
"float_precision": 3,
'date_format': frappe.db.get_value("Country", args.get("country"), "date_format"),
'number_format': get_country_info(args.get("country")).get("number_format", "#,###.##"),
})
system_settings.save()
2014-03-28 11:14:37 +00:00
accounts_settings = frappe.get_doc("Accounts Settings")
accounts_settings.auto_accounting_for_stock = 1
2013-10-08 12:29:11 +00:00
accounts_settings.save()
2014-03-28 11:14:37 +00:00
stock_settings = frappe.get_doc("Stock Settings")
stock_settings.item_naming_by = "Item Code"
stock_settings.valuation_method = "FIFO"
stock_settings.stock_uom = "Nos"
stock_settings.auto_indent = 1
2013-10-08 12:29:11 +00:00
stock_settings.save()
2014-04-07 09:39:09 +00:00
2014-03-28 11:14:37 +00:00
selling_settings = frappe.get_doc("Selling Settings")
selling_settings.cust_master_name = "Customer Name"
selling_settings.so_required = "No"
selling_settings.dn_required = "No"
2013-10-08 12:29:11 +00:00
selling_settings.save()
2014-03-28 11:14:37 +00:00
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
2013-10-08 12:29:11 +00:00
buying_settings.save()
2014-03-28 11:14:37 +00:00
notification_control = frappe.get_doc("Notification Control")
notification_control.quotation = 1
notification_control.sales_invoice = 1
notification_control.purchase_order = 1
2013-10-08 12:29:11 +00:00
notification_control.save()
2014-03-28 11:14:37 +00:00
hr_settings = frappe.get_doc("HR Settings")
hr_settings.emp_created_by = "Naming Series"
2013-10-08 12:29:11 +00:00
hr_settings.save()
2014-03-28 11:14:37 +00:00
email_settings = frappe.get_doc("Outgoing Email Settings")
email_settings.send_print_in_body_and_attachment = 1
email_settings.save()
2014-04-04 06:30:36 +00:00
# default
frappe.db.set_default("company_name", args["company_name"])
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def create_feed_and_todo():
"""update activty feed and create todo for creation of item, customer, vendor"""
from erpnext.home import make_feed
2014-02-14 10:17:51 +00:00
make_feed('Comment', 'ToDo', '', frappe.session['user'],
2013-10-08 12:29:11 +00:00
'ERNext Setup Complete!', '#6B24B3')
def create_email_digest():
from frappe.utils.user import get_system_managers
system_managers = get_system_managers(only_name=True)
2014-04-07 09:39:09 +00:00
if not system_managers:
2013-10-08 12:29:11 +00:00
return
2014-04-07 09:39:09 +00:00
2014-02-26 07:05:33 +00:00
companies = frappe.db.sql_list("select name FROM `tabCompany`")
2013-12-18 07:52:18 +00:00
for company in companies:
2014-02-26 07:05:33 +00:00
if not frappe.db.exists("Email Digest", "Default Weekly Digest - " + company):
2014-03-28 11:14:37 +00:00
edigest = frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype": "Email Digest",
"name": "Default Weekly Digest - " + company,
"company": company,
"frequency": "Weekly",
"recipient_list": "\n".join(system_managers)
})
2014-04-07 09:39:09 +00:00
for fieldname in edigest.meta.get("fields", {"fieldtype": "Check"}):
if fieldname != "scheduler_errors":
edigest.set(fieldname, 1)
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
edigest.insert()
2014-04-07 09:39:09 +00:00
# scheduler errors digest
2013-12-18 07:52:18 +00:00
if companies:
edigest = frappe.new_doc("Email Digest")
edigest.update({
2013-12-18 07:52:18 +00:00
"name": "Scheduler Errors",
"company": companies[0],
"frequency": "Daily",
"recipient_list": "\n".join(system_managers),
"scheduler_errors": 1,
"enabled": 1
})
edigest.insert()
2014-04-07 09:39:09 +00:00
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)
2013-10-08 12:29:11 +00:00
else:
fy = cstr(start_year) + '-' + cstr(start_year + 1)
return fy
2013-10-08 12:29:11 +00:00
def create_taxes(args):
for i in xrange(1,6):
if args.get("tax_" + str(i)):
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Account",
"company": args.get("company_name"),
"parent_account": "Duties and Taxes - " + args.get("company_abbr"),
"account_name": args.get("tax_" + str(i)),
"group_or_ledger": "Ledger",
"report_type": "Balance Sheet",
2013-10-08 12:29:11 +00:00
"account_type": "Tax",
"tax_rate": args.get("tax_rate_" + str(i))
}).insert()
def create_items(args):
for i in xrange(1,6):
item = args.get("item_" + str(i))
if item:
item_group = args.get("item_group_" + str(i))
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Item",
"item_code": item,
"item_name": item,
"description": item,
"is_sales_item": "Yes",
"is_stock_item": item_group!="Services" and "Yes" or "No",
"item_group": item_group,
"stock_uom": args.get("item_uom_" + str(i)),
"default_warehouse": item_group!="Service" and ("Finished Goods - " + args.get("company_abbr")) or ""
2013-10-08 12:29:11 +00:00
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
if args.get("item_img_" + str(i)):
filename, filetype, content = args.get("item_img_" + str(i)).split(",")
fileurl = save_file(filename, content, "Item", item, decode=True).file_name
2014-02-26 07:05:33 +00:00
frappe.db.set_value("Item", item, "image", fileurl)
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
for i in xrange(1,6):
item = args.get("item_buy_" + str(i))
if item:
item_group = args.get("item_buy_group_" + str(i))
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Item",
"item_code": item,
"item_name": item,
"description": item,
"is_sales_item": "No",
"is_stock_item": item_group!="Services" and "Yes" or "No",
"item_group": item_group,
"stock_uom": args.get("item_buy_uom_" + str(i)),
"default_warehouse": item_group!="Service" and ("Stores - " + args.get("company_abbr")) or ""
2013-10-08 12:29:11 +00:00
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
if args.get("item_img_" + str(i)):
filename, filetype, content = args.get("item_img_" + str(i)).split(",")
fileurl = save_file(filename, content, "Item", item, decode=True).file_name
2014-02-26 07:05:33 +00:00
frappe.db.set_value("Item", item, "image", fileurl)
2013-10-08 12:29:11 +00:00
def create_customers(args):
for i in xrange(1,6):
customer = args.get("customer_" + str(i))
if customer:
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Customer",
"customer_name": customer,
"customer_type": "Company",
"customer_group": "Commercial",
"territory": args.get("country"),
"company": args.get("company_name")
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
if args.get("customer_contact_" + str(i)):
contact = args.get("customer_contact_" + str(i)).split(" ")
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Contact",
"customer": customer,
"first_name":contact[0],
"last_name": len(contact) > 1 and contact[1] or ""
}).insert()
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def create_suppliers(args):
for i in xrange(1,6):
supplier = args.get("supplier_" + str(i))
if supplier:
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Supplier",
"supplier_name": supplier,
"supplier_type": "Local",
"company": args.get("company_name")
}).insert()
if args.get("supplier_contact_" + str(i)):
contact = args.get("supplier_contact_" + str(i)).split(" ")
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype":"Contact",
"supplier": supplier,
"first_name":contact[0],
"last_name": len(contact) > 1 and contact[1] or ""
}).insert()
def create_letter_head(args):
if args.get("attach_letterhead"):
frappe.get_doc({
"doctype":"Letter Head",
"letter_head_name": "Standard",
"is_default": 1
}).insert()
2014-04-07 09:39:09 +00:00
filename, filetype, content = args.get("attach_letterhead").split(",")
fileurl = save_file(filename, content, "Letter Head", "Standard", decode=True).file_name
2014-02-26 07:05:33 +00:00
frappe.db.set_value("Letter Head", "Standard", "content", "<img src='%s' style='max-width: 100%%;'>" % fileurl)
2014-04-07 09:39:09 +00:00
2013-10-08 12:29:11 +00:00
def add_all_roles_to(name):
2014-03-27 10:42:56 +00:00
user = frappe.get_doc("User", name)
2014-02-26 07:05:33 +00:00
for role in frappe.db.sql("""select name from tabRole"""):
2013-10-08 12:29:11 +00:00
if role[0] not in ["Administrator", "Guest", "All", "Customer", "Supplier", "Partner"]:
2014-03-27 10:42:56 +00:00
d = user.append("user_roles")
2013-10-08 12:29:11 +00:00
d.role = role[0]
2014-03-27 10:42:56 +00:00
user.save()
2013-10-08 12:29:11 +00:00
def create_territories():
"""create two default territories, one for home country and one named Rest of the World"""
2014-02-14 10:17:51 +00:00
from frappe.utils.nestedset import get_root_of
2014-04-04 06:30:36 +00:00
country = frappe.db.get_default("country")
2013-10-08 12:29:11 +00:00
root_territory = get_root_of("Territory")
for name in (country, "Rest Of The World"):
2014-02-26 07:05:33 +00:00
if name and not frappe.db.exists("Territory", name):
2014-03-28 11:14:37 +00:00
frappe.get_doc({
2013-10-08 12:29:11 +00:00
"doctype": "Territory",
"territory_name": name.replace("'", ""),
"parent_territory": root_territory,
"is_group": "No"
2014-04-07 09:39:09 +00:00
}).insert()
@frappe.whitelist()
def load_messages(language):
lang = get_lang_dict()[language]
frappe.local.lang = lang
m = get_dict("page", "setup-wizard")
m.update(get_dict("boot"))
frappe.local.response["__messages"] = m
return lang