Merge pull request #36911 from ruthra-kumar/deduplicate_gain_loss_journal_creation

fix: deduplicate gain/loss JE creation for journals as payment
This commit is contained in:
ruthra kumar
2023-09-03 10:11:43 +05:30
committed by GitHub
6 changed files with 291 additions and 27 deletions

View File

@@ -1023,6 +1023,44 @@ class AccountsController(TransactionBase):
)
)
def gain_loss_journal_already_booked(
self,
gain_loss_account,
exc_gain_loss,
ref2_dt,
ref2_dn,
ref2_detail_no,
) -> bool:
"""
Check if gain/loss is booked
"""
if res := frappe.db.get_all(
"Journal Entry Account",
filters={
"docstatus": 1,
"account": gain_loss_account,
"reference_type": ref2_dt, # this will be Journal Entry
"reference_name": ref2_dn,
"reference_detail_no": ref2_detail_no,
},
pluck="parent",
):
# deduplicate
res = list({x for x in res})
if exc_vouchers := frappe.db.get_all(
"Journal Entry",
filters={"name": ["in", res], "voucher_type": "Exchange Gain Or Loss"},
fields=["voucher_type", "total_debit", "total_credit"],
):
booked_voucher = exc_vouchers[0]
if (
booked_voucher.total_debit == exc_gain_loss
and booked_voucher.total_credit == exc_gain_loss
and booked_voucher.voucher_type == "Exchange Gain Or Loss"
):
return True
return False
def make_exchange_gain_loss_journal(self, args: dict = None) -> None:
"""
Make Exchange Gain/Loss journal for Invoices and Payments
@@ -1051,27 +1089,35 @@ class AccountsController(TransactionBase):
reverse_dr_or_cr = "debit" if dr_or_cr == "credit" else "credit"
je = create_gain_loss_journal(
self.company,
arg.get("party_type"),
arg.get("party"),
party_account,
if not self.gain_loss_journal_already_booked(
gain_loss_account,
difference_amount,
dr_or_cr,
reverse_dr_or_cr,
arg.get("against_voucher_type"),
arg.get("against_voucher"),
arg.get("idx"),
self.doctype,
self.name,
arg.get("idx"),
)
frappe.msgprint(
_("Exchange Gain/Loss amount has been booked through {0}").format(
get_link_to_form("Journal Entry", je)
arg.get("referenced_row"),
):
je = create_gain_loss_journal(
self.company,
arg.get("party_type"),
arg.get("party"),
party_account,
gain_loss_account,
difference_amount,
dr_or_cr,
reverse_dr_or_cr,
arg.get("against_voucher_type"),
arg.get("against_voucher"),
arg.get("idx"),
self.doctype,
self.name,
arg.get("referenced_row"),
arg.get("cost_center"),
)
frappe.msgprint(
_("Exchange Gain/Loss amount has been booked through {0}").format(
get_link_to_form("Journal Entry", je)
)
)
)
if self.get("doctype") == "Payment Entry":
# For Payment Entry, exchange_gain_loss field in the `references` table is the trigger for journal creation
@@ -1144,6 +1190,7 @@ class AccountsController(TransactionBase):
self.doctype,
self.name,
d.idx,
self.cost_center,
)
frappe.msgprint(
_("Exchange Gain/Loss amount has been booked through {0}").format(