mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-06 21:59:13 +00:00
refactor(supplier_scorecard_variable): replace sql with query builder (#55164)
This commit is contained in:
@@ -82,29 +82,25 @@ def get_item_workdays(scorecard):
|
|||||||
|
|
||||||
def get_total_cost_of_shipments(scorecard):
|
def get_total_cost_of_shipments(scorecard):
|
||||||
"""Gets the total cost of all shipments in the period (based on Purchase Orders)"""
|
"""Gets the total cost of all shipments in the period (based on Purchase Orders)"""
|
||||||
supplier = frappe.get_doc("Supplier", scorecard.supplier)
|
|
||||||
|
|
||||||
# Look up all PO Items with delivery dates between our dates
|
from frappe.query_builder.functions import Sum
|
||||||
data = frappe.db.sql(
|
|
||||||
"""
|
|
||||||
SELECT
|
|
||||||
SUM(po_item.base_amount)
|
|
||||||
FROM
|
|
||||||
`tabPurchase Order Item` po_item,
|
|
||||||
`tabPurchase Order` po
|
|
||||||
WHERE
|
|
||||||
po.supplier = %(supplier)s
|
|
||||||
AND po_item.schedule_date BETWEEN %(start_date)s AND %(end_date)s
|
|
||||||
AND po_item.docstatus = 1
|
|
||||||
AND po_item.parent = po.name""",
|
|
||||||
{"supplier": supplier.name, "start_date": scorecard.start_date, "end_date": scorecard.end_date},
|
|
||||||
as_dict=0,
|
|
||||||
)[0][0]
|
|
||||||
|
|
||||||
if data:
|
PO = frappe.qb.DocType("Purchase Order")
|
||||||
return data
|
PO_Item = frappe.qb.DocType("Purchase Order Item")
|
||||||
else:
|
|
||||||
return 0
|
query = (
|
||||||
|
frappe.qb.from_(PO_Item)
|
||||||
|
.join(PO)
|
||||||
|
.on(PO_Item.parent == PO.name)
|
||||||
|
.select(Sum(PO_Item.base_amount))
|
||||||
|
.where(PO.supplier == scorecard.supplier)
|
||||||
|
.where(PO_Item.schedule_date[scorecard.start_date : scorecard.end_date]) # Syntaxe BETWEEN
|
||||||
|
.where(PO_Item.docstatus == 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
result = query.run(as_list=True)
|
||||||
|
total_cost = result[0][0] if result and result[0][0] is not None else 0
|
||||||
|
return total_cost
|
||||||
|
|
||||||
|
|
||||||
def get_cost_of_delayed_shipments(scorecard):
|
def get_cost_of_delayed_shipments(scorecard):
|
||||||
|
|||||||
Reference in New Issue
Block a user