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 | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 6 | from frappe.desk.reportview import get_match_cond, get_filters_cond |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 7 | from frappe.utils import nowdate |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 8 | from collections import defaultdict |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 9 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 10 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 11 | # searches for active employees |
| 12 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 13 | conditions = [] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 14 | return frappe.db.sql("""select name, employee_name from `tabEmployee` |
| 15 | where status = 'Active' |
| 16 | and docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 17 | and ({key} like %(txt)s |
| 18 | or employee_name like %(txt)s) |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 19 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 20 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 21 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 22 | if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 23 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 24 | name, employee_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 25 | limit %(start)s, %(page_len)s""".format(**{ |
| 26 | 'key': searchfield, |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 27 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 28 | 'mcond': get_match_cond(doctype) |
| 29 | }), { |
| 30 | 'txt': "%%%s%%" % txt, |
| 31 | '_txt': txt.replace("%", ""), |
| 32 | 'start': start, |
| 33 | 'page_len': page_len |
| 34 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 35 | |
| 36 | # searches for leads which are not converted |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 37 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 38 | return frappe.db.sql("""select name, lead_name, company_name from `tabLead` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 39 | where docstatus < 2 |
| 40 | and ifnull(status, '') != 'Converted' |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 41 | and ({key} like %(txt)s |
| 42 | or lead_name like %(txt)s |
| 43 | or company_name like %(txt)s) |
| 44 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 45 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 46 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 47 | if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999), |
| 48 | if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 49 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 50 | name, lead_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 51 | limit %(start)s, %(page_len)s""".format(**{ |
| 52 | 'key': searchfield, |
| 53 | 'mcond':get_match_cond(doctype) |
| 54 | }), { |
| 55 | 'txt': "%%%s%%" % txt, |
| 56 | '_txt': txt.replace("%", ""), |
| 57 | 'start': start, |
| 58 | 'page_len': page_len |
| 59 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 60 | |
| 61 | # searches for customer |
| 62 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 63 | conditions = [] |
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"] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 70 | |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 71 | meta = frappe.get_meta("Customer") |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 72 | searchfields = meta.get_search_fields() |
| 73 | searchfields = searchfields + [f for f in [searchfield or "name", "customer_name"] \ |
| 74 | if not f in searchfields] |
| 75 | fields = fields + [f for f in searchfields if not f in fields] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 76 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 77 | fields = ", ".join(fields) |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 78 | searchfields = " or ".join([field + " like %(txt)s" for field in searchfields]) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 79 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 80 | return frappe.db.sql("""select {fields} from `tabCustomer` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 81 | where docstatus < 2 |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 82 | and ({scond}) and disabled=0 |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 83 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 84 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 85 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 86 | if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 87 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 88 | name, customer_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 89 | limit %(start)s, %(page_len)s""".format(**{ |
| 90 | "fields": fields, |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 91 | "scond": searchfields, |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 92 | "mcond": get_match_cond(doctype), |
| 93 | "fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 94 | }), { |
| 95 | 'txt': "%%%s%%" % txt, |
| 96 | '_txt': txt.replace("%", ""), |
| 97 | 'start': start, |
| 98 | 'page_len': page_len |
| 99 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 100 | |
| 101 | # searches for supplier |
| 102 | def supplier_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 103 | supp_master_name = frappe.defaults.get_user_default("supp_master_name") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 104 | if supp_master_name == "Supplier Name": |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 105 | fields = ["name", "supplier_group"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 106 | else: |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 107 | fields = ["name", "supplier_name", "supplier_group"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 108 | fields = ", ".join(fields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 109 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 110 | return frappe.db.sql("""select {field} from `tabSupplier` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 111 | where docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 112 | and ({key} like %(txt)s |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 113 | or supplier_name like %(txt)s) and disabled=0 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 114 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 115 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 116 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 117 | if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 118 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 119 | name, supplier_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 120 | limit %(start)s, %(page_len)s """.format(**{ |
| 121 | 'field': fields, |
| 122 | 'key': searchfield, |
| 123 | 'mcond':get_match_cond(doctype) |
| 124 | }), { |
| 125 | 'txt': "%%%s%%" % txt, |
| 126 | '_txt': txt.replace("%", ""), |
| 127 | 'start': start, |
| 128 | 'page_len': page_len |
| 129 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 130 | |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 131 | def tax_account_query(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 132 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
| 133 | where tabAccount.docstatus!=2 |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 134 | and account_type in (%s) |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 135 | and is_group = 0 |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 136 | and company = %s |
| 137 | and `%s` LIKE %s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 138 | order by idx desc, name |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 139 | limit %s, %s""" % |
| 140 | (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"), |
| 141 | tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt, |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 142 | start, page_len])) |
| 143 | if not tax_accounts: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 144 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 145 | where tabAccount.docstatus!=2 and is_group = 0 |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 146 | and company = %s and `%s` LIKE %s limit %s, %s""" |
| 147 | % ("%s", searchfield, "%s", "%s", "%s"), |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 148 | (filters.get("company"), "%%%s%%" % txt, start, page_len)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 149 | |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 150 | return tax_accounts |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 151 | |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 152 | def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 153 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 154 | |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 155 | description_cond = '' |
| 156 | if frappe.db.count('Item', cache=True) < 50000: |
| 157 | # scan description only if items are less than 50000 |
| 158 | description_cond = 'or tabItem.description LIKE %(txt)s' |
| 159 | |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 160 | return frappe.db.sql("""select tabItem.name, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 161 | if(length(tabItem.item_name) > 40, |
| 162 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 163 | tabItem.item_group, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 164 | if(length(tabItem.description) > 40, \ |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 165 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 166 | from tabItem |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 167 | where tabItem.docstatus < 2 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 168 | and tabItem.has_variants=0 |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 169 | and tabItem.disabled=0 |
Rushabh Mehta | 864d1ea | 2014-06-23 12:20:12 +0530 | [diff] [blame] | 170 | 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] | 171 | and (tabItem.`{key}` LIKE %(txt)s |
creador30 | dae8f1b | 2018-05-04 15:42:03 +0300 | [diff] [blame] | 172 | or tabItem.item_code LIKE %(txt)s |
ShashaQin | 425ab6b | 2016-03-02 13:07:58 +0800 | [diff] [blame] | 173 | or tabItem.item_group LIKE %(txt)s |
Anand Doshi | 7fcb3c9 | 2014-07-21 17:51:14 +0530 | [diff] [blame] | 174 | or tabItem.item_name LIKE %(txt)s |
Nabin Hait | 2b682c8 | 2018-04-04 11:40:16 +0530 | [diff] [blame] | 175 | or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s |
Zarrar | 0e3b000 | 2018-04-05 10:34:30 +0530 | [diff] [blame] | 176 | {description_cond})) |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 177 | {fcond} {mcond} |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 178 | order by |
| 179 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 180 | if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 181 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 182 | name, item_name |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 183 | limit %(start)s, %(page_len)s """.format( |
| 184 | key=searchfield, |
Nabin Hait | c628506 | 2016-03-30 13:10:25 +0530 | [diff] [blame] | 185 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 186 | mcond=get_match_cond(doctype).replace('%', '%%'), |
| 187 | description_cond = description_cond), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 188 | { |
| 189 | "today": nowdate(), |
| 190 | "txt": "%%%s%%" % txt, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 191 | "_txt": txt.replace("%", ""), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 192 | "start": start, |
| 193 | "page_len": page_len |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 194 | }, as_dict=as_dict) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 195 | |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 196 | def bom(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 197 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 198 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 199 | return frappe.db.sql("""select tabBOM.name, tabBOM.item |
| 200 | from tabBOM |
| 201 | where tabBOM.docstatus=1 |
| 202 | and tabBOM.is_active=1 |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 203 | and tabBOM.`{key}` like %(txt)s |
| 204 | {fcond} {mcond} |
| 205 | order by |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 206 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 207 | idx desc, name |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 208 | limit %(start)s, %(page_len)s """.format( |
| 209 | fcond=get_filters_cond(doctype, filters, conditions), |
| 210 | mcond=get_match_cond(doctype), |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 211 | key=frappe.db.escape(searchfield)), |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 212 | { |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 213 | 'txt': "%%%s%%" % frappe.db.escape(txt), |
| 214 | '_txt': txt.replace("%", ""), |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 215 | 'start': start or 0, |
| 216 | 'page_len': page_len or 20 |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 217 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 218 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 219 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 220 | cond = '' |
Nabin Hait | f71011a | 2014-08-21 11:34:31 +0530 | [diff] [blame] | 221 | if filters.get('customer'): |
rohitwaghchaure | e330472 | 2018-08-27 11:43:57 +0530 | [diff] [blame] | 222 | cond = """(`tabProject`.customer = '%s' or |
| 223 | ifnull(`tabProject`.customer,"")="") and""" %(frappe.db.escape(filters.get("customer"))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 224 | |
| 225 | return frappe.db.sql("""select `tabProject`.name from `tabProject` |
| 226 | where `tabProject`.status not in ("Completed", "Cancelled") |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 227 | and {cond} `tabProject`.name like %(txt)s {match_cond} |
| 228 | order by |
| 229 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 230 | idx desc, |
| 231 | `tabProject`.name asc |
| 232 | limit {start}, {page_len}""".format( |
| 233 | cond=cond, |
| 234 | match_cond=get_match_cond(doctype), |
| 235 | start=start, |
| 236 | page_len=page_len), { |
| 237 | "txt": "%{0}%".format(txt), |
Nabin Hait | df4deba | 2016-03-16 11:16:31 +0530 | [diff] [blame] | 238 | "_txt": txt.replace('%', '') |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 239 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 240 | |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 241 | |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 242 | def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict): |
| 243 | return frappe.db.sql(""" |
| 244 | select `tabDelivery Note`.name, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 245 | from `tabDelivery Note` |
| 246 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 247 | `tabDelivery Note`.docstatus = 1 |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 248 | and status not in ("Stopped", "Closed") %(fcond)s |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 249 | and ( |
| 250 | (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100) |
| 251 | or `tabDelivery Note`.grand_total = 0 |
| 252 | or ( |
| 253 | `tabDelivery Note`.is_return = 1 |
| 254 | and return_against in (select name from `tabDelivery Note` where per_billed < 100) |
| 255 | ) |
| 256 | ) |
Anand Doshi | 17350b8 | 2013-08-01 15:45:23 +0530 | [diff] [blame] | 257 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 258 | """ % { |
| 259 | "key": searchfield, |
| 260 | "fcond": get_filters_cond(doctype, filters, []), |
| 261 | "mcond": get_match_cond(doctype), |
| 262 | "txt": "%(txt)s" |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 263 | }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict) |
| 264 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 265 | |
| 266 | def get_batch_no(doctype, txt, searchfield, start, page_len, filters): |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 267 | cond = "" |
| 268 | if filters.get("posting_date"): |
Nabin Hait | 7918b92 | 2018-01-31 15:30:03 +0530 | [diff] [blame] | 269 | cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)" |
Rushabh Mehta | b6398be | 2015-08-26 10:50:16 +0530 | [diff] [blame] | 270 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 271 | batch_nos = None |
| 272 | args = { |
| 273 | 'item_code': filters.get("item_code"), |
| 274 | 'warehouse': filters.get("warehouse"), |
| 275 | 'posting_date': filters.get('posting_date'), |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 276 | 'txt': "%{0}%".format(txt), |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 277 | "start": start, |
| 278 | "page_len": page_len |
| 279 | } |
| 280 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 281 | if args.get('warehouse'): |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 282 | batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date) |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 283 | from `tabStock Ledger Entry` sle |
| 284 | INNER JOIN `tabBatch` batch on sle.batch_no = batch.name |
| 285 | where |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 286 | batch.disabled = 0 |
| 287 | and sle.item_code = %(item_code)s |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 288 | and sle.warehouse = %(warehouse)s |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 289 | and (sle.batch_no like %(txt)s |
| 290 | or batch.manufacturing_date like %(txt)s) |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 291 | and batch.docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 292 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 293 | {match_conditions} |
| 294 | group by batch_no having sum(sle.actual_qty) > 0 |
| 295 | order by batch.expiry_date, sle.batch_no desc |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 296 | 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] | 297 | |
| 298 | if batch_nos: |
| 299 | return batch_nos |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 300 | else: |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 301 | return frappe.db.sql("""select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date) from `tabBatch` batch |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 302 | where batch.disabled = 0 |
| 303 | and item = %(item_code)s |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 304 | and (name like %(txt)s |
| 305 | or manufacturing_date like %(txt)s) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 306 | and docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 307 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 308 | {match_conditions} |
| 309 | order by expiry_date, name desc |
Nabin Hait | e52ee55 | 2015-09-02 10:55:32 +0530 | [diff] [blame] | 310 | 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] | 311 | |
| 312 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 313 | filter_list = [] |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 314 | |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 315 | if isinstance(filters, dict): |
| 316 | for key, val in filters.items(): |
| 317 | if isinstance(val, (list, tuple)): |
| 318 | filter_list.append([doctype, key, val[0], val[1]]) |
| 319 | else: |
| 320 | filter_list.append([doctype, key, "=", val]) |
bhupeshg2 | e2e973f | 2015-04-14 22:15:24 +0530 | [diff] [blame] | 321 | elif isinstance(filters, list): |
| 322 | filter_list.extend(filters) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 323 | |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 324 | if "is_group" not in [d[1] for d in filter_list]: |
| 325 | filter_list.append(["Account", "is_group", "=", "0"]) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 326 | |
| 327 | if searchfield and txt: |
| 328 | filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt]) |
| 329 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 330 | return frappe.desk.reportview.execute("Account", filters = filter_list, |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 331 | fields = ["name", "parent_account"], |
| 332 | limit_start=start, limit_page_length=page_len, as_list=True) |
Anand Doshi | faefeaa | 2014-06-24 18:53:04 +0530 | [diff] [blame] | 333 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 334 | |
| 335 | @frappe.whitelist() |
| 336 | def get_income_account(doctype, txt, searchfield, start, page_len, filters): |
| 337 | from erpnext.controllers.queries import get_match_cond |
| 338 | |
| 339 | # income account can be any Credit account, |
| 340 | # but can also be a Asset account with account_type='Income Account' in special circumstances. |
| 341 | # Hence the first condition is an "OR" |
| 342 | if not filters: filters = {} |
| 343 | |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 344 | condition = "" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 345 | if filters.get("company"): |
| 346 | condition += "and tabAccount.company = %(company)s" |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 347 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 348 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 349 | where (tabAccount.report_type = "Profit and Loss" |
| 350 | or tabAccount.account_type in ("Income Account", "Temporary")) |
| 351 | and tabAccount.is_group=0 |
| 352 | and tabAccount.`{key}` LIKE %(txt)s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 353 | {condition} {match_condition} |
| 354 | order by idx desc, name""" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 355 | .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), { |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 356 | 'txt': "%%%s%%" % frappe.db.escape(txt), |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 357 | 'company': filters.get("company", "") |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 358 | }) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 359 | |
| 360 | |
| 361 | @frappe.whitelist() |
| 362 | def get_expense_account(doctype, txt, searchfield, start, page_len, filters): |
| 363 | from erpnext.controllers.queries import get_match_cond |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 364 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 365 | if not filters: filters = {} |
| 366 | |
| 367 | condition = "" |
| 368 | if filters.get("company"): |
| 369 | condition += "and tabAccount.company = %(company)s" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 370 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 371 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 372 | where (tabAccount.report_type = "Profit and Loss" |
Rohit Waghchaure | 585945e | 2018-05-17 00:22:29 +0530 | [diff] [blame] | 373 | or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed")) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 374 | and tabAccount.is_group=0 |
| 375 | and tabAccount.docstatus!=2 |
| 376 | and tabAccount.{key} LIKE %(txt)s |
| 377 | {condition} {match_condition}""" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 378 | .format(condition=condition, key=frappe.db.escape(searchfield), |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 379 | match_condition=get_match_cond(doctype)), { |
Neil Trini Lasrado | 30b97b0 | 2016-03-31 23:10:13 +0530 | [diff] [blame] | 380 | 'company': filters.get("company", ""), |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 381 | 'txt': "%%%s%%" % frappe.db.escape(txt) |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 382 | }) |
suyashphadtare | 049a88c | 2017-01-12 17:49:37 +0530 | [diff] [blame] | 383 | |
| 384 | |
| 385 | @frappe.whitelist() |
| 386 | def warehouse_query(doctype, txt, searchfield, start, page_len, filters): |
| 387 | # Should be used when item code is passed in filters. |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 388 | conditions, bin_conditions = [], [] |
| 389 | filter_dict = get_doctype_wise_filters(filters) |
| 390 | |
| 391 | sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin` |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 392 | where `tabBin`.warehouse = `tabWarehouse`.name |
| 393 | {bin_conditions} """.format( |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 394 | bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"), |
Nabin Hait | 4e6ff8c | 2017-05-09 15:09:10 +0530 | [diff] [blame] | 395 | bin_conditions, ignore_permissions=True)) |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 396 | |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 397 | query = """select `tabWarehouse`.name, |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 398 | CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty |
| 399 | from `tabWarehouse` |
| 400 | where |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 401 | `tabWarehouse`.`{key}` like '{txt}' |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 402 | {fcond} {mcond} |
| 403 | order by |
| 404 | `tabWarehouse`.name desc |
| 405 | limit |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 406 | {start}, {page_len} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 407 | """.format( |
| 408 | sub_query=sub_query, |
| 409 | key=frappe.db.escape(searchfield), |
| 410 | fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions), |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 411 | mcond=get_match_cond(doctype), |
| 412 | start=start, |
| 413 | page_len=page_len, |
| 414 | txt=frappe.db.escape('%{0}%'.format(txt)) |
| 415 | ) |
| 416 | |
| 417 | return frappe.db.sql(query) |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 418 | |
| 419 | |
| 420 | def get_doctype_wise_filters(filters): |
| 421 | # Helper function to seperate filters doctype_wise |
| 422 | filter_dict = defaultdict(list) |
| 423 | for row in filters: |
| 424 | filter_dict[row[0]].append(row) |
| 425 | return filter_dict |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 426 | |
| 427 | |
| 428 | @frappe.whitelist() |
| 429 | def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters): |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 430 | query = """select batch_id from `tabBatch` |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 431 | where disabled = 0 |
| 432 | and (expiry_date >= CURDATE() or expiry_date IS NULL) |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 433 | and name like '{txt}'""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 434 | |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 435 | if filters and filters.get('item'): |
| 436 | query += " and item = '{item}'".format(item = frappe.db.escape(filters.get('item'))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 437 | |
Sachin Mane | 64f48db | 2018-01-08 17:57:32 +0530 | [diff] [blame] | 438 | return frappe.db.sql(query, filters) |