From 5af8378471009713e4b4adf112f0343092f977e2 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 27 Mar 2025 17:33:43 +0530 Subject: [PATCH] fix: valuation rate not updating for raw materials --- .../doctype/work_order/test_work_order.py | 103 ++++++++++++++++++ erpnext/stock/stock_ledger.py | 14 ++- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py index e66a9ebfaac..da22197566a 100644 --- a/erpnext/manufacturing/doctype/work_order/test_work_order.py +++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py @@ -2788,6 +2788,109 @@ class TestWorkOrder(IntegrationTestCase): batch_no = get_batch_from_bundle(row.serial_and_batch_bundle) self.assertEqual(batch_no, itemwise_batches[row.item_code]) + def test_work_order_valuation_auto_pick(self): + fg_item = "Test FG Item For Non Transfer Item Batch" + rm_item = "Test RM Item For Non Transfer Item Batch" + + make_item(fg_item, {"is_stock_item": 1}) + make_item( + rm_item, + { + "is_stock_item": 1, + "has_batch_no": 1, + "create_new_batch": 1, + "batch_number_series": "TST-BATCH-NTI-.###", + }, + ) + + source_warehouse = "_Test Warehouse - _TC" + wip_warehouse = "Stores - _TC" + finished_goods_warehouse = create_warehouse("_Test Finished Goods Warehouse", company="_Test Company") + + batches = make_stock_in_entries_and_get_batches(rm_item, source_warehouse, wip_warehouse) + + if not frappe.db.get_value("BOM", {"item": fg_item}): + make_bom(item=fg_item, raw_materials=[rm_item]) + + wo = make_wo_order_test_record( + item=fg_item, + qty=5, + source_warehouse=source_warehouse, + wip_warehouse=wip_warehouse, + fg_warehouse=finished_goods_warehouse, + ) + + stock_entry = frappe.get_doc(make_stock_entry(wo.name, "Material Transfer for Manufacture", 5)) + stock_entry.items[0].batch_no = batches[1] + stock_entry.items[0].use_serial_batch_fields = 1 + stock_entry.submit() + stock_entry.reload() + + self.assertEqual(stock_entry.items[0].valuation_rate, 200) + + original_value = frappe.db.get_single_value( + "Stock Settings", "auto_create_serial_and_batch_bundle_for_outward" + ) + original_based_on = frappe.db.get_single_value("Stock Settings", "pick_serial_and_batch_based_on") + + frappe.db.set_single_value("Stock Settings", "auto_create_serial_and_batch_bundle_for_outward", 1) + frappe.db.set_single_value("Stock Settings", "pick_serial_and_batch_based_on", "Expiry") + + stock_entry = frappe.get_doc(make_stock_entry(wo.name, "Manufacture", 5)) + stock_entry.items[0].use_serial_batch_fields = 1 + stock_entry.submit() + stock_entry.reload() + + batch_no = get_batch_from_bundle(stock_entry.items[0].serial_and_batch_bundle) + self.assertEqual(batch_no, batches[1]) + self.assertEqual(stock_entry.items[0].valuation_rate, 200) + self.assertEqual(stock_entry.items[1].valuation_rate, 200) + + frappe.db.set_single_value( + "Stock Settings", "auto_create_serial_and_batch_bundle_for_outward", original_value + ) + frappe.db.set_single_value("Stock Settings", "pick_serial_and_batch_based_on", original_based_on) + + +def make_stock_in_entries_and_get_batches(rm_item, source_warehouse, wip_warehouse): + from erpnext.stock.doctype.stock_entry.test_stock_entry import ( + make_stock_entry as make_stock_entry_test_record, + ) + + batches = [] + for qty, rate in ((5, 100), (5, 200)): + stock_entry = make_stock_entry_test_record( + item_code=rm_item, + target=source_warehouse, + qty=qty, + basic_rate=rate, + ) + stock_entry.submit() + stock_entry.reload() + + batch_no = get_batch_from_bundle(stock_entry.items[0].serial_and_batch_bundle) + batch_doc = frappe.get_doc("Batch", batch_no) + + # keep early expiry date for the batch having rate 200 + days = 10 if rate == 100 else 1 + batch_doc.db_set("expiry_date", add_to_date(now(), days=days)) + + batches.append(batch_no) + + stock_entry = make_stock_entry_test_record( + item_code=rm_item, + target=wip_warehouse, + qty=qty, + basic_rate=rate, + ) + stock_entry.submit() + stock_entry.reload() + batch_no = get_batch_from_bundle(stock_entry.items[0].serial_and_batch_bundle) + batch_doc = frappe.get_doc("Batch", batch_no) + batch_doc.db_set("expiry_date", add_to_date(now(), days=10)) + + return batches + def make_operation(**kwargs): kwargs = frappe._dict(kwargs) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index fcda522cbc8..dace9b67550 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -1213,9 +1213,21 @@ class update_entries_after: frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount - if not sle.dependant_sle_voucher_detail_no: + if not sle.dependant_sle_voucher_detail_no or self.is_manufacture_entry_with_sabb(sle): self.recalculate_amounts_in_stock_entry(sle.voucher_no, sle.voucher_detail_no) + def is_manufacture_entry_with_sabb(self, sle): + if ( + self.args.get("sle_id") + and sle.serial_and_batch_bundle + and sle.auto_created_serial_and_batch_bundle + ): + purpose = frappe.get_cached_value("Stock Entry", sle.voucher_no, "purpose") + if purpose in ["Manufacture", "Repack"]: + return True + + return False + def recalculate_amounts_in_stock_entry(self, voucher_no, voucher_detail_no): stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True) stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False)