From c1a0ac655ea5fd3b9d8d00f36c5ea15ca5af3038 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 1 Mar 2024 17:36:56 +0530 Subject: [PATCH 1/4] fix: make use of 'flt' to prevent really low precision exc gain/loss (cherry picked from commit 0aa72f841dbfb4a19fc6f7802172dcc5667369d7) --- erpnext/controllers/accounts_controller.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 560e7715e68..0795ab0f747 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -45,6 +45,7 @@ from erpnext.accounts.party import ( from erpnext.accounts.utils import ( create_gain_loss_journal, get_account_currency, + get_currency_precision, get_fiscal_years, validate_fiscal_year, ) @@ -1169,10 +1170,12 @@ class AccountsController(TransactionBase): # 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") From 52e1c2f48b6750767fe1e047ff31499b727dcef9 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 1 Mar 2024 17:46:42 +0530 Subject: [PATCH 2/4] fix: allow gain/loss for Journals against Journals (cherry picked from commit 5b67631d40ddebcc6111aecb9c2f8882903d88f9) # Conflicts: # erpnext/accounts/doctype/journal_entry/journal_entry.py --- .../doctype/journal_entry/journal_entry.py | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 6e3019bb8f0..3f427332276 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -475,17 +475,33 @@ class JournalEntry(AccountsController): 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": +<<<<<<< HEAD account_root_type = frappe.db.get_value("Account", d.account, "root_type") if account_root_type == "Asset" and flt(d.debit) > 0: +======= + account_root_type = frappe.get_cached_value("Account", d.account, "root_type") + if account_root_type == "Asset" and flt(d.debit) > 0 and not self.system_generated_gain_loss(): +>>>>>>> 5b67631d40 (fix: allow gain/loss for Journals against Journals) 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" @@ -517,7 +533,7 @@ class JournalEntry(AccountsController): 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 From 46063518d75601d8cea175c1739de28b9d885169 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 1 Mar 2024 17:53:51 +0530 Subject: [PATCH 3/4] test: gain/loss on Journals against Journals (cherry picked from commit 8a5078b826e9390bc63069fefff3cf3de8bae449) # Conflicts: # erpnext/controllers/tests/test_accounts_controller.py --- .../tests/test_accounts_controller.py | 81 +++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py index 0c9d34d82a2..daea1d8b399 100644 --- a/erpnext/controllers/tests/test_accounts_controller.py +++ b/erpnext/controllers/tests/test_accounts_controller.py @@ -56,7 +56,8 @@ class TestAccountsController(FrappeTestCase): 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): @@ -1306,7 +1307,7 @@ class TestAccountsController(FrappeTestCase): dimension1.disabled = 1 dimension1.save() - def test_50_dimensions_filter(self): + def test_90_dimensions_filter(self): """ Test workings of dimension filters """ @@ -1378,7 +1379,7 @@ class TestAccountsController(FrappeTestCase): self.assertEqual(len(pr.payments), 1) self.disable_dimensions() - 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 @@ -1421,7 +1422,7 @@ class TestAccountsController(FrappeTestCase): ) self.disable_dimensions() - 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 @@ -1460,7 +1461,7 @@ class TestAccountsController(FrappeTestCase): ) self.disable_dimensions() - def test_53_dimension_inheritance_on_advance(self): + def test_93_dimension_inheritance_on_advance(self): self.setup_dimensions() dpt = "Research & Development" @@ -1505,4 +1506,74 @@ class TestAccountsController(FrappeTestCase): pluck="department", ), ) +<<<<<<< HEAD self.disable_dimensions() +======= + + 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, []) +>>>>>>> 8a5078b826 (test: gain/loss on Journals against Journals) From 753223da7851fffc43bb0697d9ff9b1755c6d1c0 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sat, 2 Mar 2024 15:59:48 +0530 Subject: [PATCH 4/4] chore: resolve merge conflict --- erpnext/accounts/doctype/journal_entry/journal_entry.py | 5 ----- erpnext/controllers/tests/test_accounts_controller.py | 3 --- 2 files changed, 8 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 3f427332276..7ea21c9f134 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -485,13 +485,8 @@ class JournalEntry(AccountsController): def validate_against_jv(self): for d in self.get("accounts"): if d.reference_type == "Journal Entry": -<<<<<<< HEAD - account_root_type = frappe.db.get_value("Account", d.account, "root_type") - if account_root_type == "Asset" and flt(d.debit) > 0: -======= account_root_type = frappe.get_cached_value("Account", d.account, "root_type") if account_root_type == "Asset" and flt(d.debit) > 0 and not self.system_generated_gain_loss(): ->>>>>>> 5b67631d40 (fix: allow gain/loss for Journals against Journals) frappe.throw( _( "Row #{0}: For {1}, you can select reference document only if account gets credited" diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py index daea1d8b399..e39d03dccb9 100644 --- a/erpnext/controllers/tests/test_accounts_controller.py +++ b/erpnext/controllers/tests/test_accounts_controller.py @@ -1506,9 +1506,7 @@ class TestAccountsController(FrappeTestCase): pluck="department", ), ) -<<<<<<< HEAD self.disable_dimensions() -======= def test_50_journal_against_journal(self): # Invoice in Foreign Currency @@ -1576,4 +1574,3 @@ class TestAccountsController(FrappeTestCase): 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, []) ->>>>>>> 8a5078b826 (test: gain/loss on Journals against Journals)