From f03904cd066f56f23806b5ef4f9aa2616a745fc1 Mon Sep 17 00:00:00 2001 From: Lakshit Jain <108322669+ljain112@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:46:10 +0530 Subject: [PATCH 1/2] fix: reverse debit credit for party gl entry in payment entry based on negative amount (#42367) * fix: do not absolute the amount for party gl entries * fix: reverse debit credit for party gl entry based on negative amount * refactor: reduce nesting of if condition --------- --- .../doctype/payment_entry/payment_entry.py | 138 ++++++++---------- 1 file changed, 63 insertions(+), 75 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 1422912f39..74a4ed5940 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -37,7 +37,6 @@ from erpnext.accounts.utils import ( get_account_currency, get_balance_on, get_outstanding_invoices, - get_party_types_from_account_type, ) from erpnext.controllers.accounts_controller import ( AccountsController, @@ -1247,93 +1246,82 @@ class PaymentEntry(AccountsController): self.make_advance_gl_entries(cancel=cancel) def add_party_gl_entries(self, gl_entries): - if self.party_account: - if self.payment_type == "Receive": - against_account = self.paid_to - else: - against_account = self.paid_from + if not self.party_account: + return - party_gl_dict = self.get_gl_dict( - { - "account": self.party_account, - "party_type": self.party_type, - "party": self.party, - "against": against_account, - "account_currency": self.party_account_currency, - "cost_center": self.cost_center, - "accounting_journal": self.accounting_journal, - }, - item=self, - ) + if self.payment_type == "Receive": + against_account = self.paid_to + else: + against_account = self.paid_from + party_account_type = frappe.db.get_value("Party Type", self.party_type, "account_type") + + party_gl_dict = self.get_gl_dict( + { + "account": self.party_account, + "party_type": self.party_type, + "party": self.party, + "against": against_account, + "account_currency": self.party_account_currency, + "cost_center": self.cost_center, + }, + 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") - 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 + gle = party_gl_dict.copy() - if is_return and not reverse_dr_or_cr: - dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" + allocated_amount_in_company_currency = self.calculate_base_allocated_amount_for_reference(d) - 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, - "is_advance": "No", # @dokos - } + if ( + d.reference_doctype in ["Sales Invoice", "Purchase Invoice"] + and d.allocated_amount < 0 + and ( + (party_account_type == "Receivable" and self.payment_type == "Pay") + or (party_account_type == "Payable" and self.payment_type == "Receive") ) - gl_entries.append(gle) + ): + # reversing dr_cr because because it will get reversed in gl processing due to negative amount + dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" + + 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: - dr_or_cr = "credit" if self.payment_type == "Receive" else "debit" - exchange_rate = self.get_exchange_rate() - base_unallocated_amount = self.unallocated_amount * exchange_rate + if self.unallocated_amount: + dr_or_cr = "credit" if self.payment_type == "Receive" else "debit" + 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, + } + ) + + if self.book_advance_payments_in_separate_party_account: gle.update( { - dr_or_cr + "_in_account_currency": self.unallocated_amount, - dr_or_cr: base_unallocated_amount, - "is_advance": "Yes", # @dokos + "against_voucher_type": "Payment Entry", + "against_voucher": self.name, } ) - - if self.get("book_advance_payments_in_separate_party_account"): # @dokos - gle.update( - { - "against_voucher_type": "Payment Entry", - "against_voucher": self.name, - } - ) - gl_entries.append(gle) + gl_entries.append(gle) def make_advance_gl_entries( self, entry: object | dict = None, cancel: bool = 0, update_outstanding: str = "Yes" -- GitLab From 762374d39948363f6cbfdfb0f5af763048a7c61b Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Tue, 6 Aug 2024 07:55:41 +0000 Subject: [PATCH 2/2] fix: merge conflicts --- erpnext/accounts/doctype/payment_entry/payment_entry.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 74a4ed5940..31ab8c8894 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1264,6 +1264,7 @@ class PaymentEntry(AccountsController): "against": against_account, "account_currency": self.party_account_currency, "cost_center": self.cost_center, + "accounting_journal": self.accounting_journal, }, item=self, ) @@ -1297,6 +1298,7 @@ class PaymentEntry(AccountsController): "against_voucher_type": d.reference_doctype, "against_voucher": d.reference_name, "cost_center": cost_center, + "is_advance": "No", # @dokos } ) gl_entries.append(gle) @@ -1311,10 +1313,11 @@ class PaymentEntry(AccountsController): { dr_or_cr + "_in_account_currency": self.unallocated_amount, dr_or_cr: base_unallocated_amount, + "is_advance": "Yes", # @dokos } ) - if self.book_advance_payments_in_separate_party_account: + if self.get("book_advance_payments_in_separate_party_account"): # @dokos gle.update( { "against_voucher_type": "Payment Entry", -- GitLab