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