diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 5c2dc7747bb..d4e6199ab4d 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -48,6 +48,7 @@ from erpnext.accounts.utils import ( get_account_currency, get_balance_on, get_outstanding_invoices, + get_reconciliation_effect_date, ) from erpnext.controllers.accounts_controller import ( AccountsController, @@ -1492,23 +1493,7 @@ class PaymentEntry(AccountsController): else: # For backwards compatibility # Supporting reposting on payment entries reconciled before select field introduction - reconciliation_takes_effect_on = frappe.get_cached_value( - "Company", self.company, "reconciliation_takes_effect_on" - ) - if reconciliation_takes_effect_on == "Advance Payment Date": - posting_date = self.posting_date - elif reconciliation_takes_effect_on == "Oldest Of Invoice Or Advance": - date_field = "posting_date" - if invoice.reference_doctype in ["Sales Order", "Purchase Order"]: - date_field = "transaction_date" - posting_date = frappe.db.get_value( - invoice.reference_doctype, invoice.reference_name, date_field - ) - - if getdate(posting_date) < getdate(self.posting_date): - posting_date = self.posting_date - elif reconciliation_takes_effect_on == "Reconciliation Date": - posting_date = nowdate() + posting_date = get_reconciliation_effect_date(invoice, self.company, self.posting_date) frappe.db.set_value("Payment Entry Reference", invoice.name, "reconcile_effect_on", posting_date) dr_or_cr, account = self.get_dr_and_account_for_advances(invoice) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 786bfd6f8b7..47ade441e46 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -713,23 +713,8 @@ def update_reference_in_payment_entry( update_advance_paid = [] # Update Reconciliation effect date in reference - reconciliation_takes_effect_on = frappe.get_cached_value( - "Company", payment_entry.company, "reconciliation_takes_effect_on" - ) if payment_entry.book_advance_payments_in_separate_party_account: - if reconciliation_takes_effect_on == "Advance Payment Date": - reconcile_on = payment_entry.posting_date - elif reconciliation_takes_effect_on == "Oldest Of Invoice Or Advance": - date_field = "posting_date" - if d.against_voucher_type in ["Sales Order", "Purchase Order"]: - date_field = "transaction_date" - reconcile_on = frappe.db.get_value(d.against_voucher_type, d.against_voucher, date_field) - - if getdate(reconcile_on) < getdate(payment_entry.posting_date): - reconcile_on = payment_entry.posting_date - elif reconciliation_takes_effect_on == "Reconciliation Date": - reconcile_on = nowdate() - + reconcile_on = get_reconciliation_effect_date(d, payment_entry.company, payment_entry.posting_date) reference_details.update({"reconcile_effect_on": reconcile_on}) if d.voucher_detail_no: @@ -783,6 +768,28 @@ def update_reference_in_payment_entry( return row, update_advance_paid +def get_reconciliation_effect_date(reference, company, posting_date): + reconciliation_takes_effect_on = frappe.get_cached_value( + "Company", company, "reconciliation_takes_effect_on" + ) + + if reconciliation_takes_effect_on == "Advance Payment Date": + reconcile_on = posting_date + elif reconciliation_takes_effect_on == "Oldest Of Invoice Or Advance": + date_field = "posting_date" + if reference.against_voucher_type in ["Sales Order", "Purchase Order"]: + date_field = "transaction_date" + reconcile_on = frappe.db.get_value( + reference.against_voucher_type, reference.against_voucher, date_field + ) + if getdate(reconcile_on) < getdate(posting_date): + reconcile_on = posting_date + elif reconciliation_takes_effect_on == "Reconciliation Date": + reconcile_on = nowdate() + + return reconcile_on + + def cancel_exchange_gain_loss_journal( parent_doc: dict | object, referenced_dt: str | None = None, referenced_dn: str | None = None ) -> None: