blob: e397420e3ed455c2a5744c014f795ef34f39e9a5 [file] [log] [blame]
Saurabh02875592013-07-08 18:45:55 +05301 # ERPNext - web based ERP (http://erpnext.com)
2 # Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Saurabha29b6922013-07-10 16:06:31 +053019from webnotes.utils import cstr
Saurabh02875592013-07-08 18:45:55 +053020
21def get_filters_cond(doctype, filters, conditions):
22 if filters:
23 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053024 filters = filters.items()
25 flt = []
26 for f in filters:
27 if f[1][0] == '!':
28 flt.append([doctype, f[0], '!=', f[1][1:]])
29 else:
30 flt.append([doctype, f[0], '=', f[1]])
Saurabh02875592013-07-08 18:45:55 +053031
32 from webnotes.widgets.reportview import build_filter_conditions
Saurabh2deca5f2013-07-10 14:31:29 +053033 build_filter_conditions(flt, conditions)
Saurabh02875592013-07-08 18:45:55 +053034 cond = ' and ' + ' and '.join(conditions)
35 else:
36 cond = ''
37 return cond
38
39def get_match_cond(doctype, searchfield = 'name'):
40 meta = webnotes.get_doctype(doctype)
41 from webnotes.widgets.search import get_std_fields_list
42 fields = get_std_fields_list(meta, searchfield)
43
44 from webnotes.widgets.reportview import build_match_conditions
45 cond = build_match_conditions(doctype, fields)
Saurabhf52dc072013-07-10 13:07:49 +053046
Saurabh02875592013-07-08 18:45:55 +053047 if cond:
48 cond = ' and ' + cond
49 else:
50 cond = ''
51 return cond
52
53 # searches for enabled profiles
54def profile_query(doctype, txt, searchfield, start, page_len, filters):
55 return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
Saurabha29b6922013-07-10 16:06:31 +053056 from `tabProfile`
57 where ifnull(enabled, 0)=1
58 and docstatus < 2
59 and name not in ('Administrator', 'Guest')
60 and (%(key)s like "%(txt)s"
61 or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s")
62 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053063 order by
64 case when name like "%(txt)s" then 0 else 1 end,
65 case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
66 then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053067 name asc
68 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053069 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
70
71 # searches for active employees
72def employee_query(doctype, txt, searchfield, start, page_len, filters):
73 return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053074 where status = 'Active'
75 and docstatus < 2
76 and (%(key)s like "%(txt)s"
77 or employee_name like "%(txt)s")
78 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053079 order by
80 case when name like "%(txt)s" then 0 else 1 end,
81 case when employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053082 name
83 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053084 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
85
86 # searches for leads which are not converted
87def lead_query(doctype, txt, searchfield, start, page_len, filters):
88 return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053089 where docstatus < 2
90 and ifnull(status, '') != 'Converted'
91 and (%(key)s like "%(txt)s"
92 or lead_name like "%(txt)s"
93 or company_name like "%(txt)s")
94 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053095 order by
96 case when name like "%(txt)s" then 0 else 1 end,
97 case when lead_name like "%(txt)s" then 0 else 1 end,
98 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053099 lead_name asc
100 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530101 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
102
103 # searches for customer
104def customer_query(doctype, txt, searchfield, start, page_len, filters):
105 cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +0530106
Saurabh02875592013-07-08 18:45:55 +0530107 if cust_master_name == "Customer Name":
108 fields = ["name", "customer_group", "territory"]
109 else:
110 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +0530111
Saurabh02875592013-07-08 18:45:55 +0530112 fields = ", ".join(fields)
113
Saurabha29b6922013-07-10 16:06:31 +0530114 return webnotes.conn.sql("""select %(field)s from `tabCustomer`
115 where docstatus < 2
116 and (%(key)s like "%(txt)s"
117 or customer_name like "%(txt)s")
118 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530119 order by
120 case when name like "%(txt)s" then 0 else 1 end,
121 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530122 name, customer_name
123 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530124 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
125 'start': start, 'page_len': page_len})
126
127# searches for supplier
128def supplier_query(doctype, txt, searchfield, start, page_len, filters):
129 supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
130 if supp_master_name == "Supplier Name":
131 fields = ["name", "supplier_type"]
132 else:
133 fields = ["name", "supplier_name", "supplier_type"]
134 fields = ", ".join(fields)
135
Saurabha29b6922013-07-10 16:06:31 +0530136 return webnotes.conn.sql("""select %(field)s from `tabSupplier`
137 where docstatus < 2
138 and (%(key)s like "%(txt)s"
139 or supplier_name like "%(txt)s")
140 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530141 order by
142 case when name like "%(txt)s" then 0 else 1 end,
143 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530144 name, supplier_name
145 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530146 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
147 'page_len': page_len})
148
149def item_std(doctype, txt, searchfield, start, page_len, filters):
150 return webnotes.conn.sql("""select tabItem.name,
151 if(length(tabItem.item_name) > 40,
152 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
153 if(length(tabItem.description) > 40,
154 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
155 FROM tabItem
156 WHERE tabItem.docstatus!=2
Saurabha29b6922013-07-10 16:06:31 +0530157 and tabItem.%(key)s LIKE "%(txt)s"
158 %(mcond)s
159 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
160 'mcond':get_match_cond(doctype, searchfield), 'start': start,
161 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530162
163def account_query(doctype, txt, searchfield, start, page_len, filters):
164 conditions = []
165 if not filters:
166 filters = {}
167 if not filters.group_or_ledger:
168 filters.group_or_ledger = "Ledger"
169
170 return webnotes.conn.sql("""select tabAccount.name, tabAccount.parent_account,
171 tabAccount.debit_or_credit from tabAccount
Saurabha29b6922013-07-10 16:06:31 +0530172 where tabAccount.docstatus!=2
173 and tabAccount.%(key)s LIKE "%(txt)s"
174 %(fcond)s %(mcond)s
175 limit %(start)s, %(page_len)s""" % {'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530176 'txt': "%%%s%%" % txt, 'fcond': get_filters_cond(doctype, filters, conditions),
177 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
178
179def item_query(doctype, txt, searchfield, start, page_len, filters):
180 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530181
Saurabh02875592013-07-08 18:45:55 +0530182 return webnotes.conn.sql("""select tabItem.name,
183 if(length(tabItem.item_name) > 40,
184 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
185 if(length(tabItem.description) > 40, \
186 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530187 from tabItem
188 where tabItem.docstatus!=2
189 and (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00")
190 or `tabItem`.`end_of_life` > NOW())
191 and (tabItem.%(key)s LIKE "%(txt)s"
192 or tabItem.item_name LIKE "%(txt)s")
193 %(fcond)s %(mcond)s
194 limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530195 'fcond': get_filters_cond(doctype, filters, conditions),
196 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
197
198def bom(doctype, txt, searchfield, start, page_len, filters):
199 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530200
Saurabh02875592013-07-08 18:45:55 +0530201 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
202 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530203 where tabBOM.docstatus=1
204 and tabBOM.is_active=1
205 and tabBOM.%(key)s like "%s"
206 %(fcond)s %(mcond)s
207 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
208 'fcond': get_filters_cond(doctype, filters, conditions),
209 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530210
Saurabh02875592013-07-08 18:45:55 +0530211def get_project_name(doctype, txt, searchfield, start, page_len, filters):
212 cond = ''
213 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530214 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530215
Saurabh02875592013-07-08 18:45:55 +0530216 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530217 where `tabProject`.status not in ("Completed", "Cancelled")
218 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
219 order by `tabProject`.name asc
220 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
221 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})