Merge pull request #40220 from ruthra-kumar/exc_gain_loss_on_journal_against_journals

refactor: Gain/Loss Journal creation for Journals against Journals
This commit is contained in:
ruthra kumar 2024-03-02 15:36:27 +05:30 committed by GitHub
commit f2f5c1a0eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 91 additions and 9 deletions

View File

@ -578,17 +578,28 @@ class JournalEntry(AccountsController):
elif d.party_type == "Supplier" and flt(d.credit) > 0: elif d.party_type == "Supplier" and flt(d.credit) > 0:
frappe.throw(_("Row {0}: Advance against Supplier must be debit").format(d.idx)) 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): def validate_against_jv(self):
for d in self.get("accounts"): for d in self.get("accounts"):
if d.reference_type == "Journal Entry": if d.reference_type == "Journal Entry":
account_root_type = frappe.get_cached_value("Account", d.account, "root_type") 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( frappe.throw(
_( _(
"Row #{0}: For {1}, you can select reference document only if account gets credited" "Row #{0}: For {1}, you can select reference document only if account gets credited"
).format(d.idx, d.account) ).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( frappe.throw(
_( _(
"Row #{0}: For {1}, you can select reference document only if account gets debited" "Row #{0}: For {1}, you can select reference document only if account gets debited"
@ -620,7 +631,7 @@ class JournalEntry(AccountsController):
for jvd in against_entries: for jvd in against_entries:
if flt(jvd[dr_or_cr]) > 0: if flt(jvd[dr_or_cr]) > 0:
valid = True valid = True
if not valid: if not valid and not self.system_generated_gain_loss():
frappe.throw( frappe.throw(
_("Against Journal Entry {0} does not have any unmatched {1} entry").format( _("Against Journal Entry {0} does not have any unmatched {1} entry").format(
d.reference_name, dr_or_cr d.reference_name, dr_or_cr

View File

@ -46,6 +46,7 @@ from erpnext.accounts.party import (
from erpnext.accounts.utils import ( from erpnext.accounts.utils import (
create_gain_loss_journal, create_gain_loss_journal,
get_account_currency, get_account_currency,
get_currency_precision,
get_fiscal_years, get_fiscal_years,
validate_fiscal_year, validate_fiscal_year,
) )
@ -1301,10 +1302,12 @@ class AccountsController(TransactionBase):
# These are generated by Sales/Purchase Invoice during reconciliation and advance allocation. # These are generated by Sales/Purchase Invoice during reconciliation and advance allocation.
# and below logic is only for such scenarios # and below logic is only for such scenarios
if args: if args:
precision = get_currency_precision()
for arg in args: for arg in args:
# Advance section uses `exchange_gain_loss` and reconciliation uses `difference_amount` # Advance section uses `exchange_gain_loss` and reconciliation uses `difference_amount`
if ( 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"): ) and arg.get("difference_account"):
party_account = arg.get("account") party_account = arg.get("account")

View File

@ -56,7 +56,8 @@ class TestAccountsController(FrappeTestCase):
20 series - Sales Invoice against Journals 20 series - Sales Invoice against Journals
30 series - Sales Invoice against Credit Notes 30 series - Sales Invoice against Credit Notes
40 series - Company default Cost center is unset 40 series - Company default Cost center is unset
50 series - Dimension inheritence 50 series = Journals against Journals
90 series - Dimension inheritence
""" """
def setUp(self): def setUp(self):
@ -1271,7 +1272,7 @@ class TestAccountsController(FrappeTestCase):
x.mandatory_for_pl = False x.mandatory_for_pl = False
loc.save() loc.save()
def test_50_dimensions_filter(self): def test_90_dimensions_filter(self):
""" """
Test workings of dimension filters Test workings of dimension filters
""" """
@ -1342,7 +1343,7 @@ class TestAccountsController(FrappeTestCase):
self.assertEqual(len(pr.invoices), 0) self.assertEqual(len(pr.invoices), 0)
self.assertEqual(len(pr.payments), 1) 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() self.setup_dimensions()
rate_in_account_currency = 1 rate_in_account_currency = 1
@ -1384,7 +1385,7 @@ class TestAccountsController(FrappeTestCase):
frappe.db.get_all("Journal Entry Account", filters={"parent": x.parent}, pluck="department"), 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 # Sales Invoice in Foreign Currency
self.setup_dimensions() self.setup_dimensions()
rate = 80 rate = 80
@ -1422,7 +1423,7 @@ class TestAccountsController(FrappeTestCase):
), ),
) )
def test_53_dimension_inheritance_on_advance(self): def test_93_dimension_inheritance_on_advance(self):
self.setup_dimensions() self.setup_dimensions()
dpt = "Research & Development" dpt = "Research & Development"
@ -1467,3 +1468,70 @@ class TestAccountsController(FrappeTestCase):
pluck="department", 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, [])