From e5a2b15fba27bb35496bdf6cc3eb4d67b7bcf369 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 13 Feb 2023 12:08:42 +0530 Subject: [PATCH] fix: unwanted difference amount calculation on cr note and invoice with same currency (#34020) * fix: incorrect difference amount while reconiling cr/dr notes * fix(test): catch incorrect difference amount calculation Fixed issues where difference amount was calculated for Cr Notes and Invoices of the same currency. --------- Co-authored-by: Deepesh Garg --- .../payment_reconciliation.py | 15 +++++++++++++-- .../test_payment_reconciliation.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py index 154fdc039d..675a3287fa 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py @@ -234,7 +234,7 @@ class PaymentReconciliation(Document): def allocate_entries(self, args): self.validate_entries() - invoice_exchange_map = self.get_invoice_exchange_map(args.get("invoices")) + invoice_exchange_map = self.get_invoice_exchange_map(args.get("invoices"), args.get("payments")) default_exchange_gain_loss_account = frappe.get_cached_value( "Company", self.company, "exchange_gain_loss_account" ) @@ -253,6 +253,9 @@ class PaymentReconciliation(Document): pay["amount"] = 0 inv["exchange_rate"] = invoice_exchange_map.get(inv.get("invoice_number")) + if pay.get("reference_type") in ["Sales Invoice", "Purchase Invoice"]: + pay["exchange_rate"] = invoice_exchange_map.get(pay.get("reference_name")) + res.difference_amount = self.get_difference_amount(pay, inv, res["allocated_amount"]) res.difference_account = default_exchange_gain_loss_account res.exchange_rate = inv.get("exchange_rate") @@ -407,13 +410,21 @@ class PaymentReconciliation(Document): if not self.get("payments"): frappe.throw(_("No records found in the Payments table")) - def get_invoice_exchange_map(self, invoices): + def get_invoice_exchange_map(self, invoices, payments): sales_invoices = [ d.get("invoice_number") for d in invoices if d.get("invoice_type") == "Sales Invoice" ] + + sales_invoices.extend( + [d.get("reference_name") for d in payments if d.get("reference_type") == "Sales Invoice"] + ) purchase_invoices = [ d.get("invoice_number") for d in invoices if d.get("invoice_type") == "Purchase Invoice" ] + purchase_invoices.extend( + [d.get("reference_name") for d in payments if d.get("reference_type") == "Purchase Invoice"] + ) + invoice_exchange_map = frappe._dict() if sales_invoices: diff --git a/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py index 00e3934f10..f9dda0593b 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py @@ -473,6 +473,11 @@ class TestPaymentReconciliation(FrappeTestCase): invoices = [x.as_dict() for x in pr.get("invoices")] payments = [x.as_dict() for x in pr.get("payments")] pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments})) + + # Cr Note and Invoice are of the same currency. There shouldn't any difference amount. + for row in pr.allocation: + self.assertEqual(flt(row.get("difference_amount")), 0.0) + pr.reconcile() pr.get_unreconciled_entries() @@ -506,6 +511,11 @@ class TestPaymentReconciliation(FrappeTestCase): payments = [x.as_dict() for x in pr.get("payments")] pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments})) pr.allocation[0].allocated_amount = allocated_amount + + # Cr Note and Invoice are of the same currency. There shouldn't any difference amount. + for row in pr.allocation: + self.assertEqual(flt(row.get("difference_amount")), 0.0) + pr.reconcile() # assert outstanding