fix: handle duplicate description in item-wise report

This commit is contained in:
ljain112
2025-12-09 13:34:14 +05:30
parent 5401843315
commit 2767cb04fb
2 changed files with 27 additions and 36 deletions

View File

@@ -5,7 +5,6 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.utils import flt from frappe.utils import flt
from pypika import Order
import erpnext import erpnext
from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import ( from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import (
@@ -41,16 +40,6 @@ def _execute(filters=None, additional_table_columns=None):
tax_doctype="Purchase Taxes and Charges", tax_doctype="Purchase Taxes and Charges",
) )
scrubbed_tax_fields = {}
for tax in tax_columns:
scrubbed_tax_fields.update(
{
tax + " Rate": frappe.scrub(tax + " Rate"),
tax + " Amount": frappe.scrub(tax + " Amount"),
}
)
po_pr_map = get_purchase_receipts_against_purchase_order(item_list) po_pr_map = get_purchase_receipts_against_purchase_order(item_list)
data = [] data = []
@@ -100,8 +89,8 @@ def _execute(filters=None, additional_table_columns=None):
for tax, details in itemised_tax.get(d.name, {}).items(): for tax, details in itemised_tax.get(d.name, {}).items():
row.update( row.update(
{ {
scrubbed_tax_fields[tax + " Rate"]: details.get("tax_rate", 0), f"{tax}_rate": details.get("tax_rate", 0),
scrubbed_tax_fields[tax + " Amount"]: details.get("tax_amount", 0), f"{tax}_amount": details.get("tax_amount", 0),
} }
) )
if details.get("is_other_charges"): if details.get("is_other_charges"):

View File

@@ -5,7 +5,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.query_builder import functions as fn from frappe.query_builder import functions as fn
from frappe.utils import cstr, flt from frappe.utils import flt
from frappe.utils.nestedset import get_descendants_of from frappe.utils.nestedset import get_descendants_of
from frappe.utils.xlsxutils import handle_html from frappe.utils.xlsxutils import handle_html
@@ -32,15 +32,6 @@ def _execute(filters=None, additional_table_columns=None, additional_conditions=
return columns, [], None, None, None, 0 return columns, [], None, None, None, 0
itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency) itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency)
scrubbed_tax_fields = {}
for tax in tax_columns:
scrubbed_tax_fields.update(
{
tax + " Rate": frappe.scrub(tax + " Rate"),
tax + " Amount": frappe.scrub(tax + " Amount"),
}
)
mode_of_payments = get_mode_of_payments(set(d.parent for d in item_list)) mode_of_payments = get_mode_of_payments(set(d.parent for d in item_list))
so_dn_map = get_delivery_notes_against_sales_order(item_list) so_dn_map = get_delivery_notes_against_sales_order(item_list)
@@ -101,8 +92,8 @@ def _execute(filters=None, additional_table_columns=None, additional_conditions=
for tax, details in itemised_tax.get(d.name, {}).items(): for tax, details in itemised_tax.get(d.name, {}).items():
row.update( row.update(
{ {
scrubbed_tax_fields[tax + " Rate"]: details.get("tax_rate", 0), f"{tax}_rate": details.get("tax_rate", 0),
scrubbed_tax_fields[tax + " Amount"]: details.get("tax_amount", 0), f"{tax}_amount": details.get("tax_amount", 0),
} }
) )
if details.get("is_other_charges"): if details.get("is_other_charges"):
@@ -567,15 +558,24 @@ def get_tax_accounts(
tax_details = query.run(as_dict=True) tax_details = query.run(as_dict=True)
precision = frappe.get_precision(tax_doctype, "tax_amount", currency=company_currency) or 2 precision = frappe.get_precision(tax_doctype, "tax_amount", currency=company_currency) or 2
tax_columns = set() tax_columns = {}
itemised_tax = {} itemised_tax = {}
scrubbed_description_map = {}
for row in tax_details: for row in tax_details:
description = handle_html(row.description) or row.account_head description = handle_html(row.description) or row.account_head
scrubbed_description = scrubbed_description_map.get(description)
if not scrubbed_description:
scrubbed_description = frappe.scrub(description)
scrubbed_description_map[description] = scrubbed_description
if scrubbed_description not in tax_columns and row.amount:
# as description is text editor earlier and markup can break the column convention in reports
tax_columns[scrubbed_description] = description
rate = "NA" if row.rate == 0 else row.rate rate = "NA" if row.rate == 0 else row.rate
tax_columns.add(description)
itemised_tax.setdefault(row.item_row, {}).setdefault( itemised_tax.setdefault(row.item_row, {}).setdefault(
description, scrubbed_description,
frappe._dict( frappe._dict(
{ {
"tax_rate": rate, "tax_rate": rate,
@@ -585,14 +585,16 @@ def get_tax_accounts(
), ),
) )
itemised_tax[row.item_row][description].tax_amount += flt(row.amount, precision) itemised_tax[row.item_row][scrubbed_description].tax_amount += flt(row.amount, precision)
tax_columns = sorted(tax_columns) tax_columns_list = list(tax_columns.keys())
for desc in tax_columns: tax_columns_list.sort()
for scrubbed_desc in tax_columns_list:
desc = tax_columns[scrubbed_desc]
columns.append( columns.append(
{ {
"label": _(desc + " Rate"), "label": _(desc + " Rate"),
"fieldname": frappe.scrub(desc + " Rate"), "fieldname": f"{scrubbed_desc}_rate",
"fieldtype": "Float", "fieldtype": "Float",
"width": 100, "width": 100,
} }
@@ -601,7 +603,7 @@ def get_tax_accounts(
columns.append( columns.append(
{ {
"label": _(desc + " Amount"), "label": _(desc + " Amount"),
"fieldname": frappe.scrub(desc + " Amount"), "fieldname": f"{scrubbed_desc}_amount",
"fieldtype": "Currency", "fieldtype": "Currency",
"options": "currency", "options": "currency",
"width": 100, "width": 100,
@@ -639,7 +641,7 @@ def get_tax_accounts(
}, },
] ]
return itemised_tax, tax_columns return itemised_tax, tax_columns_list
def get_tax_details_query(doctype, tax_doctype): def get_tax_details_query(doctype, tax_doctype):
@@ -756,5 +758,5 @@ def add_sub_total_row(item, total_row_map, group_by_value, tax_columns):
total_row["percent_gt"] += item["percent_gt"] total_row["percent_gt"] += item["percent_gt"]
for tax in tax_columns: for tax in tax_columns:
total_row.setdefault(frappe.scrub(tax + " Amount"), 0.0) total_row.setdefault(f"{tax}_amount", 0.0)
total_row[frappe.scrub(tax + " Amount")] += flt(item[frappe.scrub(tax + " Amount")]) total_row[f"{tax}_amount"] += flt(item[f"{tax}_amount"])