From a2426fcc9eff94b671a5a44038a0c1bb52596629 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 15 Jan 2018 17:45:46 +0530 Subject: [PATCH] Get default tax template only if tax template not selected or template is for other company (#12492) --- erpnext/controllers/accounts_controller.py | 19 ++++++++++++++++--- .../crm/doctype/opportunity/opportunity.py | 2 +- erpnext/public/js/controllers/transaction.js | 3 ++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index e1764720a0..81d2786c10 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -54,6 +54,7 @@ class AccountsController(TransactionBase): if self.meta.get_field("taxes_and_charges"): self.validate_enabled_taxes_and_charges() + self.validate_tax_account_company() self.validate_party() self.validate_currency() @@ -255,6 +256,14 @@ class AccountsController(TransactionBase): if frappe.db.get_value(taxes_and_charges_doctype, self.taxes_and_charges, "disabled"): frappe.throw(_("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges)) + def validate_tax_account_company(self): + for d in self.get("taxes"): + if d.account_head: + tax_account_company = frappe.db.get_value("Account", d.account_head, "company") + if tax_account_company != self.company: + frappe.throw(_("Row #{0}: Account {1} does not belong to company {2}") + .format(d.idx, d.account_head, self.company)) + def get_gl_dict(self, args, account_currency=None): """this method populates the common properties of a gl entry record""" @@ -722,11 +731,15 @@ def get_tax_rate(account_head): return frappe.db.get_value("Account", account_head, ["tax_rate", "account_name"], as_dict=True) @frappe.whitelist() -def get_default_taxes_and_charges(master_doctype, company=None): +def get_default_taxes_and_charges(master_doctype, tax_template=None, company=None): if not company: return {} - default_tax = frappe.db.get_value(master_doctype, - {"is_default": 1, "company": company}) + if tax_template and company: + tax_template_company = frappe.db.get_value(master_doctype, tax_template, "company") + if tax_template_company == company: + return + + default_tax = frappe.db.get_value(master_doctype, {"is_default": 1, "company": company}) return { 'taxes_and_charges': default_tax, diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 9b5d6a6335..75f9deac22 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -232,7 +232,7 @@ def make_quotation(source_name, target_doc=None): quotation.conversion_rate = exchange_rate # get default taxes - taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template", quotation.company) + taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template", company=quotation.company) if taxes.get('taxes'): quotation.update(taxes) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index b7ad866982..e198c7eecf 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -244,10 +244,11 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges", args: { "master_doctype": taxes_and_charges_field.options, + "tax_template": me.frm.doc.taxes_and_charges, "company": me.frm.doc.company }, callback: function(r) { - if(!r.exc) { + if(!r.exc && r.message) { frappe.run_serially([ () => { // directly set in doc, so as not to call triggers