From 8f4a6d248f4a4ee901b918483aa373c64f8e25cd Mon Sep 17 00:00:00 2001 From: Diptanil Saha Date: Wed, 11 Jun 2025 10:58:18 +0530 Subject: [PATCH] fix: fetch correct item tax template on item rate update (#47955) --- erpnext/public/js/controllers/transaction.js | 27 ++++++++++---------- erpnext/stock/get_item_details.py | 13 ++++++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 7992678dff2..c42bf978fad 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -44,23 +44,22 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe if (item.item_code && item.rate) { frappe.call({ - method: "frappe.client.get_value", + method: "erpnext.stock.get_item_details.get_item_tax_template", args: { - doctype: "Item Tax", - parent: "Item", - filters: { - parent: item.item_code, - minimum_net_rate: ["<=", item.rate], - maximum_net_rate: [">=", item.rate] - }, - fieldname: "item_tax_template" + ctx: { + item_code: item.item_code, + company: frm.doc.company, + base_net_rate: item.base_net_rate, + tax_category: frm.doc.tax_category, + item_tax_template: item.item_tax_template, + posting_date: frm.doc.posting_date, + bill_date: frm.doc.bill_date, + transaction_date: frm.doc.transaction_date, + } }, callback: function(r) { - const tax_rule = r.message; - - let matched_template = tax_rule ? tax_rule.item_tax_template : null; - - frappe.model.set_value(cdt, cdn, 'item_tax_template', matched_template); + const item_tax_template = r.message; + frappe.model.set_value(cdt, cdn, 'item_tax_template', item_tax_template); } }); } diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 00bf974175a..88faa841586 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -672,8 +672,9 @@ def get_item_tax_info(doc, tax_category, item_codes, item_rates=None, item_tax_t return out +@frappe.whitelist() @erpnext.normalize_ctx_input(ItemDetailsCtx) -def get_item_tax_template(ctx: ItemDetailsCtx, item, out: ItemDetails): +def get_item_tax_template(ctx: ItemDetailsCtx, item=None, out: ItemDetails | None = None): """ Determines item_tax template from item or parent item groups. @@ -691,6 +692,12 @@ def get_item_tax_template(ctx: ItemDetailsCtx, item, out: ItemDetails): "base_net_rate": float } """ + if not item: + if not ctx.get("item_code"): + frappe.throw(_("Item/Item Code required to get Item Tax Template.")) + else: + item = frappe.get_cached_doc("Item", ctx.item_code) + item_tax_template = None if item.taxes: item_tax_template = _get_item_tax_template(ctx, item.taxes, out) @@ -702,9 +709,11 @@ def get_item_tax_template(ctx: ItemDetailsCtx, item, out: ItemDetails): item_tax_template = _get_item_tax_template(ctx, item_group_doc.taxes, out) item_group = item_group_doc.parent_item_group - if ctx.get("child_doctype") and item_tax_template: + if out and ctx.get("child_doctype") and item_tax_template: out.update(get_fetch_values(ctx.get("child_doctype"), "item_tax_template", item_tax_template)) + return item_tax_template + @erpnext.normalize_ctx_input(ItemDetailsCtx) def _get_item_tax_template(