blob: 8a990771ba9e8064d5f022f2d2c2a7c079f032c7 [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
Saurabh02875592013-07-08 18:45:55 +0530148def account_query(doctype, txt, searchfield, start, page_len, filters):
149 conditions = []
150 if not filters:
151 filters = {}
152 if not filters.group_or_ledger:
153 filters.group_or_ledger = "Ledger"
154
Nabin Hait9a380ef2013-07-16 17:24:17 +0530155 return webnotes.conn.sql("""
156 select tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit
157 from tabAccount
Saurabha29b6922013-07-10 16:06:31 +0530158 where tabAccount.docstatus!=2
Nabin Hait9a380ef2013-07-16 17:24:17 +0530159 and
Saurabha29b6922013-07-10 16:06:31 +0530160 and tabAccount.%(key)s LIKE "%(txt)s"
161 %(fcond)s %(mcond)s
162 limit %(start)s, %(page_len)s""" % {'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530163 'txt': "%%%s%%" % txt, 'fcond': get_filters_cond(doctype, filters, conditions),
164 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Nabin Hait9a380ef2013-07-16 17:24:17 +0530165
166def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
167 return webnotes.conn.sql("""select name, parent_account, debit_or_credit
168 from tabAccount
169 where tabAccount.docstatus!=2
170 and (account_type in (%s) or
171 (ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) )
172 and group_or_ledger = 'Ledger'
173 and company = %s
174 and `%s` LIKE %s
175 limit %s, %s""" %
176 (", ".join(['%s']*len(filters.get("account_type"))),
177 "%s", "%s", searchfield, "%s", "%s", "%s"),
178 tuple(filters.get("account_type") + [filters.get("debit_or_credit"),
179 filters.get("company"), "%%%s%%" % txt, start, page_len]))
Saurabh02875592013-07-08 18:45:55 +0530180
181def item_query(doctype, txt, searchfield, start, page_len, filters):
182 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530183
Saurabh02875592013-07-08 18:45:55 +0530184 return webnotes.conn.sql("""select tabItem.name,
185 if(length(tabItem.item_name) > 40,
186 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
187 if(length(tabItem.description) > 40, \
188 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530189 from tabItem
190 where tabItem.docstatus!=2
191 and (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00")
192 or `tabItem`.`end_of_life` > NOW())
193 and (tabItem.%(key)s LIKE "%(txt)s"
194 or tabItem.item_name LIKE "%(txt)s")
195 %(fcond)s %(mcond)s
196 limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530197 'fcond': get_filters_cond(doctype, filters, conditions),
198 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
199
200def bom(doctype, txt, searchfield, start, page_len, filters):
201 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530202
Saurabh02875592013-07-08 18:45:55 +0530203 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
204 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530205 where tabBOM.docstatus=1
206 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530207 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530208 %(fcond)s %(mcond)s
209 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
210 'fcond': get_filters_cond(doctype, filters, conditions),
211 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530212
Saurabh02875592013-07-08 18:45:55 +0530213def get_project_name(doctype, txt, searchfield, start, page_len, filters):
214 cond = ''
215 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530216 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530217
Saurabh02875592013-07-08 18:45:55 +0530218 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530219 where `tabProject`.status not in ("Completed", "Cancelled")
220 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
221 order by `tabProject`.name asc
222 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Nabin Haitda46a372013-07-15 17:52:10 +0530223 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})
224
225def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters):
226 return webnotes.conn.sql("""select ref_currency from `tabItem Price`
227 where price_list_name = %s and buying_or_selling = %s
228 and `%s` like %s order by ref_currency asc limit %s, %s""" %
229 ("%s", "%s", searchfield, "%s", "%s", "%s"),
230 (filters["price_list_name"], filters['buying_or_selling'], "%%%s%%" % txt,
231 start, page_len))