blob: 896147fb2b1e7db4fdccd10fd64d9241ea829817 [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):
23 filters = map(lambda f: [doctype, f[0], "=", f[1]], filters.items())
24
25 from webnotes.widgets.reportview import build_filter_conditions
26 build_filter_conditions(filters, conditions)
27 cond = ' and ' + ' and '.join(conditions)
28 else:
29 cond = ''
30 return cond
31
32def get_match_cond(doctype, searchfield = 'name'):
33 meta = webnotes.get_doctype(doctype)
34 from webnotes.widgets.search import get_std_fields_list
35 fields = get_std_fields_list(meta, searchfield)
36
37 from webnotes.widgets.reportview import build_match_conditions
38 cond = build_match_conditions(doctype, fields)
39 if cond:
40 cond = ' and ' + cond
41 else:
42 cond = ''
43 return cond
44
45 # searches for enabled profiles
46def profile_query(doctype, txt, searchfield, start, page_len, filters):
47 return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
48 from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and
49 name not in ('Administrator', 'Guest') and (%(key)s like "%(txt)s" or
50 concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s") %(mcond)s
51 order by
52 case when name like "%(txt)s" then 0 else 1 end,
53 case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
54 then 0 else 1 end,
55 name asc limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
56 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
57
58 # searches for active employees
59def employee_query(doctype, txt, searchfield, start, page_len, filters):
60 return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
61 where status = 'Active' and docstatus < 2 and
62 (%(key)s like "%(txt)s" or employee_name like "%(txt)s") %(mcond)s
63 order by
64 case when name like "%(txt)s" then 0 else 1 end,
65 case when employee_name like "%(txt)s" then 0 else 1 end,
66 name limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
67 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
68
69 # searches for leads which are not converted
70def lead_query(doctype, txt, searchfield, start, page_len, filters):
71 return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
72 where docstatus < 2 and ifnull(status, '') != 'Converted' and
73 (%(key)s like "%(txt)s" or lead_name like "%(txt)s" or company_name like "%(txt)s") %(mcond)s
74 order by
75 case when name like "%(txt)s" then 0 else 1 end,
76 case when lead_name like "%(txt)s" then 0 else 1 end,
77 case when company_name like "%(txt)s" then 0 else 1 end,
78 lead_name asc limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
79 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
80
81 # searches for customer
82def customer_query(doctype, txt, searchfield, start, page_len, filters):
83 cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
84 if cust_master_name == "Customer Name":
85 fields = ["name", "customer_group", "territory"]
86 else:
87 fields = ["name", "customer_name", "customer_group", "territory"]
88 fields = ", ".join(fields)
89
90 return webnotes.conn.sql("""select %(field)s from `tabCustomer` where docstatus < 2 and
91 (%(key)s like "%(txt)s" or customer_name like "%(txt)s") %(mcond)s
92 order by
93 case when name like "%(txt)s" then 0 else 1 end,
94 case when customer_name like "%(txt)s" then 0 else 1 end,
95 name, customer_name limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
96 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
97 'start': start, 'page_len': page_len})
98
99# searches for supplier
100def supplier_query(doctype, txt, searchfield, start, page_len, filters):
101 supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
102 if supp_master_name == "Supplier Name":
103 fields = ["name", "supplier_type"]
104 else:
105 fields = ["name", "supplier_name", "supplier_type"]
106 fields = ", ".join(fields)
107
108 return webnotes.conn.sql("""select %(field)s from `tabSupplier` where docstatus < 2 and \
109 (%(key)s like "%(txt)s" or supplier_name like "%(txt)s") %(mcond)s
110 order by
111 case when name like "%(txt)s" then 0 else 1 end,
112 case when supplier_name like "%(txt)s" then 0 else 1 end,
113 name, supplier_name limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
114 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
115 'page_len': page_len})
116
117def item_std(doctype, txt, searchfield, start, page_len, filters):
118 return webnotes.conn.sql("""select tabItem.name,
119 if(length(tabItem.item_name) > 40,
120 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
121 if(length(tabItem.description) > 40,
122 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
123 FROM tabItem
124 WHERE tabItem.docstatus!=2
125 AND tabItem.%(key)s LIKE "%(txt)s" %(mcond)s limit %(start)s, %(page_len)s """ %
126 {'key': searchfield, 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
127 'start': start, 'page_len': page_len})
128
129def account_query(doctype, txt, searchfield, start, page_len, filters):
130 conditions = []
131 if not filters:
132 filters = {}
133 if not filters.group_or_ledger:
134 filters.group_or_ledger = "Ledger"
135
136 return webnotes.conn.sql("""select tabAccount.name, tabAccount.parent_account,
137 tabAccount.debit_or_credit from tabAccount
138 where tabAccount.docstatus!=2 and tabAccount.%(key)s LIKE "%(txt)s"
139 %(fcond)s %(mcond)s limit %(start)s, %(page_len)s""" % {'key': searchfield,
140 'txt': "%%%s%%" % txt, 'fcond': get_filters_cond(doctype, filters, conditions),
141 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
142
143def item_query(doctype, txt, searchfield, start, page_len, filters):
144 conditions = []
145 return webnotes.conn.sql("""select tabItem.name,
146 if(length(tabItem.item_name) > 40,
147 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
148 if(length(tabItem.description) > 40, \
149 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
150 FROM tabItem
151 WHERE tabItem.docstatus!=2
152 AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00")
153 OR `tabItem`.`end_of_life` > NOW())
154 AND (tabItem.%(key)s LIKE "%(txt)s" OR tabItem.item_name LIKE "%(txt)s") %(fcond)s
155 %(mcond)s limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
156 'fcond': get_filters_cond(doctype, filters, conditions),
157 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
158
159def bom(doctype, txt, searchfield, start, page_len, filters):
160 conditions = []
161 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
162 from tabBOM
163 where tabBOM.docstatus=1 and tabBOM.is_active=1
164 and tabBOM.%(key)s like "%s" %(fcond)s %(mcond)s limit %(start)s,
165 %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
166 'fcond': get_filters_cond(doctype, filters, conditions), 'mcond':get_match_cond(doctype, searchfield),
167 'start': start, 'page_len': page_len})
168
169# erpnext.queries.task = function() {
170# return { query: "projects.utils.query_task" };
171# };
172
173def get_project_name(doctype, txt, searchfield, start, page_len, filters):
174 cond = ''
175 if filters['customer']:
176 cond = '(`tabProject`.customer = filter[customer] or ifnull(`tabProject`.customer,"")="") '
177 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
178 where `tabProject`.status not in ("Completed", "Cancelled") and %(cond)s %(mcond)s
179 `tabProject`.name like "%(txt)s" order by `tabProject`.name asc limit %(start)s, %(page_len)s """ %
180 {'cond': cond,'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})