fix: invalid ledger entries on payment against reverse payments
On Advance payments booked on separate party account, reconciliation
against a reverse payment posted invalid ledger entries.
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 1ff1cb7..b990260 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -1316,6 +1316,26 @@
):
self.add_advance_gl_for_reference(gl_entries, ref)
+ def get_dr_and_account_for_advances(self, reference):
+ if reference.reference_doctype == "Sales Invoice":
+ return "credit", reference.account
+
+ if reference.reference_doctype == "Payment Entry":
+ reverse_payment_details = frappe.db.get_all(
+ "Payment Entry",
+ filters={"name": reference.reference_name},
+ fields=["payment_type", "party_type"],
+ )[0]
+ if (
+ reverse_payment_details.payment_type == "Pay"
+ and reverse_payment_details.party_type == "Customer"
+ ):
+ return "credit", self.party_account
+ else:
+ return "debit", self.party_account
+
+ return "debit", reference.account
+
def add_advance_gl_for_reference(self, gl_entries, invoice):
args_dict = {
"party_type": self.party_type,
@@ -1335,8 +1355,8 @@
if getdate(posting_date) < getdate(self.posting_date):
posting_date = self.posting_date
- dr_or_cr = "credit" if invoice.reference_doctype in ["Sales Invoice", "Payment Entry"] else "debit"
- args_dict["account"] = invoice.account
+ dr_or_cr, account = self.get_dr_and_account_for_advances(invoice)
+ args_dict["account"] = account
args_dict[dr_or_cr] = invoice.allocated_amount
args_dict[dr_or_cr + "_in_account_currency"] = invoice.allocated_amount
args_dict.update(