From d6001e5ef9b9fd05b3e534c25cc36f388f8e57ee Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:31:16 +0530 Subject: [PATCH] fix: stock entry not fetching expired batches (backport #44863) (#44868) fix: stock entry not fetching expired batches (#44863) (cherry picked from commit c9b143b509c18a31b65f252d495b69eb24ab4f53) Co-authored-by: rohitwaghchaure --- erpnext/controllers/stock_controller.py | 2 +- .../stock/doctype/stock_entry/stock_entry.js | 1 + .../stock/doctype/stock_entry/stock_entry.py | 46 +++++++++++++++---- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 34556f725d4..6e013746199 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -156,7 +156,7 @@ class StockController(AccountsController): 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"): diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index 0e39c2a9756..df6a61d335b 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -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", []); diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index f362f9d3da9..febc814b978 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -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):