Merge pull request #33679 from s-aga-r/refactor/qb/pick-list

refactor: rewrite `pick_list.py` queries in `QB`
This commit is contained in:
Sagar Sharma 2023-01-18 18:26:08 +05:30 committed by GitHub
commit 60ec7b6cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,7 @@ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.model.mapper import map_child_doc from frappe.model.mapper import map_child_doc
from frappe.query_builder import Case from frappe.query_builder import Case
from frappe.query_builder.functions import Locate from frappe.query_builder.functions import IfNull, Locate, Sum
from frappe.utils import cint, floor, flt, today from frappe.utils import cint, floor, flt, today
from frappe.utils.nestedset import get_descendants_of from frappe.utils.nestedset import get_descendants_of
@ -503,42 +503,30 @@ def get_available_item_locations_for_serialized_item(
def get_available_item_locations_for_batched_item( def get_available_item_locations_for_batched_item(
item_code, from_warehouses, required_qty, company item_code, from_warehouses, required_qty, company
): ):
warehouse_condition = "and warehouse in %(warehouses)s" if from_warehouses else "" sle = frappe.qb.DocType("Stock Ledger Entry")
batch_locations = frappe.db.sql( batch = frappe.qb.DocType("Batch")
"""
SELECT query = (
sle.`warehouse`, frappe.qb.from_(sle)
sle.`batch_no`, .from_(batch)
SUM(sle.`actual_qty`) AS `qty` .select(sle.warehouse, sle.batch_no, Sum(sle.actual_qty).as_("qty"))
FROM .where(
`tabStock Ledger Entry` sle, `tabBatch` batch (sle.batch_no == batch.name)
WHERE & (sle.item_code == item_code)
sle.batch_no = batch.name & (sle.company == company)
and sle.`item_code`=%(item_code)s & (batch.disabled == 0)
and sle.`company` = %(company)s & (sle.is_cancelled == 0)
and batch.disabled = 0 & (IfNull(batch.expiry_date, "2200-01-01") > today())
and sle.is_cancelled=0 )
and IFNULL(batch.`expiry_date`, '2200-01-01') > %(today)s .groupby(sle.warehouse, sle.batch_no, sle.item_code)
{warehouse_condition} .having(Sum(sle.actual_qty) > 0)
GROUP BY .orderby(IfNull(batch.expiry_date, "2200-01-01"), batch.creation, sle.batch_no, sle.warehouse)
sle.`warehouse`,
sle.`batch_no`,
sle.`item_code`
HAVING `qty` > 0
ORDER BY IFNULL(batch.`expiry_date`, '2200-01-01'), batch.`creation`, sle.`batch_no`, sle.`warehouse`
""".format(
warehouse_condition=warehouse_condition
),
{ # nosec
"item_code": item_code,
"company": company,
"today": today(),
"warehouses": from_warehouses,
},
as_dict=1,
) )
return batch_locations if from_warehouses:
query = query.where(sle.warehouse.isin(from_warehouses))
return query.run(as_dict=True)
def get_available_item_locations_for_serial_and_batched_item( def get_available_item_locations_for_serial_and_batched_item(