From eeead1d777d9f70cbe91180d80ee32b028fa25a6 Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Sat, 29 Dec 2018 02:25:00 +0500 Subject: [PATCH] fix: Only load transaction's company's tax accounts in item tax map --- .../item_tax_template/item_tax_template.js | 11 ++- erpnext/public/js/controllers/transaction.js | 72 ++++++++++--------- erpnext/stock/get_item_details.py | 11 +-- 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/erpnext/accounts/doctype/item_tax_template/item_tax_template.js b/erpnext/accounts/doctype/item_tax_template/item_tax_template.js index 9765d02beb..42abdb809b 100644 --- a/erpnext/accounts/doctype/item_tax_template/item_tax_template.js +++ b/erpnext/accounts/doctype/item_tax_template/item_tax_template.js @@ -3,13 +3,12 @@ frappe.ui.form.on('Item Tax Template', { setup: function(frm) { - frm.set_query("tax_type", function(doc) { + frm.set_query("tax_type", "taxes", function(doc) { return { - query: "erpnext.controllers.queries.tax_account_query", - filters: { - "account_type": ['Tax', 'Chargeable', 'Income Account', 'Expense Account'], - "company": doc.company - } + filters: [ + ['Account', 'is_group', '=', 0], + ['Account', 'account_type', 'in', ['Tax', 'Chargeable', 'Income Account', 'Expense Account', 'Expenses Included In Valuation']] + ] } }); } diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index d95ca04584..f409f617f3 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -565,6 +565,7 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ frappe.run_serially([ () => me.frm.script_manager.trigger("currency"), + () => me.update_item_tax_map(), () => me.apply_default_taxes(), () => me.apply_pricing_rule() ]); @@ -1312,40 +1313,8 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ var me = this; if(me.frm.updating_party_details) return; - var item_codes = []; - $.each(this.frm.doc.items || [], function(i, item) { - if(item.item_code) { - item_codes.push(item.item_code); - } - }); - frappe.run_serially([ - () => { - if(item_codes.length) { - return this.frm.call({ - method: "erpnext.stock.get_item_details.get_item_tax_info", - args: { - tax_category: cstr(me.frm.doc.tax_category), - item_codes: item_codes - }, - callback: function(r) { - if(!r.exc) { - $.each(me.frm.doc.items || [], function(i, item) { - if(item.item_code && r.message.hasOwnProperty(item.item_code)) { - item.item_tax_template = r.message[item.item_code].item_tax_template; - item.item_tax_rate = r.message[item.item_code].item_tax_rate; - me.add_taxes_from_item_tax_template(item.item_tax_rate); - } else { - item.item_tax_template = ""; - item.item_tax_rate = "{}"; - } - }); - me.calculate_taxes_and_totals(); - } - } - }); - } - }, + () => this.update_item_tax_map(), () => erpnext.utils.set_taxes(this.frm, "tax_category"), ]); }, @@ -1360,6 +1329,7 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ return this.frm.call({ method: "erpnext.stock.get_item_details.get_item_tax_map", args: { + company: me.frm.doc.company, item_tax_template: item.item_tax_template, as_json: true }, @@ -1377,6 +1347,42 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ } }, + update_item_tax_map: function() { + var me = this; + var item_codes = []; + $.each(this.frm.doc.items || [], function(i, item) { + if(item.item_code) { + item_codes.push(item.item_code); + } + }); + + if(item_codes.length) { + return this.frm.call({ + method: "erpnext.stock.get_item_details.get_item_tax_info", + args: { + company: me.frm.doc.company, + tax_category: cstr(me.frm.doc.tax_category), + item_codes: item_codes + }, + callback: function(r) { + if(!r.exc) { + $.each(me.frm.doc.items || [], function(i, item) { + if(item.item_code && r.message.hasOwnProperty(item.item_code)) { + item.item_tax_template = r.message[item.item_code].item_tax_template; + item.item_tax_rate = r.message[item.item_code].item_tax_rate; + me.add_taxes_from_item_tax_template(item.item_tax_rate); + } else { + item.item_tax_template = ""; + item.item_tax_rate = "{}"; + } + }); + me.calculate_taxes_and_totals(); + } + } + }); + } + }, + is_recurring: function() { // set default values for recurring documents if(this.frm.doc.is_recurring && this.frm.doc.__islocal) { diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 0f46acf8be..a644245b11 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -45,7 +45,7 @@ def get_item_details(args): out = get_basic_details(args, item) get_item_tax_template(args, item, out) - out["item_tax_rate"] = get_item_tax_map(out.get("item_tax_template"), as_json=True) + out["item_tax_rate"] = get_item_tax_map(args.company, out.get("item_tax_template"), as_json=True) get_party_item_code(args, item, out) @@ -306,7 +306,7 @@ def get_basic_details(args, item): return out @frappe.whitelist() -def get_item_tax_info(tax_category, item_codes): +def get_item_tax_info(company, tax_category, item_codes): out = {} if isinstance(item_codes, string_types): item_codes = json.loads(item_codes) @@ -317,7 +317,7 @@ def get_item_tax_info(tax_category, item_codes): out[item_code] = {} item = frappe.get_cached_doc("Item", item_code) get_item_tax_template({"tax_category": tax_category}, item, out[item_code]) - out[item_code]["item_tax_rate"] = get_item_tax_map(out[item_code].get("item_tax_template"), as_json=True) + out[item_code]["item_tax_rate"] = get_item_tax_map(company, out[item_code].get("item_tax_template"), as_json=True) return out @@ -348,12 +348,13 @@ def _get_item_tax_template(args, taxes, out): return None @frappe.whitelist() -def get_item_tax_map(item_tax_template, as_json=True): +def get_item_tax_map(company, item_tax_template, as_json=True): item_tax_map = {} if item_tax_template: template = frappe.get_cached_doc("Item Tax Template", item_tax_template) for d in template.taxes: - item_tax_map[d.tax_type] = d.tax_rate + if frappe.get_cached_value("Account", d.tax_type, "company") == company: + item_tax_map[d.tax_type] = d.tax_rate return json.dumps(item_tax_map) if as_json else item_tax_map