Skip to content

Commit

Permalink
fix: pick serial nos from selected batch only (backport #37988) (#38011)
Browse files Browse the repository at this point in the history
fix: pick serial nos from selected batch only (#37988)

* fix: pick current serial nos from selected batch only

* test: add test case for current qty and current serial nos

(cherry picked from commit db29180)

Co-authored-by: s-aga-r <sagarsharma.s312@gmail.com>
  • Loading branch information
mergify[bot] and s-aga-r authored Nov 9, 2023
1 parent 3f433b1 commit 0405aae
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ def get_stock_balance_for(
with_valuation_rate=with_valuation_rate,
with_serial_no=has_serial_no,
inventory_dimensions_dict=inventory_dimensions_dict,
batch_no=batch_no,
)

if has_serial_no:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,52 @@ def test_backdated_stock_reco_for_batch_item_dont_have_future_sle(self):

self.assertEqual(sr2.docstatus, 1)

def test_current_qty_and_current_serial_no_count(self):
# Step - 1: Create a Serial Batch Item
item = self.make_item(
properties={
"is_stock_item": 1,
"has_serial_no": 1,
"serial_no_series": "TEST-SERIAL-.###",
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "TEST-BATCH-.###",
}
).name

# Step - 2: Inward stock in multiple Batches
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry

se1 = make_stock_entry(
item_code=item,
target="_Test Warehouse - _TC",
qty=10,
)
se2 = make_stock_entry(
item_code=item,
target="_Test Warehouse - _TC",
qty=5,
)

# Step - 3: Create Stock Reconciliation
sr = create_stock_reconciliation(
item_code=item,
warehouse="_Test Warehouse - _TC",
qty=0,
batch_no=se1.items[0].batch_no,
do_not_submit=True,
)

# Test - 1: Current Serial No Count should be equal to Current Qty
self.assertEqual(sr.items[0].current_qty, se1.items[0].qty)
self.assertEqual(len(sr.items[0].current_serial_no.split("\n")), sr.items[0].current_qty)

sr.items[0].batch_no = se2.items[0].batch_no
sr.save()

self.assertEqual(sr.items[0].current_qty, se2.items[0].qty)
self.assertEqual(len(sr.items[0].current_serial_no.split("\n")), sr.items[0].current_qty)


def create_batch_item_with_batch(item_name, batch_id):
batch_item_doc = create_item(item_name, is_stock_item=1)
Expand Down
21 changes: 14 additions & 7 deletions erpnext/stock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def get_stock_balance(
with_valuation_rate=False,
with_serial_no=False,
inventory_dimensions_dict=None,
batch_no=None,
):
"""Returns stock balance quantity at given warehouse on given posting date or current date.
Expand Down Expand Up @@ -124,6 +125,9 @@ def get_stock_balance(

if with_valuation_rate:
if with_serial_no:
if batch_no:
args["batch_no"] = batch_no

serial_nos = get_serial_nos_data_after_transactions(args)

return (
Expand All @@ -140,27 +144,30 @@ def get_stock_balance(


def get_serial_nos_data_after_transactions(args):

serial_nos = set()
args = frappe._dict(args)
sle = frappe.qb.DocType("Stock Ledger Entry")

stock_ledger_entries = (
sle = frappe.qb.DocType("Stock Ledger Entry")
query = (
frappe.qb.from_(sle)
.select("serial_no", "actual_qty")
.select(sle.serial_no, sle.actual_qty)
.where(
(sle.item_code == args.item_code)
(sle.is_cancelled == 0)
& (sle.item_code == args.item_code)
& (sle.warehouse == args.warehouse)
& (
CombineDatetime(sle.posting_date, sle.posting_time)
< CombineDatetime(args.posting_date, args.posting_time)
)
& (sle.is_cancelled == 0)
)
.orderby(sle.posting_date, sle.posting_time, sle.creation)
.run(as_dict=1)
)

if args.batch_no:
query = query.where(sle.batch_no == args.batch_no)

stock_ledger_entries = query.run(as_dict=True)

for stock_ledger_entry in stock_ledger_entries:
changed_serial_no = get_serial_nos_data(stock_ledger_entry.serial_no)
if stock_ledger_entry.actual_qty > 0:
Expand Down

0 comments on commit 0405aae

Please sign in to comment.