refactor: simplify exchange logic on cr/dr note reconciliation

This commit is contained in:
ruthra kumar
2023-06-26 15:31:20 +05:30
parent 2f638ae32a
commit af75f6cea7

View File

@@ -336,7 +336,6 @@ class PaymentReconciliation(Document):
entry_list = [] entry_list = []
dr_or_cr_notes = [] dr_or_cr_notes = []
difference_entries = []
for row in self.get("allocation"): for row in self.get("allocation"):
reconciled_entry = [] reconciled_entry = []
if row.invoice_number and row.allocated_amount: if row.invoice_number and row.allocated_amount:
@@ -348,16 +347,17 @@ class PaymentReconciliation(Document):
payment_details = self.get_payment_details(row, dr_or_cr) payment_details = self.get_payment_details(row, dr_or_cr)
reconciled_entry.append(payment_details) reconciled_entry.append(payment_details)
if payment_details.difference_amount: if payment_details.difference_amount and row.reference_type not in [
difference_entries.append( "Sales Invoice",
self.make_difference_entry(payment_details, do_not_save_and_submit=bool(dr_or_cr_notes)) "Purchase Invoice",
) ]:
self.make_difference_entry(payment_details)
if entry_list: if entry_list:
reconcile_against_document(entry_list, skip_ref_details_update_for_pe) reconcile_against_document(entry_list, skip_ref_details_update_for_pe)
if dr_or_cr_notes: if dr_or_cr_notes:
reconcile_dr_cr_note(dr_or_cr_notes, difference_entries, self.company) reconcile_dr_cr_note(dr_or_cr_notes, self.company)
@frappe.whitelist() @frappe.whitelist()
def reconcile(self): def reconcile(self):
@@ -385,7 +385,7 @@ class PaymentReconciliation(Document):
self.get_unreconciled_entries() self.get_unreconciled_entries()
def make_difference_entry(self, row, do_not_save_and_submit=False): def make_difference_entry(self, row):
journal_entry = frappe.new_doc("Journal Entry") journal_entry = frappe.new_doc("Journal Entry")
journal_entry.voucher_type = "Exchange Gain Or Loss" journal_entry.voucher_type = "Exchange Gain Or Loss"
journal_entry.company = self.company journal_entry.company = self.company
@@ -433,9 +433,8 @@ class PaymentReconciliation(Document):
journal_entry.append("accounts", journal_account) journal_entry.append("accounts", journal_account)
if not do_not_save_and_submit: journal_entry.save()
journal_entry.save() journal_entry.submit()
journal_entry.submit()
return journal_entry return journal_entry
@@ -603,13 +602,16 @@ class PaymentReconciliation(Document):
return condition return condition
def reconcile_dr_cr_note(dr_cr_notes, difference_entries, company): def reconcile_dr_cr_note(dr_cr_notes, company):
def find_difference_entry(voucher_type, voucher_no): def get_difference_row(inv):
for jv in difference_entries: if inv.difference_amount != 0 and inv.difference_account:
accounts = iter(jv.accounts) difference_row = {
for account in accounts: "account": inv.difference_account,
if account.reference_type == voucher_type and account.reference_name == voucher_no: inv.dr_or_cr: abs(inv.difference_amount) if inv.difference_amount > 0 else 0,
return next(accounts) reconcile_dr_or_cr: abs(inv.difference_amount) if inv.difference_amount < 0 else 0,
"cost_center": erpnext.get_default_cost_center(company),
}
return difference_row
for inv in dr_cr_notes: for inv in dr_cr_notes:
voucher_type = "Credit Note" if inv.voucher_type == "Sales Invoice" else "Debit Note" voucher_type = "Credit Note" if inv.voucher_type == "Sales Invoice" else "Debit Note"
@@ -656,7 +658,7 @@ def reconcile_dr_cr_note(dr_cr_notes, difference_entries, company):
} }
) )
if difference_entry := find_difference_entry(inv.against_voucher_type, inv.against_voucher): if difference_entry := get_difference_row(inv):
jv.append("accounts", difference_entry) jv.append("accounts", difference_entry)
jv.flags.ignore_mandatory = True jv.flags.ignore_mandatory = True