Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
| 6 | |
| 7 | def get_filters_cond(doctype, filters, conditions): |
| 8 | if filters: |
| 9 | if isinstance(filters, dict): |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 10 | filters = filters.items() |
| 11 | flt = [] |
| 12 | for f in filters: |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 13 | if isinstance(f[1], basestring) and f[1][0] == '!': |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 14 | flt.append([doctype, f[0], '!=', f[1][1:]]) |
| 15 | else: |
| 16 | flt.append([doctype, f[0], '=', f[1]]) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 17 | |
| 18 | from webnotes.widgets.reportview import build_filter_conditions |
Saurabh | 2deca5f | 2013-07-10 14:31:29 +0530 | [diff] [blame] | 19 | build_filter_conditions(flt, conditions) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 20 | cond = ' and ' + ' and '.join(conditions) |
| 21 | else: |
| 22 | cond = '' |
| 23 | return cond |
| 24 | |
| 25 | def get_match_cond(doctype, searchfield = 'name'): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 26 | from webnotes.widgets.reportview import build_match_conditions |
Anand Doshi | 7180eb6 | 2013-07-30 14:40:44 +0530 | [diff] [blame] | 27 | cond = build_match_conditions(doctype) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 28 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 29 | if cond: |
| 30 | cond = ' and ' + cond |
| 31 | else: |
| 32 | cond = '' |
| 33 | return cond |
| 34 | |
| 35 | # searches for enabled profiles |
| 36 | def profile_query(doctype, txt, searchfield, start, page_len, filters): |
| 37 | 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] | 38 | from `tabProfile` |
| 39 | where ifnull(enabled, 0)=1 |
| 40 | and docstatus < 2 |
| 41 | and name not in ('Administrator', 'Guest') |
| 42 | and (%(key)s like "%(txt)s" |
| 43 | or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s") |
| 44 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 45 | order by |
| 46 | case when name like "%(txt)s" then 0 else 1 end, |
| 47 | case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s" |
| 48 | then 0 else 1 end, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 49 | name asc |
| 50 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 51 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 52 | |
| 53 | # searches for active employees |
| 54 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
| 55 | return webnotes.conn.sql("""select name, employee_name from `tabEmployee` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 56 | where status = 'Active' |
| 57 | and docstatus < 2 |
| 58 | and (%(key)s like "%(txt)s" |
| 59 | or employee_name like "%(txt)s") |
| 60 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 61 | order by |
| 62 | case when name like "%(txt)s" then 0 else 1 end, |
| 63 | case when employee_name like "%(txt)s" then 0 else 1 end, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 64 | name |
| 65 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 66 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 67 | |
| 68 | # searches for leads which are not converted |
| 69 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
| 70 | return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 71 | where docstatus < 2 |
| 72 | and ifnull(status, '') != 'Converted' |
| 73 | and (%(key)s like "%(txt)s" |
| 74 | or lead_name like "%(txt)s" |
| 75 | or company_name like "%(txt)s") |
| 76 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 77 | order by |
| 78 | case when name like "%(txt)s" then 0 else 1 end, |
| 79 | case when lead_name like "%(txt)s" then 0 else 1 end, |
| 80 | case when company_name like "%(txt)s" then 0 else 1 end, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 81 | lead_name asc |
| 82 | limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 83 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
| 84 | |
| 85 | # searches for customer |
| 86 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
| 87 | cust_master_name = webnotes.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 88 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 89 | if cust_master_name == "Customer Name": |
| 90 | fields = ["name", "customer_group", "territory"] |
| 91 | else: |
| 92 | fields = ["name", "customer_name", "customer_group", "territory"] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 93 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 94 | fields = ", ".join(fields) |
| 95 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 96 | return webnotes.conn.sql("""select %(field)s from `tabCustomer` |
| 97 | where docstatus < 2 |
| 98 | and (%(key)s like "%(txt)s" |
| 99 | or customer_name like "%(txt)s") |
| 100 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 101 | order by |
| 102 | case when name like "%(txt)s" then 0 else 1 end, |
| 103 | case when customer_name like "%(txt)s" then 0 else 1 end, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 104 | name, customer_name |
| 105 | limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 106 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), |
| 107 | 'start': start, 'page_len': page_len}) |
| 108 | |
| 109 | # searches for supplier |
| 110 | def supplier_query(doctype, txt, searchfield, start, page_len, filters): |
| 111 | supp_master_name = webnotes.defaults.get_user_default("supp_master_name") |
| 112 | if supp_master_name == "Supplier Name": |
| 113 | fields = ["name", "supplier_type"] |
| 114 | else: |
| 115 | fields = ["name", "supplier_name", "supplier_type"] |
| 116 | fields = ", ".join(fields) |
| 117 | |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 118 | return webnotes.conn.sql("""select %(field)s from `tabSupplier` |
| 119 | where docstatus < 2 |
| 120 | and (%(key)s like "%(txt)s" |
| 121 | or supplier_name like "%(txt)s") |
| 122 | %(mcond)s |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 123 | order by |
| 124 | case when name like "%(txt)s" then 0 else 1 end, |
| 125 | case when supplier_name like "%(txt)s" then 0 else 1 end, |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 126 | name, supplier_name |
| 127 | limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 128 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start, |
| 129 | 'page_len': page_len}) |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 130 | |
| 131 | def tax_account_query(doctype, txt, searchfield, start, page_len, filters): |
| 132 | return webnotes.conn.sql("""select name, parent_account, debit_or_credit |
| 133 | from tabAccount |
| 134 | where tabAccount.docstatus!=2 |
| 135 | and (account_type in (%s) or |
| 136 | (ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) ) |
| 137 | and group_or_ledger = 'Ledger' |
| 138 | and company = %s |
| 139 | and `%s` LIKE %s |
| 140 | limit %s, %s""" % |
| 141 | (", ".join(['%s']*len(filters.get("account_type"))), |
| 142 | "%s", "%s", searchfield, "%s", "%s", "%s"), |
| 143 | tuple(filters.get("account_type") + [filters.get("debit_or_credit"), |
| 144 | filters.get("company"), "%%%s%%" % txt, start, page_len])) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 145 | |
| 146 | def item_query(doctype, txt, searchfield, start, page_len, filters): |
| 147 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 148 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 149 | return webnotes.conn.sql("""select tabItem.name, |
| 150 | if(length(tabItem.item_name) > 40, |
| 151 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
| 152 | if(length(tabItem.description) > 40, \ |
| 153 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 154 | from tabItem |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 155 | where tabItem.docstatus<2 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 156 | and (tabItem.%(key)s LIKE "%(txt)s" |
| 157 | or tabItem.item_name LIKE "%(txt)s") |
| 158 | %(fcond)s %(mcond)s |
| 159 | limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 160 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 161 | 'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 162 | |
| 163 | def bom(doctype, txt, searchfield, start, page_len, filters): |
| 164 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 165 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 166 | return webnotes.conn.sql("""select tabBOM.name, tabBOM.item |
| 167 | from tabBOM |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 168 | where tabBOM.docstatus=1 |
| 169 | and tabBOM.is_active=1 |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 170 | and tabBOM.%(key)s like "%(txt)s" |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 171 | %(fcond)s %(mcond)s |
| 172 | limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt, |
| 173 | 'fcond': get_filters_cond(doctype, filters, conditions), |
| 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 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 176 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 177 | cond = '' |
| 178 | if filters['customer']: |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 179 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 180 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 181 | return webnotes.conn.sql("""select `tabProject`.name from `tabProject` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 182 | where `tabProject`.status not in ("Completed", "Cancelled") |
| 183 | and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s |
| 184 | order by `tabProject`.name asc |
| 185 | limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt, |
Nabin Hait | da46a37 | 2013-07-15 17:52:10 +0530 | [diff] [blame] | 186 | 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len}) |
| 187 | |
| 188 | def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters): |
| 189 | return webnotes.conn.sql("""select ref_currency from `tabItem Price` |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 190 | where price_list = %s and buying_or_selling = %s |
Nabin Hait | da46a37 | 2013-07-15 17:52:10 +0530 | [diff] [blame] | 191 | and `%s` like %s order by ref_currency asc limit %s, %s""" % |
| 192 | ("%s", "%s", searchfield, "%s", "%s", "%s"), |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 193 | (filters["price_list"], filters['buying_or_selling'], "%%%s%%" % txt, |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 194 | start, page_len)) |
| 195 | |
| 196 | def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters): |
| 197 | return webnotes.conn.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name |
| 198 | from `tabDelivery Note` |
| 199 | where `tabDelivery Note`.`%(key)s` like %(txt)s %(fcond)s and |
| 200 | (ifnull((select sum(qty) from `tabDelivery Note Item` where |
| 201 | `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) > |
| 202 | ifnull((select sum(qty) from `tabSales Invoice Item` where |
| 203 | `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0)) |
| 204 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
| 205 | limit %(start)s, %(page_len)s""" % { |
| 206 | "key": searchfield, |
| 207 | "fcond": get_filters_cond(doctype, filters, []), |
| 208 | "mcond": get_match_cond(doctype), |
| 209 | "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s" |
Nabin Hait | eaaa5b1 | 2013-08-14 14:41:08 +0530 | [diff] [blame] | 210 | }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }) |