Skip to content

Commit 5f1bb1f

Browse files
rohitwaghchauremergify[bot]
authored andcommitted
fix: valuation for moving average with batches
(cherry picked from commit cdfbc73)
1 parent 79dacfd commit 5f1bb1f

5 files changed

Lines changed: 14 additions & 6 deletions

File tree

erpnext/stock/deprecated_serial_batch.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ def set_balance_value_from_sl_entries(self) -> None:
228228
(sle.item_code == self.sle.item_code)
229229
& (sle.warehouse == self.sle.warehouse)
230230
& (sle.batch_no.isnotnull())
231-
& (batch.use_batchwise_valuation == 0)
232231
& (sle.is_cancelled == 0)
233232
& (sle.batch_no.isin(self.non_batchwise_valuation_batches))
234233
)
@@ -278,7 +277,6 @@ def get_last_sle_for_non_batch(self):
278277
(sle.item_code == self.sle.item_code)
279278
& (sle.warehouse == self.sle.warehouse)
280279
& (sle.batch_no.isnotnull())
281-
& (batch.use_batchwise_valuation == 0)
282280
& (sle.is_cancelled == 0)
283281
)
284282
.where(timestamp_condition)
@@ -318,7 +316,6 @@ def get_last_sle_for_sabb_no_batchwise_valuation(self):
318316
(sabb.item_code == self.sle.item_code)
319317
& (sabb.warehouse == self.sle.warehouse)
320318
& (sabb_entry.batch_no.isnotnull())
321-
& (batch.use_batchwise_valuation == 0)
322319
& (sabb.is_cancelled == 0)
323320
& (sabb.docstatus == 1)
324321
)
@@ -378,7 +375,6 @@ def set_balance_value_from_bundle(self) -> None:
378375
(bundle.item_code == self.sle.item_code)
379376
& (bundle.warehouse == self.sle.warehouse)
380377
& (bundle_child.batch_no.isnotnull())
381-
& (batch.use_batchwise_valuation == 0)
382378
& (bundle.is_cancelled == 0)
383379
& (bundle.docstatus == 1)
384380
& (bundle.type_of_transaction.isin(["Inward", "Outward"]))

erpnext/stock/doctype/batch/batch.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,13 @@ def item_has_batch_enabled(self):
157157
frappe.throw(_("The selected item cannot have Batch"))
158158

159159
def set_batchwise_valuation(self):
160+
from erpnext.stock.utils import get_valuation_method
161+
160162
if self.is_new():
163+
if get_valuation_method(self.item) != "FIFO":
164+
self.use_batchwise_valuation = 0
165+
return
166+
161167
if frappe.db.get_single_value("Stock Settings", "do_not_use_batchwise_valuation"):
162168
self.use_batchwise_valuation = 0
163169
return

erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3327,7 +3327,7 @@ def test_do_not_use_batchwise_valuation_rate(self):
33273327
bundle = dn.items[0].serial_and_batch_bundle
33283328

33293329
valuation_rate = frappe.db.get_value("Serial and Batch Bundle", bundle, "avg_rate")
3330-
self.assertEqual(valuation_rate, 100)
3330+
self.assertEqual(valuation_rate, 150)
33313331

33323332
doc = frappe.get_doc("Stock Settings")
33333333
doc.do_not_use_batchwise_valuation = 1

erpnext/stock/doctype/stock_ledger_entry/test_stock_ledger_entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def test_batchwise_item_valuation_moving_average(self):
484484
dns = create_delivery_note_entries_for_batchwise_item_valuation_test(dn_entry_list)
485485
sle_details = fetch_sle_details_for_doc_list(dns, ["stock_value_difference"])
486486
svd_list = [-1 * d["stock_value_difference"] for d in sle_details]
487-
expected_incoming_rates = expected_abs_svd = [75.0, 125.0, 75.0, 125.0]
487+
expected_incoming_rates = expected_abs_svd = [100.0, 100.0, 100.0, 100.0]
488488

489489
self.assertEqual(expected_abs_svd, svd_list, "Incorrect 'Stock Value Difference' values")
490490
for dn, _incoming_rate in zip(dns, expected_incoming_rates, strict=False):

erpnext/stock/serial_batch_bundle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,19 @@ def get_batch_no_ledgers(self) -> list[dict]:
683683
return query.run(as_dict=True)
684684

685685
def prepare_batches(self):
686+
from erpnext.stock.utils import get_valuation_method
687+
686688
self.batches = self.batch_nos
687689
if isinstance(self.batch_nos, dict):
688690
self.batches = list(self.batch_nos.keys())
689691

690692
self.batchwise_valuation_batches = []
691693
self.non_batchwise_valuation_batches = []
692694

695+
if get_valuation_method(self.sle.item_code) == "Moving Average":
696+
self.non_batchwise_valuation_batches = self.batches
697+
return
698+
693699
batches = frappe.get_all(
694700
"Batch", filters={"name": ("in", self.batches), "use_batchwise_valuation": 1}, fields=["name"]
695701
)

0 commit comments

Comments
 (0)