From dd39da0b77f73dc9006c036b960b63404cc2c2f5 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 4 Dec 2023 10:08:37 +0530 Subject: [PATCH] fix: incorrectly treating normal payment as advance (#38437) fix: treating normal payment as advance --- .../doctype/payment_entry/payment_entry.py | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index dfb5caf2d352..1282ab60392e 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -106,9 +106,17 @@ def on_submit(self): self.set_status() def set_liability_account(self): - if not self.book_advance_payments_in_separate_party_account: + # Auto setting liability account should only be done during 'draft' status + if self.docstatus > 0: return + if not frappe.db.get_value( + "Company", self.company, "book_advance_payments_in_separate_party_account" + ): + return + + # Important to set this flag for the gl building logic to work properly + self.book_advance_payments_in_separate_party_account = True account_type = frappe.get_value( "Account", {"name": self.party_account, "company": self.company}, "account_type" ) @@ -118,11 +126,13 @@ def set_liability_account(self): ): return - if self.unallocated_amount == 0: - for d in self.references: - if d.reference_doctype in ["Sales Order", "Purchase Order"]: - break - else: + if self.references: + allowed_types = frozenset(["Sales Order", "Purchase Order"]) + reference_types = set([x.reference_doctype for x in self.references]) + + # If there are referencers other than `allowed_types`, treat this as a normal payment entry + if reference_types - allowed_types: + self.book_advance_payments_in_separate_party_account = False return liability_account = get_party_account(