Merge pull request #9750 from rohitwaghchaure/taxable_amount_issue

[Fix] Taxable amount in tax breakup showing wrong value for duplicate items in the invoice
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 6f1c2c9..800e6a9 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 @@
 
 	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 @@
 			"rate": 10
 		})
 		si.insert()
-		
-		tax_breakup_html = '''\n<div class="tax-break-up" style="overflow-x: auto;">\n\t<table class="table table-bordered table-hover">\n\t\t<thead><tr><th class="text-left" style="min-width: 120px;">Item Name</th><th class="text-right" style="min-width: 80px;">Taxable Amount</th><th class="text-right" style="min-width: 80px;">_Test Account Service Tax - _TC</th></tr></thead>\n\t\t<tbody><tr><td>_Test Item</td><td class="text-right">\u20b9 5,000.00</td><td class="text-right">(10.0%) \u20b9 500.00</td></tr></tbody>\n\t</table>\n</div>'''
+
+		tax_breakup_html = '''\n<div class="tax-break-up" style="overflow-x: auto;">\n\t<table class="table table-bordered table-hover">\n\t\t<thead><tr><th class="text-left" style="min-width: 120px;">Item Name</th><th class="text-right" style="min-width: 80px;">Taxable Amount</th><th class="text-right" style="min-width: 80px;">_Test Account Service Tax - _TC</th></tr></thead>\n\t\t<tbody><tr><td>_Test Item</td><td class="text-right">\u20b9 10,000.00</td><td class="text-right">(10.0%) \u20b9 1,000.00</td></tr></tbody>\n\t</table>\n</div>'''
 		
 		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 8eb83af..72785da 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -517,9 +517,9 @@
 		
 		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 @@
 	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 @@
 			
 	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 @@
 					 + item_tax_record[head[0]][1] + "</td>")
 			else:
 				taxes.append("<td></td>")
-		
+
+		item_code = item.item_code or item.item_name
 		rows.append("<tr><td>{item_name}</td><td class='text-right'>{taxable_amount}</td>{taxes}</tr>".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 e917a75..e09925d 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -693,19 +693,26 @@
 
 		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("<tr><td>%(item_name)s</td><td class='text-right'>%(taxable_amount)s</td>%(taxes)s</tr>", {
 				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]] ?