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