Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe 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 |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 6 | from frappe.desk.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 |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 8 | from frappe.utils import nowdate |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 9 | |
| 10 | def get_filters_cond(doctype, filters, conditions): |
| 11 | if filters: |
Anand Doshi | 1fef2fa | 2015-09-14 18:27:21 +0530 | [diff] [blame] | 12 | flt = filters |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 13 | if isinstance(filters, dict): |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 14 | filters = filters.items() |
| 15 | flt = [] |
| 16 | for f in filters: |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 17 | if isinstance(f[1], basestring) and f[1][0] == '!': |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 18 | flt.append([doctype, f[0], '!=', f[1][1:]]) |
| 19 | else: |
Nabin Hait | 8693b5c | 2016-09-04 10:31:41 +0530 | [diff] [blame] | 20 | value = frappe.db.escape(f[1]) if isinstance(f[1], basestring) else f[1] |
| 21 | flt.append([doctype, f[0], '=', value]) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 22 | |
Rushabh Mehta | 2e7ad89 | 2014-03-06 12:28:47 +0530 | [diff] [blame] | 23 | query = DatabaseQuery(doctype) |
| 24 | query.filters = flt |
| 25 | query.conditions = conditions |
Nabin Hait | 23649c6 | 2014-05-07 19:18:15 +0530 | [diff] [blame] | 26 | query.build_filter_conditions(flt, conditions) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 27 | |
| 28 | cond = ' and ' + ' and '.join(query.conditions) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 29 | else: |
| 30 | cond = '' |
| 31 | return cond |
| 32 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 33 | # searches for active employees |
| 34 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 35 | return frappe.db.sql("""select name, employee_name from `tabEmployee` |
| 36 | where status = 'Active' |
| 37 | and docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 38 | and ({key} like %(txt)s |
| 39 | or employee_name like %(txt)s) |
| 40 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 41 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 42 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 43 | if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 44 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 45 | name, employee_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 46 | limit %(start)s, %(page_len)s""".format(**{ |
| 47 | 'key': searchfield, |
| 48 | 'mcond': get_match_cond(doctype) |
| 49 | }), { |
| 50 | 'txt': "%%%s%%" % txt, |
| 51 | '_txt': txt.replace("%", ""), |
| 52 | 'start': start, |
| 53 | 'page_len': page_len |
| 54 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 55 | |
| 56 | # searches for leads which are not converted |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 57 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 58 | return frappe.db.sql("""select name, lead_name, company_name from `tabLead` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 59 | where docstatus < 2 |
| 60 | and ifnull(status, '') != 'Converted' |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 61 | and ({key} like %(txt)s |
| 62 | or lead_name like %(txt)s |
| 63 | or company_name like %(txt)s) |
| 64 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 65 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 66 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 67 | if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999), |
| 68 | if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 69 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 70 | name, lead_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 71 | limit %(start)s, %(page_len)s""".format(**{ |
| 72 | 'key': searchfield, |
| 73 | 'mcond':get_match_cond(doctype) |
| 74 | }), { |
| 75 | 'txt': "%%%s%%" % txt, |
| 76 | '_txt': txt.replace("%", ""), |
| 77 | 'start': start, |
| 78 | 'page_len': page_len |
| 79 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 80 | |
| 81 | # searches for customer |
| 82 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 83 | cust_master_name = frappe.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 84 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 85 | if cust_master_name == "Customer Name": |
| 86 | fields = ["name", "customer_group", "territory"] |
| 87 | else: |
| 88 | fields = ["name", "customer_name", "customer_group", "territory"] |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 89 | |
| 90 | meta = frappe.get_meta("Customer") |
| 91 | fields = fields + [f for f in meta.get_search_fields() if not f in fields] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 92 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 93 | fields = ", ".join(fields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 94 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 95 | return frappe.db.sql("""select {fields} from `tabCustomer` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 96 | where docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 97 | and ({key} like %(txt)s |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 98 | or customer_name like %(txt)s) and disabled=0 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 99 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 100 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 101 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 102 | if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 103 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 104 | name, customer_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 105 | limit %(start)s, %(page_len)s""".format(**{ |
| 106 | "fields": fields, |
| 107 | "key": searchfield, |
| 108 | "mcond": get_match_cond(doctype) |
| 109 | }), { |
| 110 | 'txt': "%%%s%%" % txt, |
| 111 | '_txt': txt.replace("%", ""), |
| 112 | 'start': start, |
| 113 | 'page_len': page_len |
| 114 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 115 | |
| 116 | # searches for supplier |
| 117 | def supplier_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 118 | supp_master_name = frappe.defaults.get_user_default("supp_master_name") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 119 | if supp_master_name == "Supplier Name": |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 120 | fields = ["name", "supplier_type"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 121 | else: |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 122 | fields = ["name", "supplier_name", "supplier_type"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 123 | fields = ", ".join(fields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 124 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 125 | return frappe.db.sql("""select {field} from `tabSupplier` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 126 | where docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 127 | and ({key} like %(txt)s |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 128 | or supplier_name like %(txt)s) and disabled=0 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 129 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 130 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 131 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 132 | if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 133 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 134 | name, supplier_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 135 | limit %(start)s, %(page_len)s """.format(**{ |
| 136 | 'field': fields, |
| 137 | 'key': searchfield, |
| 138 | 'mcond':get_match_cond(doctype) |
| 139 | }), { |
| 140 | 'txt': "%%%s%%" % txt, |
| 141 | '_txt': txt.replace("%", ""), |
| 142 | 'start': start, |
| 143 | 'page_len': page_len |
| 144 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 145 | |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 146 | def tax_account_query(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 147 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
| 148 | where tabAccount.docstatus!=2 |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 149 | and account_type in (%s) |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 150 | and is_group = 0 |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 151 | and company = %s |
| 152 | and `%s` LIKE %s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 153 | order by idx desc, name |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 154 | limit %s, %s""" % |
| 155 | (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"), |
| 156 | tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt, |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 157 | start, page_len])) |
| 158 | if not tax_accounts: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 159 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 160 | where tabAccount.docstatus!=2 and is_group = 0 |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 161 | and company = %s and `%s` LIKE %s limit %s, %s""" |
| 162 | % ("%s", searchfield, "%s", "%s", "%s"), |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 163 | (filters.get("company"), "%%%s%%" % txt, start, page_len)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 164 | |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 165 | return tax_accounts |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 166 | |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 167 | def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 168 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 169 | |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 170 | return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 171 | if(length(tabItem.item_name) > 40, |
| 172 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 173 | if(length(tabItem.description) > 40, \ |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 174 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 175 | from tabItem |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 176 | where tabItem.docstatus < 2 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 177 | and tabItem.has_variants=0 |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 178 | and tabItem.disabled=0 |
Rushabh Mehta | 864d1ea | 2014-06-23 12:20:12 +0530 | [diff] [blame] | 179 | and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00') |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 180 | and (tabItem.`{key}` LIKE %(txt)s |
ShashaQin | 425ab6b | 2016-03-02 13:07:58 +0800 | [diff] [blame] | 181 | or tabItem.item_group LIKE %(txt)s |
Anand Doshi | 7fcb3c9 | 2014-07-21 17:51:14 +0530 | [diff] [blame] | 182 | or tabItem.item_name LIKE %(txt)s |
| 183 | or tabItem.description LIKE %(txt)s) |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 184 | {fcond} {mcond} |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 185 | order by |
| 186 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 187 | if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 188 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 189 | name, item_name |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 190 | limit %(start)s, %(page_len)s """.format(key=searchfield, |
Nabin Hait | c628506 | 2016-03-30 13:10:25 +0530 | [diff] [blame] | 191 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
| 192 | mcond=get_match_cond(doctype).replace('%', '%%')), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 193 | { |
| 194 | "today": nowdate(), |
| 195 | "txt": "%%%s%%" % txt, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 196 | "_txt": txt.replace("%", ""), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 197 | "start": start, |
| 198 | "page_len": page_len |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 199 | }, as_dict=as_dict) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 200 | |
| 201 | def bom(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 202 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 203 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 204 | return frappe.db.sql("""select tabBOM.name, tabBOM.item |
| 205 | from tabBOM |
| 206 | where tabBOM.docstatus=1 |
| 207 | and tabBOM.is_active=1 |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 208 | and tabBOM.`{key}` like %(txt)s |
| 209 | {fcond} {mcond} |
| 210 | order by |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 211 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 212 | idx desc, name |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 213 | limit %(start)s, %(page_len)s """.format( |
| 214 | fcond=get_filters_cond(doctype, filters, conditions), |
| 215 | mcond=get_match_cond(doctype), |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 216 | key=frappe.db.escape(searchfield)), |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 217 | { |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 218 | 'txt': "%%%s%%" % frappe.db.escape(txt), |
| 219 | '_txt': txt.replace("%", ""), |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 220 | 'start': start, |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 221 | 'page_len': page_len |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 222 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 223 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 224 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 225 | cond = '' |
Nabin Hait | f71011a | 2014-08-21 11:34:31 +0530 | [diff] [blame] | 226 | if filters.get('customer'): |
Saurabh | ab462d2 | 2013-07-09 16:18:52 +0530 | [diff] [blame] | 227 | cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and' |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 228 | |
| 229 | return frappe.db.sql("""select `tabProject`.name from `tabProject` |
| 230 | where `tabProject`.status not in ("Completed", "Cancelled") |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 231 | and {cond} `tabProject`.name like %(txt)s {match_cond} |
| 232 | order by |
| 233 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 234 | idx desc, |
| 235 | `tabProject`.name asc |
| 236 | limit {start}, {page_len}""".format( |
| 237 | cond=cond, |
| 238 | match_cond=get_match_cond(doctype), |
| 239 | start=start, |
| 240 | page_len=page_len), { |
| 241 | "txt": "%{0}%".format(txt), |
Nabin Hait | df4deba | 2016-03-16 11:16:31 +0530 | [diff] [blame] | 242 | "_txt": txt.replace('%', '') |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 243 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 244 | |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 245 | 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] | 246 | return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 247 | from `tabDelivery Note` |
| 248 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 249 | `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s |
shreyas | 2563f40 | 2016-01-25 17:44:09 +0530 | [diff] [blame] | 250 | and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0) |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 251 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
| 252 | limit %(start)s, %(page_len)s""" % { |
| 253 | "key": searchfield, |
| 254 | "fcond": get_filters_cond(doctype, filters, []), |
| 255 | "mcond": get_match_cond(doctype), |
| 256 | "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s" |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 257 | }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }) |
| 258 | |
| 259 | def get_batch_no(doctype, txt, searchfield, start, page_len, filters): |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 260 | cond = "" |
| 261 | if filters.get("posting_date"): |
| 262 | cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)" |
Rushabh Mehta | b6398be | 2015-08-26 10:50:16 +0530 | [diff] [blame] | 263 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 264 | batch_nos = None |
| 265 | args = { |
| 266 | 'item_code': filters.get("item_code"), |
| 267 | 'warehouse': filters.get("warehouse"), |
| 268 | 'posting_date': filters.get('posting_date'), |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 269 | 'txt': "%{0}%".format(txt), |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 270 | "start": start, |
| 271 | "page_len": page_len |
| 272 | } |
| 273 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 274 | if args.get('warehouse'): |
| 275 | batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date |
| 276 | from `tabStock Ledger Entry` sle |
| 277 | INNER JOIN `tabBatch` batch on sle.batch_no = batch.name |
| 278 | where |
| 279 | sle.item_code = %(item_code)s |
| 280 | and sle.warehouse = %(warehouse)s |
| 281 | and sle.batch_no like %(txt)s |
| 282 | and batch.docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 283 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 284 | {match_conditions} |
| 285 | group by batch_no having sum(sle.actual_qty) > 0 |
| 286 | order by batch.expiry_date, sle.batch_no desc |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 287 | limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 288 | |
| 289 | if batch_nos: |
| 290 | return batch_nos |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 291 | else: |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 292 | return frappe.db.sql("""select name, expiry_date from `tabBatch` batch |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 293 | where item = %(item_code)s |
| 294 | and name like %(txt)s |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 295 | and docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 296 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 297 | {match_conditions} |
| 298 | order by expiry_date, name desc |
Nabin Hait | e52ee55 | 2015-09-02 10:55:32 +0530 | [diff] [blame] | 299 | limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args) |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 300 | |
| 301 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 302 | filter_list = [] |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 303 | |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 304 | if isinstance(filters, dict): |
| 305 | for key, val in filters.items(): |
| 306 | if isinstance(val, (list, tuple)): |
| 307 | filter_list.append([doctype, key, val[0], val[1]]) |
| 308 | else: |
| 309 | filter_list.append([doctype, key, "=", val]) |
bhupeshg2 | e2e973f | 2015-04-14 22:15:24 +0530 | [diff] [blame] | 310 | elif isinstance(filters, list): |
| 311 | filter_list.extend(filters) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 312 | |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 313 | if "is_group" not in [d[1] for d in filter_list]: |
| 314 | filter_list.append(["Account", "is_group", "=", "0"]) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 315 | |
| 316 | if searchfield and txt: |
| 317 | filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt]) |
| 318 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 319 | return frappe.desk.reportview.execute("Account", filters = filter_list, |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 320 | fields = ["name", "parent_account"], |
| 321 | limit_start=start, limit_page_length=page_len, as_list=True) |
Anand Doshi | faefeaa | 2014-06-24 18:53:04 +0530 | [diff] [blame] | 322 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 323 | |
| 324 | @frappe.whitelist() |
| 325 | def get_income_account(doctype, txt, searchfield, start, page_len, filters): |
| 326 | from erpnext.controllers.queries import get_match_cond |
| 327 | |
| 328 | # income account can be any Credit account, |
| 329 | # but can also be a Asset account with account_type='Income Account' in special circumstances. |
| 330 | # Hence the first condition is an "OR" |
| 331 | if not filters: filters = {} |
| 332 | |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 333 | condition = "" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 334 | if filters.get("company"): |
| 335 | condition += "and tabAccount.company = %(company)s" |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 336 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 337 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 338 | where (tabAccount.report_type = "Profit and Loss" |
| 339 | or tabAccount.account_type in ("Income Account", "Temporary")) |
| 340 | and tabAccount.is_group=0 |
| 341 | and tabAccount.`{key}` LIKE %(txt)s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 342 | {condition} {match_condition} |
| 343 | order by idx desc, name""" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 344 | .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), { |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 345 | 'txt': "%%%s%%" % frappe.db.escape(txt), |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 346 | 'company': filters.get("company", "") |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 347 | }) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 348 | |
| 349 | |
| 350 | @frappe.whitelist() |
| 351 | def get_expense_account(doctype, txt, searchfield, start, page_len, filters): |
| 352 | from erpnext.controllers.queries import get_match_cond |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 353 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 354 | if not filters: filters = {} |
| 355 | |
| 356 | condition = "" |
| 357 | if filters.get("company"): |
| 358 | condition += "and tabAccount.company = %(company)s" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 359 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 360 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 361 | where (tabAccount.report_type = "Profit and Loss" |
| 362 | or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary")) |
| 363 | and tabAccount.is_group=0 |
| 364 | and tabAccount.docstatus!=2 |
| 365 | and tabAccount.{key} LIKE %(txt)s |
| 366 | {condition} {match_condition}""" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 367 | .format(condition=condition, key=frappe.db.escape(searchfield), |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 368 | match_condition=get_match_cond(doctype)), { |
Neil Trini Lasrado | 30b97b0 | 2016-03-31 23:10:13 +0530 | [diff] [blame] | 369 | 'company': filters.get("company", ""), |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 370 | 'txt': "%%%s%%" % frappe.db.escape(txt) |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 371 | }) |