Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 1 | # 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 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 19 | from webnotes.utils import cstr |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 20 | |
| 21 | def get_filters_cond(doctype, filters, conditions): |
| 22 | if filters: |
| 23 | if isinstance(filters, dict): |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 24 | 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]]) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 31 | |
| 32 | from webnotes.widgets.reportview import build_filter_conditions |
Saurabh | 2deca5f | 2013-07-10 14:31:29 +0530 | [diff] [blame] | 33 | build_filter_conditions(flt, conditions) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 34 | cond = ' and ' + ' and '.join(conditions) |
| 35 | else: |
| 36 | cond = '' |
| 37 | return cond |
| 38 | |
| 39 | def 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) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 46 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 47 | if cond: |
| 48 | cond = ' and ' + cond |
| 49 | else: |
| 50 | cond = '' |
| 51 | return cond |
| 52 | |
| 53 | # searches for enabled profiles |
| 54 | def profile_query(doctype, txt, searchfield, start, page_len, filters): |
| 55 | return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name) |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 56 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 63 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 67 | name asc |
| 68 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 69 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 70 | |
| 71 | # searches for active employees |
| 72 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
| 73 | return webnotes.conn.sql("""select name, employee_name from `tabEmployee` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 74 | where status = 'Active' |
| 75 | and docstatus < 2 |
| 76 | and (%(key)s like "%(txt)s" |
| 77 | or employee_name like "%(txt)s") |
| 78 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 79 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 82 | name |
| 83 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 84 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 85 | |
| 86 | # searches for leads which are not converted |
| 87 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
| 88 | return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 89 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 95 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 99 | lead_name asc |
| 100 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 101 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 102 | |
| 103 | # searches for customer |
| 104 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
| 105 | cust_master_name = webnotes.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 106 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 107 | if cust_master_name == "Customer Name": |
| 108 | fields = ["name", "customer_group", "territory"] |
| 109 | else: |
| 110 | fields = ["name", "customer_name", "customer_group", "territory"] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 111 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 112 | fields = ", ".join(fields) |
| 113 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 114 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 119 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 122 | name, customer_name |
| 123 | limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 124 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), |
| 125 | 'start': start, 'page_len': page_len}) |
| 126 | |
| 127 | # searches for supplier |
| 128 | def 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 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 136 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 141 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 144 | name, supplier_name |
| 145 | limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 146 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start, |
| 147 | 'page_len': page_len}) |
| 148 | |
| 149 | def 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 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 157 | 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}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 162 | |
| 163 | def 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 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 172 | 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, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 176 | 'txt': "%%%s%%" % txt, 'fcond': get_filters_cond(doctype, filters, conditions), |
| 177 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 178 | |
| 179 | def item_query(doctype, txt, searchfield, start, page_len, filters): |
| 180 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 181 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 182 | 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 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 187 | 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, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 195 | 'fcond': get_filters_cond(doctype, filters, conditions), |
| 196 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 197 | |
| 198 | def bom(doctype, txt, searchfield, start, page_len, filters): |
| 199 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 200 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 201 | return webnotes.conn.sql("""select tabBOM.name, tabBOM.item |
| 202 | from tabBOM |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 203 | 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}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 210 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 211 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 212 | cond = '' |
| 213 | if filters['customer']: |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 214 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 215 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 216 | return webnotes.conn.sql("""select `tabProject`.name from `tabProject` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 217 | 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}) |