blob: a6bfa53e6855c7fa09ebb3691bc00fe5ca5b4fe2 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe 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
Rushabh Mehtac0bb4532014-09-09 16:15:35 +05306from frappe.desk.reportview import get_match_cond
Rushabh Mehta2e7ad892014-03-06 12:28:47 +05307from frappe.model.db_query import DatabaseQuery
Nabin Hait2ed71ba2015-03-20 15:06:30 +05308from frappe.utils import nowdate
Saurabh02875592013-07-08 18:45:55 +05309
10def get_filters_cond(doctype, filters, conditions):
11 if filters:
Anand Doshi1fef2fa2015-09-14 18:27:21 +053012 flt = filters
Saurabh02875592013-07-08 18:45:55 +053013 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053014 filters = filters.items()
15 flt = []
16 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053017 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053018 flt.append([doctype, f[0], '!=', f[1][1:]])
19 else:
20 flt.append([doctype, f[0], '=', f[1]])
Anand Doshibd67e872014-04-11 16:51:27 +053021
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053022 query = DatabaseQuery(doctype)
23 query.filters = flt
24 query.conditions = conditions
Nabin Hait23649c62014-05-07 19:18:15 +053025 query.build_filter_conditions(flt, conditions)
Anand Doshibd67e872014-04-11 16:51:27 +053026
27 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053028 else:
29 cond = ''
30 return cond
31
Saurabh02875592013-07-08 18:45:55 +053032 # searches for active employees
33def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +053034 return frappe.db.sql("""select name, employee_name from `tabEmployee`
35 where status = 'Active'
36 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053037 and ({key} like %(txt)s
38 or employee_name like %(txt)s)
39 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053040 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053041 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
42 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053043 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053044 limit %(start)s, %(page_len)s""".format(**{
45 'key': searchfield,
46 'mcond': get_match_cond(doctype)
47 }), {
48 'txt': "%%%s%%" % txt,
49 '_txt': txt.replace("%", ""),
50 'start': start,
51 'page_len': page_len
52 })
Saurabh02875592013-07-08 18:45:55 +053053
54 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053055def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053056 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053057 where docstatus < 2
58 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053059 and ({key} like %(txt)s
60 or lead_name like %(txt)s
61 or company_name like %(txt)s)
62 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053063 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053064 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
65 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
66 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053067 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053068 limit %(start)s, %(page_len)s""".format(**{
69 'key': searchfield,
70 'mcond':get_match_cond(doctype)
71 }), {
72 'txt': "%%%s%%" % txt,
73 '_txt': txt.replace("%", ""),
74 'start': start,
75 'page_len': page_len
76 })
Saurabh02875592013-07-08 18:45:55 +053077
78 # searches for customer
79def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053080 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053081
Saurabh02875592013-07-08 18:45:55 +053082 if cust_master_name == "Customer Name":
83 fields = ["name", "customer_group", "territory"]
84 else:
85 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053086
Anand Doshibd67e872014-04-11 16:51:27 +053087 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053088
Anand Doshi48d3b542014-07-09 13:15:03 +053089 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053090 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053091 and ({key} like %(txt)s
92 or customer_name like %(txt)s)
93 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053094 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053095 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
96 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +053097 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053098 limit %(start)s, %(page_len)s""".format(**{
99 "fields": fields,
100 "key": searchfield,
101 "mcond": get_match_cond(doctype)
102 }), {
103 'txt': "%%%s%%" % txt,
104 '_txt': txt.replace("%", ""),
105 'start': start,
106 'page_len': page_len
107 })
Saurabh02875592013-07-08 18:45:55 +0530108
109# searches for supplier
110def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530111 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530112 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530113 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530114 else:
Saurabh02875592013-07-08 18:45:55 +0530115 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530116 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530117
Anand Doshi48d3b542014-07-09 13:15:03 +0530118 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530119 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530120 and ({key} like %(txt)s
121 or supplier_name like %(txt)s)
122 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530123 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530124 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
125 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +0530126 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530127 limit %(start)s, %(page_len)s """.format(**{
128 'field': fields,
129 'key': searchfield,
130 'mcond':get_match_cond(doctype)
131 }), {
132 'txt': "%%%s%%" % txt,
133 '_txt': txt.replace("%", ""),
134 'start': start,
135 'page_len': page_len
136 })
Anand Doshibd67e872014-04-11 16:51:27 +0530137
Nabin Hait9a380ef2013-07-16 17:24:17 +0530138def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530139 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
140 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530141 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530142 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530143 and company = %s
144 and `%s` LIKE %s
Anand Doshibd67e872014-04-11 16:51:27 +0530145 limit %s, %s""" %
146 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
147 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530148 start, page_len]))
149 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530150 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530151 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530152 and company = %s and `%s` LIKE %s limit %s, %s"""
153 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530154 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530155
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530156 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530157
158def item_query(doctype, txt, searchfield, start, page_len, filters):
159 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 Mehtaef276042014-09-30 17:41:33 +0530168 and ifnull(tabItem.has_variants, 0)=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530169 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 +0530170 and (tabItem.`{key}` LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530171 or tabItem.item_name LIKE %(txt)s
172 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530173 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530174 order by
175 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
176 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
177 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530178 limit %(start)s, %(page_len)s """.format(key=searchfield,
179 fcond=get_filters_cond(doctype, filters, conditions),
Anand Doshibd67e872014-04-11 16:51:27 +0530180 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530181 {
182 "today": nowdate(),
183 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530184 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530185 "start": start,
186 "page_len": page_len
187 })
Saurabh02875592013-07-08 18:45:55 +0530188
189def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530190 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530191
Anand Doshibd67e872014-04-11 16:51:27 +0530192 return frappe.db.sql("""select tabBOM.name, tabBOM.item
193 from tabBOM
194 where tabBOM.docstatus=1
195 and tabBOM.is_active=1
196 and tabBOM.%(key)s like "%(txt)s"
197 %(fcond)s %(mcond)s
Nabin Hait93cdee42015-06-27 12:51:00 +0530198 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % frappe.db.escape(txt),
Anand Doshibd67e872014-04-11 16:51:27 +0530199 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530200 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530201
Saurabh02875592013-07-08 18:45:55 +0530202def get_project_name(doctype, txt, searchfield, start, page_len, filters):
203 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530204 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530205 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530206
207 return frappe.db.sql("""select `tabProject`.name from `tabProject`
208 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530209 and {cond} `tabProject`.name like %s {match_cond}
Anand Doshibd67e872014-04-11 16:51:27 +0530210 order by `tabProject`.name asc
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530211 limit {start}, {page_len}""".format(cond=cond, match_cond=get_match_cond(doctype),
212 start=start, page_len=page_len), "%{0}%".format(txt))
Anand Doshibd67e872014-04-11 16:51:27 +0530213
Anand Doshi17350b82013-08-01 15:45:23 +0530214def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530215 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530216 from `tabDelivery Note`
217 where `tabDelivery Note`.`%(key)s` like %(txt)s and
Anand Doshi1dadf352013-09-03 16:21:01 +0530218 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshibd67e872014-04-11 16:51:27 +0530219 (ifnull((select sum(qty) from `tabDelivery Note Item` where
Anand Doshi17350b82013-08-01 15:45:23 +0530220 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
Anand Doshibd67e872014-04-11 16:51:27 +0530221 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530222 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530223 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
224 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
225 limit %(start)s, %(page_len)s""" % {
226 "key": searchfield,
227 "fcond": get_filters_cond(doctype, filters, []),
228 "mcond": get_match_cond(doctype),
229 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530230 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
231
232def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530233 cond = ""
234 if filters.get("posting_date"):
235 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530236
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530237 batch_nos = None
238 args = {
239 'item_code': filters.get("item_code"),
240 'warehouse': filters.get("warehouse"),
241 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530242 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530243 "start": start,
244 "page_len": page_len
245 }
246
Anand Doshi0dc79f42015-04-06 12:59:34 +0530247 if args.get('warehouse'):
248 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
249 from `tabStock Ledger Entry` sle
250 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
251 where
252 sle.item_code = %(item_code)s
253 and sle.warehouse = %(warehouse)s
254 and sle.batch_no like %(txt)s
255 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530256 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530257 {match_conditions}
258 group by batch_no having sum(sle.actual_qty) > 0
259 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530260 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530261
262 if batch_nos:
263 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530264 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530265 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530266 where item = %(item_code)s
267 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530268 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530269 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530270 {match_conditions}
271 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530272 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530273
274def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530275 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530276
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530277 if isinstance(filters, dict):
278 for key, val in filters.items():
279 if isinstance(val, (list, tuple)):
280 filter_list.append([doctype, key, val[0], val[1]])
281 else:
282 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530283 elif isinstance(filters, list):
284 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530285
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530286 if "is_group" not in [d[1] for d in filter_list]:
287 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530288
289 if searchfield and txt:
290 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
291
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530292 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530293 fields = ["name", "parent_account"],
294 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530295