blob: ea1c4fe9ebaa987c06db278953fdeae62518f285 [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:
12 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053013 filters = filters.items()
14 flt = []
15 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053016 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053017 flt.append([doctype, f[0], '!=', f[1][1:]])
18 else:
19 flt.append([doctype, f[0], '=', f[1]])
Anand Doshibd67e872014-04-11 16:51:27 +053020
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053021 query = DatabaseQuery(doctype)
22 query.filters = flt
23 query.conditions = conditions
Nabin Hait23649c62014-05-07 19:18:15 +053024 query.build_filter_conditions(flt, conditions)
Anand Doshibd67e872014-04-11 16:51:27 +053025
26 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053027 else:
28 cond = ''
29 return cond
30
Saurabh02875592013-07-08 18:45:55 +053031 # searches for active employees
32def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +053033 return frappe.db.sql("""select name, employee_name from `tabEmployee`
34 where status = 'Active'
35 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053036 and ({key} like %(txt)s
37 or employee_name like %(txt)s)
38 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053039 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053040 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
41 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053042 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053043 limit %(start)s, %(page_len)s""".format(**{
44 'key': searchfield,
45 'mcond': get_match_cond(doctype)
46 }), {
47 'txt': "%%%s%%" % txt,
48 '_txt': txt.replace("%", ""),
49 'start': start,
50 'page_len': page_len
51 })
Saurabh02875592013-07-08 18:45:55 +053052
53 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053054def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053055 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053056 where docstatus < 2
57 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053058 and ({key} like %(txt)s
59 or lead_name like %(txt)s
60 or company_name like %(txt)s)
61 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053062 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053063 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
64 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
65 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053066 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053067 limit %(start)s, %(page_len)s""".format(**{
68 'key': searchfield,
69 'mcond':get_match_cond(doctype)
70 }), {
71 'txt': "%%%s%%" % txt,
72 '_txt': txt.replace("%", ""),
73 'start': start,
74 'page_len': page_len
75 })
Saurabh02875592013-07-08 18:45:55 +053076
77 # searches for customer
78def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053079 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053080
Saurabh02875592013-07-08 18:45:55 +053081 if cust_master_name == "Customer Name":
82 fields = ["name", "customer_group", "territory"]
83 else:
84 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053085
Anand Doshibd67e872014-04-11 16:51:27 +053086 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053087
Anand Doshi48d3b542014-07-09 13:15:03 +053088 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053089 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053090 and ({key} like %(txt)s
91 or customer_name like %(txt)s)
92 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053093 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053094 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
95 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +053096 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053097 limit %(start)s, %(page_len)s""".format(**{
98 "fields": fields,
99 "key": searchfield,
100 "mcond": get_match_cond(doctype)
101 }), {
102 'txt': "%%%s%%" % txt,
103 '_txt': txt.replace("%", ""),
104 'start': start,
105 'page_len': page_len
106 })
Saurabh02875592013-07-08 18:45:55 +0530107
108# searches for supplier
109def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530110 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530111 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530112 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530113 else:
Saurabh02875592013-07-08 18:45:55 +0530114 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530115 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530116
Anand Doshi48d3b542014-07-09 13:15:03 +0530117 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530118 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530119 and ({key} like %(txt)s
120 or supplier_name like %(txt)s)
121 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530122 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530123 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
124 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +0530125 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530126 limit %(start)s, %(page_len)s """.format(**{
127 'field': fields,
128 'key': searchfield,
129 'mcond':get_match_cond(doctype)
130 }), {
131 'txt': "%%%s%%" % txt,
132 '_txt': txt.replace("%", ""),
133 'start': start,
134 'page_len': page_len
135 })
Anand Doshibd67e872014-04-11 16:51:27 +0530136
Nabin Hait9a380ef2013-07-16 17:24:17 +0530137def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530138 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
139 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530140 and account_type in (%s)
Nabin Hait9a380ef2013-07-16 17:24:17 +0530141 and group_or_ledger = 'Ledger'
142 and company = %s
143 and `%s` LIKE %s
Anand Doshibd67e872014-04-11 16:51:27 +0530144 limit %s, %s""" %
145 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
146 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530147 start, page_len]))
148 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530149 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
150 where tabAccount.docstatus!=2 and group_or_ledger = 'Ledger'
151 and company = %s and `%s` LIKE %s limit %s, %s"""
152 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530153 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530154
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530155 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530156
157def item_query(doctype, txt, searchfield, start, page_len, filters):
158 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530159
Anand Doshibd67e872014-04-11 16:51:27 +0530160 return frappe.db.sql("""select tabItem.name,
161 if(length(tabItem.item_name) > 40,
162 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530163 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530164 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530165 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530166 where tabItem.docstatus < 2
Rushabh Mehtaef276042014-09-30 17:41:33 +0530167 and ifnull(tabItem.has_variants, 0)=0
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):
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530232 if not filters.get("posting_date"):
233 filters["posting_date"] = nowdate()
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530234
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530235 batch_nos = None
236 args = {
237 'item_code': filters.get("item_code"),
238 'warehouse': filters.get("warehouse"),
239 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530240 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530241 "start": start,
242 "page_len": page_len
243 }
244
Anand Doshi0dc79f42015-04-06 12:59:34 +0530245 if args.get('warehouse'):
246 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
247 from `tabStock Ledger Entry` sle
248 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
249 where
250 sle.item_code = %(item_code)s
251 and sle.warehouse = %(warehouse)s
252 and sle.batch_no like %(txt)s
253 and batch.docstatus < 2
254 and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)
255 {match_conditions}
256 group by batch_no having sum(sle.actual_qty) > 0
257 order by batch.expiry_date, sle.batch_no desc
258 limit %(start)s, %(page_len)s""".format(match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530259
260 if batch_nos:
261 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530262 else:
Anand Doshi0dc79f42015-04-06 12:59:34 +0530263 return frappe.db.sql("""select name, expiry_date from `tabBatch`
264 where item = %(item_code)s
265 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530266 and docstatus < 2
Anand Doshi0dc79f42015-04-06 12:59:34 +0530267 and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)
268 {match_conditions}
269 order by expiry_date, name desc
270 limit %(start)s, %(page_len)s""".format(match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530271
272def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530273 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530274
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530275 if isinstance(filters, dict):
276 for key, val in filters.items():
277 if isinstance(val, (list, tuple)):
278 filter_list.append([doctype, key, val[0], val[1]])
279 else:
280 filter_list.append([doctype, key, "=", val])
281
282 if "group_or_ledger" not in [d[1] for d in filter_list]:
283 filter_list.append(["Account", "group_or_ledger", "=", "Ledger"])
284
285 if searchfield and txt:
286 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
287
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530288 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530289 fields = ["name", "parent_account"],
290 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530291