diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 58c0f23e05e..6c7fb5f5744 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -24,6 +24,7 @@ from erpnext.accounts.party import get_party_account from erpnext.accounts.utils import ( cancel_exchange_gain_loss_journal, get_account_currency, + get_advance_payment_doctypes, get_balance_on, get_stock_accounts, get_stock_and_account_balance, @@ -238,9 +239,10 @@ class JournalEntry(AccountsController): def update_advance_paid(self): advance_paid = frappe._dict() + advance_payment_doctypes = get_advance_payment_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 d4e6199ab4d..de4305f86e0 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -46,6 +46,7 @@ from erpnext.accounts.party import ( from erpnext.accounts.utils import ( cancel_exchange_gain_loss_journal, get_account_currency, + get_advance_payment_doctypes, get_balance_on, get_outstanding_invoices, get_reconciliation_effect_date, @@ -1029,7 +1030,7 @@ 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"): + if d.reference_doctype in get_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 @@ -1309,8 +1310,7 @@ class PaymentEntry(AccountsController): if not self.party_account: return - advance_payment_doctypes = frappe.get_hooks("advance_payment_doctypes") - + advance_payment_doctypes = get_advance_payment_doctypes() if self.payment_type == "Receive": against_account = self.paid_to else: @@ -1684,12 +1684,15 @@ class PaymentEntry(AccountsController): return flt(gl_dict.get(field, 0) / (conversion_rate or 1)) def update_advance_paid(self): - if self.payment_type in ("Receive", "Pay") and self.party: - for d in self.get("references"): - if d.allocated_amount and d.reference_doctype in frappe.get_hooks("advance_payment_doctypes"): - frappe.get_doc( - d.reference_doctype, d.reference_name, for_update=True - ).set_total_advance_paid() + if self.payment_type not in ("Receive", "Pay") or not self.party: + return + + advance_payment_doctypes = get_advance_payment_doctypes() + for d in self.get("references"): + 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() def on_recurring(self, reference_doc, auto_repeat_doc): self.reference_no = reference_doc.name diff --git a/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py b/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py index 7612294a85c..e57b90f11f7 100644 --- a/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py +++ b/erpnext/accounts/doctype/unreconcile_payment/unreconcile_payment.py @@ -12,6 +12,7 @@ from frappe.utils.data import comma_and from erpnext.accounts.utils import ( cancel_exchange_gain_loss_journal, + get_advance_payment_doctypes, unlink_ref_doc_from_payment_entries, update_voucher_outstanding, ) @@ -84,7 +85,7 @@ class UnreconcilePayment(Document): update_voucher_outstanding( alloc.reference_doctype, alloc.reference_name, alloc.account, alloc.party_type, alloc.party ) - if doc.doctype in frappe.get_hooks("advance_payment_doctypes"): + if doc.doctype in get_advance_payment_doctypes(): doc.set_total_advance_paid() frappe.db.set_value("Unreconcile Payment Entries", alloc.name, "unlinked", True) diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py index e87fa699908..a2237ea9ac2 100644 --- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py +++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py @@ -15,7 +15,11 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_accounting_dimensions, get_dimension_with_children, ) -from erpnext.accounts.utils import get_currency_precision, get_party_types_from_account_type +from erpnext.accounts.utils import ( + get_advance_payment_doctypes, + get_currency_precision, + get_party_types_from_account_type, +) # This report gives a summary of all Outstanding Invoices considering the following @@ -62,6 +66,7 @@ class ReceivablePayableReport: frappe.db.get_single_value("Accounts Settings", "receivable_payable_fetch_method") or "Buffered Cursor" ) # Fail Safe + self.advance_payment_doctypes = get_advance_payment_doctypes() def run(self, args): self.filters.update(args) @@ -85,6 +90,7 @@ class ReceivablePayableReport: self.party_details = {} self.invoices = set() self.skip_total_row = 0 + self.advance_payment_doctypes = get_advance_payment_doctypes() if self.filters.get("group_by_party"): self.previous_party = "" @@ -181,7 +187,10 @@ class ReceivablePayableReport: if key not in self.voucher_balance: self.voucher_balance[key] = self.build_voucher_dict(ple) - if ple.voucher_type == ple.against_voucher_type and ple.voucher_no == ple.against_voucher_no: + if (ple.voucher_type == ple.against_voucher_type and ple.voucher_no == ple.against_voucher_no) or ( + ple.voucher_type in ("Payment Entry", "Journal Entry") + and ple.against_voucher_type in self.advance_payment_doctypes + ): self.voucher_balance[key].cost_center = ple.cost_center self.get_invoices(ple) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 47ade441e46..c775773cd71 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -629,6 +629,7 @@ def update_reference_in_journal_entry(d, journal_entry, do_not_save=False): # Update Advance Paid in SO/PO since they might be getting unlinked update_advance_paid = [] + if jv_detail.get("reference_type") in ["Sales Order", "Purchase Order"]: update_advance_paid.append((jv_detail.reference_type, jv_detail.reference_name)) @@ -2263,6 +2264,15 @@ def get_party_types_from_account_type(account_type): return frappe.db.get_all("Party Type", {"account_type": account_type}, pluck="name") +def get_advance_payment_doctypes(): + """ + Get list of advance payment doctypes based on type. + :param type: Optional, can be "receivable" or "payable". If not provided, returns both. + """ + + return frappe.get_hooks("advance_payment_doctypes") + + def run_ledger_health_checks(): health_monitor_settings = frappe.get_doc("Ledger Health Monitor") if health_monitor_settings.enable_health_monitor: diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 5466c9e0e72..33c5783861d 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -51,6 +51,9 @@ from erpnext.accounts.utils import ( get_fiscal_years, validate_fiscal_year, ) +from erpnext.accounts.utils import ( + get_advance_payment_doctypes as _get_advance_payment_doctypes, +) from erpnext.buying.utils import update_last_purchase_rate from erpnext.controllers.print_settings import ( set_print_templates_for_item_table, @@ -386,9 +389,7 @@ class AccountsController(TransactionBase): adv = qb.DocType("Advance Payment Ledger Entry") qb.from_(adv).delete().where(adv.voucher_type.eq(self.doctype) & adv.voucher_no.eq(self.name)).run() - advance_payment_doctypes = frappe.get_hooks("advance_payment_doctypes") - - if self.doctype in advance_payment_doctypes: + if self.doctype in self.get_advance_payment_doctypes(): qb.from_(adv).delete().where( adv.against_voucher_type.eq(self.doctype) & adv.against_voucher_no.eq(self.name) ).run() @@ -2912,7 +2913,7 @@ class AccountsController(TransactionBase): repost_ledger.submit() def get_advance_payment_doctypes(self) -> list: - return frappe.get_hooks("advance_payment_doctypes") + return _get_advance_payment_doctypes() def make_advance_payment_ledger_for_journal(self): advance_payment_doctypes = self.get_advance_payment_doctypes()