Merge pull request #40220 from ruthra-kumar/exc_gain_loss_on_journal_against_journals

refactor: Gain/Loss Journal creation for Journals against Journals
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 2a02cd7..835d202 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -578,17 +578,28 @@
 					elif d.party_type == "Supplier" and flt(d.credit) > 0:
 						frappe.throw(_("Row {0}: Advance against Supplier must be debit").format(d.idx))
 
+	def system_generated_gain_loss(self):
+		return (
+			self.voucher_type == "Exchange Gain Or Loss"
+			and self.multi_currency
+			and self.is_system_generated
+		)
+
 	def validate_against_jv(self):
 		for d in self.get("accounts"):
 			if d.reference_type == "Journal Entry":
 				account_root_type = frappe.get_cached_value("Account", d.account, "root_type")
-				if account_root_type == "Asset" and flt(d.debit) > 0:
+				if account_root_type == "Asset" and flt(d.debit) > 0 and not self.system_generated_gain_loss():
 					frappe.throw(
 						_(
 							"Row #{0}: For {1}, you can select reference document only if account gets credited"
 						).format(d.idx, d.account)
 					)
-				elif account_root_type == "Liability" and flt(d.credit) > 0:
+				elif (
+					account_root_type == "Liability"
+					and flt(d.credit) > 0
+					and not self.system_generated_gain_loss()
+				):
 					frappe.throw(
 						_(
 							"Row #{0}: For {1}, you can select reference document only if account gets debited"
@@ -620,7 +631,7 @@
 					for jvd in against_entries:
 						if flt(jvd[dr_or_cr]) > 0:
 							valid = True
-					if not valid:
+					if not valid and not self.system_generated_gain_loss():
 						frappe.throw(
 							_("Against Journal Entry {0} does not have any unmatched {1} entry").format(
 								d.reference_name, dr_or_cr
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 6b6324c..a3db196 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -46,6 +46,7 @@
 from erpnext.accounts.utils import (
 	create_gain_loss_journal,
 	get_account_currency,
+	get_currency_precision,
 	get_fiscal_years,
 	validate_fiscal_year,
 )
@@ -1301,10 +1302,12 @@
 				# These are generated by Sales/Purchase Invoice during reconciliation and advance allocation.
 				# and below logic is only for such scenarios
 				if args:
+					precision = get_currency_precision()
 					for arg in args:
 						# Advance section uses `exchange_gain_loss` and reconciliation uses `difference_amount`
 						if (
-							arg.get("difference_amount", 0) != 0 or arg.get("exchange_gain_loss", 0) != 0
+							flt(arg.get("difference_amount", 0), precision) != 0
+							or flt(arg.get("exchange_gain_loss", 0), precision) != 0
 						) and arg.get("difference_account"):
 
 							party_account = arg.get("account")
diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py
index d2a3574..2170628 100644
--- a/erpnext/controllers/tests/test_accounts_controller.py
+++ b/erpnext/controllers/tests/test_accounts_controller.py
@@ -56,7 +56,8 @@
 	20 series - Sales Invoice against Journals
 	30 series - Sales Invoice against Credit Notes
 	40 series - Company default Cost center is unset
-	50 series - Dimension inheritence
+	50 series = Journals against Journals
+	90 series - Dimension inheritence
 	"""
 
 	def setUp(self):
@@ -1271,7 +1272,7 @@
 			x.mandatory_for_pl = False
 		loc.save()
 
-	def test_50_dimensions_filter(self):
+	def test_90_dimensions_filter(self):
 		"""
 		Test workings of dimension filters
 		"""
@@ -1342,7 +1343,7 @@
 		self.assertEqual(len(pr.invoices), 0)
 		self.assertEqual(len(pr.payments), 1)
 
-	def test_51_cr_note_should_inherit_dimension(self):
+	def test_91_cr_note_should_inherit_dimension(self):
 		self.setup_dimensions()
 		rate_in_account_currency = 1
 
@@ -1384,7 +1385,7 @@
 					frappe.db.get_all("Journal Entry Account", filters={"parent": x.parent}, pluck="department"),
 				)
 
-	def test_52_dimension_inhertiance_exc_gain_loss(self):
+	def test_92_dimension_inhertiance_exc_gain_loss(self):
 		# Sales Invoice in Foreign Currency
 		self.setup_dimensions()
 		rate = 80
@@ -1422,7 +1423,7 @@
 			),
 		)
 
-	def test_53_dimension_inheritance_on_advance(self):
+	def test_93_dimension_inheritance_on_advance(self):
 		self.setup_dimensions()
 		dpt = "Research & Development"
 
@@ -1467,3 +1468,70 @@
 				pluck="department",
 			),
 		)
+
+	def test_50_journal_against_journal(self):
+		# Invoice in Foreign Currency
+		journal_as_invoice = self.create_journal_entry(
+			acc1=self.debit_usd,
+			acc1_exc_rate=83,
+			acc2=self.cash,
+			acc1_amount=1,
+			acc2_amount=83,
+			acc2_exc_rate=1,
+		)
+		journal_as_invoice.accounts[0].party_type = "Customer"
+		journal_as_invoice.accounts[0].party = self.customer
+		journal_as_invoice = journal_as_invoice.save().submit()
+
+		# Payment
+		journal_as_payment = self.create_journal_entry(
+			acc1=self.debit_usd,
+			acc1_exc_rate=75,
+			acc2=self.cash,
+			acc1_amount=-1,
+			acc2_amount=-75,
+			acc2_exc_rate=1,
+		)
+		journal_as_payment.accounts[0].party_type = "Customer"
+		journal_as_payment.accounts[0].party = self.customer
+		journal_as_payment = journal_as_payment.save().submit()
+
+		# Reconcile the remaining amount
+		pr = self.create_payment_reconciliation()
+		# pr.receivable_payable_account = self.debit_usd
+		pr.get_unreconciled_entries()
+		self.assertEqual(len(pr.invoices), 1)
+		self.assertEqual(len(pr.payments), 1)
+		invoices = [x.as_dict() for x in pr.invoices]
+		payments = [x.as_dict() for x in pr.payments]
+		pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments}))
+		pr.reconcile()
+		self.assertEqual(len(pr.invoices), 0)
+		self.assertEqual(len(pr.payments), 0)
+
+		# There should be no outstanding in both currencies
+		journal_as_invoice.reload()
+		self.assert_ledger_outstanding(journal_as_invoice.doctype, journal_as_invoice.name, 0.0, 0.0)
+
+		# Exchange Gain/Loss Journal should've been created.
+		exc_je_for_si = self.get_journals_for(journal_as_invoice.doctype, journal_as_invoice.name)
+		exc_je_for_je = self.get_journals_for(journal_as_payment.doctype, journal_as_payment.name)
+		self.assertNotEqual(exc_je_for_si, [])
+		self.assertEqual(
+			len(exc_je_for_si), 2
+		)  # payment also has reference. so, there are 2 journals referencing invoice
+		self.assertEqual(len(exc_je_for_je), 1)
+		self.assertIn(exc_je_for_je[0], exc_je_for_si)
+
+		# Cancel Payment
+		journal_as_payment.reload()
+		journal_as_payment.cancel()
+
+		journal_as_invoice.reload()
+		self.assert_ledger_outstanding(journal_as_invoice.doctype, journal_as_invoice.name, 83.0, 1.0)
+
+		# Exchange Gain/Loss Journal should've been cancelled
+		exc_je_for_si = self.get_journals_for(journal_as_invoice.doctype, journal_as_invoice.name)
+		exc_je_for_je = self.get_journals_for(journal_as_payment.doctype, journal_as_payment.name)
+		self.assertEqual(exc_je_for_si, [])
+		self.assertEqual(exc_je_for_je, [])