blob: b876a2b502961a8d07f21fe3841c56f14d78cdfb [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Saurabh02875592013-07-08 18:45:55 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe.widgets.reportview import get_match_cond
Rushabh Mehta2e7ad892014-03-06 12:28:47 +05307from frappe.model.db_query import DatabaseQuery
Saurabh02875592013-07-08 18:45:55 +05308
9def get_filters_cond(doctype, filters, conditions):
10 if filters:
11 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053012 filters = filters.items()
13 flt = []
14 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053015 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053016 flt.append([doctype, f[0], '!=', f[1][1:]])
17 else:
18 flt.append([doctype, f[0], '=', f[1]])
Anand Doshibd67e872014-04-11 16:51:27 +053019
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053020 query = DatabaseQuery(doctype)
21 query.filters = flt
22 query.conditions = conditions
23 query.build_filter_conditions()
Anand Doshibd67e872014-04-11 16:51:27 +053024
25 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053026 else:
27 cond = ''
28 return cond
29
Saurabh02875592013-07-08 18:45:55 +053030 # searches for active employees
31def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +053032 return frappe.db.sql("""select name, employee_name from `tabEmployee`
33 where status = 'Active'
34 and docstatus < 2
35 and (%(key)s like "%(txt)s"
36 or employee_name like "%(txt)s")
Saurabha29b6922013-07-10 16:06:31 +053037 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +053038 order by
Anand Doshi652bc072014-04-16 15:21:46 +053039 if(locate("%(_txt)s", name), locate("%(_txt)s", name), 99999),
40 if(locate("%(_txt)s", employee_name), locate("%(_txt)s", item_name), 99999),
41 name, employee_name
Anand Doshibd67e872014-04-11 16:51:27 +053042 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +053043 '_txt': txt.replace("%", ""),
Rushabh Mehta45418752014-03-06 11:18:37 +053044 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053045
46 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053047def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053048 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053049 where docstatus < 2
50 and ifnull(status, '') != 'Converted'
51 and (%(key)s like "%(txt)s"
52 or lead_name like "%(txt)s"
53 or company_name like "%(txt)s")
Saurabha29b6922013-07-10 16:06:31 +053054 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +053055 order by
Anand Doshi652bc072014-04-16 15:21:46 +053056 if(locate("%(_txt)s", name), locate("%(_txt)s", name), 99999),
57 if(locate("%(_txt)s", lead_name), locate("%(_txt)s", name), 99999),
58 if(locate("%(_txt)s", company_name), locate("%(_txt)s", name), 99999),
59 name, lead_name
Anand Doshibd67e872014-04-11 16:51:27 +053060 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +053061 '_txt': txt.replace("%", ""),
Rushabh Mehta45418752014-03-06 11:18:37 +053062 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053063
64 # searches for customer
65def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053066 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053067
Saurabh02875592013-07-08 18:45:55 +053068 if cust_master_name == "Customer Name":
69 fields = ["name", "customer_group", "territory"]
70 else:
71 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053072
Anand Doshibd67e872014-04-11 16:51:27 +053073 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053074
Anand Doshibd67e872014-04-11 16:51:27 +053075 return frappe.db.sql("""select %(field)s from `tabCustomer`
76 where docstatus < 2
77 and (%(key)s like "%(txt)s"
78 or customer_name like "%(txt)s")
Saurabha29b6922013-07-10 16:06:31 +053079 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +053080 order by
Anand Doshi652bc072014-04-16 15:21:46 +053081 if(locate("%(_txt)s", name), locate("%(_txt)s", name), 99999),
82 if(locate("%(_txt)s", customer_name), locate("%(_txt)s", name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +053083 name, customer_name
84 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Anand Doshi652bc072014-04-16 15:21:46 +053085 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""),
86 'mcond':get_match_cond(doctype),
Saurabh02875592013-07-08 18:45:55 +053087 'start': start, 'page_len': page_len})
88
89# searches for supplier
90def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053091 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +053092 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +053093 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +053094 else:
Saurabh02875592013-07-08 18:45:55 +053095 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +053096 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053097
Anand Doshibd67e872014-04-11 16:51:27 +053098 return frappe.db.sql("""select %(field)s from `tabSupplier`
99 where docstatus < 2
100 and (%(key)s like "%(txt)s"
101 or supplier_name like "%(txt)s")
Saurabha29b6922013-07-10 16:06:31 +0530102 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +0530103 order by
Anand Doshi652bc072014-04-16 15:21:46 +0530104 if(locate("%(_txt)s", name), locate("%(_txt)s", name), 99999),
105 if(locate("%(_txt)s", supplier_name), locate("%(_txt)s", name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +0530106 name, supplier_name
107 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Anand Doshi652bc072014-04-16 15:21:46 +0530108 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""),
109 'mcond':get_match_cond(doctype), 'start': start,
Saurabh02875592013-07-08 18:45:55 +0530110 'page_len': page_len})
Anand Doshibd67e872014-04-11 16:51:27 +0530111
Nabin Hait9a380ef2013-07-16 17:24:17 +0530112def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530113 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
114 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530115 and account_type in (%s)
Nabin Hait9a380ef2013-07-16 17:24:17 +0530116 and group_or_ledger = 'Ledger'
117 and company = %s
118 and `%s` LIKE %s
Anand Doshibd67e872014-04-11 16:51:27 +0530119 limit %s, %s""" %
120 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
121 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530122 start, page_len]))
123 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530124 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
125 where tabAccount.docstatus!=2 and group_or_ledger = 'Ledger'
126 and company = %s and `%s` LIKE %s limit %s, %s"""
127 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530128 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530129
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530130 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530131
132def item_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530133 from frappe.utils import nowdate
Anand Doshibd67e872014-04-11 16:51:27 +0530134
Saurabh02875592013-07-08 18:45:55 +0530135 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530136
Anand Doshibd67e872014-04-11 16:51:27 +0530137 return frappe.db.sql("""select tabItem.name,
138 if(length(tabItem.item_name) > 40,
139 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530140 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530141 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530142 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530143 where tabItem.docstatus < 2
144 and (ifnull(tabItem.end_of_life, '') = '' or tabItem.end_of_life > %(today)s)
145 and (tabItem.`{key}` LIKE %(txt)s
Anand Doshibd67e872014-04-11 16:51:27 +0530146 or tabItem.item_name LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530147 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530148 order by
149 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
150 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
151 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530152 limit %(start)s, %(page_len)s """.format(key=searchfield,
153 fcond=get_filters_cond(doctype, filters, conditions),
Anand Doshibd67e872014-04-11 16:51:27 +0530154 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530155 {
156 "today": nowdate(),
157 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530158 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530159 "start": start,
160 "page_len": page_len
161 })
Saurabh02875592013-07-08 18:45:55 +0530162
163def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530164 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530165
Anand Doshibd67e872014-04-11 16:51:27 +0530166 return frappe.db.sql("""select tabBOM.name, tabBOM.item
167 from tabBOM
168 where tabBOM.docstatus=1
169 and tabBOM.is_active=1
170 and tabBOM.%(key)s like "%(txt)s"
171 %(fcond)s %(mcond)s
172 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
173 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530174 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530175
Saurabh02875592013-07-08 18:45:55 +0530176def get_project_name(doctype, txt, searchfield, start, page_len, filters):
177 cond = ''
178 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530179 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530180
181 return frappe.db.sql("""select `tabProject`.name from `tabProject`
182 where `tabProject`.status not in ("Completed", "Cancelled")
183 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
184 order by `tabProject`.name asc
185 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530186 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
Anand Doshibd67e872014-04-11 16:51:27 +0530187
Anand Doshi17350b82013-08-01 15:45:23 +0530188def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530189 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530190 from `tabDelivery Note`
191 where `tabDelivery Note`.`%(key)s` like %(txt)s and
Anand Doshi1dadf352013-09-03 16:21:01 +0530192 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshibd67e872014-04-11 16:51:27 +0530193 (ifnull((select sum(qty) from `tabDelivery Note Item` where
Anand Doshi17350b82013-08-01 15:45:23 +0530194 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
Anand Doshibd67e872014-04-11 16:51:27 +0530195 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530196 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530197 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
198 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
199 limit %(start)s, %(page_len)s""" % {
200 "key": searchfield,
201 "fcond": get_filters_cond(doctype, filters, []),
202 "mcond": get_match_cond(doctype),
203 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530204 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
205
206def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530207 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530208
209 if filters.has_key('warehouse'):
Anand Doshibd67e872014-04-11 16:51:27 +0530210 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
211 where item_code = '%(item_code)s'
212 and warehouse = '%(warehouse)s'
213 and batch_no like '%(txt)s'
214 and exists(select * from `tabBatch`
215 where name = sle.batch_no
216 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
217 and docstatus != 2)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530218 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +0530219 group by batch_no having sum(actual_qty) > 0
220 order by batch_no desc
221 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
222 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
223 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530224 'start': start, 'page_len': page_len})
225 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530226 return frappe.db.sql("""select name from tabBatch
227 where docstatus != 2
228 and item = '%(item_code)s'
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530229 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
Anand Doshibd67e872014-04-11 16:51:27 +0530230 and name like '%(txt)s'
231 %(mcond)s
232 order by name desc
233 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
234 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
235 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530236 'page_len': page_len})