Tax breakup for actual amount and fixes in itemised purchase register (#10515)

* Tax breakup for actual amount and fixes in itemised pur register

* Rounding of itemised tax breakup amount
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
index 3171ff0..fa458df 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
@@ -2,7 +2,7 @@
 # License: GNU General Public License v3. See license.txt
 
 from __future__ import unicode_literals
-import frappe
+import frappe, erpnext
 from frappe import _
 from frappe.utils import flt
 from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import get_tax_accounts
@@ -14,10 +14,12 @@
 	if not filters: filters = {}
 	columns = get_columns(additional_table_columns)
 
+	company_currency = erpnext.get_company_currency(filters.company)
+
 	item_list = get_items(filters, additional_query_columns)
 	aii_account_map = get_aii_accounts()
 	if item_list:
-		itemised_tax, tax_columns = get_tax_accounts(item_list, columns,
+		itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency,
 			doctype="Purchase Invoice", tax_doctype="Purchase Taxes and Charges")
 
 	columns.append({
@@ -26,7 +28,7 @@
 		"fieldtype": "Data",
 		"width": 80
 	})
-	company_currency = frappe.db.get_value("Company", filters.company, "default_currency")
+
 	po_pr_map = get_purchase_receipts_against_purchase_order(item_list)
 
 	data = []
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
index 7009d5d..0fc5831 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
@@ -2,9 +2,10 @@
 # License: GNU General Public License v3. See license.txt
 
 from __future__ import unicode_literals
-import frappe
+import frappe, erpnext
 from frappe import _
 from frappe.utils import flt
+from frappe.model.meta import get_field_precision
 from erpnext.accounts.report.sales_register.sales_register import get_mode_of_payments
 
 def execute(filters=None):
@@ -14,16 +15,17 @@
 	if not filters: filters = {}
 	columns = get_columns(additional_table_columns)
 
+	company_currency = erpnext.get_company_currency(filters.company)
+
 	item_list = get_items(filters, additional_query_columns)
 	if item_list:
-		itemised_tax, tax_columns = get_tax_accounts(item_list, columns)
+		itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency)
 	columns.append({
 		"fieldname": "currency",
 		"label": _("Currency"),
 		"fieldtype": "Data",
 		"width": 80
 	})
-	company_currency = frappe.db.get_value("Company", filters.get("company"), "default_currency")
 	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)
 
@@ -140,16 +142,25 @@
 
 	return so_dn_map
 
-def get_tax_accounts(item_list, columns, doctype="Sales Invoice", tax_doctype="Sales Taxes and Charges"):
+def get_tax_accounts(item_list, columns, company_currency,
+		doctype="Sales Invoice", tax_doctype="Sales Taxes and Charges"):
 	import json
 	item_row_map = {}
 	tax_columns = []
 	invoice_item_row = {}
 	itemised_tax = {}
+
+	tax_amount_precision = get_field_precision(frappe.get_meta(tax_doctype).get_field("tax_amount"),
+		currency=company_currency) or 2
+
 	for d in item_list:
 		invoice_item_row.setdefault(d.parent, []).append(d)
 		item_row_map.setdefault(d.parent, {}).setdefault(d.item_code, []).append(d)
 
