mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-17 08:35:00 +00:00
refactor: portal query in timesheet.py (#47336)
* refactor: portal query in timesheet.py * fix: use criterion.any to fix query
This commit is contained in:
@@ -500,7 +500,6 @@ def get_timesheets_list(doctype, txt, filters, limit_start, limit_page_length=20
|
||||
user = frappe.session.user
|
||||
# find customer name from contact.
|
||||
customer = ""
|
||||
timesheets = []
|
||||
|
||||
contact = frappe.db.exists("Contact", {"user": user})
|
||||
if contact:
|
||||
@@ -509,31 +508,43 @@ def get_timesheets_list(doctype, txt, filters, limit_start, limit_page_length=20
|
||||
customer = contact.get_link_for("Customer")
|
||||
|
||||
if customer:
|
||||
sales_invoices = [
|
||||
d.name for d in frappe.get_all("Sales Invoice", filters={"customer": customer})
|
||||
] or [None]
|
||||
projects = [d.name for d in frappe.get_all("Project", filters={"customer": customer})]
|
||||
# Return timesheet related data to web portal.
|
||||
timesheets = frappe.db.sql(
|
||||
f"""
|
||||
SELECT
|
||||
ts.name, tsd.activity_type, ts.status, ts.total_billable_hours,
|
||||
COALESCE(ts.sales_invoice, tsd.sales_invoice) AS sales_invoice, tsd.project
|
||||
FROM `tabTimesheet` ts, `tabTimesheet Detail` tsd
|
||||
WHERE tsd.parent = ts.name AND
|
||||
(
|
||||
ts.sales_invoice IN %(sales_invoices)s OR
|
||||
tsd.sales_invoice IN %(sales_invoices)s OR
|
||||
tsd.project IN %(projects)s
|
||||
)
|
||||
ORDER BY `end_date` ASC
|
||||
LIMIT {limit_page_length} offset {limit_start}
|
||||
""",
|
||||
dict(sales_invoices=sales_invoices, projects=projects),
|
||||
as_dict=True,
|
||||
) # nosec
|
||||
sales_invoices = frappe.get_all("Sales Invoice", filters={"customer": customer}, pluck="name")
|
||||
projects = frappe.get_all("Project", filters={"customer": customer}, pluck="name")
|
||||
|
||||
return timesheets
|
||||
# Return timesheet related data to web portal.
|
||||
table = frappe.qb.DocType("Timesheet")
|
||||
child_table = frappe.qb.DocType("Timesheet Detail")
|
||||
query = (
|
||||
frappe.qb.from_(table)
|
||||
.join(child_table)
|
||||
.on(table.name == child_table.parent)
|
||||
.select(
|
||||
table.name,
|
||||
child_table.activity_type,
|
||||
table.status,
|
||||
table.total_billable_hours,
|
||||
(table.sales_invoice | child_table.sales_invoice).as_("sales_invoice"),
|
||||
child_table.project,
|
||||
)
|
||||
.orderby(table.end_date)
|
||||
.limit(limit_page_length)
|
||||
.offset(limit_start)
|
||||
)
|
||||
|
||||
conditions = []
|
||||
if sales_invoices:
|
||||
conditions.extend(
|
||||
[table.sales_invoice.isin(sales_invoices), child_table.sales_invoice.isin(sales_invoices)]
|
||||
)
|
||||
if projects:
|
||||
conditions.append(child_table.project.isin(projects))
|
||||
|
||||
if conditions:
|
||||
query = query.where(frappe.qb.terms.Criterion.any(conditions))
|
||||
|
||||
return query.run(as_dict=True)
|
||||
else:
|
||||
return {}
|
||||
|
||||
|
||||
def get_list_context(context=None):
|
||||
|
||||
Reference in New Issue
Block a user