refactor: rewrite get_stock_value_on()
queries in QB
This commit is contained in:
parent
eaa5f1fd9d
commit
e43bc38e05
@ -9,6 +9,7 @@ import frappe
|
|||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.query_builder.functions import CombineDatetime
|
from frappe.query_builder.functions import CombineDatetime
|
||||||
from frappe.utils import cstr, flt, get_link_to_form, nowdate, nowtime
|
from frappe.utils import cstr, flt, get_link_to_form, nowdate, nowtime
|
||||||
|
from pypika.terms import ExistsCriterion
|
||||||
|
|
||||||
import erpnext
|
import erpnext
|
||||||
from erpnext.stock.valuation import FIFOValuation, LIFOValuation
|
from erpnext.stock.valuation import FIFOValuation, LIFOValuation
|
||||||
@ -57,39 +58,39 @@ def get_stock_value_on(warehouse=None, posting_date=None, item_code=None):
|
|||||||
if not posting_date:
|
if not posting_date:
|
||||||
posting_date = nowdate()
|
posting_date = nowdate()
|
||||||
|
|
||||||
values, condition = [posting_date], ""
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
||||||
|
query = (
|
||||||
|
frappe.qb.from_(sle)
|
||||||
|
.select(
|
||||||
|
sle.item_code,
|
||||||
|
sle.stock_value,
|
||||||
|
sle.name,
|
||||||
|
sle.warehouse,
|
||||||
|
)
|
||||||
|
.where((sle.posting_date <= posting_date) & (sle.is_cancelled == 0))
|
||||||
|
.orderby(CombineDatetime(sle.posting_date, sle.posting_time), order=frappe.qb.desc)
|
||||||
|
.orderby(sle.creation, order=frappe.qb.desc)
|
||||||
|
)
|
||||||
|
|
||||||
if warehouse:
|
if warehouse:
|
||||||
|
|
||||||
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
||||||
|
|
||||||
if is_group:
|
if is_group:
|
||||||
values.extend([lft, rgt])
|
wh = frappe.qb.DocType("Warehouse")
|
||||||
condition += "and exists (\
|
query = query.where(
|
||||||
select name from `tabWarehouse` wh where wh.name = sle.warehouse\
|
ExistsCriterion(
|
||||||
and wh.lft >= %s and wh.rgt <= %s)"
|
frappe.qb.from_(wh)
|
||||||
|
.select(wh.name)
|
||||||
|
.where((wh.name == sle.warehouse) & (wh.lft >= lft) & (wh.rgt <= rgt))
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
values.append(warehouse)
|
query = query.where(sle.warehouse == warehouse)
|
||||||
condition += " AND warehouse = %s"
|
|
||||||
|
|
||||||
if item_code:
|
if item_code:
|
||||||
values.append(item_code)
|
query = query.where(sle.item_code == item_code)
|
||||||
condition += " AND item_code = %s"
|
|
||||||
|
|
||||||
stock_ledger_entries = frappe.db.sql(
|
stock_ledger_entries = query.run(as_dict=True)
|
||||||
"""
|
|
||||||
SELECT item_code, stock_value, name, warehouse
|
|
||||||
FROM `tabStock Ledger Entry` sle
|
|
||||||
WHERE posting_date <= %s {0}
|
|
||||||
and is_cancelled = 0
|
|
||||||
ORDER BY timestamp(posting_date, posting_time) DESC, creation DESC
|
|
||||||
""".format(
|
|
||||||
condition
|
|
||||||
),
|
|
||||||
values,
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
sle_map = {}
|
sle_map = {}
|
||||||
for sle in stock_ledger_entries:
|
for sle in stock_ledger_entries:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user