fix: batch filter not working in stock ledger report (backport #39934) (#39935)

fix: batch filter not working in stock ledger report

(cherry picked from commit a995e87567609ba4808e7bf2632b76004750aff0)

Co-authored-by: Rohit Waghchaure <rohitw1991@gmail.com>
(cherry picked from commit 15135952fcc259469b2c90cbc63aad20444a993b)
This commit is contained in:
mergify[bot] 2024-02-16 20:47:20 +05:30 committed by Mergify
parent 6d6a3a10fb
commit 3cd315973c

View File

@ -320,15 +320,45 @@ def get_stock_ledger_entries(filters, items):
if items:
query = query.where(sle.item_code.isin(items))
for field in ["voucher_no", "batch_no", "project", "company"]:
for field in ["voucher_no", "project", "company"]:
if filters.get(field) and field not in inventory_dimension_fields:
query = query.where(sle[field] == filters.get(field))
if filters.get("batch_no"):
bundles = get_serial_and_batch_bundles(filters)
if bundles:
query = query.where(
(sle.serial_and_batch_bundle.isin(bundles)) | (sle.batch_no == filters.batch_no)
)
else:
query = query.where(sle.batch_no == filters.batch_no)
query = apply_warehouse_filter(query, sle, filters)
return query.run(as_dict=True)
def get_serial_and_batch_bundles(filters):
SBB = frappe.qb.DocType("Serial and Batch Bundle")
SBE = frappe.qb.DocType("Serial and Batch Entry")
query = (
frappe.qb.from_(SBE)
.inner_join(SBB)
.on(SBE.parent == SBB.name)
.select(SBE.parent)
.where(
(SBB.docstatus == 1)
& (SBB.has_batch_no == 1)
& (SBB.voucher_no.notnull())
& (SBE.batch_no == filters.batch_no)
)
)
return query.run(pluck=SBE.parent)
def get_inventory_dimension_fields():
return [dimension.fieldname for dimension in get_inventory_dimensions()]