Skip to content

Commit

Permalink
Merge pull request #36911 from ruthra-kumar/deduplicate_gain_loss_jou…
Browse files Browse the repository at this point in the history
…rnal_creation

fix: deduplicate gain/loss JE creation for journals as payment
  • Loading branch information
ruthra-kumar authored Sep 3, 2023
2 parents 4f0bb45 + 0366928 commit 27e0dd9
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_jv_entries(self):
"Journal Entry" as reference_type, t1.name as reference_name,
t1.posting_date, t1.remark as remarks, t2.name as reference_row,
{dr_or_cr} as amount, t2.is_advance, t2.exchange_rate,
t2.account_currency as currency
t2.account_currency as currency, t2.cost_center as cost_center
from
`tabJournal Entry` t1, `tabJournal Entry Account` t2
where
Expand Down Expand Up @@ -209,6 +209,7 @@ def get_dr_or_cr_notes(self):
"amount": -(inv.outstanding_in_account_currency),
"posting_date": inv.posting_date,
"currency": inv.currency,
"cost_center": inv.cost_center,
}
)
)
Expand Down Expand Up @@ -357,6 +358,7 @@ def get_allocated_entry(self, pay, inv, allocated_amount):
"allocated_amount": allocated_amount,
"difference_amount": pay.get("difference_amount"),
"currency": inv.get("currency"),
"cost_center": pay.get("cost_center"),
}
)

Expand Down Expand Up @@ -431,6 +433,7 @@ def get_payment_details(self, row, dr_or_cr):
"allocated_amount": flt(row.get("allocated_amount")),
"difference_amount": flt(row.get("difference_amount")),
"difference_account": row.get("difference_account"),
"cost_center": row.get("cost_center"),
}
)

Expand Down Expand Up @@ -603,7 +606,7 @@ def reconcile_dr_cr_note(dr_cr_notes, company):
inv.dr_or_cr: abs(inv.allocated_amount),
"reference_type": inv.against_voucher_type,
"reference_name": inv.against_voucher,
"cost_center": erpnext.get_default_cost_center(company),
"cost_center": inv.cost_center or erpnext.get_default_cost_center(company),
"exchange_rate": inv.exchange_rate,
"user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} against {inv.against_voucher}",
},
Expand All @@ -618,7 +621,7 @@ def reconcile_dr_cr_note(dr_cr_notes, company):
),
"reference_type": inv.voucher_type,
"reference_name": inv.voucher_no,
"cost_center": erpnext.get_default_cost_center(company),
"cost_center": inv.cost_center or erpnext.get_default_cost_center(company),
"exchange_rate": inv.exchange_rate,
"user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} from {inv.voucher_no}",
},
Expand Down Expand Up @@ -657,4 +660,5 @@ def reconcile_dr_cr_note(dr_cr_notes, company):
inv.against_voucher_type,
inv.against_voucher,
None,
inv.cost_center,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"column_break_7",
"difference_account",
"exchange_rate",
"currency"
"currency",
"cost_center"
],
"fields": [
{
Expand Down Expand Up @@ -144,11 +145,17 @@
"fieldtype": "Float",
"label": "Exchange Rate",
"read_only": 1
},
{
"fieldname": "cost_center",
"fieldtype": "Link",
"label": "Cost Center",
"options": "Cost Center"
}
],
"istable": 1,
"links": [],
"modified": "2022-12-24 21:01:14.882747",
"modified": "2023-09-03 07:52:33.684217",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Payment Reconciliation Allocation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"sec_break1",
"remark",
"currency",
"exchange_rate"
"exchange_rate",
"cost_center"
],
"fields": [
{
Expand Down Expand Up @@ -98,11 +99,17 @@
"fieldtype": "Float",
"hidden": 1,
"label": "Exchange Rate"
},
{
"fieldname": "cost_center",
"fieldtype": "Link",
"label": "Cost Center",
"options": "Cost Center"
}
],
"istable": 1,
"links": [],
"modified": "2022-11-08 18:18:36.268760",
"modified": "2023-09-03 07:43:29.965353",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Payment Reconciliation Payment",
Expand Down
16 changes: 12 additions & 4 deletions erpnext/accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,12 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n

