From 4e367dedec0babd38072338d084beb0d14906976 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 23 Jan 2025 11:15:51 +0530 Subject: [PATCH] fix: validate non-stock item for exchange loss/gain (backport #45306) (#45380) fix: validate non-stock item for exchange loss/gain (#45306) * fix: validate non-stock item * test: add unit test to validate non-stock item exchange difference * fix: use usd supplier (cherry picked from commit 05579959f26027daf246341540ef759c2cf16855) Co-authored-by: Rethik M <85231069+rs-rethik@users.noreply.github.com> --- .../purchase_invoice/purchase_invoice.py | 1 + .../purchase_invoice/test_purchase_invoice.py | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index dc6ee6c1469..2b4242aa233 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1126,6 +1126,7 @@ class PurchaseInvoice(BuyingController): exchange_rate_map[item.purchase_receipt] and self.conversion_rate != exchange_rate_map[item.purchase_receipt] and item.net_rate == net_rate_map[item.pr_detail] + and item.item_code in stock_items ): discrepancy_caused_by_exchange_rate_difference = ( item.qty * item.net_rate diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 4d62c0d354d..bc28edbf396 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -372,6 +372,53 @@ class TestPurchaseInvoice(FrappeTestCase, StockTestMixin): self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) + def test_purchase_invoice_with_exchange_rate_difference_for_non_stock_item(self): + from erpnext.stock.doctype.purchase_receipt.purchase_receipt import ( + make_purchase_invoice as create_purchase_invoice, + ) + + # Creating Purchase Invoice with USD currency + pr = frappe.new_doc("Purchase Receipt") + pr.currency = "USD" + pr.company = "_Test Company with perpetual inventory" + pr.conversion_rate = (70,) + pr.supplier = "_Test Supplier USD" + pr.append( + "items", + { + "item_code": "_Test Non Stock Item", + "qty": 1, + "rate": 100, + }, + ) + pr.append( + "items", + {"item_code": "_Test Item", "qty": 1, "rate": 5, "warehouse": "Stores - TCP1"}, + ) + pr.insert() + pr.submit() + + # Createing purchase invoice against Purchase Receipt + pi = create_purchase_invoice(pr.name) + pi.conversion_rate = 80 + pi.credit_to = "_Test Payable USD - TCP1" + pi.insert() + pi.submit() + + # Get exchnage gain and loss account + exchange_gain_loss_account = frappe.db.get_value("Company", pi.company, "exchange_gain_loss_account") + + # fetching the latest GL Entry with exchange gain and loss account account + amount = frappe.db.get_value( + "GL Entry", {"account": exchange_gain_loss_account, "voucher_no": pi.name}, "debit" + ) + + discrepancy_caused_by_exchange_rate_diff = abs( + pi.items[1].base_net_amount - pr.items[1].base_net_amount + ) + + self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount) + def test_purchase_invoice_change_naming_series(self): pi = frappe.copy_doc(test_records[1]) pi.insert()