Merge pull request #32153 from s-aga-r/refactor/report/work-order-stock-report

refactor: rewrite Work Order Stock Report queries in QB
This commit is contained in:
Sagar Sharma 2022-09-10 16:51:00 +05:30 committed by GitHub
commit e4a1cf0cd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.query_builder.functions import IfNull
from frappe.utils import cint from frappe.utils import cint
@ -17,70 +18,70 @@ def execute(filters=None):
def get_item_list(wo_list, filters): def get_item_list(wo_list, filters):
out = [] out = []
# Add a row for each item/qty if wo_list:
for wo_details in wo_list: bin = frappe.qb.DocType("Bin")
desc = frappe.db.get_value("BOM", wo_details.bom_no, "description") bom = frappe.qb.DocType("BOM")
bom_item = frappe.qb.DocType("BOM Item")
for wo_item_details in frappe.db.get_values( # Add a row for each item/qty
"Work Order Item", {"parent": wo_details.name}, ["item_code", "source_warehouse"], as_dict=1 for wo_details in wo_list:
): desc = frappe.db.get_value("BOM", wo_details.bom_no, "description")
item_list = frappe.db.sql( for wo_item_details in frappe.db.get_values(
"""SELECT "Work Order Item", {"parent": wo_details.name}, ["item_code", "source_warehouse"], as_dict=1
bom_item.item_code as item_code, ):
ifnull(ledger.actual_qty*bom.quantity/bom_item.stock_qty,0) as build_qty item_list = (
FROM frappe.qb.from_(bom)
`tabBOM` as bom, `tabBOM Item` AS bom_item .from_(bom_item)
LEFT JOIN `tabBin` AS ledger .left_join(bin)
ON bom_item.item_code = ledger.item_code .on(
AND ledger.warehouse = ifnull(%(warehouse)s,%(filterhouse)s) (bom_item.item_code == bin.item_code)
WHERE & (bin.warehouse == IfNull(wo_item_details.source_warehouse, filters.warehouse))
bom.name = bom_item.parent )
and bom_item.item_code = %(item_code)s .select(
and bom.name = %(bom)s bom_item.item_code.as_("item_code"),
GROUP BY IfNull(bin.actual_qty * bom.quantity / bom_item.stock_qty, 0).as_("build_qty"),
bom_item.item_code""", )
{ .where(
"bom": wo_details.bom_no, (bom.name == bom_item.parent)
"warehouse": wo_item_details.source_warehouse, & (bom_item.item_code == wo_item_details.item_code)
"filterhouse": filters.warehouse, & (bom.name == wo_details.bom_no)
"item_code": wo_item_details.item_code, )
}, .groupby(bom_item.item_code)
as_dict=1, ).run(as_dict=1)
)
stock_qty = 0 stock_qty = 0
count = 0 count = 0
buildable_qty = wo_details.qty buildable_qty = wo_details.qty
for item in item_list: for item in item_list:
count = count + 1 count = count + 1
if item.build_qty >= (wo_details.qty - wo_details.produced_qty): if item.build_qty >= (wo_details.qty - wo_details.produced_qty):
stock_qty = stock_qty + 1 stock_qty = stock_qty + 1
elif buildable_qty >= item.build_qty: elif buildable_qty >= item.build_qty:
buildable_qty = item.build_qty buildable_qty = item.build_qty
if count == stock_qty: if count == stock_qty:
build = "Y" build = "Y"
else: else:
build = "N" build = "N"
row = frappe._dict( row = frappe._dict(
{ {
"work_order": wo_details.name, "work_order": wo_details.name,
"status": wo_details.status, "status": wo_details.status,
"req_items": cint(count), "req_items": cint(count),
"instock": stock_qty, "instock": stock_qty,
"description": desc, "description": desc,
"source_warehouse": wo_item_details.source_warehouse, "source_warehouse": wo_item_details.source_warehouse,
"item_code": wo_item_details.item_code, "item_code": wo_item_details.item_code,
"bom_no": wo_details.bom_no, "bom_no": wo_details.bom_no,
"qty": wo_details.qty, "qty": wo_details.qty,
"buildable_qty": buildable_qty, "buildable_qty": buildable_qty,
"ready_to_build": build, "ready_to_build": build,
} }
) )
out.append(row) out.append(row)
return out return out