blob: 449a4938a9ae609c029901cb90f43c9d64bcb30b [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]])
Saurabh02875592013-07-08 18:45:55 +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()
24
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 Doshie9baaa62014-02-26 12:35:33 +053032 return frappe.db.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053033 where status = 'Active'
34 and docstatus < 2
35 and (%(key)s like "%(txt)s"
36 or employee_name like "%(txt)s")
37 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053038 order by
39 case when name like "%(txt)s" then 0 else 1 end,
40 case when employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053041 name
42 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +053043 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053044
45 # searches for leads which are not converted
46def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053047 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053048 where docstatus < 2
49 and ifnull(status, '') != 'Converted'
50 and (%(key)s like "%(txt)s"
51 or lead_name like "%(txt)s"
52 or company_name like "%(txt)s")
53 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053054 order by
55 case when name like "%(txt)s" then 0 else 1 end,
56 case when lead_name like "%(txt)s" then 0 else 1 end,
57 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053058 lead_name asc
59 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +053060 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053061
62 # searches for customer
63def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053064 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053065
Saurabh02875592013-07-08 18:45:55 +053066 if cust_master_name == "Customer Name":
67 fields = ["name", "customer_group", "territory"]
68 else:
69 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053070
Saurabh02875592013-07-08 18:45:55 +053071 fields = ", ".join(fields)
72
Anand Doshie9baaa62014-02-26 12:35:33 +053073 return frappe.db.sql("""select %(field)s from `tabCustomer`
Saurabha29b6922013-07-10 16:06:31 +053074 where docstatus < 2
75 and (%(key)s like "%(txt)s"
76 or customer_name like "%(txt)s")
77 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053078 order by
79 case when name like "%(txt)s" then 0 else 1 end,
80 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053081 name, customer_name
82 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Rushabh Mehta45418752014-03-06 11:18:37 +053083 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Saurabh02875592013-07-08 18:45:55 +053084 'start': start, 'page_len': page_len})
85
86# searches for supplier
87def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053088 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Saurabh02875592013-07-08 18:45:55 +053089 if supp_master_name == "Supplier Name":
90 fields = ["name", "supplier_type"]
91 else:
92 fields = ["name", "supplier_name", "supplier_type"]
93 fields = ", ".join(fields)
94
Anand Doshie9baaa62014-02-26 12:35:33 +053095 return frappe.db.sql("""select %(field)s from `tabSupplier`
Saurabha29b6922013-07-10 16:06:31 +053096 where docstatus < 2
97 and (%(key)s like "%(txt)s"
98 or supplier_name like "%(txt)s")
99 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530100 order by
101 case when name like "%(txt)s" then 0 else 1 end,
102 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530103 name, supplier_name
104 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Rushabh Mehta45418752014-03-06 11:18:37 +0530105 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype), 'start': start,
Saurabh02875592013-07-08 18:45:55 +0530106 'page_len': page_len})
Nabin Hait9a380ef2013-07-16 17:24:17 +0530107
108def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Nabin Hait49a6f072014-03-03 18:40:24 +0530109 return frappe.db.sql("""select name, parent_account from tabAccount
Nabin Hait9a380ef2013-07-16 17:24:17 +0530110 where tabAccount.docstatus!=2
Nabin Hait49a6f072014-03-03 18:40:24 +0530111 and (account_type in (%s) or root_type = %s)
Nabin Hait9a380ef2013-07-16 17:24:17 +0530112 and group_or_ledger = 'Ledger'
113 and company = %s
114 and `%s` LIKE %s
115 limit %s, %s""" %
116 (", ".join(['%s']*len(filters.get("account_type"))),
117 "%s", "%s", searchfield, "%s", "%s", "%s"),
Nabin Hait49a6f072014-03-03 18:40:24 +0530118 tuple(filters.get("account_type") + [filters.get("root_type"),
Nabin Hait9a380ef2013-07-16 17:24:17 +0530119 filters.get("company"), "%%%s%%" % txt, start, page_len]))
Saurabh02875592013-07-08 18:45:55 +0530120
121def item_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530122 from frappe.utils import nowdate
Anand Doshi22c0d782013-11-04 16:23:04 +0530123
Saurabh02875592013-07-08 18:45:55 +0530124 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530125
Anand Doshie9baaa62014-02-26 12:35:33 +0530126 return frappe.db.sql("""select tabItem.name,
Saurabh02875592013-07-08 18:45:55 +0530127 if(length(tabItem.item_name) > 40,
128 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
129 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530130 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530131 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530132 where tabItem.docstatus < 2
133 and (ifnull(tabItem.end_of_life, '') = '' or tabItem.end_of_life > %(today)s)
134 and (tabItem.`{key}` LIKE %(txt)s
135 or tabItem.item_name LIKE %(txt)s)
136 {fcond} {mcond}
137 limit %(start)s, %(page_len)s """.format(key=searchfield,
138 fcond=get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530139 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530140 {
141 "today": nowdate(),
142 "txt": "%%%s%%" % txt,
143 "start": start,
144 "page_len": page_len
145 })
Saurabh02875592013-07-08 18:45:55 +0530146
147def bom(doctype, txt, searchfield, start, page_len, filters):
148 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530149
Anand Doshie9baaa62014-02-26 12:35:33 +0530150 return frappe.db.sql("""select tabBOM.name, tabBOM.item
Saurabh02875592013-07-08 18:45:55 +0530151 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530152 where tabBOM.docstatus=1
153 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530154 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530155 %(fcond)s %(mcond)s
156 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
157 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530158 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530159
Saurabh02875592013-07-08 18:45:55 +0530160def get_project_name(doctype, txt, searchfield, start, page_len, filters):
161 cond = ''
162 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530163 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530164
Anand Doshie9baaa62014-02-26 12:35:33 +0530165 return frappe.db.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530166 where `tabProject`.status not in ("Completed", "Cancelled")
167 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
168 order by `tabProject`.name asc
169 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530170 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
Anand Doshi17350b82013-08-01 15:45:23 +0530171
172def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530173 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshi17350b82013-08-01 15:45:23 +0530174 from `tabDelivery Note`
Anand Doshi1dadf352013-09-03 16:21:01 +0530175 where `tabDelivery Note`.`%(key)s` like %(txt)s and
176 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshi17350b82013-08-01 15:45:23 +0530177 (ifnull((select sum(qty) from `tabDelivery Note Item` where
178 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
179 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530180 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530181 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
182 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
183 limit %(start)s, %(page_len)s""" % {
184 "key": searchfield,
185 "fcond": get_filters_cond(doctype, filters, []),
186 "mcond": get_match_cond(doctype),
187 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530188 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
189
190def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530191 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530192
193 if filters.has_key('warehouse'):
Anand Doshie9baaa62014-02-26 12:35:33 +0530194 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530195 where item_code = '%(item_code)s'
196 and warehouse = '%(warehouse)s'
197 and batch_no like '%(txt)s'
198 and exists(select * from `tabBatch`
199 where name = sle.batch_no
200 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
201 and docstatus != 2)
202 %(mcond)s
203 group by batch_no having sum(actual_qty) > 0
204 order by batch_no desc
205 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
206 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
Rushabh Mehta45418752014-03-06 11:18:37 +0530207 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530208 'start': start, 'page_len': page_len})
209 else:
Anand Doshie9baaa62014-02-26 12:35:33 +0530210 return frappe.db.sql("""select name from tabBatch
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530211 where docstatus != 2
212 and item = '%(item_code)s'
213 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
214 and name like '%(txt)s'
215 %(mcond)s
216 order by name desc
217 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
218 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530219 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530220 'page_len': page_len})