# update ref in advance entry
if voucher_type == "Journal Entry":
update_reference_in_journal_entry(entry, doc, do_not_save=True)
referenced_row = update_reference_in_journal_entry(entry, doc, do_not_save=False)
# advance section in sales/purchase invoice and reconciliation tool,both pass on exchange gain/loss
# amount and account in args
doc.make_exchange_gain_loss_journal(args)
# referenced_row is used to deduplicate gain/loss journal
entry.update({"referenced_row": referenced_row})
doc.make_exchange_gain_loss_journal([entry])
else:
update_reference_in_payment_entry(
entry, doc, do_not_save=True, skip_ref_details_update_for_pe=skip_ref_details_update_for_pe
Expand Down Expand Up @@ -627,6 +629,8 @@ def update_reference_in_journal_entry(d, journal_entry, do_not_save=False):
if not do_not_save:
journal_entry.save(ignore_permissions=True)

return new_row.name


def update_reference_in_payment_entry(
d, payment_entry, do_not_save=False, skip_ref_details_update_for_pe=False
Expand Down Expand Up @@ -1750,6 +1754,7 @@ def query_for_outstanding(self):
ple.posting_date,
ple.due_date,
ple.account_currency.as_("currency"),
ple.cost_center.as_("cost_center"),
Sum(ple.amount).as_("amount"),
Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"),
)
Expand Down Expand Up @@ -1812,6 +1817,7 @@ def query_for_outstanding(self):
).as_("paid_amount_in_account_currency"),
Table("vouchers").due_date,
Table("vouchers").currency,
Table("vouchers").cost_center.as_("cost_center"),
)
.where(Criterion.all(filter_on_outstanding_amount))
)
Expand Down Expand Up @@ -1895,12 +1901,14 @@ def create_gain_loss_journal(
ref2_dt,
ref2_dn,
ref2_detail_no,
cost_center,
) -> str:
journal_entry = frappe.new_doc("Journal Entry")
journal_entry.voucher_type = "Exchange Gain Or Loss"
journal_entry.company = company
journal_entry.posting_date = nowdate()
journal_entry.multi_currency = 1
journal_entry.is_system_generated = True

party_account_currency = frappe.get_cached_value("Account", party_account, "account_currency")

Expand All @@ -1919,7 +1927,7 @@ def create_gain_loss_journal(
"party": party,
"account_currency": party_account_currency,
"exchange_rate": 0,
"cost_center": erpnext.get_default_cost_center(company),
"cost_center": cost_center or erpnext.get_default_cost_center(company),
"reference_type": ref1_dt,
"reference_name": ref1_dn,
"reference_detail_no": ref1_detail_no,
Expand All @@ -1935,7 +1943,7 @@ def create_gain_loss_journal(
"account": gain_loss_account,
"account_currency": gain_loss_account_currency,
"exchange_rate": 1,
"cost_center": erpnext.get_default_cost_center(company),
"cost_center": cost_center or erpnext.get_default_cost_center(company),
"reference_type": ref2_dt,
"reference_name": ref2_dn,
"reference_detail_no": ref2_detail_no,
Expand Down
79 changes: 63 additions & 16 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,44 @@ def make_precision_loss_gl_entry(self, gl_entries):
)
)

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
Expand Down Expand Up @@ -1051,27 +1089,35 @@ def make_exchange_gain_loss_journal(self, args: dict = None) -> None:

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
Expand Down Expand Up @@ -1144,6 +1190,7 @@ def make_exchange_gain_loss_journal(self, args: dict = None) -> None:
self.doctype,
self.name,
d.idx,
self.cost_center,
)
frappe.msgprint(
_("Exchange Gain/Loss amount has been booked through {0}").format(
Expand Down
Loading

0 comments on commit 27e0dd9

Please sign in to comment.