Merge pull request #32304 from s-aga-r/refactor/report/exponential-smoothing-forecasting
refactor: rewrite `Exponential Smoothing Forecasting` queries in `QB`
This commit is contained in:
commit
07f87b9147
@ -96,38 +96,39 @@ class ForecastingReport(ExponentialSmoothingForecast):
|
|||||||
value["avg"] = flt(sum(list_of_period_value)) / flt(sum(total_qty))
|
value["avg"] = flt(sum(list_of_period_value)) / flt(sum(total_qty))
|
||||||
|
|
||||||
def get_data_for_forecast(self):
|
def get_data_for_forecast(self):
|
||||||
cond = ""
|
parent = frappe.qb.DocType(self.doctype)
|
||||||
if self.filters.item_code:
|
child = frappe.qb.DocType(self.child_doctype)
|
||||||
cond = " AND soi.item_code = %s" % (frappe.db.escape(self.filters.item_code))
|
|
||||||
|
|
||||||
warehouses = []
|
|
||||||
if self.filters.warehouse:
|
|
||||||
warehouses = get_child_warehouses(self.filters.warehouse)
|
|
||||||
cond += " AND soi.warehouse in ({})".format(",".join(["%s"] * len(warehouses)))
|
|
||||||
|
|
||||||
input_data = [self.filters.from_date, self.filters.company]
|
|
||||||
if warehouses:
|
|
||||||
input_data.extend(warehouses)
|
|
||||||
|
|
||||||
date_field = "posting_date" if self.doctype == "Delivery Note" else "transaction_date"
|
date_field = "posting_date" if self.doctype == "Delivery Note" else "transaction_date"
|
||||||
|
|
||||||
return frappe.db.sql(
|
query = (
|
||||||
"""
|
frappe.qb.from_(parent)
|
||||||
SELECT
|
.from_(child)
|
||||||
so.{date_field} as posting_date, soi.item_code, soi.warehouse,
|
.select(
|
||||||
soi.item_name, soi.stock_qty as qty, soi.base_amount as amount
|
parent[date_field].as_("posting_date"),
|
||||||
FROM
|
child.item_code,
|
||||||
`tab{doc}` so, `tab{child_doc}` soi
|
child.warehouse,
|
||||||
WHERE
|
child.item_name,
|
||||||
so.docstatus = 1 AND so.name = soi.parent AND
|
child.stock_qty.as_("qty"),
|
||||||
so.{date_field} < %s AND so.company = %s {cond}
|
child.base_amount.as_("amount"),
|
||||||
""".format(
|
)
|
||||||
doc=self.doctype, child_doc=self.child_doctype, date_field=date_field, cond=cond
|
.where(
|
||||||
),
|
(parent.docstatus == 1)
|
||||||
tuple(input_data),
|
& (parent.name == child.parent)
|
||||||
as_dict=1,
|
& (parent[date_field] < self.filters.from_date)
|
||||||
|
& (parent.company == self.filters.company)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.filters.item_code:
|
||||||
|
query = query.where(child.item_code == self.filters.item_code)
|
||||||
|
|
||||||
|
if self.filters.warehouse:
|
||||||
|
warehouses = get_child_warehouses(self.filters.warehouse) or []
|
||||||
|
query = query.where(child.warehouse.isin(warehouses))
|
||||||
|
|
||||||
|
return query.run(as_dict=True)
|
||||||
|
|
||||||
def prepare_final_data(self):
|
def prepare_final_data(self):
|
||||||
self.data = []
|
self.data = []
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user