Skip to content

Commit

Permalink
fix: negative batch issue (backport #38688) (#38694)
Browse files Browse the repository at this point in the history
fix: negative batch issue (#38688)

(cherry picked from commit 69d7a64)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure committed Dec 12, 2023
1 parent b68e1f6 commit a75081b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ def validate_batch_inventory(self):
"item_code": self.item_code,
"warehouse": self.warehouse,
"batch_no": batches,
"consider_negative_batches": True,
}
)
)
Expand All @@ -714,6 +715,9 @@ def validate_batch_inventory(self):
available_batches = get_available_batches_qty(available_batches)
for batch_no in batches:
if batch_no not in available_batches or available_batches[batch_no] < 0:
if flt(available_batches.get(batch_no)) < 0:
self.validate_negative_batch(batch_no, available_batches[batch_no])

self.throw_error_message(
f"Batch {bold(batch_no)} is not available in the selected warehouse {self.warehouse}"
)
Expand Down Expand Up @@ -1486,7 +1490,8 @@ def get_auto_batch_nos(kwargs):
available_batches, stock_ledgers_batches, pos_invoice_batches, sre_reserved_batches
)

available_batches = list(filter(lambda x: x.qty > 0, available_batches))
if not kwargs.consider_negative_batches:
available_batches = list(filter(lambda x: x.qty > 0, available_batches))

if not qty:
return available_batches
Expand Down
39 changes: 39 additions & 0 deletions erpnext/stock/doctype/stock_entry/test_stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,45 @@ def test_enqueue_action(self):
self.assertFalse(doc.is_enqueue_action())
frappe.flags.in_test = True

def test_negative_batch(self):
item_code = "Test Negative Batch Item - 001"
make_item(
item_code,
{"has_batch_no": 1, "create_new_batch": 1, "batch_naming_series": "Test-BCH-NNS.#####"},
)

se1 = make_stock_entry(
item_code=item_code,
purpose="Material Receipt",
qty=100,
target="_Test Warehouse - _TC",
)

se1.reload()

batch_no = get_batch_from_bundle(se1.items[0].serial_and_batch_bundle)

se2 = make_stock_entry(
item_code=item_code,
purpose="Material Issue",
batch_no=batch_no,
qty=10,
source="_Test Warehouse - _TC",
)

se2.reload()

se3 = make_stock_entry(
item_code=item_code,
purpose="Material Receipt",
qty=100,
target="_Test Warehouse - _TC",
)

se3.reload()

self.assertRaises(frappe.ValidationError, se1.cancel)


def make_serialized_item(**args):
args = frappe._dict(args)
Expand Down

0 comments on commit a75081b

Please sign in to comment.