refactor: remove join in sql

(cherry picked from commit 9638151f9d)
This commit is contained in:
mithili
2025-07-24 18:40:34 +05:30
committed by Mergify
parent 8314059bf7
commit 8d25269de6

View File

@@ -83,21 +83,19 @@ def get_columns(filters):
def get_entries(filters): def get_entries(filters):
date_field = "transaction_date" if filters.get("doctype") == "Sales Order" else "posting_date" date_field = "transaction_date" if filters.get("doctype") == "Sales Order" else "posting_date"
company_currency = frappe.db.get_value("Company", filters.get("company"), "default_currency")
conditions = get_conditions(filters, date_field) conditions = get_conditions(filters, date_field)
entries = frappe.db.sql( entries = frappe.db.sql(
""" """
SELECT SELECT
doc.name, doc.customer, doc.territory, doc.{} as posting_date, doc.base_net_total as amount, name, customer, territory, {} as posting_date, base_net_total as amount,
doc.sales_partner, doc.commission_rate, doc.total_commission, company.default_currency as currency sales_partner, commission_rate, total_commission, '{}' as currency
FROM FROM
`tab{}` as doc `tab{}`
JOIN
`tabCompany` as company ON company.name = doc.company
WHERE WHERE
{} and doc.docstatus = 1 and doc.sales_partner is not null {} and docstatus = 1 and sales_partner is not null
and doc.sales_partner != '' order by doc.name desc, doc.sales_partner and sales_partner != '' order by name desc, sales_partner
""".format(date_field, filters.get("doctype"), conditions), """.format(date_field, company_currency, filters.get("doctype"), conditions),
filters, filters,
as_dict=1, as_dict=1,
) )
@@ -110,15 +108,15 @@ def get_conditions(filters, date_field):
for field in ["company", "customer", "territory"]: for field in ["company", "customer", "territory"]:
if filters.get(field): if filters.get(field):
conditions += f" and doc.{field} = %({field})s" conditions += f" and {field} = %({field})s"
if filters.get("sales_partner"): if filters.get("sales_partner"):
conditions += " and doc.sales_partner = %(sales_partner)s" conditions += " and sales_partner = %(sales_partner)s"
if filters.get("from_date"): if filters.get("from_date"):
conditions += f" and doc.{date_field} >= %(from_date)s" conditions += f" and {date_field} >= %(from_date)s"
if filters.get("to_date"): if filters.get("to_date"):
conditions += f" and doc.{date_field} <= %(to_date)s" conditions += f" and {date_field} <= %(to_date)s"
return conditions return conditions