Skip to content

Commit

Permalink
fix: stock entry not fetching expired batches (backport #44863) (#44868)
Browse files Browse the repository at this point in the history
fix: stock entry not fetching expired batches (#44863)

(cherry picked from commit c9b143b)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Dec 23, 2024
1 parent f32cf84 commit d6001e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def validate_serialized_batch(self):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

is_material_issue = False
if self.doctype == "Stock Entry" and self.purpose == "Material Issue":
if self.doctype == "Stock Entry" and self.purpose in ["Material Issue", "Material Transfer"]:
is_material_issue = True

for d in self.get("items"):
Expand Down
1 change: 1 addition & 0 deletions erpnext/stock/doctype/stock_entry/stock_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ frappe.ui.form.on("Stock Entry", {
function () {
frappe.call({
method: "erpnext.stock.doctype.stock_entry.stock_entry.get_expired_batch_items",
freeze: true,
callback: function (r) {
if (!r.exc && r.message) {
frm.set_value("items", []);
Expand Down
46 changes: 37 additions & 9 deletions erpnext/stock/doctype/stock_entry/stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,17 +2975,45 @@ def get_uom_details(item_code, uom, qty):

@frappe.whitelist()
def get_expired_batch_items():
return frappe.db.sql(
"""select b.item, sum(sle.actual_qty) as qty, sle.batch_no, sle.warehouse, sle.stock_uom\
from `tabBatch` b, `tabStock Ledger Entry` sle
where b.expiry_date <= %s
and b.expiry_date is not NULL
and b.batch_id = sle.batch_no and sle.is_cancelled = 0
group by sle.warehouse, sle.item_code, sle.batch_no""",
(nowdate()),
as_dict=1,
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import get_auto_batch_nos

expired_batches = get_expired_batches()
if not expired_batches:
return []

expired_batches_stock = get_auto_batch_nos(
frappe._dict(
{
"batch_no": list(expired_batches.keys()),
"for_stock_levels": True,
}
)
)

for row in expired_batches_stock:
row.update(expired_batches.get(row.batch_no))

return expired_batches_stock


def get_expired_batches():
batch = frappe.qb.DocType("Batch")

data = (
frappe.qb.from_(batch)
.select(batch.item, batch.name.as_("batch_no"), batch.stock_uom)
.where((batch.expiry_date <= nowdate()) & (batch.expiry_date.isnotnull()))
).run(as_dict=True)

if not data:
return []

expired_batches = frappe._dict()
for row in data:
expired_batches[row.batch_no] = row

return expired_batches


@frappe.whitelist()
def get_warehouse_details(args):
Expand Down

0 comments on commit d6001e5

Please sign in to comment.