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 |
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): |
| 120 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 121 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 122 | return webnotes.conn.sql("""select tabItem.name, |
| 123 | if(length(tabItem.item_name) > 40, |
| 124 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
| 125 | if(length(tabItem.description) > 40, \ |
| 126 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 127 | from tabItem |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 128 | where tabItem.docstatus<2 |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 129 | and (tabItem.%(key)s LIKE "%(txt)s" |
| 130 | or tabItem.item_name LIKE "%(txt)s") |
| 131 | %(fcond)s %(mcond)s |
| 132 | limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 133 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 9eb2868 | 2013-07-29 13:07:30 +0530 | [diff] [blame] | 134 | 'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 135 | |
| 136 | def bom(doctype, txt, searchfield, start, page_len, filters): |
| 137 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 138 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 139 | return webnotes.conn.sql("""select tabBOM.name, tabBOM.item |
| 140 | from tabBOM |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 141 | where tabBOM.docstatus=1 |
| 142 | and tabBOM.is_active=1 |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 143 | and tabBOM.%(key)s like "%(txt)s" |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 144 | %(fcond)s %(mcond)s |
| 145 | limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt, |
| 146 | 'fcond': get_filters_cond(doctype, filters, conditions), |
| 147 | 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 148 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 149 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 150 | cond = '' |
| 151 | if filters['customer']: |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 152 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 153 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 154 | return webnotes.conn.sql("""select `tabProject`.name from `tabProject` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 155 | where `tabProject`.status not in ("Completed", "Cancelled") |
| 156 | and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s |
| 157 | order by `tabProject`.name asc |
| 158 | limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt, |
Nabin Hait | da46a37 | 2013-07-15 17:52:10 +0530 | [diff] [blame] | 159 | 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len}) |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 160 | |
| 161 | def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters): |
| 162 | return webnotes.conn.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name |
| 163 | from `tabDelivery Note` |
Anand Doshi | 1dadf35 | 2013-09-03 16:21:01 +0530 | [diff] [blame] | 164 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
| 165 | `tabDelivery Note`.docstatus = 1 %(fcond)s and |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 166 | (ifnull((select sum(qty) from `tabDelivery Note Item` where |
| 167 | `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) > |
| 168 | ifnull((select sum(qty) from `tabSales Invoice Item` where |
Anand Doshi | 1dadf35 | 2013-09-03 16:21:01 +0530 | [diff] [blame] | 169 | `tabSales Invoice Item`.docstatus = 1 and |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 170 | `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0)) |
| 171 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
| 172 | limit %(start)s, %(page_len)s""" % { |
| 173 | "key": searchfield, |
| 174 | "fcond": get_filters_cond(doctype, filters, []), |
| 175 | "mcond": get_match_cond(doctype), |
| 176 | "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s" |
Nabin Hait | eaaa5b1 | 2013-08-14 14:41:08 +0530 | [diff] [blame] | 177 | }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }) |