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 |
| 19 | |
| 20 | def get_filters_cond(doctype, filters, conditions): |
| 21 | if filters: |
| 22 | if isinstance(filters, dict): |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 23 | filters = filters.items() |
| 24 | flt = [] |
| 25 | for f in filters: |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 26 | if isinstance(f[1], basestring) and f[1][0] == '!': |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 27 | flt.append([doctype, f[0], '!=', f[1][1:]]) |
| 28 | else: |
| 29 | flt.append([doctype, f[0], '=', f[1]]) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 30 | |
| 31 | from webnotes.widgets.reportview import build_filter_conditions |
Saurabh | 2deca5f | 2013-07-10 14:31:29 +0530 | [diff] [blame] | 32 | build_filter_conditions(flt, conditions) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 33 | cond = ' and ' + ' and '.join(conditions) |
| 34 | else: |
| 35 | cond = '' |
| 36 | return cond |
| 37 | |
| 38 | def get_match_cond(doctype, searchfield = 'name'): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 39 | from webnotes.widgets.reportview import build_match_conditions |
Anand Doshi | 7180eb6 | 2013-07-30 14:40:44 +0530 | [diff] [blame] | 40 | cond = build_match_conditions(doctype) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 41 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 42 | if cond: |
| 43 | cond = ' and ' + cond |
| 44 | else: |
| 45 | cond = '' |
| 46 | return cond |
| 47 | |
| 48 | # searches for enabled profiles |
| 49 | def profile_query(doctype, txt, searchfield, start, page_len, filters): |
| 50 | 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] | 51 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 58 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 62 | name asc |
| 63 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 64 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 65 | |
| 66 | # searches for active employees |
| 67 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
| 68 | return webnotes.conn.sql("""select name, employee_name from `tabEmployee` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 69 | where status = 'Active' |
| 70 | and docstatus < 2 |
| 71 | and (%(key)s like "%(txt)s" |
| 72 | or employee_name like "%(txt)s") |
| 73 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 74 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 77 | name |
| 78 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 79 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 80 | |
| 81 | # searches for leads which are not converted |
| 82 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
| 83 | return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 84 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 90 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 94 | lead_name asc |
| 95 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 96 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 97 | |
| 98 | # searches for customer |
| 99 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
| 100 | cust_master_name = webnotes.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 101 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 102 | if cust_master_name == "Customer Name": |
| 103 | fields = ["name", "customer_group", "territory"] |
| 104 | else: |
| 105 | fields = ["name", "customer_name", "customer_group", "territory"] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 106 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 107 | fields = ", ".join(fields) |
| 108 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 109 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 114 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 117 | name, customer_name |
| 118 | limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 119 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), |
| 120 | 'start': start, 'page_len': page_len}) |
| 121 | |
| 122 | # searches for supplier |
| 123 | def 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 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 131 | 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 |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 136 | 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, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 139 | name, supplier_name |
| 140 | limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 141 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start, |
| 142 | 'page_len': page_len}) |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 143 | |
| 144 | def 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])) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 158 | |
| 159 | def item_query(doctype, txt, searchfield, start, page_len, filters): |
| 160 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 161 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 162 | 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 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 167 | from tabItem |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 168 | where tabItem.docstatus<2 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 169 | 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, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 173 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 174 | 'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 175 | |
| 176 | def bom(doctype, txt, searchfield, start, page_len, filters): |
| 177 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 178 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 179 | return webnotes.conn.sql("""select tabBOM.name, tabBOM.item |
| 180 | from tabBOM |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 181 | where tabBOM.docstatus=1 |
| 182 | and tabBOM.is_active=1 |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 183 | and tabBOM.%(key)s like "%(txt)s" |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 184 | %(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}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 188 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 189 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 190 | cond = '' |
| 191 | if filters['customer']: |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 192 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 193 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 194 | return webnotes.conn.sql("""select `tabProject`.name from `tabProject` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 195 | 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 Hait | da46a37 | 2013-07-15 17:52:10 +0530 | [diff] [blame] | 199 | 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len}) |
| 200 | |
| 201 | def 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, |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 207 | start, page_len)) |
| 208 | |
| 209 | def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters): |
| 210 | return webnotes.conn.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name |
| 211 | from `tabDelivery Note` |
| 212 | where `tabDelivery Note`.`%(key)s` like %(txt)s %(fcond)s and |
| 213 | (ifnull((select sum(qty) from `tabDelivery Note Item` where |
| 214 | `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) > |
| 215 | ifnull((select sum(qty) from `tabSales Invoice Item` where |
| 216 | `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0)) |
| 217 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
| 218 | limit %(start)s, %(page_len)s""" % { |
| 219 | "key": searchfield, |
| 220 | "fcond": get_filters_cond(doctype, filters, []), |
| 221 | "mcond": get_match_cond(doctype), |
| 222 | "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s" |
| 223 | }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }, debug=True) |