From 58114e7b24788f0244b89401235a6a20721b9603 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 28 Nov 2023 16:50:22 +0530 Subject: [PATCH] refactor: use different GL build logic for advance as liability --- .../doctype/payment_entry/payment_entry.py | 112 ++++++++++-------- erpnext/accounts/utils.py | 2 +- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 0344e3de9f..63c5fa92d3 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1055,64 +1055,76 @@ class PaymentEntry(AccountsController): item=self, ) - for d in self.get("references"): - # re-defining dr_or_cr for every reference in order to avoid the last value affecting calculation of reverse - dr_or_cr = "credit" if self.payment_type == "Receive" else "debit" - cost_center = self.cost_center - if d.reference_doctype == "Sales Invoice" and not cost_center: - cost_center = frappe.db.get_value(d.reference_doctype, d.reference_name, "cost_center") - - gle = party_gl_dict.copy() - - allocated_amount_in_company_currency = self.calculate_base_allocated_amount_for_reference(d) - - if self.book_advance_payments_in_separate_party_account: - against_voucher_type = "Payment Entry" - against_voucher = self.name - else: - against_voucher_type = d.reference_doctype - against_voucher = d.reference_name - - reverse_dr_or_cr = 0 - if d.reference_doctype in ["Sales Invoice", "Purchase Invoice"]: - is_return = frappe.db.get_value(d.reference_doctype, d.reference_name, "is_return") - payable_party_types = get_party_types_from_account_type("Payable") - receivable_party_types = get_party_types_from_account_type("Receivable") - if is_return and self.party_type in receivable_party_types and (self.payment_type == "Pay"): - reverse_dr_or_cr = 1 - elif ( - is_return and self.party_type in payable_party_types and (self.payment_type == "Receive") - ): - reverse_dr_or_cr = 1 - - if is_return and not reverse_dr_or_cr: - dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" - - gle.update( - { - dr_or_cr: abs(allocated_amount_in_company_currency), - dr_or_cr + "_in_account_currency": abs(d.allocated_amount), - "against_voucher_type": against_voucher_type, - "against_voucher": against_voucher, - "cost_center": cost_center, - } - ) - gl_entries.append(gle) - dr_or_cr = "credit" if self.payment_type == "Receive" else "debit" - if self.unallocated_amount: - exchange_rate = self.get_exchange_rate() - base_unallocated_amount = self.unallocated_amount * exchange_rate + if self.book_advance_payments_in_separate_party_account: + if self.payment_type == "Receive": + amount = self.base_paid_amount + else: + amount = self.base_received_amount gle = party_gl_dict.copy() gle.update( { - dr_or_cr + "_in_account_currency": self.unallocated_amount, - dr_or_cr: base_unallocated_amount, + dr_or_cr: amount, + # TODO: handle multi currency payments + dr_or_cr + "_in_account_currency": amount, + "against_voucher_type": "Payment Entry", + "against_voucher": self.name, + "cost_center": self.cost_center, } ) - gl_entries.append(gle) + else: + for d in self.get("references"): + # re-defining dr_or_cr for every reference in order to avoid the last value affecting calculation of reverse + dr_or_cr = "credit" if self.payment_type == "Receive" else "debit" + cost_center = self.cost_center + if d.reference_doctype == "Sales Invoice" and not cost_center: + cost_center = frappe.db.get_value(d.reference_doctype, d.reference_name, "cost_center") + + gle = party_gl_dict.copy() + + allocated_amount_in_company_currency = self.calculate_base_allocated_amount_for_reference(d) + reverse_dr_or_cr = 0 + + if d.reference_doctype in ["Sales Invoice", "Purchase Invoice"]: + is_return = frappe.db.get_value(d.reference_doctype, d.reference_name, "is_return") + payable_party_types = get_party_types_from_account_type("Payable") + receivable_party_types = get_party_types_from_account_type("Receivable") + if is_return and self.party_type in receivable_party_types and (self.payment_type == "Pay"): + reverse_dr_or_cr = 1 + elif ( + is_return and self.party_type in payable_party_types and (self.payment_type == "Receive") + ): + reverse_dr_or_cr = 1 + + if is_return and not reverse_dr_or_cr: + dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" + + gle.update( + { + dr_or_cr: abs(allocated_amount_in_company_currency), + dr_or_cr + "_in_account_currency": abs(d.allocated_amount), + "against_voucher_type": d.reference_doctype, + "against_voucher": d.reference_name, + "cost_center": cost_center, + } + ) + gl_entries.append(gle) + + if self.unallocated_amount: + exchange_rate = self.get_exchange_rate() + base_unallocated_amount = self.unallocated_amount * exchange_rate + + gle = party_gl_dict.copy() + gle.update( + { + dr_or_cr + "_in_account_currency": self.unallocated_amount, + dr_or_cr: base_unallocated_amount, + } + ) + + gl_entries.append(gle) def make_advance_gl_entries(self, against_voucher_type=None, against_voucher=None, cancel=0): if self.book_advance_payments_in_separate_party_account: diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index d133307d39..e2632d3de4 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -499,7 +499,7 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n if voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account: # both ledgers must be posted to for `Advance as Liability` - doc.make_gl_entries() + doc.make_advance_gl_entries() else: gl_map = doc.build_gl_map() create_payment_ledger_entry(gl_map, update_outstanding="No", cancel=0, adv_adj=1)