fix: valuation for moving average with batches

(cherry picked from commit cdfbc73f4c)
This commit is contained in:
Rohit Waghchaure
2025-03-18 14:42:40 +05:30
committed by Mergify
parent 79dacfdef8
commit 5f1bb1f1ba
5 changed files with 14 additions and 6 deletions

View File

@@ -228,7 +228,6 @@ class DeprecatedBatchNoValuation:
(sle.item_code == self.sle.item_code)
& (sle.warehouse == self.sle.warehouse)
& (sle.batch_no.isnotnull())
& (batch.use_batchwise_valuation == 0)
& (sle.is_cancelled == 0)
& (sle.batch_no.isin(self.non_batchwise_valuation_batches))
)
@@ -278,7 +277,6 @@ class DeprecatedBatchNoValuation:
(sle.item_code == self.sle.item_code)
& (sle.warehouse == self.sle.warehouse)
& (sle.batch_no.isnotnull())
& (batch.use_batchwise_valuation == 0)
& (sle.is_cancelled == 0)
)
.where(timestamp_condition)
@@ -318,7 +316,6 @@ class DeprecatedBatchNoValuation:
(sabb.item_code == self.sle.item_code)
& (sabb.warehouse == self.sle.warehouse)
& (sabb_entry.batch_no.isnotnull())
& (batch.use_batchwise_valuation == 0)
& (sabb.is_cancelled == 0)
& (sabb.docstatus == 1)
)
@@ -378,7 +375,6 @@ class DeprecatedBatchNoValuation:
(bundle.item_code == self.sle.item_code)
& (bundle.warehouse == self.sle.warehouse)
& (bundle_child.batch_no.isnotnull())
& (batch.use_batchwise_valuation == 0)
& (bundle.is_cancelled == 0)
& (bundle.docstatus == 1)
& (bundle.type_of_transaction.isin(["Inward", "Outward"]))

View File

@@ -157,7 +157,13 @@ class Batch(Document):
frappe.throw(_("The selected item cannot have Batch"))
def set_batchwise_valuation(self):
from erpnext.stock.utils import get_valuation_method
if self.is_new():
if get_valuation_method(self.item) != "FIFO":
self.use_batchwise_valuation = 0
return
if frappe.db.get_single_value("Stock Settings", "do_not_use_batchwise_valuation"):
self.use_batchwise_valuation = 0
return

View File

@@ -3327,7 +3327,7 @@ class TestPurchaseReceipt(FrappeTestCase):
bundle = dn.items[0].serial_and_batch_bundle
valuation_rate = frappe.db.get_value("Serial and Batch Bundle", bundle, "avg_rate")
self.assertEqual(valuation_rate, 100)
self.assertEqual(valuation_rate, 150)
doc = frappe.get_doc("Stock Settings")
doc.do_not_use_batchwise_valuation = 1

View File

@@ -484,7 +484,7 @@ class TestStockLedgerEntry(FrappeTestCase, StockTestMixin):
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]
expected_incoming_rates = expected_abs_svd = [100.0, 100.0, 100.0, 100.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):

View File

@@ -683,6 +683,8 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
return query.run(as_dict=True)
def prepare_batches(self):
from erpnext.stock.utils import get_valuation_method
self.batches = self.batch_nos
if isinstance(self.batch_nos, dict):
self.batches = list(self.batch_nos.keys())
@@ -690,6 +692,10 @@ class BatchNoValuation(DeprecatedBatchNoValuation):
self.batchwise_valuation_batches = []
self.non_batchwise_valuation_batches = []
if get_valuation_method(self.sle.item_code) == "Moving Average":
self.non_batchwise_valuation_batches = self.batches
return
batches = frappe.get_all(
"Batch", filters={"name": ("in", self.batches), "use_batchwise_valuation": 1}, fields=["name"]
)