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 | 49a6f07 | 2014-03-03 18:40:24 +0530 | [diff] [blame] | 109 | return 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 | 49a6f07 | 2014-03-03 18:40:24 +0530 | [diff] [blame] | 111 | and (account_type in (%s) or root_type = %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""" % |
| 116 | (", ".join(['%s']*len(filters.get("account_type"))), |
| 117 | "%s", "%s", searchfield, "%s", "%s", "%s"), |
Nabin Hait | 49a6f07 | 2014-03-03 18:40:24 +0530 | [diff] [blame] | 118 | tuple(filters.get("account_type") + [filters.get("root_type"), |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 119 | filters.get("company"), "%%%s%%" % txt, start, page_len])) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 120 | |
| 121 | def item_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 122 | from frappe.utils import nowdate |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 123 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 124 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 125 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 126 | return frappe.db.sql("""select tabItem.name, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 127 | if(length(tabItem.item_name) > 40, |
| 128 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
| 129 | if(length(tabItem.description) > 40, \ |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 130 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 131 | from tabItem |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 132 | where tabItem.docstatus < 2 |
| 133 | and (ifnull(tabItem.end_of_life, '') = '' or tabItem.end_of_life > %(today)s) |
| 134 | and (tabItem.`{key}` LIKE %(txt)s |
| 135 | or tabItem.item_name LIKE %(txt)s) |
| 136 | {fcond} {mcond} |
| 137 | limit %(start)s, %(page_len)s """.format(key=searchfield, |
| 138 | fcond=get_filters_cond(doctype, filters, conditions), |
Rushabh Mehta | 4541875 | 2014-03-06 11:18:37 +0530 | [diff] [blame] | 139 | mcond=get_match_cond(doctype)), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 140 | { |
| 141 | "today": nowdate(), |
| 142 | "txt": "%%%s%%" % txt, |
| 143 | "start": start, |
| 144 | "page_len": page_len |
| 145 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 146 | |
| 147 | def bom(doctype, txt, searchfield, start, page_len, filters): |
| 148 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 149 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 150 | return frappe.db.sql("""select tabBOM.name, tabBOM.item |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 151 | from tabBOM |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 152 | where tabBOM.docstatus=1 |
| 153 | and tabBOM.is_active=1 |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 154 | and tabBOM.%(key)s like "%(txt)s" |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 155 | %(fcond)s %(mcond)s |
| 156 | limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt, |
| 157 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Rushabh Mehta | 4541875 | 2014-03-06 11:18:37 +0530 | [diff] [blame] | 158 | 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len}) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 159 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 160 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 161 | cond = '' |
| 162 | if filters['customer']: |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 163 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 164 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 165 | return frappe.db.sql("""select `tabProject`.name from `tabProject` |
Saurabh | a29b692 | 2013-07-10 16:06:31 +0530 | [diff] [blame] | 166 | where `tabProject`.status not in ("Completed", "Cancelled") |
| 167 | and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s |
| 168 | order by `tabProject`.name asc |
| 169 | limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt, |
Rushabh Mehta | 4541875 | 2014-03-06 11:18:37 +0530 | [diff] [blame] | 170 | 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len}) |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 171 | |
| 172 | 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] | 173 | return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 174 | from `tabDelivery Note` |
Anand Doshi | 1dadf35 | 2013-09-03 16:21:01 +0530 | [diff] [blame] | 175 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
| 176 | `tabDelivery Note`.docstatus = 1 %(fcond)s and |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 177 | (ifnull((select sum(qty) from `tabDelivery Note Item` where |
| 178 | `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) > |
| 179 | ifnull((select sum(qty) from `tabSales Invoice Item` where |
Anand Doshi | 1dadf35 | 2013-09-03 16:21:01 +0530 | [diff] [blame] | 180 | `tabSales Invoice Item`.docstatus = 1 and |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 181 | `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0)) |
| 182 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
| 183 | limit %(start)s, %(page_len)s""" % { |
| 184 | "key": searchfield, |
| 185 | "fcond": get_filters_cond(doctype, filters, []), |
| 186 | "mcond": get_match_cond(doctype), |
| 187 | "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s" |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 188 | }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }) |
| 189 | |
| 190 | def get_batch_no(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 191 | from erpnext.controllers.queries import get_match_cond |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 192 | |
| 193 | if filters.has_key('warehouse'): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 194 | return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 195 | where item_code = '%(item_code)s' |
| 196 | and warehouse = '%(warehouse)s' |
| 197 | and batch_no like '%(txt)s' |
| 198 | and exists(select * from `tabBatch` |
| 199 | where name = sle.batch_no |
| 200 | and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s') |
| 201 | and docstatus != 2) |
| 202 | %(mcond)s |
| 203 | group by batch_no having sum(actual_qty) > 0 |
| 204 | order by batch_no desc |
| 205 | limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'], |
| 206 | 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'], |
Rushabh Mehta | 4541875 | 2014-03-06 11:18:37 +0530 | [diff] [blame] | 207 | 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype), |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 208 | 'start': start, 'page_len': page_len}) |
| 209 | else: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 210 | return frappe.db.sql("""select name from tabBatch |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 211 | where docstatus != 2 |
| 212 | and item = '%(item_code)s' |
| 213 | and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s') |
| 214 | and name like '%(txt)s' |
| 215 | %(mcond)s |
| 216 | order by name desc |
| 217 | limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'], |
| 218 | 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt, |
Rushabh Mehta | 4541875 | 2014-03-06 11:18:37 +0530 | [diff] [blame] | 219 | 'mcond':get_match_cond(doctype),'start': start, |
Anand Doshi | 1788c8b | 2013-11-05 12:10:08 +0530 | [diff] [blame] | 220 | 'page_len': page_len}) |