From 296fbfeaacecdefc0087c3addbc0d5103561deb7 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Mon, 10 Jul 2017 13:03:29 +0530 Subject: [PATCH] [Fix] Taxable amount in tax breakup showing wrong value for duplicate items in the invoice --- .../doctype/sales_invoice/test_sales_invoice.py | 14 ++++++++++++-- erpnext/controllers/taxes_and_totals.py | 17 +++++++++++------ .../public/js/controllers/taxes_and_totals.js | 15 +++++++++++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 6f1c2c973d..800e6a91b8 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -1107,6 +1107,16 @@ class TestSalesInvoice(unittest.TestCase): def test_item_wise_tax_breakup(self): si = create_sales_invoice(qty=100, rate=50, do_not_save=True) + si.append("items", { + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC", + "qty": 100, + "rate": 50, + "income_account": "Sales - _TC", + "expense_account": "Cost of Goods Sold - _TC", + "cost_center": "_Test Cost Center - _TC" + }) + si.append("taxes", { "charge_type": "On Net Total", "account_head": "_Test Account Service Tax - _TC", @@ -1115,8 +1125,8 @@ class TestSalesInvoice(unittest.TestCase): "rate": 10 }) si.insert() - - tax_breakup_html = '''\n
\n\t\n\t\t\n\t\t\n\t
Item NameTaxable Amount_Test Account Service Tax - _TC
_Test Item\u20b9 5,000.00(10.0%) \u20b9 500.00
\n
''' + + tax_breakup_html = '''\n
\n\t\n\t\t\n\t\t\n\t
Item NameTaxable Amount_Test Account Service Tax - _TC
_Test Item\u20b9 10,000.00(10.0%) \u20b9 1,000.00
\n
''' self.assertEqual(si.other_charges_calculation, tax_breakup_html) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 8eb83af6d5..72785da563 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -517,9 +517,9 @@ class calculate_taxes_and_totals(object): headings = get_table_column_headings(tax_accounts) - distinct_items = self.get_distinct_items() + distinct_items, taxable_amount = self.get_distinct_items() - rows = get_table_rows(distinct_items, item_tax, tax_accounts, company_currency) + rows = get_table_rows(distinct_items, item_tax, tax_accounts, company_currency, taxable_amount) if not rows: self.doc.other_charges_calculation = "" @@ -568,13 +568,17 @@ class calculate_taxes_and_totals(object): def get_distinct_items(self): distinct_item_names = [] distinct_items = [] + taxable_amount = {} for item in self.doc.items: item_code = item.item_code or item.item_name if item_code not in distinct_item_names: distinct_item_names.append(item_code) distinct_items.append(item) + taxable_amount[item_code] = item.net_amount + else: + taxable_amount[item_code] = taxable_amount.get(item_code, 0) + item.net_amount - return distinct_items + return distinct_items, taxable_amount def get_table_column_headings(tax_accounts): headings_name = [_("Item Name"), _("Taxable Amount")] + [d[1] for d in tax_accounts] @@ -587,7 +591,7 @@ def get_table_column_headings(tax_accounts): return headings -def get_table_rows(distinct_items, item_tax, tax_accounts, company_currency): +def get_table_rows(distinct_items, item_tax, tax_accounts, company_currency, taxable_amount): rows = [] for item in distinct_items: item_tax_record = item_tax.get(item.item_code or item.item_name) @@ -601,10 +605,11 @@ def get_table_rows(distinct_items, item_tax, tax_accounts, company_currency): + item_tax_record[head[0]][1] + "") else: taxes.append("") - + + item_code = item.item_code or item.item_name rows.append("{item_name}{taxable_amount}{taxes}".format(**{ "item_name": item.item_name, - "taxable_amount": fmt_money(item.net_amount, item.precision("net_amount"), company_currency), + "taxable_amount": fmt_money(taxable_amount.get(item_code, 0), item.precision("net_amount"), company_currency), "taxes": "".join(taxes) })) diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js index e917a75d6d..e09925d059 100644 --- a/erpnext/public/js/controllers/taxes_and_totals.js +++ b/erpnext/public/js/controllers/taxes_and_totals.js @@ -693,19 +693,26 @@ erpnext.taxes_and_totals = erpnext.payments.extend({ var distinct_item_names = []; var distinct_items = []; + var taxable_amount = {}; $.each(this.frm.doc["items"] || [], function(i, item) { - if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) { - distinct_item_names.push(item.item_code || item.item_name); + var item_code = item.item_code || item.item_name; + if(distinct_item_names.indexOf(item_code)===-1) { + distinct_item_names.push(item_code); distinct_items.push(item); + taxable_amount[item_code] = item.net_amount; + } else { + taxable_amount[item_code] = taxable_amount[item_code] + item.net_amount; } }); var rows = $.map(distinct_items, function(item) { - var item_tax_record = item_tax[item.item_code || item.item_name]; + var item_code = item.item_code || item.item_name; + var item_tax_record = item_tax[item_code]; if(!item_tax_record) { return null; } + return repl("%(item_name)s%(taxable_amount)s%(taxes)s", { item_name: item.item_name, - taxable_amount: format_currency(item.net_amount, + taxable_amount: format_currency(taxable_amount[item_code], company_currency, precision("net_amount", item)), taxes: $.map(tax_accounts, function(head) { return item_tax_record[head[0]] ?