From 65b4dbab2a310a7acab755a6eee089287ec90190 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 28 Nov 2023 17:04:07 +0530 Subject: [PATCH] refactor: return the newly added reference upon reconciliation (cherry picked from commit ecb533c4d1971d5963f9d8ff4b038fa4b62cf516) --- erpnext/accounts/utils.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 718af512b7..934fafb820 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -473,6 +473,8 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n doc = frappe.get_doc(voucher_type, voucher_no) frappe.flags.ignore_party_validation = True + # For payments with `Advance` in separate account feature enabled, only new ledger entries are posted for each reference. + # No need to cancel/delete payment ledger entries if not (voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account): _delete_pl_entries(voucher_type, voucher_no) @@ -489,7 +491,7 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n entry.update({"referenced_row": referenced_row}) doc.make_exchange_gain_loss_journal([entry]) else: - update_reference_in_payment_entry( + referenced_row = update_reference_in_payment_entry( entry, doc, do_not_save=True, skip_ref_details_update_for_pe=skip_ref_details_update_for_pe ) @@ -498,8 +500,8 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n doc = frappe.get_doc(entry.voucher_type, entry.voucher_no) if voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account: - # both ledgers must be posted to for `Advance as Liability` - doc.make_advance_gl_entries() + # both ledgers must be posted to for `Advance` in separate account feature + doc.make_advance_gl_entries(referenced_row, update_outstanding="No") else: gl_map = doc.build_gl_map() create_payment_ledger_entry(gl_map, update_outstanding="No", cancel=0, adv_adj=1) @@ -676,11 +678,12 @@ def update_reference_in_payment_entry( new_row.docstatus = 1 for field in list(reference_details): new_row.set(field, reference_details[field]) - + row = new_row else: new_row = payment_entry.append("references") new_row.docstatus = 1 new_row.update(reference_details) + row = new_row payment_entry.flags.ignore_validate_update_after_submit = True payment_entry.clear_unallocated_reference_document_rows() @@ -693,6 +696,7 @@ def update_reference_in_payment_entry( if not do_not_save: payment_entry.save(ignore_permissions=True) + return row def cancel_exchange_gain_loss_journal( @@ -869,7 +873,13 @@ def remove_ref_doc_link_from_pe( try: pe_doc = frappe.get_doc("Payment Entry", pe) pe_doc.set_amounts() - pe_doc.make_advance_gl_entries(against_voucher_type=ref_type, against_voucher=ref_no, cancel=1) + + # Call cancel on only removed reference + references = [ + x for x in pe_doc.references if x.reference_doctype == ref_type and x.reference_name == ref_no + ] + [pe_doc.make_advance_gl_entries(x, cancel=1) for x in references] + pe_doc.clear_unallocated_reference_document_rows() pe_doc.validate_payment_type_with_outstanding() except Exception as e: