From 5fc19dab54a2672ec131dbf97974b2bde277aa76 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 28 Nov 2023 17:13:43 +0530 Subject: [PATCH] refactor: 'make_advance_gl_entries' method make_advance_gl_entries -> add_advance_gl_entries -> add_advance_gl_for_reference 'make_advance_gl_entries' - main method thats builds and post GL entries for all or one specific reference based on parameters 'add_advance_gl_entries' - build GL map for all or one specific reference. Return an array of dict. 'add_advance_gl_for_reference' - utility function to build gl entries. returns dict. --- .../doctype/payment_entry/payment_entry.py | 66 +++++++------------ 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 63c5fa92d3..c732c0a1c5 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1126,53 +1126,31 @@ class PaymentEntry(AccountsController): gl_entries.append(gle) - def make_advance_gl_entries(self, against_voucher_type=None, against_voucher=None, cancel=0): + def make_advance_gl_entries( + self, entry: object | dict = None, cancel: bool = 0, update_outstanding: str = "Yes" + ): + gl_entries = [] + self.add_advance_gl_entries(gl_entries, entry) + + if cancel: + make_reverse_gl_entries(gl_entries, partial_cancel=True) + else: + make_gl_entries(gl_entries, update_outstanding=update_outstanding) + + def add_advance_gl_entries(self, gl_entries: list, entry: object | dict | None): + """ + If 'entry' is passed, GL enties only for that reference is added. + """ if self.book_advance_payments_in_separate_party_account: - gl_entries = [] - for d in self.get("references"): - if d.reference_doctype in ("Sales Invoice", "Purchase Invoice", "Journal Entry"): - if not (against_voucher_type and against_voucher) or ( - d.reference_doctype == against_voucher_type and d.reference_name == against_voucher - ): - self.make_invoice_liability_entry(gl_entries, d) + references = [x for x in self.get("references")] + if entry: + references = [x for x in self.get("references") if x.name == entry.name] - if cancel: - for entry in gl_entries: - frappe.db.set_value( - "GL Entry", - { - "voucher_no": self.name, - "voucher_type": self.doctype, - "voucher_detail_no": entry.voucher_detail_no, - "against_voucher_type": entry.against_voucher_type, - "against_voucher": entry.against_voucher, - }, - "is_cancelled", - 1, - ) + for ref in references: + if ref.reference_doctype in ("Sales Invoice", "Purchase Invoice", "Journal Entry"): + self.add_advance_gl_for_reference(gl_entries, ref) - make_reverse_gl_entries(gl_entries=gl_entries, partial_cancel=True) - return - - # same reference added to payment entry - for gl_entry in gl_entries.copy(): - if frappe.db.exists( - "GL Entry", - { - "account": gl_entry.account, - "voucher_type": gl_entry.voucher_type, - "voucher_no": gl_entry.voucher_no, - "voucher_detail_no": gl_entry.voucher_detail_no, - "debit": gl_entry.debit, - "credit": gl_entry.credit, - "is_cancelled": 0, - }, - ): - gl_entries.remove(gl_entry) - - make_gl_entries(gl_entries) - - def make_invoice_liability_entry(self, gl_entries, invoice): + def add_advance_gl_for_reference(self, gl_entries, invoice): args_dict = { "party_type": self.party_type, "party": self.party,