blob: d5f8dcb94f28e5d3af5bd49eb1a3361668a4cd1c [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
19
20def get_filters_cond(doctype, filters, conditions):
21 if filters:
22 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053023 filters = filters.items()
24 flt = []
25 for f in filters:
26 if f[1][0] == '!':
27 flt.append([doctype, f[0], '!=', f[1][1:]])
28 else:
29 flt.append([doctype, f[0], '=', f[1]])
Saurabh02875592013-07-08 18:45:55 +053030
31 from webnotes.widgets.reportview import build_filter_conditions
Saurabh2deca5f2013-07-10 14:31:29 +053032 build_filter_conditions(flt, conditions)
Saurabh02875592013-07-08 18:45:55 +053033 cond = ' and ' + ' and '.join(conditions)
34 else:
35 cond = ''
36 return cond
37
38def get_match_cond(doctype, searchfield = 'name'):
39 meta = webnotes.get_doctype(doctype)
40 from webnotes.widgets.search import get_std_fields_list
41 fields = get_std_fields_list(meta, searchfield)
42
43 from webnotes.widgets.reportview import build_match_conditions
44 cond = build_match_conditions(doctype, fields)
Saurabhf52dc072013-07-10 13:07:49 +053045
Saurabh02875592013-07-08 18:45:55 +053046 if cond:
47 cond = ' and ' + cond
48 else:
49 cond = ''
50 return cond
51
52 # searches for enabled profiles
53def profile_query(doctype, txt, searchfield, start, page_len, filters):
54 return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
Saurabha29b6922013-07-10 16:06:31 +053055 from `tabProfile`
56 where ifnull(enabled, 0)=1
57 and docstatus < 2
58 and name not in ('Administrator', 'Guest')
59 and (%(key)s like "%(txt)s"
60 or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s")
61 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053062 order by
63 case when name like "%(txt)s" then 0 else 1 end,
64 case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
65 then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053066 name asc
67 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053068 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
69
70 # searches for active employees
71def employee_query(doctype, txt, searchfield, start, page_len, filters):
72 return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053073 where status = 'Active'
74 and docstatus < 2
75 and (%(key)s like "%(txt)s"
76 or employee_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 employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053081 name
82 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053083 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
84
85 # searches for leads which are not converted
86def lead_query(doctype, txt, searchfield, start, page_len, filters):
87 return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053088 where docstatus < 2
89 and ifnull(status, '') != 'Converted'
90 and (%(key)s like "%(txt)s"
91 or lead_name like "%(txt)s"
92 or company_name like "%(txt)s")
93 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053094 order by
95 case when name like "%(txt)s" then 0 else 1 end,
96 case when lead_name like "%(txt)s" then 0 else 1 end,
97 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053098 lead_name asc
99 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530100 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
101
102 # searches for customer
103def customer_query(doctype, txt, searchfield, start, page_len, filters):
104 cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +0530105
Saurabh02875592013-07-08 18:45:55 +0530106 if cust_master_name == "Customer Name":
107 fields = ["name", "customer_group", "territory"]
108 else:
109 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +0530110
Saurabh02875592013-07-08 18:45:55 +0530111 fields = ", ".join(fields)
112
Saurabha29b6922013-07-10 16:06:31 +0530113 return webnotes.conn.sql("""select %(field)s from `tabCustomer`
114 where docstatus < 2
115 and (%(key)s like "%(txt)s"
116 or customer_name like "%(txt)s")
117 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530118 order by
119 case when name like "%(txt)s" then 0 else 1 end,
120 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530121 name, customer_name
122 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530123 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
124 'start': start, 'page_len': page_len})
125
126# searches for supplier
127def supplier_query(doctype, txt, searchfield, start, page_len, filters):
128 supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
129 if supp_master_name == "Supplier Name":
130 fields = ["name", "supplier_type"]
131 else:
132 fields = ["name", "supplier_name", "supplier_type"]
133 fields = ", ".join(fields)
134
Saurabha29b6922013-07-10 16:06:31 +0530135 return webnotes.conn.sql("""select %(field)s from `tabSupplier`
136 where docstatus < 2
137 and (%(key)s like "%(txt)s"
138 or supplier_name like "%(txt)s")
139 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530140 order by
141 case when name like "%(txt)s" then 0 else 1 end,
142 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530143 name, supplier_name
144 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530145 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
146 'page_len': page_len})
147
148def item_std(doctype, txt, searchfield, start, page_len, filters):
149 return webnotes.conn.sql("""select tabItem.name,
150 if(length(tabItem.item_name) > 40,
151 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
152 if(length(tabItem.description) > 40,
153 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
154 FROM tabItem
155 WHERE tabItem.docstatus!=2
Saurabha29b6922013-07-10 16:06:31 +0530156 and tabItem.%(key)s LIKE "%(txt)s"
157 %(mcond)s
158 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
159 'mcond':get_match_cond(doctype, searchfield), 'start': start,
160 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530161
162def account_query(doctype, txt, searchfield, start, page_len, filters):
163 conditions = []
164 if not filters:
165 filters = {}
166 if not filters.group_or_ledger:
167 filters.group_or_ledger = "Ledger"
168
169 return webnotes.conn.sql("""select tabAccount.name, tabAccount.parent_account,
170 tabAccount.debit_or_credit from tabAccount
Saurabha29b6922013-07-10 16:06:31 +0530171 where tabAccount.docstatus!=2
172 and tabAccount.%(key)s LIKE "%(txt)s"
173 %(fcond)s %(mcond)s
174 limit %(start)s, %(page_len)s""" % {'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530175 'txt': "%%%s%%" % txt, 'fcond': get_filters_cond(doctype, filters, conditions),
176 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
177
178def item_query(doctype, txt, searchfield, start, page_len, filters):
179 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530180
Saurabh02875592013-07-08 18:45:55 +0530181 return webnotes.conn.sql("""select tabItem.name,
182 if(length(tabItem.item_name) > 40,
183 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
184 if(length(tabItem.description) > 40, \
185 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530186 from tabItem
187 where tabItem.docstatus!=2
188 and (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00")
189 or `tabItem`.`end_of_life` > NOW())
190 and (tabItem.%(key)s LIKE "%(txt)s"
191 or tabItem.item_name LIKE "%(txt)s")
192 %(fcond)s %(mcond)s
193 limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530194 'fcond': get_filters_cond(doctype, filters, conditions),
195 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
196
197def bom(doctype, txt, searchfield, start, page_len, filters):
198 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530199
Saurabh02875592013-07-08 18:45:55 +0530200 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
201 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530202 where tabBOM.docstatus=1
203 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530204 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530205 %(fcond)s %(mcond)s
206 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
207 'fcond': get_filters_cond(doctype, filters, conditions),
208 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530209
Saurabh02875592013-07-08 18:45:55 +0530210def get_project_name(doctype, txt, searchfield, start, page_len, filters):
211 cond = ''
212 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530213 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530214
Saurabh02875592013-07-08 18:45:55 +0530215 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530216 where `tabProject`.status not in ("Completed", "Cancelled")
217 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
218 order by `tabProject`.name asc
219 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Nabin Haitda46a372013-07-15 17:52:10 +0530220 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})
221
222def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters):
223 return webnotes.conn.sql("""select ref_currency from `tabItem Price`
224 where price_list_name = %s and buying_or_selling = %s
225 and `%s` like %s order by ref_currency asc limit %s, %s""" %
226 ("%s", "%s", searchfield, "%s", "%s", "%s"),
227 (filters["price_list_name"], filters['buying_or_selling'], "%%%s%%" % txt,
228 start, page_len))