blob: d5555324191f6afa260e935e76e3557a029cc14a [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
Nabin Hait23649c62014-05-07 19:18:15 +053023 query.build_filter_conditions(flt, 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
Anand Doshi48d3b542014-07-09 13:15:03 +053035 and ({key} like %(txt)s
36 or employee_name like %(txt)s)
37 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053038 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053039 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
40 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053041 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053042 limit %(start)s, %(page_len)s""".format(**{
43 'key': searchfield,
44 'mcond': get_match_cond(doctype)
45 }), {
46 'txt': "%%%s%%" % txt,
47 '_txt': txt.replace("%", ""),
48 'start': start,
49 'page_len': page_len
50 })
Saurabh02875592013-07-08 18:45:55 +053051
52 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053053def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053054 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053055 where docstatus < 2
56 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053057 and ({key} like %(txt)s
58 or lead_name like %(txt)s
59 or company_name like %(txt)s)
60 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053061 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053062 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
63 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
64 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053065 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053066 limit %(start)s, %(page_len)s""".format(**{
67 'key': searchfield,
68 'mcond':get_match_cond(doctype)
69 }), {
70 'txt': "%%%s%%" % txt,
71 '_txt': txt.replace("%", ""),
72 'start': start,
73 'page_len': page_len
74 })
Saurabh02875592013-07-08 18:45:55 +053075
76 # searches for customer
77def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053078 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053079
Saurabh02875592013-07-08 18:45:55 +053080 if cust_master_name == "Customer Name":
81 fields = ["name", "customer_group", "territory"]
82 else:
83 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053084
Anand Doshibd67e872014-04-11 16:51:27 +053085 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053086
Anand Doshi48d3b542014-07-09 13:15:03 +053087 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053088 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053089 and ({key} like %(txt)s
90 or customer_name like %(txt)s)
91 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053092 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053093 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
94 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +053095 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053096 limit %(start)s, %(page_len)s""".format(**{
97 "fields": fields,
98 "key": searchfield,
99 "mcond": get_match_cond(doctype)
100 }), {
101 'txt': "%%%s%%" % txt,
102 '_txt': txt.replace("%", ""),
103 'start': start,
104 'page_len': page_len
105 })
Saurabh02875592013-07-08 18:45:55 +0530106
107# searches for supplier
108def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530109 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530110 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530111 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530112 else:
Saurabh02875592013-07-08 18:45:55 +0530113 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530114 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530115
Anand Doshi48d3b542014-07-09 13:15:03 +0530116 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530117 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530118 and ({key} like %(txt)s
119 or supplier_name like %(txt)s)
120 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530121 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530122 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
123 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +0530124 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530125 limit %(start)s, %(page_len)s """.format(**{
126 'field': fields,
127 'key': searchfield,
128 'mcond':get_match_cond(doctype)
129 }), {
130 'txt': "%%%s%%" % txt,
131 '_txt': txt.replace("%", ""),
132 'start': start,
133 'page_len': page_len
134 })
Anand Doshibd67e872014-04-11 16:51:27 +0530135
Nabin Hait9a380ef2013-07-16 17:24:17 +0530136def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530137 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
138 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530139 and account_type in (%s)
Nabin Hait9a380ef2013-07-16 17:24:17 +0530140 and group_or_ledger = 'Ledger'
141 and company = %s
142 and `%s` LIKE %s
Anand Doshibd67e872014-04-11 16:51:27 +0530143 limit %s, %s""" %
144 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
145 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530146 start, page_len]))
147 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530148 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
149 where tabAccount.docstatus!=2 and group_or_ledger = 'Ledger'
150 and company = %s and `%s` LIKE %s limit %s, %s"""
151 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530152 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530153
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530154 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530155
156def item_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530157 from frappe.utils import nowdate
Anand Doshibd67e872014-04-11 16:51:27 +0530158
Saurabh02875592013-07-08 18:45:55 +0530159 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530160
Anand Doshibd67e872014-04-11 16:51:27 +0530161 return frappe.db.sql("""select tabItem.name,
162 if(length(tabItem.item_name) > 40,
163 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530164 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530165 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530166 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530167 where tabItem.docstatus < 2
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530168 and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
Anand Doshi22c0d782013-11-04 16:23:04 +0530169 and (tabItem.`{key}` LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530170 or tabItem.item_name LIKE %(txt)s
171 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530172 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530173 order by
174 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
175 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
176 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530177 limit %(start)s, %(page_len)s """.format(key=searchfield,
178 fcond=get_filters_cond(doctype, filters, conditions),
Anand Doshibd67e872014-04-11 16:51:27 +0530179 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530180 {
181 "today": nowdate(),
182 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530183 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530184 "start": start,
185 "page_len": page_len
186 })
Saurabh02875592013-07-08 18:45:55 +0530187
188def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530189 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530190
Anand Doshibd67e872014-04-11 16:51:27 +0530191 return frappe.db.sql("""select tabBOM.name, tabBOM.item
192 from tabBOM
193 where tabBOM.docstatus=1
194 and tabBOM.is_active=1
195 and tabBOM.%(key)s like "%(txt)s"
196 %(fcond)s %(mcond)s
197 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
198 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530199 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530200
Saurabh02875592013-07-08 18:45:55 +0530201def get_project_name(doctype, txt, searchfield, start, page_len, filters):
202 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530203 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530204 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530205
206 return frappe.db.sql("""select `tabProject`.name from `tabProject`
207 where `tabProject`.status not in ("Completed", "Cancelled")
208 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
209 order by `tabProject`.name asc
210 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530211 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
Anand Doshibd67e872014-04-11 16:51:27 +0530212
Anand Doshi17350b82013-08-01 15:45:23 +0530213def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530214 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530215 from `tabDelivery Note`
216 where `tabDelivery Note`.`%(key)s` like %(txt)s and
Anand Doshi1dadf352013-09-03 16:21:01 +0530217 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshibd67e872014-04-11 16:51:27 +0530218 (ifnull((select sum(qty) from `tabDelivery Note Item` where
Anand Doshi17350b82013-08-01 15:45:23 +0530219 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
Anand Doshibd67e872014-04-11 16:51:27 +0530220 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530221 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530222 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
223 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
224 limit %(start)s, %(page_len)s""" % {
225 "key": searchfield,
226 "fcond": get_filters_cond(doctype, filters, []),
227 "mcond": get_match_cond(doctype),
228 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530229 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
230
231def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530232 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530233
234 if filters.has_key('warehouse'):
Anand Doshibd67e872014-04-11 16:51:27 +0530235 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
236 where item_code = '%(item_code)s'
237 and warehouse = '%(warehouse)s'
238 and batch_no like '%(txt)s'
239 and exists(select * from `tabBatch`
240 where name = sle.batch_no
241 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
242 and docstatus != 2)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530243 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +0530244 group by batch_no having sum(actual_qty) > 0
245 order by batch_no desc
246 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
247 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
248 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530249 'start': start, 'page_len': page_len})
250 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530251 return frappe.db.sql("""select name from tabBatch
252 where docstatus != 2
253 and item = '%(item_code)s'
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530254 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
Anand Doshibd67e872014-04-11 16:51:27 +0530255 and name like '%(txt)s'
256 %(mcond)s
257 order by name desc
258 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
259 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
260 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530261 'page_len': page_len})
Nabin Haitea4aa042014-05-28 12:56:28 +0530262
263def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530264 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530265
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530266 if isinstance(filters, dict):
267 for key, val in filters.items():
268 if isinstance(val, (list, tuple)):
269 filter_list.append([doctype, key, val[0], val[1]])
270 else:
271 filter_list.append([doctype, key, "=", val])
272
273 if "group_or_ledger" not in [d[1] for d in filter_list]:
274 filter_list.append(["Account", "group_or_ledger", "=", "Ledger"])
275
276 if searchfield and txt:
277 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
278
279 return frappe.widgets.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530280 fields = ["name", "parent_account"],
281 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530282