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:
commit
e4a1cf0cd2
@ -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,6 +18,11 @@ def execute(filters=None):
|
|||||||
def get_item_list(wo_list, filters):
|
def get_item_list(wo_list, filters):
|
||||||
out = []
|
out = []
|
||||||
|
|
||||||
|
if wo_list:
|
||||||
|
bin = frappe.qb.DocType("Bin")
|
||||||
|
bom = frappe.qb.DocType("BOM")
|
||||||
|
bom_item = frappe.qb.DocType("BOM Item")
|
||||||
|
|
||||||
# Add a row for each item/qty
|
# Add a row for each item/qty
|
||||||
for wo_details in wo_list:
|
for wo_details in wo_list:
|
||||||
desc = frappe.db.get_value("BOM", wo_details.bom_no, "description")
|
desc = frappe.db.get_value("BOM", wo_details.bom_no, "description")
|
||||||
@ -24,30 +30,25 @@ def get_item_list(wo_list, filters):
|
|||||||
for wo_item_details in frappe.db.get_values(
|
for wo_item_details in frappe.db.get_values(
|
||||||
"Work Order Item", {"parent": wo_details.name}, ["item_code", "source_warehouse"], as_dict=1
|
"Work Order Item", {"parent": wo_details.name}, ["item_code", "source_warehouse"], as_dict=1
|
||||||
):
|
):
|
||||||
|
item_list = (
|
||||||
item_list = frappe.db.sql(
|
frappe.qb.from_(bom)
|
||||||
"""SELECT
|
.from_(bom_item)
|
||||||
bom_item.item_code as item_code,
|
.left_join(bin)
|
||||||
ifnull(ledger.actual_qty*bom.quantity/bom_item.stock_qty,0) as build_qty
|
.on(
|
||||||
FROM
|
(bom_item.item_code == bin.item_code)
|
||||||
`tabBOM` as bom, `tabBOM Item` AS bom_item
|
& (bin.warehouse == IfNull(wo_item_details.source_warehouse, filters.warehouse))
|
||||||
LEFT JOIN `tabBin` AS ledger
|
|
||||||
ON bom_item.item_code = ledger.item_code
|
|
||||||
AND ledger.warehouse = ifnull(%(warehouse)s,%(filterhouse)s)
|
|
||||||
WHERE
|
|
||||||
bom.name = bom_item.parent
|
|
||||||
and bom_item.item_code = %(item_code)s
|
|
||||||
and bom.name = %(bom)s
|
|
||||||
GROUP BY
|
|
||||||
bom_item.item_code""",
|
|
||||||
{
|
|
||||||
"bom": wo_details.bom_no,
|
|
||||||
"warehouse": wo_item_details.source_warehouse,
|
|
||||||
"filterhouse": filters.warehouse,
|
|
||||||
"item_code": wo_item_details.item_code,
|
|
||||||
},
|
|
||||||
as_dict=1,
|
|
||||||
)
|
)
|
||||||
|
.select(
|
||||||
|
bom_item.item_code.as_("item_code"),
|
||||||
|
IfNull(bin.actual_qty * bom.quantity / bom_item.stock_qty, 0).as_("build_qty"),
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
(bom.name == bom_item.parent)
|
||||||
|
& (bom_item.item_code == wo_item_details.item_code)
|
||||||
|
& (bom.name == wo_details.bom_no)
|
||||||
|
)
|
||||||
|
.groupby(bom_item.item_code)
|
||||||
|
).run(as_dict=1)
|
||||||
|
|
||||||
stock_qty = 0
|
stock_qty = 0
|
||||||
count = 0
|
count = 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user