diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 7db099f5da11..d51f52f65c58 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -3176,10 +3176,12 @@ def set_child_tax_template_and_map(item, child_item, parent_doc): ) child_item.item_tax_template = _get_item_tax_template(ctx, item.taxes) - if child_item.get("item_tax_template"): - child_item.item_tax_rate = get_item_tax_map( - parent_doc.get("company"), child_item.item_tax_template, as_json=True - ) + child_item.item_tax_rate = get_item_tax_map( + parent_doc.get("company"), + child_item.item_tax_template, + as_json=True, + parent_doc=parent_doc, + ) def add_taxes_from_tax_template(child_item, parent_doc, db_insert=True): diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index aa417d0ef157..a14cde116af3 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -501,9 +501,7 @@ def get_current_tax_and_net_amount(self, item, tax, item_tax_map): ) elif tax.charge_type == "On Net Total": - if not item_tax_map: - current_net_amount = item.net_amount - elif tax.account_head in item_tax_map: + if tax.account_head in item_tax_map: current_net_amount = item.net_amount current_tax_amount = (tax_rate / 100.0) * item.net_amount elif tax.charge_type == "On Previous Row Amount": diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 6033d7f10f19..c8f649a78873 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -101,6 +101,7 @@ def get_item_details( ctx.company, out.item_tax_template or ctx.item_tax_template, as_json=True, + parent_doc=doc, ) get_party_item_code(ctx, item, out) @@ -689,13 +690,26 @@ def is_within_valid_range(ctx: ItemDetailsCtx, tax) -> bool: @frappe.whitelist() -def get_item_tax_map(company, item_tax_template, as_json=True): +def get_item_tax_map(company, tax_template=None, as_json=True, parent_doc: dict | Document | None = None): item_tax_map = {} - if item_tax_template: - template = frappe.get_cached_doc("Item Tax Template", item_tax_template) + doctype = "Item Tax Template" + if not tax_template and parent_doc: + doctype = frappe.get_meta(parent_doc).get_link_doctype("taxes_and_charges") + tax_template = parent_doc.get("taxes_and_charges") + + if doctype and tax_template: + template = frappe.get_cached_doc(doctype, tax_template) for d in template.taxes: - if frappe.get_cached_value("Account", d.tax_type, "company") == company: - item_tax_map[d.tax_type] = d.tax_rate + try: + # Item Tax Template + account_head = d.tax_type + rate = d.tax_rate + except AttributeError: + # Sales/Purchase Taxes and Charges + account_head = d.account_head + rate = d.rate + if frappe.get_cached_value("Account", account_head, "company") == company: + item_tax_map[account_head] = rate return json.dumps(item_tax_map) if as_json else item_tax_map