blob: caa2f6f0650c111885c6f7e6a1f6f5108db1d015 [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'):
Saurabh02875592013-07-08 18:45:55 +053039 from webnotes.widgets.reportview import build_match_conditions
Anand Doshi7180eb62013-07-30 14:40:44 +053040 cond = build_match_conditions(doctype)
Saurabhf52dc072013-07-10 13:07:49 +053041
Saurabh02875592013-07-08 18:45:55 +053042 if cond:
43 cond = ' and ' + cond
44 else:
45 cond = ''
46 return cond
47
48 # searches for enabled profiles
49def profile_query(doctype, txt, searchfield, start, page_len, filters):
50 return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
Saurabha29b6922013-07-10 16:06:31 +053051 from `tabProfile`
52 where ifnull(enabled, 0)=1
53 and docstatus < 2
54 and name not in ('Administrator', 'Guest')
55 and (%(key)s like "%(txt)s"
56 or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s")
57 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053058 order by
59 case when name like "%(txt)s" then 0 else 1 end,
60 case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
61 then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053062 name asc
63 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053064 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
65
66 # searches for active employees
67def employee_query(doctype, txt, searchfield, start, page_len, filters):
68 return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053069 where status = 'Active'
70 and docstatus < 2
71 and (%(key)s like "%(txt)s"
72 or employee_name like "%(txt)s")
73 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053074 order by
75 case when name like "%(txt)s" then 0 else 1 end,
76 case when employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053077 name
78 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053079 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
80
81 # searches for leads which are not converted
82def lead_query(doctype, txt, searchfield, start, page_len, filters):
83 return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053084 where docstatus < 2
85 and ifnull(status, '') != 'Converted'
86 and (%(key)s like "%(txt)s"
87 or lead_name like "%(txt)s"
88 or company_name like "%(txt)s")
89 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053090 order by
91 case when name like "%(txt)s" then 0 else 1 end,
92 case when lead_name like "%(txt)s" then 0 else 1 end,
93 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053094 lead_name asc
95 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053096 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
97
98 # searches for customer
99def customer_query(doctype, txt, searchfield, start, page_len, filters):
100 cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +0530101
Saurabh02875592013-07-08 18:45:55 +0530102 if cust_master_name == "Customer Name":
103 fields = ["name", "customer_group", "territory"]
104 else:
105 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +0530106
Saurabh02875592013-07-08 18:45:55 +0530107 fields = ", ".join(fields)
108
Saurabha29b6922013-07-10 16:06:31 +0530109 return webnotes.conn.sql("""select %(field)s from `tabCustomer`
110 where docstatus < 2
111 and (%(key)s like "%(txt)s"
112 or customer_name like "%(txt)s")
113 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530114 order by
115 case when name like "%(txt)s" then 0 else 1 end,
116 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530117 name, customer_name
118 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530119 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
120 'start': start, 'page_len': page_len})
121
122# searches for supplier
123def supplier_query(doctype, txt, searchfield, start, page_len, filters):
124 supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
125 if supp_master_name == "Supplier Name":
126 fields = ["name", "supplier_type"]
127 else:
128 fields = ["name", "supplier_name", "supplier_type"]
129 fields = ", ".join(fields)
130
Saurabha29b6922013-07-10 16:06:31 +0530131 return webnotes.conn.sql("""select %(field)s from `tabSupplier`
132 where docstatus < 2
133 and (%(key)s like "%(txt)s"
134 or supplier_name like "%(txt)s")
135 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530136 order by
137 case when name like "%(txt)s" then 0 else 1 end,
138 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530139 name, supplier_name
140 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530141 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
142 'page_len': page_len})
Nabin Hait9a380ef2013-07-16 17:24:17 +0530143
144def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
145 return webnotes.conn.sql("""select name, parent_account, debit_or_credit
146 from tabAccount
147 where tabAccount.docstatus!=2
148 and (account_type in (%s) or
149 (ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) )
150 and group_or_ledger = 'Ledger'
151 and company = %s
152 and `%s` LIKE %s
153 limit %s, %s""" %
154 (", ".join(['%s']*len(filters.get("account_type"))),
155 "%s", "%s", searchfield, "%s", "%s", "%s"),
156 tuple(filters.get("account_type") + [filters.get("debit_or_credit"),
157 filters.get("company"), "%%%s%%" % txt, start, page_len]))
Saurabh02875592013-07-08 18:45:55 +0530158
159def item_query(doctype, txt, searchfield, start, page_len, filters):
160 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530161
Saurabh02875592013-07-08 18:45:55 +0530162 return webnotes.conn.sql("""select tabItem.name,
163 if(length(tabItem.item_name) > 40,
164 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
165 if(length(tabItem.description) > 40, \
166 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530167 from tabItem
Anand Doshi9eb28682013-07-29 13:07:30 +0530168 where tabItem.docstatus<2
Saurabha29b6922013-07-10 16:06:31 +0530169 and (tabItem.%(key)s LIKE "%(txt)s"
170 or tabItem.item_name LIKE "%(txt)s")
171 %(fcond)s %(mcond)s
172 limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530173 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi9eb28682013-07-29 13:07:30 +0530174 'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530175
176def bom(doctype, txt, searchfield, start, page_len, filters):
177 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530178
Saurabh02875592013-07-08 18:45:55 +0530179 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
180 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530181 where tabBOM.docstatus=1
182 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530183 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530184 %(fcond)s %(mcond)s
185 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
186 'fcond': get_filters_cond(doctype, filters, conditions),
187 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530188
Saurabh02875592013-07-08 18:45:55 +0530189def get_project_name(doctype, txt, searchfield, start, page_len, filters):
190 cond = ''
191 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530192 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530193
Saurabh02875592013-07-08 18:45:55 +0530194 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530195 where `tabProject`.status not in ("Completed", "Cancelled")
196 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
197 order by `tabProject`.name asc
198 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Nabin Haitda46a372013-07-15 17:52:10 +0530199 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})
200
201def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters):
202 return webnotes.conn.sql("""select ref_currency from `tabItem Price`
203 where price_list_name = %s and buying_or_selling = %s
204 and `%s` like %s order by ref_currency asc limit %s, %s""" %
205 ("%s", "%s", searchfield, "%s", "%s", "%s"),
206 (filters["price_list_name"], filters['buying_or_selling'], "%%%s%%" % txt,
207 start, page_len))