refactor: if() to CASE WHEN (#31360)
* refactor: if() to CASE WHEN * fix: remove duplicate order by * fix: remove extraneous table * style: reformat to black spec Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
parent
a0cab761b8
commit
ea28ed1bb3
@ -29,8 +29,8 @@ def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
or employee_name like %(txt)s)
|
or employee_name like %(txt)s)
|
||||||
{fcond} {mcond}
|
{fcond} {mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
|
(case when locate(%(_txt)s, employee_name) > 0 then locate(%(_txt)s, employee_name) else 99999 end),
|
||||||
idx desc,
|
idx desc,
|
||||||
name, employee_name
|
name, employee_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
@ -60,9 +60,9 @@ def lead_query(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
or company_name like %(txt)s)
|
or company_name like %(txt)s)
|
||||||
{mcond}
|
{mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
|
(case when locate(%(_txt)s, lead_name) > 0 then locate(%(_txt)s, lead_name) else 99999 end),
|
||||||
if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
|
(case when locate(%(_txt)s, company_name) > 0 then locate(%(_txt)s, company_name) else 99999 end),
|
||||||
idx desc,
|
idx desc,
|
||||||
name, lead_name
|
name, lead_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
@ -96,8 +96,8 @@ def customer_query(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
and ({scond}) and disabled=0
|
and ({scond}) and disabled=0
|
||||||
{fcond} {mcond}
|
{fcond} {mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
|
(case when locate(%(_txt)s, customer_name) > 0 then locate(%(_txt)s, customer_name) else 99999 end),
|
||||||
idx desc,
|
idx desc,
|
||||||
name, customer_name
|
name, customer_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
@ -130,11 +130,11 @@ def supplier_query(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
where docstatus < 2
|
where docstatus < 2
|
||||||
and ({key} like %(txt)s
|
and ({key} like %(txt)s
|
||||||
or supplier_name like %(txt)s) and disabled=0
|
or supplier_name like %(txt)s) and disabled=0
|
||||||
and (on_hold = 0 or (on_hold = 1 and CURDATE() > release_date))
|
and (on_hold = 0 or (on_hold = 1 and CURRENT_DATE > release_date))
|
||||||
{mcond}
|
{mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
|
(case when locate(%(_txt)s, supplier_name) > 0 then locate(%(_txt)s, supplier_name) else 99999 end),
|
||||||
idx desc,
|
idx desc,
|
||||||
name, supplier_name
|
name, supplier_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
@ -305,15 +305,15 @@ def bom(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
|
|
||||||
return frappe.db.sql(
|
return frappe.db.sql(
|
||||||
"""select {fields}
|
"""select {fields}
|
||||||
from tabBOM
|
from `tabBOM`
|
||||||
where tabBOM.docstatus=1
|
where `tabBOM`.docstatus=1
|
||||||
and tabBOM.is_active=1
|
and `tabBOM`.is_active=1
|
||||||
and tabBOM.`{key}` like %(txt)s
|
and `tabBOM`.`{key}` like %(txt)s
|
||||||
{fcond} {mcond}
|
{fcond} {mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
idx desc, name
|
idx desc, name
|
||||||
limit %(start)s, %(page_len)s """.format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
fields=", ".join(fields),
|
fields=", ".join(fields),
|
||||||
fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
|
fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
|
||||||
mcond=get_match_cond(doctype).replace("%", "%%"),
|
mcond=get_match_cond(doctype).replace("%", "%%"),
|
||||||
@ -348,8 +348,8 @@ def get_project_name(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
`tabProject`.status not in ('Completed', 'Cancelled')
|
`tabProject`.status not in ('Completed', 'Cancelled')
|
||||||
and {cond} {scond} {match_cond}
|
and {cond} {scond} {match_cond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, `tabProject`.name) > 0 then locate(%(_txt)s, `tabProject`.name) else 99999 end),
|
||||||
idx desc,
|
`tabProject`.idx desc,
|
||||||
`tabProject`.name asc
|
`tabProject`.name asc
|
||||||
limit {page_len} offset {start}""".format(
|
limit {page_len} offset {start}""".format(
|
||||||
fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]),
|
fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]),
|
||||||
|
|||||||
@ -443,7 +443,7 @@ class StatusUpdater(Document):
|
|||||||
"""update `tab%(target_parent_dt)s`
|
"""update `tab%(target_parent_dt)s`
|
||||||
set %(target_parent_field)s = round(
|
set %(target_parent_field)s = round(
|
||||||
ifnull((select
|
ifnull((select
|
||||||
ifnull(sum(if(abs(%(target_ref_field)s) > abs(%(target_field)s), abs(%(target_field)s), abs(%(target_ref_field)s))), 0)
|
ifnull(sum(case when abs(%(target_ref_field)s) > abs(%(target_field)s) then abs(%(target_field)s) else abs(%(target_ref_field)s) end), 0)
|
||||||
/ sum(abs(%(target_ref_field)s)) * 100
|
/ sum(abs(%(target_ref_field)s)) * 100
|
||||||
from `tab%(target_dt)s` where parent='%(name)s' having sum(abs(%(target_ref_field)s)) > 0), 0), 6)
|
from `tab%(target_dt)s` where parent='%(name)s' having sum(abs(%(target_ref_field)s)) > 0), 0), 6)
|
||||||
%(update_modified)s
|
%(update_modified)s
|
||||||
@ -455,9 +455,9 @@ class StatusUpdater(Document):
|
|||||||
if args.get("status_field"):
|
if args.get("status_field"):
|
||||||
frappe.db.sql(
|
frappe.db.sql(
|
||||||
"""update `tab%(target_parent_dt)s`
|
"""update `tab%(target_parent_dt)s`
|
||||||
set %(status_field)s = if(%(target_parent_field)s<0.001,
|
set %(status_field)s = (case when %(target_parent_field)s<0.001 then 'Not %(keyword)s'
|
||||||
'Not %(keyword)s', if(%(target_parent_field)s>=99.999999,
|
else case when %(target_parent_field)s>=99.999999 then 'Fully %(keyword)s'
|
||||||
'Fully %(keyword)s', 'Partly %(keyword)s'))
|
else 'Partly %(keyword)s' end end)
|
||||||
where name='%(name)s'"""
|
where name='%(name)s'"""
|
||||||
% args
|
% args
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1035,8 +1035,8 @@ def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
{emp_cond}
|
{emp_cond}
|
||||||
{fcond} {mcond}
|
{fcond} {mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
|
(case when locate(%(_txt)s, employee_name) > 0 then locate(%(_txt)s, employee_name) else 99999 end),
|
||||||
idx desc,
|
idx desc,
|
||||||
name, employee_name
|
name, employee_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
|
|||||||
@ -387,8 +387,8 @@ def get_users_for_project(doctype, txt, searchfield, start, page_len, filters):
|
|||||||
or full_name like %(txt)s)
|
or full_name like %(txt)s)
|
||||||
{fcond} {mcond}
|
{fcond} {mcond}
|
||||||
order by
|
order by
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
|
||||||
if(locate(%(_txt)s, full_name), locate(%(_txt)s, full_name), 99999),
|
(case when locate(%(_txt)s, full_name) > 0 then locate(%(_txt)s, full_name) else 99999 end)
|
||||||
idx desc,
|
idx desc,
|
||||||
name, full_name
|
name, full_name
|
||||||
limit %(page_len)s offset %(start)s""".format(
|
limit %(page_len)s offset %(start)s""".format(
|
||||||
|
|||||||
@ -699,7 +699,7 @@ def get_pending_work_orders(doctype, txt, searchfield, start, page_length, filte
|
|||||||
AND `company` = %(company)s
|
AND `company` = %(company)s
|
||||||
AND `name` like %(txt)s
|
AND `name` like %(txt)s
|
||||||
ORDER BY
|
ORDER BY
|
||||||
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), name
|
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end) name
|
||||||
LIMIT
|
LIMIT
|
||||||
%(start)s, %(page_length)s""",
|
%(start)s, %(page_length)s""",
|
||||||
{
|
{
|
||||||
|
|||||||
@ -499,7 +499,7 @@ def add_additional_uom_columns(columns, result, include_uom, conversion_factors)
|
|||||||
|
|
||||||
def get_incoming_outgoing_rate_for_cancel(item_code, voucher_type, voucher_no, voucher_detail_no):
|
def get_incoming_outgoing_rate_for_cancel(item_code, voucher_type, voucher_no, voucher_detail_no):
|
||||||
outgoing_rate = frappe.db.sql(
|
outgoing_rate = frappe.db.sql(
|
||||||
"""SELECT abs(stock_value_difference / actual_qty)
|
"""SELECT CASE WHEN actual_qty = 0 THEN 0 ELSE abs(stock_value_difference / actual_qty) END
|
||||||
FROM `tabStock Ledger Entry`
|
FROM `tabStock Ledger Entry`
|
||||||
WHERE voucher_type = %s and voucher_no = %s
|
WHERE voucher_type = %s and voucher_no = %s
|
||||||
and item_code = %s and voucher_detail_no = %s
|
and item_code = %s and voucher_detail_no = %s
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user