From 619646226f9acf718a8901f2f6fdd4be8af495db Mon Sep 17 00:00:00 2001 From: rethik Date: Fri, 17 Jan 2025 15:23:31 +0530 Subject: [PATCH 1/2] fix: validate non-stock item --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 37b39a79cf14..42e54e0ccf0a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1128,6 +1128,7 @@ def make_item_gl_entries(self, gl_entries): 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 From 30949a34c824f42f8af52a40cef1be0e9bf83fbc Mon Sep 17 00:00:00 2001 From: rethik Date: Fri, 17 Jan 2025 17:49:36 +0530 Subject: [PATCH 2/2] test: add unit test to validate non-stock item exchange difference --- .../purchase_invoice/test_purchase_invoice.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 64159d7b0ac2..6facc7d05bf2 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -383,6 +383,53 @@ def test_purchase_invoice_with_exchange_rate_difference(self): 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" + 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(self.globalTestRecords["Purchase Invoice"][1]) pi.insert()