From 95718acc9a9a24ee5982313bc3f9481277a0f93d Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Tue, 18 Mar 2025 18:41:57 +0530 Subject: [PATCH] test: test case for FIFO batch valuation (cherry picked from commit ad9ac1f058703b6e7b9b2930a355787bed149448) --- erpnext/stock/doctype/batch/batch.py | 2 +- .../test_stock_ledger_entry.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 4aeb165c7b5..47acebb6634 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -160,7 +160,7 @@ class Batch(Document): from erpnext.stock.utils import get_valuation_method if self.is_new(): - if get_valuation_method(self.item) != "FIFO": + if get_valuation_method(self.item) == "Moving Average": self.use_batchwise_valuation = 0 return diff --git a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py index c0171b7a710..8658316ad90 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py @@ -456,6 +456,45 @@ class TestStockLedgerEntry(FrappeTestCase, StockTestMixin): frappe.set_user("Administrator") user.remove_roles("Stock Manager") + def test_batchwise_item_valuation_fifo(self): + item, warehouses, batches = setup_item_valuation_test(valuation_method="FIFO") + + # Incoming Entries for Stock Value check + pr_entry_list = [ + (item, warehouses[0], batches[0], 1, 100), + (item, warehouses[0], batches[1], 1, 50), + (item, warehouses[0], batches[0], 1, 150), + (item, warehouses[0], batches[1], 1, 100), + ] + prs = create_purchase_receipt_entries_for_batchwise_item_valuation_test(pr_entry_list) + sle_details = fetch_sle_details_for_doc_list(prs, ["stock_value"]) + sv_list = [d["stock_value"] for d in sle_details] + expected_sv = [100, 150, 300, 400] + self.assertEqual(expected_sv, sv_list, "Incorrect 'Stock Value' values") + + # Outgoing Entries for Stock Value Difference check + dn_entry_list = [ + (item, warehouses[0], batches[1], 1, 200), + (item, warehouses[0], batches[0], 1, 200), + (item, warehouses[0], batches[1], 1, 200), + (item, warehouses[0], batches[0], 1, 200), + ] + + frappe.flags.use_serial_and_batch_fields = True + dns = create_delivery_note_entries_for_batchwise_item_valuation_test(dn_entry_list) + sle_details = fetch_sle_details_for_doc_list(dns, ["stock_value_difference"]) + svd_list = [-1 * d["stock_value_difference"] for d in sle_details] + expected_incoming_rates = expected_abs_svd = [75.0, 125.0, 75.0, 125.0] + + self.assertEqual(expected_abs_svd, svd_list, "Incorrect 'Stock Value Difference' values") + for dn, _incoming_rate in zip(dns, expected_incoming_rates, strict=False): + self.assertTrue( + dn.items[0].incoming_rate in expected_abs_svd, + "Incorrect 'Incoming Rate' values fetched for DN items", + ) + + frappe.flags.use_serial_and_batch_fields = False + def test_batchwise_item_valuation_moving_average(self): item, warehouses, batches = setup_item_valuation_test(valuation_method="Moving Average")