+	conditions = ""
+	if doctype == "Purchase Invoice":
+		conditions = " and category in ('Total', 'Valuation and Total')"
+
 	tax_details = frappe.db.sql("""
 		select
 			parent, description, item_wise_tax_detail,
@@ -159,8 +170,9 @@
 			parenttype = %s and docstatus = 1
 			and (description is not null and description != '')
 			and parent in (%s)
+			%s
 		order by description
-	""" % (tax_doctype, '%s', ', '.join(['%s']*len(invoice_item_row))),
+	""" % (tax_doctype, '%s', ', '.join(['%s']*len(invoice_item_row)), conditions),
 		tuple([doctype] + invoice_item_row.keys()))
 
 	for parent, description, item_wise_tax_detail, charge_type, tax_amount in tax_details:
@@ -192,7 +204,7 @@
 						if item_tax_amount:
 							itemised_tax.setdefault(d.name, {})[description] = frappe._dict({
 								"tax_rate": tax_rate,
-								"tax_amount": item_tax_amount
+								"tax_amount": flt(item_tax_amount, tax_amount_precision)
 							})
 
 			except ValueError:
@@ -201,7 +213,8 @@
 			for d in invoice_item_row.get(parent, []):
 				itemised_tax.setdefault(d.name, {})[description] = frappe._dict({
 					"tax_rate": "NA",
-					"tax_amount": flt((tax_amount * d.base_net_amount) / d.base_net_total)
+					"tax_amount": flt((tax_amount * d.base_net_amount) / d.base_net_total,
+						tax_amount_precision)
 				})
 
 	tax_columns.sort()
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 97bd771..c627664 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -270,7 +270,7 @@
 		if tax.item_wise_tax_detail.get(key):
 			item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
 
-		tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
+		tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount)]
 
 	def round_off_totals(self, tax):
 		tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
@@ -521,12 +521,20 @@
 	frappe.flags.company = doc.company
 	
 	# get headers
-	tax_accounts = list(set([d.description for d in doc.taxes]))
+	tax_accounts = []
+	for tax in doc.taxes:
+		if getattr(tax, "category", None) and tax.category=="Valuation":
+			continue
+		if tax.description not in tax_accounts:
+			tax_accounts.append(tax.description)
+
 	headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts)
 	
 	# get tax breakup data
 	itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(doc)
-	
+
+	get_rounded_tax_amount(itemised_tax, doc.precision("tax_amount", "taxes"))
+
 	frappe.flags.company = None
 	
 	return frappe.render_template(
@@ -554,6 +562,9 @@
 def get_itemised_tax(taxes):
 	itemised_tax = {}
 	for tax in taxes:
+		if getattr(tax, "category", None) and tax.category=="Valuation":
+			continue
+
 		tax_amount_precision = tax.precision("tax_amount")
 		tax_rate_precision = tax.precision("rate")
 		
@@ -562,16 +573,16 @@
 		for item_code, tax_data in item_tax_map.items():
 			itemised_tax.setdefault(item_code, frappe._dict())
 			
-			if isinstance(tax_data, list) and tax_data[0]:
+			if isinstance(tax_data, list):
 				precision = tax_amount_precision if tax.charge_type == "Actual" else tax_rate_precision
 				
 				itemised_tax[item_code][tax.description] = frappe._dict(dict(
-					tax_rate=flt(tax_data[0], precision),
-					tax_amount=flt(tax_data[1], tax_amount_precision)
+					tax_rate=flt(tax_data[0]),
+					tax_amount=flt(tax_data[1])
 				))
 			else:
 				itemised_tax[item_code][tax.description] = frappe._dict(dict(
-					tax_rate=flt(tax_data, tax_rate_precision),
+					tax_rate=flt(tax_data),
 					tax_amount=0.0
 				))
 
@@ -584,4 +595,10 @@
 		itemised_taxable_amount.setdefault(item_code, 0)
 		itemised_taxable_amount[item_code] += item.net_amount
 
-	return itemised_taxable_amount
\ No newline at end of file
+	return itemised_taxable_amount
+
+def get_rounded_tax_amount(itemised_tax, precision):
+	# Rounding based on tax_amount precision
+	for taxes in itemised_tax.values():
+		for tax_account in taxes:
+			taxes[tax_account]["tax_amount"] = flt(taxes[tax_account]["tax_amount"], precision)
\ No newline at end of file
diff --git a/erpnext/templates/includes/itemised_tax_breakup.html b/erpnext/templates/includes/itemised_tax_breakup.html
index 2ffc8b4..75212d5 100644
--- a/erpnext/templates/includes/itemised_tax_breakup.html
+++ b/erpnext/templates/includes/itemised_tax_breakup.html
@@ -22,7 +22,9 @@
 						{% set tax_details = taxes.get(tax_account) %}
 						{% if tax_details %}
 							<td class='text-right'>
-								({{ tax_details.tax_rate }})
+								{% if tax_details.tax_rate or not tax_details.tax_amount %}
+									({{ tax_details.tax_rate }})
+								{% endif %}
 								{{ frappe.utils.fmt_money(tax_details.tax_amount, None, company_currency) }}
 							</td>
 						{% else %}