refactor: convert heatmap queries to QB (#33581)
Uses new `UnixTimestamp` function, don't backport.
This commit is contained in:
@@ -2,8 +2,13 @@
|
||||
# License: GNU General Public License v3. See license.txt
|
||||
|
||||
|
||||
from collections import defaultdict
|
||||
from itertools import chain
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.query_builder import Interval
|
||||
from frappe.query_builder.functions import Count, CurDate, UnixTimestamp
|
||||
from frappe.utils import flt
|
||||
from frappe.utils.nestedset import NestedSet, get_root_of
|
||||
|
||||
@@ -77,61 +82,31 @@ def on_doctype_update():
|
||||
frappe.db.add_index("Sales Person", ["lft", "rgt"])
|
||||
|
||||
|
||||
def get_timeline_data(doctype, name):
|
||||
def get_timeline_data(doctype: str, name: str) -> dict[int, int]:
|
||||
def _fetch_activity(doctype: str, date_field: str):
|
||||
sales_team = frappe.qb.DocType("Sales Team")
|
||||
transaction = frappe.qb.DocType(doctype)
|
||||
|
||||
out = {}
|
||||
|
||||
out.update(
|
||||
dict(
|
||||
frappe.db.sql(
|
||||
"""select
|
||||
unix_timestamp(dt.transaction_date), count(st.parenttype)
|
||||
from
|
||||
`tabSales Order` dt, `tabSales Team` st
|
||||
where
|
||||
st.sales_person = %s and st.parent = dt.name and dt.transaction_date > date_sub(curdate(), interval 1 year)
|
||||
group by dt.transaction_date """,
|
||||
name,
|
||||
)
|
||||
return dict(
|
||||
frappe.qb.from_(transaction)
|
||||
.join(sales_team)
|
||||
.on(transaction.name == sales_team.parent)
|
||||
.select(UnixTimestamp(transaction[date_field]), Count("*"))
|
||||
.where(sales_team.sales_person == name)
|
||||
.where(transaction[date_field] > CurDate() - Interval(years=1))
|
||||
.groupby(transaction[date_field])
|
||||
.run()
|
||||
)
|
||||
)
|
||||
|
||||
sales_invoice = dict(
|
||||
frappe.db.sql(
|
||||
"""select
|
||||
unix_timestamp(dt.posting_date), count(st.parenttype)
|
||||
from
|
||||
`tabSales Invoice` dt, `tabSales Team` st
|
||||
where
|
||||
st.sales_person = %s and st.parent = dt.name and dt.posting_date > date_sub(curdate(), interval 1 year)
|
||||
group by dt.posting_date """,
|
||||
name,
|
||||
)
|
||||
)
|
||||
sales_order_activity = _fetch_activity("Sales Order", "transaction_date")
|
||||
sales_invoice_activity = _fetch_activity("Sales Invoice", "posting_date")
|
||||
delivery_note_activity = _fetch_activity("Delivery Note", "posting_date")
|
||||
|
||||
for key in sales_invoice:
|
||||
if out.get(key):
|
||||
out[key] += sales_invoice[key]
|
||||
else:
|
||||
out[key] = sales_invoice[key]
|
||||
merged_activities = defaultdict(int)
|
||||
|
||||
delivery_note = dict(
|
||||
frappe.db.sql(
|
||||
"""select
|
||||
unix_timestamp(dt.posting_date), count(st.parenttype)
|
||||
from
|
||||
`tabDelivery Note` dt, `tabSales Team` st
|
||||
where
|
||||
st.sales_person = %s and st.parent = dt.name and dt.posting_date > date_sub(curdate(), interval 1 year)
|
||||
group by dt.posting_date """,
|
||||
name,
|
||||
)
|
||||
)
|
||||
for ts, count in chain(
|
||||
sales_order_activity.items(), sales_invoice_activity.items(), delivery_note_activity.items()
|
||||
):
|
||||
merged_activities[ts] += count
|
||||
|
||||
for key in delivery_note:
|
||||
if out.get(key):
|
||||
out[key] += delivery_note[key]
|
||||
else:
|
||||
out[key] = delivery_note[key]
|
||||
|
||||
return out
|
||||
return merged_activities
|
||||
|
||||
Reference in New Issue
Block a user