From c88ce552425f077f59e98458799819ff3dd72742 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Mon, 22 Jan 2024 22:04:30 +0100 Subject: [PATCH] fix: advance payment doctypes to keep input/output distinction --- .../accounts/doctype/journal_entry/journal_entry.py | 5 ++++- .../accounts/doctype/payment_entry/payment_entry.py | 10 ++++++++-- erpnext/accounts/utils.py | 10 ++++++++-- erpnext/controllers/accounts_controller.py | 5 ++++- erpnext/hooks.py | 3 ++- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 40d552bc88..7579da86cd 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -186,9 +186,12 @@ class JournalEntry(AccountsController): def update_advance_paid(self): advance_paid = frappe._dict() + advance_payment_doctypes = frappe.get_hooks( + "advance_payment_customer_doctypes" + ) + frappe.get_hooks("advance_payment_supplier_doctypes") for d in self.get("accounts"): if d.is_advance: - if d.reference_type in frappe.get_hooks("advance_payment_doctypes"): + if d.reference_type in advance_payment_doctypes: advance_paid.setdefault(d.reference_type, []).append(d.reference_name) for voucher_type, order_list in advance_paid.items(): diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index dbebbb00fa..b8781ef120 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -925,7 +925,10 @@ class PaymentEntry(AccountsController): def calculate_base_allocated_amount_for_reference(self, d) -> float: base_allocated_amount = 0 - if d.reference_doctype in frappe.get_hooks("advance_payment_doctypes"): + advance_payment_doctypes = frappe.get_hooks( + "advance_payment_customer_doctypes" + ) + frappe.get_hooks("advance_payment_supplier_doctypes") + if d.reference_doctype in advance_payment_doctypes: # When referencing Sales/Purchase Order, use the source/target exchange rate depending on payment type. # This is so there are no Exchange Gain/Loss generated for such doctypes @@ -1423,8 +1426,11 @@ class PaymentEntry(AccountsController): def update_advance_paid(self): if self.payment_type in ("Receive", "Pay") and self.party: + advance_payment_doctypes = frappe.get_hooks( + "advance_payment_customer_doctypes" + ) + frappe.get_hooks("advance_payment_supplier_doctypes") for d in self.get("references"): - if d.allocated_amount and d.reference_doctype in frappe.get_hooks("advance_payment_doctypes"): + if d.allocated_amount and d.reference_doctype in advance_payment_doctypes: frappe.get_doc( d.reference_doctype, d.reference_name, for_update=True ).set_total_advance_paid() diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 19095bc46e..9b70629994 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -600,7 +600,10 @@ def update_reference_in_journal_entry(d, journal_entry, do_not_save=False): jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0] # Update Advance Paid in SO/PO since they might be getting unlinked - if jv_detail.get("reference_type") in ("Sales Order", "Purchase Order"): + advance_payment_doctypes = frappe.get_hooks( + "advance_payment_customer_doctypes" + ) + frappe.get_hooks("advance_payment_supplier_doctypes") + if jv_detail.get("reference_type") in advance_payment_doctypes: frappe.get_doc(jv_detail.reference_type, jv_detail.reference_name).set_total_advance_paid() if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0: @@ -673,7 +676,10 @@ def update_reference_in_payment_entry( existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0] # Update Advance Paid in SO/PO since they are getting unlinked - if existing_row.get("reference_doctype") in ("Sales Order", "Purchase Order"): + advance_payment_doctypes = frappe.get_hooks( + "advance_payment_customer_doctypes" + ) + frappe.get_hooks("advance_payment_supplier_doctypes") + if existing_row.get("reference_doctype") in advance_payment_doctypes: frappe.get_doc( existing_row.reference_doctype, existing_row.reference_name ).set_total_advance_paid() diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 8848a3c385..ed0c1d7383 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1749,7 +1749,10 @@ class AccountsController(TransactionBase): def set_total_advance_paid(self): ple = frappe.qb.DocType("Payment Ledger Entry") - party = self.customer if self.doctype == "Sales Order" else self.supplier + if self.doctype in frappe.get_hooks("advance_payment_customer_doctypes"): + party = self.customer + if self.doctype in frappe.get_hooks("advance_payment_supplier_doctypes"): + party = self.supplier advance = ( frappe.qb.from_(ple) .select(ple.account_currency, Abs(Sum(ple.amount_in_account_currency)).as_("amount")) diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 6efb893e63..e21d7bd79b 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -481,7 +481,8 @@ payment_gateway_enabled = "erpnext.accounts.utils.create_payment_gateway_account communication_doctypes = ["Customer", "Supplier"] -advance_payment_doctypes = ["Sales Order", "Purchase Order"] +advance_payment_customer_doctypes = ["Sales Order"] +advance_payment_supplier_doctypes = ["Purchase Order"] invoice_doctypes = ["Sales Invoice", "Purchase Invoice"]