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 |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 6 | import erpnext |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 7 | from frappe.desk.reportview import get_match_cond, get_filters_cond |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 8 | from frappe.utils import nowdate, getdate |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 9 | from collections import defaultdict |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 10 | from erpnext.stock.get_item_details import _get_item_tax_template |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 11 | from frappe.utils import unique |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 12 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 13 | # searches for active employees |
| 14 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 15 | conditions = [] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 16 | fields = get_fields("Employee", ["name", "employee_name"]) |
| 17 | |
| 18 | return frappe.db.sql("""select {fields} from `tabEmployee` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 19 | where status = 'Active' |
| 20 | and docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 21 | and ({key} like %(txt)s |
| 22 | or employee_name like %(txt)s) |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 23 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 24 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 25 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 26 | if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 27 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 28 | name, employee_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 29 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 30 | 'fields': ", ".join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 31 | 'key': searchfield, |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 32 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 33 | 'mcond': get_match_cond(doctype) |
| 34 | }), { |
| 35 | 'txt': "%%%s%%" % txt, |
| 36 | '_txt': txt.replace("%", ""), |
| 37 | 'start': start, |
| 38 | 'page_len': page_len |
| 39 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 40 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 41 | |
| 42 | # searches for leads which are not converted |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 43 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 44 | fields = get_fields("Lead", ["name", "lead_name", "company_name"]) |
| 45 | |
| 46 | return frappe.db.sql("""select {fields} from `tabLead` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 47 | where docstatus < 2 |
| 48 | and ifnull(status, '') != 'Converted' |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 49 | and ({key} like %(txt)s |
| 50 | or lead_name like %(txt)s |
| 51 | or company_name like %(txt)s) |
| 52 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 53 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 54 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 55 | if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999), |
| 56 | if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 57 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 58 | name, lead_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 59 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 60 | 'fields': ", ".join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 61 | 'key': searchfield, |
| 62 | 'mcond':get_match_cond(doctype) |
| 63 | }), { |
| 64 | 'txt': "%%%s%%" % txt, |
| 65 | '_txt': txt.replace("%", ""), |
| 66 | 'start': start, |
| 67 | 'page_len': page_len |
| 68 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 69 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 70 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 71 | # searches for customer |
| 72 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 73 | conditions = [] |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 74 | cust_master_name = frappe.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 75 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 76 | if cust_master_name == "Customer Name": |
| 77 | fields = ["name", "customer_group", "territory"] |
| 78 | else: |
| 79 | fields = ["name", "customer_name", "customer_group", "territory"] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 80 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 81 | fields = get_fields("Customer", fields) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 82 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 83 | searchfields = frappe.get_meta("Customer").get_search_fields() |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 84 | searchfields = " or ".join([field + " like %(txt)s" for field in searchfields]) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 85 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 86 | return frappe.db.sql("""select {fields} from `tabCustomer` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 87 | where docstatus < 2 |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 88 | and ({scond}) and disabled=0 |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 89 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 90 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 91 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 92 | if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 93 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 94 | name, customer_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 95 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 96 | "fields": ", ".join(fields), |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 97 | "scond": searchfields, |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 98 | "mcond": get_match_cond(doctype), |
| 99 | "fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 100 | }), { |
| 101 | 'txt': "%%%s%%" % txt, |
| 102 | '_txt': txt.replace("%", ""), |
| 103 | 'start': start, |
| 104 | 'page_len': page_len |
| 105 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 106 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 107 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 108 | # searches for supplier |
| 109 | def supplier_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 110 | supp_master_name = frappe.defaults.get_user_default("supp_master_name") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 111 | if supp_master_name == "Supplier Name": |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 112 | fields = ["name", "supplier_group"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 113 | else: |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 114 | fields = ["name", "supplier_name", "supplier_group"] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 115 | |
| 116 | fields = get_fields("Supplier", fields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 117 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 118 | return frappe.db.sql("""select {field} from `tabSupplier` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 119 | where docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 120 | and ({key} like %(txt)s |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 121 | or supplier_name like %(txt)s) and disabled=0 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 122 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 123 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 124 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 125 | if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 126 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 127 | name, supplier_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 128 | limit %(start)s, %(page_len)s """.format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 129 | 'field': ', '.join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 130 | 'key': searchfield, |
| 131 | 'mcond':get_match_cond(doctype) |
| 132 | }), { |
| 133 | 'txt': "%%%s%%" % txt, |
| 134 | '_txt': txt.replace("%", ""), |
| 135 | 'start': start, |
| 136 | 'page_len': page_len |
| 137 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 138 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 139 | |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 140 | def tax_account_query(doctype, txt, searchfield, start, page_len, filters): |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 141 | company_currency = erpnext.get_company_currency(filters.get('company')) |
| 142 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 143 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
| 144 | where tabAccount.docstatus!=2 |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 145 | and account_type in (%s) |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 146 | and is_group = 0 |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 147 | and company = %s |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 148 | and account_currency = %s |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 149 | and `%s` LIKE %s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 150 | order by idx desc, name |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 151 | limit %s, %s""" % |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 152 | (", ".join(['%s']*len(filters.get("account_type"))), "%s", "%s", searchfield, "%s", "%s", "%s"), |
| 153 | tuple(filters.get("account_type") + [filters.get("company"), company_currency, "%%%s%%" % txt, |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 154 | start, page_len])) |
| 155 | if not tax_accounts: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 156 | tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 157 | where tabAccount.docstatus!=2 and is_group = 0 |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 158 | and company = %s and account_currency = %s and `%s` LIKE %s limit %s, %s""" #nosec |
| 159 | % ("%s", "%s", searchfield, "%s", "%s", "%s"), |
| 160 | (filters.get("company"), company_currency, "%%%s%%" % txt, start, page_len)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 162 | return tax_accounts |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 163 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 164 | |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 165 | def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 166 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 167 | |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 168 | #Get searchfields from meta and use in Item Link field query |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 169 | meta = frappe.get_meta("Item", cached=True) |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 170 | searchfields = meta.get_search_fields() |
| 171 | |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 172 | if "description" in searchfields: |
| 173 | searchfields.remove("description") |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 174 | |
Rohit Waghchaure | c42312e | 2019-11-19 19:05:23 +0530 | [diff] [blame] | 175 | columns = '' |
| 176 | extra_searchfields = [field for field in searchfields |
| 177 | if not field in ["name", "item_group", "description"]] |
| 178 | |
| 179 | if extra_searchfields: |
| 180 | columns = ", " + ", ".join(extra_searchfields) |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 181 | |
| 182 | searchfields = searchfields + [field for field in[searchfield or "name", "item_code", "item_group", "item_name"] |
| 183 | if not field in searchfields] |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 184 | searchfields = " or ".join([field + " like %(txt)s" for field in searchfields]) |
| 185 | |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 186 | description_cond = '' |
| 187 | if frappe.db.count('Item', cache=True) < 50000: |
| 188 | # scan description only if items are less than 50000 |
| 189 | description_cond = 'or tabItem.description LIKE %(txt)s' |
| 190 | |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 191 | return frappe.db.sql("""select tabItem.name, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 192 | if(length(tabItem.item_name) > 40, |
| 193 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 194 | tabItem.item_group, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 195 | if(length(tabItem.description) > 40, \ |
Rohit Waghchaure | c42312e | 2019-11-19 19:05:23 +0530 | [diff] [blame] | 196 | concat(substr(tabItem.description, 1, 40), "..."), description) as description |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 197 | {columns} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 198 | from tabItem |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 199 | where tabItem.docstatus < 2 |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 200 | and tabItem.disabled=0 |
rohitwaghchaure | 7978907 | 2020-05-21 18:10:13 +0530 | [diff] [blame] | 201 | and tabItem.has_variants=0 |
Rushabh Mehta | 864d1ea | 2014-06-23 12:20:12 +0530 | [diff] [blame] | 202 | and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00') |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 203 | and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s) |
Rohit Waghchaure | 2bfb063 | 2019-03-02 21:47:55 +0530 | [diff] [blame] | 204 | {description_cond}) |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 205 | {fcond} {mcond} |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 206 | order by |
| 207 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 208 | if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 209 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 210 | name, item_name |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 211 | limit %(start)s, %(page_len)s """.format( |
| 212 | key=searchfield, |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 213 | columns=columns, |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 214 | scond=searchfields, |
Nabin Hait | c628506 | 2016-03-30 13:10:25 +0530 | [diff] [blame] | 215 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 216 | mcond=get_match_cond(doctype).replace('%', '%%'), |
| 217 | description_cond = description_cond), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 218 | { |
| 219 | "today": nowdate(), |
| 220 | "txt": "%%%s%%" % txt, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 221 | "_txt": txt.replace("%", ""), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 222 | "start": start, |
| 223 | "page_len": page_len |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 224 | }, as_dict=as_dict) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 225 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 226 | |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 227 | def bom(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 228 | conditions = [] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 229 | fields = get_fields("BOM", ["name", "item"]) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 230 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 231 | return frappe.db.sql("""select {fields} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 232 | from tabBOM |
| 233 | where tabBOM.docstatus=1 |
| 234 | and tabBOM.is_active=1 |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 235 | and tabBOM.`{key}` like %(txt)s |
| 236 | {fcond} {mcond} |
| 237 | order by |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 238 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 239 | idx desc, name |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 240 | limit %(start)s, %(page_len)s """.format( |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 241 | fields=", ".join(fields), |
Mangesh-Khairnar | 6a79691 | 2019-07-08 10:40:40 +0530 | [diff] [blame] | 242 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Karthikeyan S | 747c262 | 2019-07-19 22:49:21 +0530 | [diff] [blame] | 243 | mcond=get_match_cond(doctype).replace('%', '%%'), |
| 244 | key=searchfield), |
Mangesh-Khairnar | 6a79691 | 2019-07-08 10:40:40 +0530 | [diff] [blame] | 245 | { |
Karthikeyan S | 747c262 | 2019-07-19 22:49:21 +0530 | [diff] [blame] | 246 | 'txt': '%' + txt + '%', |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 247 | '_txt': txt.replace("%", ""), |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 248 | 'start': start or 0, |
| 249 | 'page_len': page_len or 20 |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 250 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 251 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 252 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 253 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 254 | cond = '' |
Nabin Hait | f71011a | 2014-08-21 11:34:31 +0530 | [diff] [blame] | 255 | if filters.get('customer'): |
Suraj Shetty | 6ea3de9 | 2018-09-26 18:15:53 +0530 | [diff] [blame] | 256 | cond = """(`tabProject`.customer = %s or |
rohitwaghchaure | e330472 | 2018-08-27 11:43:57 +0530 | [diff] [blame] | 257 | ifnull(`tabProject`.customer,"")="") and""" %(frappe.db.escape(filters.get("customer"))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 258 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 259 | fields = get_fields("Project", ["name"]) |
| 260 | |
| 261 | return frappe.db.sql("""select {fields} from `tabProject` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 262 | where `tabProject`.status not in ("Completed", "Cancelled") |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 263 | and {cond} `tabProject`.name like %(txt)s {match_cond} |
| 264 | order by |
| 265 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 266 | idx desc, |
| 267 | `tabProject`.name asc |
| 268 | limit {start}, {page_len}""".format( |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 269 | fields=", ".join(['`tabProject`.{0}'.format(f) for f in fields]), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 270 | cond=cond, |
| 271 | match_cond=get_match_cond(doctype), |
| 272 | start=start, |
| 273 | page_len=page_len), { |
| 274 | "txt": "%{0}%".format(txt), |
Nabin Hait | df4deba | 2016-03-16 11:16:31 +0530 | [diff] [blame] | 275 | "_txt": txt.replace('%', '') |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 276 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 277 | |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 278 | |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 279 | def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict): |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 280 | fields = get_fields("Delivery Note", ["name", "customer", "posting_date"]) |
| 281 | |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 282 | return frappe.db.sql(""" |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 283 | select %(fields)s |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 284 | from `tabDelivery Note` |
| 285 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 286 | `tabDelivery Note`.docstatus = 1 |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 287 | and status not in ("Stopped", "Closed") %(fcond)s |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 288 | and ( |
| 289 | (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100) |
| 290 | or `tabDelivery Note`.grand_total = 0 |
| 291 | or ( |
| 292 | `tabDelivery Note`.is_return = 1 |
| 293 | and return_against in (select name from `tabDelivery Note` where per_billed < 100) |
| 294 | ) |
| 295 | ) |
rohitwaghchaure | d07a3e1 | 2019-05-15 07:46:28 +0530 | [diff] [blame] | 296 | %(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(start)s, %(page_len)s |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 297 | """ % { |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 298 | "fields": ", ".join(["`tabDelivery Note`.{0}".format(f) for f in fields]), |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 299 | "key": searchfield, |
| 300 | "fcond": get_filters_cond(doctype, filters, []), |
| 301 | "mcond": get_match_cond(doctype), |
rohitwaghchaure | d07a3e1 | 2019-05-15 07:46:28 +0530 | [diff] [blame] | 302 | "start": start, |
| 303 | "page_len": page_len, |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 304 | "txt": "%(txt)s" |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 305 | }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict) |
| 306 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 307 | |
| 308 | def get_batch_no(doctype, txt, searchfield, start, page_len, filters): |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 309 | cond = "" |
| 310 | if filters.get("posting_date"): |
Nabin Hait | 7918b92 | 2018-01-31 15:30:03 +0530 | [diff] [blame] | 311 | 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] | 312 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 313 | batch_nos = None |
| 314 | args = { |
| 315 | 'item_code': filters.get("item_code"), |
| 316 | 'warehouse': filters.get("warehouse"), |
| 317 | 'posting_date': filters.get('posting_date'), |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 318 | 'txt': "%{0}%".format(txt), |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 319 | "start": start, |
| 320 | "page_len": page_len |
| 321 | } |
| 322 | |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 323 | having_clause = "having sum(sle.actual_qty) > 0" |
| 324 | if filters.get("is_return"): |
| 325 | having_clause = "" |
| 326 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 327 | if args.get('warehouse'): |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 328 | batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, |
| 329 | concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date) |
| 330 | from `tabStock Ledger Entry` sle |
| 331 | INNER JOIN `tabBatch` batch on sle.batch_no = batch.name |
| 332 | where |
| 333 | batch.disabled = 0 |
| 334 | and sle.item_code = %(item_code)s |
| 335 | and sle.warehouse = %(warehouse)s |
| 336 | and (sle.batch_no like %(txt)s |
Sun Howwrongbum | 088be37 | 2019-12-24 12:29:25 +0530 | [diff] [blame] | 337 | or batch.expiry_date like %(txt)s |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 338 | or batch.manufacturing_date like %(txt)s) |
| 339 | and batch.docstatus < 2 |
| 340 | {cond} |
| 341 | {match_conditions} |
| 342 | group by batch_no {having_clause} |
| 343 | order by batch.expiry_date, sle.batch_no desc |
| 344 | limit %(start)s, %(page_len)s""".format( |
| 345 | cond=cond, |
| 346 | match_conditions=get_match_cond(doctype), |
| 347 | having_clause = having_clause |
| 348 | ), args) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 349 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 350 | return batch_nos |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 351 | else: |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 352 | 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] | 353 | where batch.disabled = 0 |
| 354 | and item = %(item_code)s |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 355 | and (name like %(txt)s |
Sun Howwrongbum | 088be37 | 2019-12-24 12:29:25 +0530 | [diff] [blame] | 356 | or expiry_date like %(txt)s |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 357 | or manufacturing_date like %(txt)s) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 358 | and docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 359 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 360 | {match_conditions} |
| 361 | order by expiry_date, name desc |
Nabin Hait | e52ee55 | 2015-09-02 10:55:32 +0530 | [diff] [blame] | 362 | 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] | 363 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 364 | |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 365 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 366 | filter_list = [] |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 367 | |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 368 | if isinstance(filters, dict): |
| 369 | for key, val in filters.items(): |
| 370 | if isinstance(val, (list, tuple)): |
| 371 | filter_list.append([doctype, key, val[0], val[1]]) |
| 372 | else: |
| 373 | filter_list.append([doctype, key, "=", val]) |
bhupeshg2 | e2e973f | 2015-04-14 22:15:24 +0530 | [diff] [blame] | 374 | elif isinstance(filters, list): |
| 375 | filter_list.extend(filters) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 376 | |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 377 | if "is_group" not in [d[1] for d in filter_list]: |
| 378 | filter_list.append(["Account", "is_group", "=", "0"]) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 379 | |
| 380 | if searchfield and txt: |
| 381 | filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt]) |
| 382 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 383 | return frappe.desk.reportview.execute("Account", filters = filter_list, |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 384 | fields = ["name", "parent_account"], |
| 385 | limit_start=start, limit_page_length=page_len, as_list=True) |
Anand Doshi | faefeaa | 2014-06-24 18:53:04 +0530 | [diff] [blame] | 386 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 387 | |
Marica | 299e217 | 2020-04-28 13:00:04 +0530 | [diff] [blame] | 388 | def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters): |
| 389 | return frappe.db.sql("""select distinct bo.name, bo.blanket_order_type, bo.to_date |
| 390 | from `tabBlanket Order` bo, `tabBlanket Order Item` boi |
| 391 | where |
| 392 | boi.parent = bo.name |
| 393 | and boi.item_code = {item_code} |
| 394 | and bo.blanket_order_type = '{blanket_order_type}' |
| 395 | and bo.company = {company} |
| 396 | and bo.docstatus = 1""" |
| 397 | .format(item_code = frappe.db.escape(filters.get("item")), |
| 398 | blanket_order_type = filters.get("blanket_order_type"), |
| 399 | company = frappe.db.escape(filters.get("company")) |
| 400 | )) |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 401 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 402 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 403 | @frappe.whitelist() |
| 404 | def get_income_account(doctype, txt, searchfield, start, page_len, filters): |
| 405 | from erpnext.controllers.queries import get_match_cond |
| 406 | |
| 407 | # income account can be any Credit account, |
| 408 | # but can also be a Asset account with account_type='Income Account' in special circumstances. |
| 409 | # Hence the first condition is an "OR" |
| 410 | if not filters: filters = {} |
| 411 | |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 412 | condition = "" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 413 | if filters.get("company"): |
| 414 | condition += "and tabAccount.company = %(company)s" |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 415 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 416 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 417 | where (tabAccount.report_type = "Profit and Loss" |
| 418 | or tabAccount.account_type in ("Income Account", "Temporary")) |
| 419 | and tabAccount.is_group=0 |
| 420 | and tabAccount.`{key}` LIKE %(txt)s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 421 | {condition} {match_condition} |
| 422 | order by idx desc, name""" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 423 | .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), { |
Suraj Shetty | 4b404c4 | 2018-09-27 15:39:34 +0530 | [diff] [blame] | 424 | 'txt': '%' + txt + '%', |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 425 | 'company': filters.get("company", "") |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 426 | }) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 427 | |
| 428 | |
| 429 | @frappe.whitelist() |
| 430 | def get_expense_account(doctype, txt, searchfield, start, page_len, filters): |
| 431 | from erpnext.controllers.queries import get_match_cond |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 432 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 433 | if not filters: filters = {} |
| 434 | |
| 435 | condition = "" |
| 436 | if filters.get("company"): |
| 437 | condition += "and tabAccount.company = %(company)s" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 438 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 439 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 440 | where (tabAccount.report_type = "Profit and Loss" |
Mangesh-Khairnar | 5619db2 | 2019-08-21 14:49:24 +0530 | [diff] [blame] | 441 | or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed", "Capital Work in Progress")) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 442 | and tabAccount.is_group=0 |
| 443 | and tabAccount.docstatus!=2 |
| 444 | and tabAccount.{key} LIKE %(txt)s |
| 445 | {condition} {match_condition}""" |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 446 | .format(condition=condition, key=searchfield, |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 447 | match_condition=get_match_cond(doctype)), { |
Neil Trini Lasrado | 30b97b0 | 2016-03-31 23:10:13 +0530 | [diff] [blame] | 448 | 'company': filters.get("company", ""), |
Suraj Shetty | 4b404c4 | 2018-09-27 15:39:34 +0530 | [diff] [blame] | 449 | 'txt': '%' + txt + '%' |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 450 | }) |
suyashphadtare | 049a88c | 2017-01-12 17:49:37 +0530 | [diff] [blame] | 451 | |
| 452 | |
| 453 | @frappe.whitelist() |
| 454 | def warehouse_query(doctype, txt, searchfield, start, page_len, filters): |
| 455 | # Should be used when item code is passed in filters. |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 456 | conditions, bin_conditions = [], [] |
| 457 | filter_dict = get_doctype_wise_filters(filters) |
| 458 | |
| 459 | sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin` |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 460 | where `tabBin`.warehouse = `tabWarehouse`.name |
| 461 | {bin_conditions} """.format( |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 462 | bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"), |
Nabin Hait | 4e6ff8c | 2017-05-09 15:09:10 +0530 | [diff] [blame] | 463 | bin_conditions, ignore_permissions=True)) |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 464 | |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 465 | query = """select `tabWarehouse`.name, |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 466 | CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty |
| 467 | from `tabWarehouse` |
| 468 | where |
Suraj Shetty | 6ea3de9 | 2018-09-26 18:15:53 +0530 | [diff] [blame] | 469 | `tabWarehouse`.`{key}` like {txt} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 470 | {fcond} {mcond} |
| 471 | order by |
| 472 | `tabWarehouse`.name desc |
| 473 | limit |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 474 | {start}, {page_len} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 475 | """.format( |
| 476 | sub_query=sub_query, |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 477 | key=searchfield, |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 478 | fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions), |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 479 | mcond=get_match_cond(doctype), |
| 480 | start=start, |
| 481 | page_len=page_len, |
| 482 | txt=frappe.db.escape('%{0}%'.format(txt)) |
| 483 | ) |
| 484 | |
| 485 | return frappe.db.sql(query) |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 486 | |
| 487 | |
| 488 | def get_doctype_wise_filters(filters): |
| 489 | # Helper function to seperate filters doctype_wise |
| 490 | filter_dict = defaultdict(list) |
| 491 | for row in filters: |
| 492 | filter_dict[row[0]].append(row) |
| 493 | return filter_dict |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 494 | |
| 495 | |
| 496 | @frappe.whitelist() |
| 497 | def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters): |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 498 | query = """select batch_id from `tabBatch` |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 499 | where disabled = 0 |
| 500 | and (expiry_date >= CURDATE() or expiry_date IS NULL) |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 501 | and name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 502 | |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 503 | if filters and filters.get('item'): |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 504 | query += " and item = {item}".format(item = frappe.db.escape(filters.get('item'))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 505 | |
Sachin Mane | 64f48db | 2018-01-08 17:57:32 +0530 | [diff] [blame] | 506 | return frappe.db.sql(query, filters) |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 507 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 508 | |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 509 | @frappe.whitelist() |
| 510 | def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters): |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 511 | item_filters = [ |
| 512 | ['manufacturer', 'like', '%' + txt + '%'], |
| 513 | ['item_code', '=', filters.get("item_code")] |
| 514 | ] |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 515 | |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 516 | item_manufacturers = frappe.get_all( |
| 517 | "Item Manufacturer", |
| 518 | fields=["manufacturer", "manufacturer_part_no"], |
| 519 | filters=item_filters, |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 520 | limit_start=start, |
| 521 | limit_page_length=page_len, |
| 522 | as_list=1 |
| 523 | ) |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 524 | return item_manufacturers |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 525 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 526 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 527 | @frappe.whitelist() |
| 528 | def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters): |
| 529 | query = """ |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 530 | select pr.name |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 531 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem |
| 532 | where pr.docstatus = 1 and pritem.parent = pr.name |
| 533 | and pr.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
| 534 | |
| 535 | if filters and filters.get('item_code'): |
| 536 | query += " and pritem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code'))) |
| 537 | |
| 538 | return frappe.db.sql(query, filters) |
| 539 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 540 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 541 | @frappe.whitelist() |
| 542 | def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters): |
| 543 | query = """ |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 544 | select pi.name |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 545 | from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem |
| 546 | where pi.docstatus = 1 and piitem.parent = pi.name |
| 547 | and pi.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
| 548 | |
| 549 | if filters and filters.get('item_code'): |
| 550 | query += " and piitem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code'))) |
| 551 | |
| 552 | return frappe.db.sql(query, filters) |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 553 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 554 | |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 555 | @frappe.whitelist() |
| 556 | def get_tax_template(doctype, txt, searchfield, start, page_len, filters): |
| 557 | |
| 558 | item_doc = frappe.get_cached_doc('Item', filters.get('item_code')) |
| 559 | item_group = filters.get('item_group') |
| 560 | taxes = item_doc.taxes or [] |
| 561 | |
| 562 | while item_group: |
| 563 | item_group_doc = frappe.get_cached_doc('Item Group', item_group) |
| 564 | taxes += item_group_doc.taxes or [] |
| 565 | item_group = item_group_doc.parent_item_group |
| 566 | |
| 567 | if not taxes: |
| 568 | return frappe.db.sql(""" SELECT name FROM `tabItem Tax Template` """) |
| 569 | else: |
| 570 | args = { |
| 571 | 'item_code': filters.get('item_code'), |
| 572 | 'posting_date': filters.get('valid_from'), |
| 573 | 'tax_category': filters.get('tax_category') |
| 574 | } |
| 575 | |
| 576 | taxes = _get_item_tax_template(args, taxes, for_validate=True) |
| 577 | return [(d,) for d in set(taxes)] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 578 | |
| 579 | |
| 580 | def get_fields(doctype, fields=[]): |
| 581 | meta = frappe.get_meta(doctype) |
| 582 | fields.extend(meta.get_search_fields()) |
| 583 | |
| 584 | if meta.title_field and not meta.title_field.strip() in fields: |
| 585 | fields.insert(1, meta.title_field.strip()) |
| 586 | |
| 587 | return unique(fields) |