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