From f4779df9b3a65d80754ded9d0f420dc1c76766c6 Mon Sep 17 00:00:00 2001 From: Bhavesh Maheshwari <34086262+bhavesh95863@users.noreply.github.com> Date: Thu, 3 Nov 2022 10:59:20 +0530 Subject: [PATCH 1/2] fix: mysql syntax issue (cherry picked from commit 4d9bbd4c9c06811086e0ae4e2c16763fe7d171a3) --- erpnext/stock/doctype/pick_list/pick_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py index 9c1c7e5679..8bad9bc272 100644 --- a/erpnext/stock/doctype/pick_list/pick_list.py +++ b/erpnext/stock/doctype/pick_list/pick_list.py @@ -699,7 +699,8 @@ def get_pending_work_orders(doctype, txt, searchfield, start, page_length, filte AND `company` = %(company)s AND `name` like %(txt)s ORDER BY - (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end) name + (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end), + name LIMIT %(start)s, %(page_length)s""", { From 3ca9d53d1b91b11ea262a1d3ddc9d68335c1bc06 Mon Sep 17 00:00:00 2001 From: Sagar Sharma Date: Sat, 5 Nov 2022 10:28:06 +0530 Subject: [PATCH 2/2] refactor: rewrite query in `QB` (cherry picked from commit 2f145f99129503359b9a79c247fe69e30ac1bf8e) --- erpnext/stock/doctype/pick_list/pick_list.py | 44 ++++++++------------ 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py index 8bad9bc272..5de7fedc4e 100644 --- a/erpnext/stock/doctype/pick_list/pick_list.py +++ b/erpnext/stock/doctype/pick_list/pick_list.py @@ -10,6 +10,8 @@ import frappe from frappe import _ from frappe.model.document import Document from frappe.model.mapper import map_child_doc +from frappe.query_builder import Case +from frappe.query_builder.functions import Locate from frappe.utils import cint, floor, flt, today from frappe.utils.nestedset import get_descendants_of @@ -686,32 +688,22 @@ def create_stock_entry(pick_list): @frappe.whitelist() def get_pending_work_orders(doctype, txt, searchfield, start, page_length, filters, as_dict): - return frappe.db.sql( - """ - SELECT - `name`, `company`, `planned_start_date` - FROM - `tabWork Order` - WHERE - `status` not in ('Completed', 'Stopped') - AND `qty` > `material_transferred_for_manufacturing` - AND `docstatus` = 1 - AND `company` = %(company)s - AND `name` like %(txt)s - ORDER BY - (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end), - name - LIMIT - %(start)s, %(page_length)s""", - { - "txt": "%%%s%%" % txt, - "_txt": txt.replace("%", ""), - "start": start, - "page_length": frappe.utils.cint(page_length), - "company": filters.get("company"), - }, - as_dict=as_dict, - ) + wo = frappe.qb.DocType("Work Order") + return ( + frappe.qb.from_(wo) + .select(wo.name, wo.company, wo.planned_start_date) + .where( + (wo.status.notin(["Completed", "Stopped"])) + & (wo.qty > wo.material_transferred_for_manufacturing) + & (wo.docstatus == 1) + & (wo.company == filters.get("company")) + & (wo.name.like("%{0}%".format(txt))) + ) + .orderby(Case().when(Locate(txt, wo.name) > 0, Locate(txt, wo.name)).else_(99999)) + .orderby(wo.name) + .limit(cint(page_length)) + .offset(start) + ).run(as_dict=as_dict) @frappe.whitelist()