From c1f65dd863cd39222b50eeab4e9d80a22cea0298 Mon Sep 17 00:00:00 2001 From: nabinhait Date: Tue, 29 Jul 2014 16:59:20 +0530 Subject: [PATCH] Validation: Account must belong to the same company --- erpnext/accounts/doctype/account/account.py | 25 +++++++++++--------- erpnext/setup/doctype/company/company.py | 17 +++++++++++-- erpnext/stock/doctype/warehouse/warehouse.py | 23 ++++++++++++++---- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 014dde51e9..5b034f8b23 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -41,21 +41,24 @@ class Account(Document): throw(_("Invalid Master Name")) def validate_parent(self): - """Fetch Parent Details and validation for account not to be created under ledger""" + """Fetch Parent Details and validate parent account""" if self.parent_account: par = frappe.db.get_value("Account", self.parent_account, - ["name", "group_or_ledger", "report_type", "root_type"], as_dict=1) + ["name", "group_or_ledger", "report_type", "root_type", "company"], as_dict=1) if not par: - throw(_("Parent account does not exist")) - elif par["name"] == self.name: - throw(_("You can not assign itself as parent account")) - elif par["group_or_ledger"] != 'Group': - throw(_("Parent account can not be a ledger")) + throw(_("Account {0}: Parent account {1} does not exist").format(self.name, self.parent_account)) + elif par.name == self.name: + throw(_("Account {0}: You can not assign itself as parent account").format(self.name)) + elif par.group_or_ledger != 'Group': + throw(_("Account {0}: Parent account {1} can not be a ledger").format(self.name, self.parent_account)) + elif par.company != self.company: + throw(_("Account {0}: Parent account {1} does not belong to company: {2}") + .format(self.name, self.parent_account, self.company)) - if par["report_type"]: - self.report_type = par["report_type"] - if par["root_type"]: - self.root_type = par["root_type"] + if par.report_type: + self.report_type = par.report_type + if par.root_type: + self.root_type = par.root_type def validate_root_details(self): #does not exists parent diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index f8e043a927..bd623e6ad6 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -35,6 +35,18 @@ class Company(Document): self.default_currency != self.previous_default_currency and \ self.check_if_transactions_exist(): frappe.throw(_("Cannot change company's default currency, because there are existing transactions. Transactions must be cancelled to change the default currency.")) + + self.validate_default_accounts() + + def validate_default_accounts(self): + for field in ["default_bank_account", "default_cash_account", "receivables_group", "payables_group", + "default_expense_account", "default_income_account", "stock_received_but_not_billed", + "stock_adjustment_account", "expenses_included_in_valuation"]: + if self.get(field): + for_company = frappe.db.get_value("Account", self.get(field), "company") + if for_company != self.name: + frappe.throw(_("Account {0} does not belong to company: {1}") + .format(self.get(field), self.name)) def on_update(self): if not frappe.db.sql("""select name from tabAccount @@ -60,7 +72,7 @@ class Company(Document): for whname in (_("Stores"), _("Work In Progress"), _("Finished Goods")): if not frappe.db.exists("Warehouse", whname + " - " + self.abbr): stock_group = frappe.db.get_value("Account", {"account_type": "Stock", - "group_or_ledger": "Group"}) + "group_or_ledger": "Group", "company": self.name}) if stock_group: frappe.get_doc({ "doctype":"Warehouse", @@ -115,7 +127,8 @@ class Company(Document): _set_default_account("expenses_included_in_valuation", "Expenses Included In Valuation") if not self.default_income_account: - self.db_set("default_income_account", frappe.db.get_value("Account", {"account_name": _("Sales")})) + self.db_set("default_income_account", frappe.db.get_value("Account", + {"account_name": _("Sales"), "company": self.name})) def create_default_cost_center(self): cc_list = [ diff --git a/erpnext/stock/doctype/warehouse/warehouse.py b/erpnext/stock/doctype/warehouse/warehouse.py index 2b6892890a..0095bbb4e5 100644 --- a/erpnext/stock/doctype/warehouse/warehouse.py +++ b/erpnext/stock/doctype/warehouse/warehouse.py @@ -22,11 +22,16 @@ class Warehouse(Document): self.update_parent_account() def update_parent_account(self): - if not getattr(self, "__islocal", None) and (self.create_account_under != - frappe.db.get_value("Warehouse", self.name, "create_account_under")): - warehouse_account = frappe.db.get_value("Account", - {"account_type": "Warehouse", "company": self.company, - "master_name": self.name}, ["name", "parent_account"]) + + if not getattr(self, "__islocal", None) \ + and (self.create_account_under != frappe.db.get_value("Warehouse", self.name, "create_account_under")): + + self.validate_parent_account() + + warehouse_account = frappe.db.get_value("Account", + {"account_type": "Warehouse", "company": self.company, "master_name": self.name}, + ["name", "parent_account"]) + if warehouse_account and warehouse_account[1] != self.create_account_under: acc_doc = frappe.get_doc("Account", warehouse_account[0]) acc_doc.parent_account = self.create_account_under @@ -57,13 +62,21 @@ class Warehouse(Document): msgprint(_("Account head {0} created").format(ac_doc.name)) def validate_parent_account(self): + if not self.company: + frappe.throw(_("Warehouse {0}: Company is mandatory").format(self.name)) + if not self.create_account_under: parent_account = frappe.db.get_value("Account", {"account_name": "Stock Assets", "company": self.company}) + if parent_account: self.create_account_under = parent_account else: frappe.throw(_("Please enter parent account group for warehouse account")) + elif frappe.db.get_value("Account", self.create_account_under, "company") != self.company: + frappe.throw(_("Warehouse {0}: Parent account {1} does not bolong to the company {2}") + .format(self.name, self.create_account_under, self.company)) + def on_trash(self): # delete bin