blob: 6bc9ea3dcdef88b656016096b838dff7825f7414 [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
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 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
198 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
199 '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")
209 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
210 order by `tabProject`.name asc
211 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530212 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
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):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530233 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530234
235 if filters.has_key('warehouse'):
Anand Doshibd67e872014-04-11 16:51:27 +0530236 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
237 where item_code = '%(item_code)s'
238 and warehouse = '%(warehouse)s'
239 and batch_no like '%(txt)s'
240 and exists(select * from `tabBatch`
241 where name = sle.batch_no
242 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
243 and docstatus != 2)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530244 %(mcond)s
Anand Doshibd67e872014-04-11 16:51:27 +0530245 group by batch_no having sum(actual_qty) > 0
246 order by batch_no desc
247 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
248 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
249 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530250 'start': start, 'page_len': page_len})
251 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530252 return frappe.db.sql("""select name from tabBatch
253 where docstatus != 2
254 and item = '%(item_code)s'
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530255 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
Anand Doshibd67e872014-04-11 16:51:27 +0530256 and name like '%(txt)s'
257 %(mcond)s
258 order by name desc
259 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
260 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
261 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530262 'page_len': page_len})
Nabin Haitea4aa042014-05-28 12:56:28 +0530263
264def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530265 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530266
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530267 if isinstance(filters, dict):
268 for key, val in filters.items():
269 if isinstance(val, (list, tuple)):
270 filter_list.append([doctype, key, val[0], val[1]])
271 else:
272 filter_list.append([doctype, key, "=", val])
273
274 if "group_or_ledger" not in [d[1] for d in filter_list]:
275 filter_list.append(["Account", "group_or_ledger", "=", "Ledger"])
276
277 if searchfield and txt:
278 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
279
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530280 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530281 fields = ["name", "parent_account"],
282 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530283