Merge pull request #22589 from deepeshgarg007/rcm_fix

fix: Do not add GST tax amount in grand total for reverse charge invoices
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 98d68f8..7b1062f 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -438,6 +438,8 @@
 
 		self.make_tax_gl_entries(gl_entries)
 
+		gl_entries = make_regional_gl_entries(gl_entries, self)
+
 		gl_entries = merge_similar_entries(gl_entries)
 
 		self.make_payment_gl_entries(gl_entries)
@@ -1108,6 +1110,10 @@
 	})
 	return list_context
 
+@erpnext.allow_regional
+def make_regional_gl_entries(gl_entries, doc):
+	return gl_entries
+
 @frappe.whitelist()
 def make_debit_note(source_name, target_doc=None):
 	from erpnext.controllers.sales_and_purchase_return import make_return_doc
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 70217dc..e8dda20 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -246,7 +246,7 @@
 		"on_trash": "erpnext.regional.check_deletion_permission"
 	},
 	"Purchase Invoice": {
-		"on_submit": "erpnext.regional.india.utils.make_reverse_charge_entries"
+		"validate": "erpnext.regional.india.utils.update_grand_total_for_rcm"
 	},
 	"Payment Entry": {
 		"on_submit": ["erpnext.regional.create_transaction_log", "erpnext.accounts.doctype.payment_request.payment_request.update_payment_req_status"],
@@ -374,7 +374,8 @@
 		'erpnext.controllers.taxes_and_totals.get_itemised_tax_breakup_data': 'erpnext.regional.india.utils.get_itemised_tax_breakup_data',
 		'erpnext.accounts.party.get_regional_address_details': 'erpnext.regional.india.utils.get_regional_address_details',
 		'erpnext.hr.utils.calculate_annual_eligible_hra_exemption': 'erpnext.regional.india.utils.calculate_annual_eligible_hra_exemption',
-		'erpnext.hr.utils.calculate_hra_exemption_for_period': 'erpnext.regional.india.utils.calculate_hra_exemption_for_period'
+		'erpnext.hr.utils.calculate_hra_exemption_for_period': 'erpnext.regional.india.utils.calculate_hra_exemption_for_period',
+		'erpnext.accounts.doctype.purchase_invoice.purchase_invoice.make_regional_gl_entries': 'erpnext.regional.india.utils.make_regional_gl_entries'
 	},
 	'United Arab Emirates': {
 		'erpnext.controllers.taxes_and_totals.update_itemised_tax_data': 'erpnext.regional.united_arab_emirates.utils.update_itemised_tax_data'
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index 961b8c6..24bb137 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -1,7 +1,7 @@
 from __future__ import unicode_literals
 import frappe, re, json
 from frappe import _
-from frappe.utils import cstr, flt, date_diff, nowdate
+from frappe.utils import cstr, flt, date_diff, nowdate, round_based_on_smallest_currency_fraction, money_in_words
 from erpnext.regional.india import states, state_numbers
 from erpnext.controllers.taxes_and_totals import get_itemised_tax, get_itemised_taxable_amount
 from erpnext.controllers.accounts_controller import get_taxes_and_charges
@@ -648,6 +648,7 @@
 	else:
 		return int(state_code)
 
+@frappe.whitelist()
 def get_gst_accounts(company, account_wise=False):
 	gst_accounts = frappe._dict()
 	gst_settings_accounts = frappe.get_all("GST Account",
@@ -666,14 +667,49 @@
 
 	return gst_accounts
 
-def make_reverse_charge_entries(doc, method):
+def update_grand_total_for_rcm(doc, method):
+	if doc.reverse_charge == 'Y':
+		gst_accounts = get_gst_accounts(doc.company)
+		gst_account_list = gst_accounts.get('cgst_account') + gst_accounts.get('sgst_account') \
+			+ gst_accounts.get('igst_account')
+
+		gst_tax = 0
+		for tax in doc.get('taxes'):
+			if tax.category not in ("Total", "Valuation and Total"):
+				continue
+
+			if flt(tax.base_tax_amount_after_discount_amount) and tax.account_head in gst_account_list:
+				gst_tax += tax.base_tax_amount_after_discount_amount
+
+		doc.taxes_and_charges_added -= gst_tax
+		doc.total_taxes_and_charges -= gst_tax
+
+		update_totals(gst_tax, doc)
+
+def update_totals(gst_tax, doc):
+	doc.grand_total -= gst_tax
+
+	if doc.meta.get_field("rounded_total"):
+		if doc.is_rounded_total_disabled():
+			doc.outstanding_amount = doc.grand_total
+		else:
+			doc.rounded_total = round_based_on_smallest_currency_fraction(doc.grand_total,
+				doc.currency, doc.precision("rounded_total"))
+
+			doc.rounding_adjustment += flt(doc.rounded_total - doc.grand_total,
+				doc.precision("rounding_adjustment"))
+
+			doc.outstanding_amount = doc.base_rounded_total
+
+	doc.in_words = money_in_words(doc.grand_total, doc.currency)
+
+def make_regional_gl_entries(gl_entries, doc):
 	country = frappe.get_cached_value('Company', doc.company, 'country')
 
 	if country != 'India':
 		return
 
 	if doc.reverse_charge == 'Y':
-		gl_entries = []
 		gst_accounts = get_gst_accounts(doc.company)
 		gst_account_list = gst_accounts.get('cgst_account') + gst_accounts.get('sgst_account') \
 			+ gst_accounts.get('igst_account')
@@ -698,19 +734,4 @@
 					}, account_currency, item=tax)
 				)
 
-				gl_entries.append(doc.get_gl_dict(
-					{
-						"account": doc.credit_to if doc.doctype == 'Purchase Invoice' else doc.debit_to,
-						"cost_center": doc.cost_center,
-						"posting_date": doc.posting_date,
-						"party_type": 'Supplier',
-						"party": doc.supplier,
-						"against": tax.account_head,
-						"debit": tax.base_tax_amount_after_discount_amount,
-						"debit_in_account_currency": tax.base_tax_amount_after_discount_amount \
-							if account_currency==doc.company_currency \
-							else tax.tax_amount_after_discount_amount
-					}, account_currency, item=doc)
-				)
-
-		make_gl_entries(gl_entries)
\ No newline at end of file
+	return gl_entries
\ No newline at end of file