From 1f53f2ce04e902295d941853558c05449cc860b0 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 24 Sep 2023 10:55:04 +0530 Subject: [PATCH] refactor: don't split based on reference while reconciling advance --- .../doctype/payment_entry/payment_entry.py | 69 +++++++++++-------- erpnext/accounts/utils.py | 8 +-- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 38a520996c9e..8c119c983808 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1085,46 +1085,57 @@ def add_party_gl_entries(self, gl_entries): "credit" if erpnext.get_party_account_type(self.party_type) == "Receivable" else "debit" ) - for d in self.get("references"): - 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") - + if self.book_advance_payments_in_separate_party_account: 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 + if self.payment_type == "Receive": + amount = self.base_paid_amount else: - against_voucher_type = d.reference_doctype - against_voucher = d.reference_name + amount = self.base_received_amount gle.update( { - dr_or_cr: allocated_amount_in_company_currency, - dr_or_cr + "_in_account_currency": d.allocated_amount, - "against_voucher_type": against_voucher_type, - "against_voucher": against_voucher, - "cost_center": cost_center, + dr_or_cr: amount, + 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"): + 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") - if self.unallocated_amount: - exchange_rate = self.get_exchange_rate() - base_unallocated_amount = self.unallocated_amount * exchange_rate + gle = party_gl_dict.copy() - gle = party_gl_dict.copy() - gle.update( - { - dr_or_cr + "_in_account_currency": self.unallocated_amount, - dr_or_cr: base_unallocated_amount, - } - ) + allocated_amount_in_company_currency = self.calculate_base_allocated_amount_for_reference(d) - gl_entries.append(gle) + gle.update( + { + dr_or_cr: allocated_amount_in_company_currency, + dr_or_cr + "_in_account_currency": 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 dd1419868433..c77ed4bbbfcc 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -466,10 +466,8 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n # cancel advance entry doc = frappe.get_doc(voucher_type, voucher_no) frappe.flags.ignore_party_validation = True - _delete_pl_entries(voucher_type, voucher_no) - - if voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account: - _delete_gl_entries(voucher_type, voucher_no) + if not (voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account): + _delete_pl_entries(voucher_type, voucher_no) for entry in entries: check_if_advance_entry_modified(entry) @@ -494,7 +492,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)