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 |
rohitwaghchaure | c59371a | 2021-06-03 20:02:58 +0530 | [diff] [blame] | 7 | import json |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 8 | from frappe.desk.reportview import get_match_cond, get_filters_cond |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 9 | from frappe.utils import nowdate, getdate |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 10 | from collections import defaultdict |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 11 | from erpnext.stock.get_item_details import _get_item_tax_template |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 12 | from frappe.utils import unique |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 13 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 14 | # searches for active employees |
| 15 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 16 | @frappe.validate_and_sanitize_search_inputs |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 17 | def employee_query(doctype, txt, searchfield, start, page_len, filters): |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 18 | conditions = [] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 19 | fields = get_fields("Employee", ["name", "employee_name"]) |
| 20 | |
| 21 | return frappe.db.sql("""select {fields} from `tabEmployee` |
Anurag Mishra | fc98abe | 2021-06-23 11:21:38 +0530 | [diff] [blame] | 22 | where status in ('Active', 'Suspended') |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 23 | and docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 24 | and ({key} like %(txt)s |
| 25 | or employee_name like %(txt)s) |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 26 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 27 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 28 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 29 | if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 30 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 31 | name, employee_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 32 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 33 | 'fields': ", ".join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 34 | 'key': searchfield, |
Kanchan Chauhan | 7652b85 | 2016-11-16 15:29:01 +0530 | [diff] [blame] | 35 | 'fcond': get_filters_cond(doctype, filters, conditions), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 36 | 'mcond': get_match_cond(doctype) |
| 37 | }), { |
| 38 | 'txt': "%%%s%%" % txt, |
| 39 | '_txt': txt.replace("%", ""), |
| 40 | 'start': start, |
| 41 | 'page_len': page_len |
| 42 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 43 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 44 | |
| 45 | # searches for leads which are not converted |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 46 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 47 | @frappe.validate_and_sanitize_search_inputs |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 48 | def lead_query(doctype, txt, searchfield, start, page_len, filters): |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 49 | fields = get_fields("Lead", ["name", "lead_name", "company_name"]) |
| 50 | |
| 51 | return frappe.db.sql("""select {fields} from `tabLead` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 52 | where docstatus < 2 |
| 53 | and ifnull(status, '') != 'Converted' |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 54 | and ({key} like %(txt)s |
| 55 | or lead_name like %(txt)s |
| 56 | or company_name like %(txt)s) |
| 57 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 58 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 59 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 60 | if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999), |
| 61 | if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 62 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 63 | name, lead_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 64 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 65 | 'fields': ", ".join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 66 | 'key': searchfield, |
| 67 | 'mcond':get_match_cond(doctype) |
| 68 | }), { |
| 69 | 'txt': "%%%s%%" % txt, |
| 70 | '_txt': txt.replace("%", ""), |
| 71 | 'start': start, |
| 72 | 'page_len': page_len |
| 73 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 74 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 75 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 76 | # searches for customer |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 77 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 78 | @frappe.validate_and_sanitize_search_inputs |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 79 | def customer_query(doctype, txt, searchfield, start, page_len, filters): |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 80 | conditions = [] |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 81 | cust_master_name = frappe.defaults.get_user_default("cust_master_name") |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 82 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 83 | if cust_master_name == "Customer Name": |
| 84 | fields = ["name", "customer_group", "territory"] |
| 85 | else: |
| 86 | fields = ["name", "customer_name", "customer_group", "territory"] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 87 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 88 | fields = get_fields("Customer", fields) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 89 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 90 | searchfields = frappe.get_meta("Customer").get_search_fields() |
Ankush Menat | a9c84f7 | 2021-06-11 16:00:48 +0530 | [diff] [blame] | 91 | searchfields = " or ".join(field + " like %(txt)s" for field in searchfields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 92 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 93 | return frappe.db.sql("""select {fields} from `tabCustomer` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 94 | where docstatus < 2 |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 95 | and ({scond}) and disabled=0 |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 96 | {fcond} {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 97 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 98 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 99 | if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 100 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 101 | name, customer_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 102 | limit %(start)s, %(page_len)s""".format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 103 | "fields": ", ".join(fields), |
Console Admin | 8623166 | 2017-06-23 20:32:52 +0300 | [diff] [blame] | 104 | "scond": searchfields, |
KanchanChauhan | 4b888b9 | 2017-07-25 14:03:01 +0530 | [diff] [blame] | 105 | "mcond": get_match_cond(doctype), |
| 106 | "fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 107 | }), { |
| 108 | 'txt': "%%%s%%" % txt, |
| 109 | '_txt': txt.replace("%", ""), |
| 110 | 'start': start, |
| 111 | 'page_len': page_len |
| 112 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 113 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 114 | |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 115 | # searches for supplier |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 116 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 117 | @frappe.validate_and_sanitize_search_inputs |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 118 | def supplier_query(doctype, txt, searchfield, start, page_len, filters): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 119 | supp_master_name = frappe.defaults.get_user_default("supp_master_name") |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 120 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 121 | if supp_master_name == "Supplier Name": |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 122 | fields = ["name", "supplier_group"] |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 123 | else: |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 124 | fields = ["name", "supplier_name", "supplier_group"] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 125 | |
| 126 | fields = get_fields("Supplier", fields) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 127 | |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 128 | return frappe.db.sql("""select {field} from `tabSupplier` |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 129 | where docstatus < 2 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 130 | and ({key} like %(txt)s |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 131 | or supplier_name like %(txt)s) and disabled=0 |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 132 | {mcond} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 133 | order by |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 134 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 135 | if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 136 | idx desc, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 137 | name, supplier_name |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 138 | limit %(start)s, %(page_len)s """.format(**{ |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 139 | 'field': ', '.join(fields), |
Anand Doshi | 48d3b54 | 2014-07-09 13:15:03 +0530 | [diff] [blame] | 140 | 'key': searchfield, |
| 141 | 'mcond':get_match_cond(doctype) |
| 142 | }), { |
| 143 | 'txt': "%%%s%%" % txt, |
| 144 | '_txt': txt.replace("%", ""), |
| 145 | 'start': start, |
| 146 | 'page_len': page_len |
| 147 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 148 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 149 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 150 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 151 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | 9a380ef | 2013-07-16 17:24:17 +0530 | [diff] [blame] | 152 | def tax_account_query(doctype, txt, searchfield, start, page_len, filters): |
Deepesh Garg | fbf6e56 | 2020-03-31 10:45:32 +0530 | [diff] [blame] | 153 | company_currency = erpnext.get_company_currency(filters.get('company')) |
| 154 | |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 155 | def get_accounts(with_account_type_filter): |
| 156 | account_type_condition = '' |
| 157 | if with_account_type_filter: |
| 158 | account_type_condition = "AND account_type in %(account_types)s" |
| 159 | |
| 160 | accounts = frappe.db.sql(""" |
| 161 | SELECT name, parent_account |
| 162 | FROM `tabAccount` |
| 163 | WHERE `tabAccount`.docstatus!=2 |
| 164 | {account_type_condition} |
| 165 | AND is_group = 0 |
| 166 | AND company = %(company)s |
| 167 | AND account_currency = %(currency)s |
| 168 | AND `{searchfield}` LIKE %(txt)s |
prssanna | de7a2bc | 2020-09-21 13:57:04 +0530 | [diff] [blame] | 169 | {mcond} |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 170 | ORDER BY idx DESC, name |
| 171 | LIMIT %(offset)s, %(limit)s |
prssanna | de7a2bc | 2020-09-21 13:57:04 +0530 | [diff] [blame] | 172 | """.format( |
| 173 | account_type_condition=account_type_condition, |
| 174 | searchfield=searchfield, |
| 175 | mcond=get_match_cond(doctype) |
| 176 | ), |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 177 | dict( |
| 178 | account_types=filters.get("account_type"), |
| 179 | company=filters.get("company"), |
| 180 | currency=company_currency, |
| 181 | txt="%{}%".format(txt), |
| 182 | offset=start, |
| 183 | limit=page_len |
| 184 | ) |
| 185 | ) |
| 186 | |
| 187 | return accounts |
| 188 | |
| 189 | tax_accounts = get_accounts(True) |
| 190 | |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 191 | if not tax_accounts: |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 192 | tax_accounts = get_accounts(False) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 193 | |
Nabin Hait | 0c21e2a | 2014-03-21 11:14:49 +0530 | [diff] [blame] | 194 | return tax_accounts |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 195 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 196 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 197 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 198 | @frappe.validate_and_sanitize_search_inputs |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 199 | def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False): |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 200 | conditions = [] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 201 | |
rohitwaghchaure | c59371a | 2021-06-03 20:02:58 +0530 | [diff] [blame] | 202 | if isinstance(filters, str): |
| 203 | filters = json.loads(filters) |
| 204 | |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 205 | #Get searchfields from meta and use in Item Link field query |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 206 | meta = frappe.get_meta("Item", cached=True) |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 207 | searchfields = meta.get_search_fields() |
| 208 | |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 209 | if "description" in searchfields: |
| 210 | searchfields.remove("description") |
Rohit Waghchaure | 721b413 | 2021-06-02 14:13:09 +0530 | [diff] [blame] | 211 | |
Rohit Waghchaure | c42312e | 2019-11-19 19:05:23 +0530 | [diff] [blame] | 212 | columns = '' |
| 213 | extra_searchfields = [field for field in searchfields |
| 214 | if not field in ["name", "item_group", "description"]] |
| 215 | |
| 216 | if extra_searchfields: |
| 217 | columns = ", " + ", ".join(extra_searchfields) |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 218 | |
| 219 | searchfields = searchfields + [field for field in[searchfield or "name", "item_code", "item_group", "item_name"] |
| 220 | if not field in searchfields] |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 221 | searchfields = " or ".join([field + " like %(txt)s" for field in searchfields]) |
| 222 | |
Rohit Waghchaure | 721b413 | 2021-06-02 14:13:09 +0530 | [diff] [blame] | 223 | if filters and isinstance(filters, dict) and filters.get('supplier'): |
| 224 | item_group_list = frappe.get_all('Supplier Item Group', |
| 225 | filters = {'supplier': filters.get('supplier')}, fields = ['item_group']) |
| 226 | |
noahjacob | ca2fb47 | 2021-05-12 16:25:07 +0530 | [diff] [blame] | 227 | item_groups = [] |
| 228 | for i in item_group_list: |
| 229 | item_groups.append(i.item_group) |
| 230 | |
| 231 | del filters['supplier'] |
| 232 | |
| 233 | if item_groups: |
| 234 | filters['item_group'] = ['in', item_groups] |
Rohit Waghchaure | 721b413 | 2021-06-02 14:13:09 +0530 | [diff] [blame] | 235 | |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 236 | description_cond = '' |
| 237 | if frappe.db.count('Item', cache=True) < 50000: |
| 238 | # scan description only if items are less than 50000 |
| 239 | description_cond = 'or tabItem.description LIKE %(txt)s' |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 240 | return frappe.db.sql("""select tabItem.name, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 241 | if(length(tabItem.item_name) > 40, |
| 242 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, |
Prateeksha Singh | 984a7a7 | 2018-05-17 17:29:36 +0530 | [diff] [blame] | 243 | tabItem.item_group, |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 244 | if(length(tabItem.description) > 40, \ |
Rohit Waghchaure | c42312e | 2019-11-19 19:05:23 +0530 | [diff] [blame] | 245 | concat(substr(tabItem.description, 1, 40), "..."), description) as description |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 246 | {columns} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 247 | from tabItem |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 248 | where tabItem.docstatus < 2 |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 249 | and tabItem.disabled=0 |
rohitwaghchaure | 7978907 | 2020-05-21 18:10:13 +0530 | [diff] [blame] | 250 | and tabItem.has_variants=0 |
Rushabh Mehta | 864d1ea | 2014-06-23 12:20:12 +0530 | [diff] [blame] | 251 | 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] | 252 | 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] | 253 | {description_cond}) |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 254 | {fcond} {mcond} |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 255 | order by |
| 256 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 257 | if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 258 | idx desc, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 259 | name, item_name |
Rushabh Mehta | bc4e2cd | 2017-10-17 12:30:34 +0530 | [diff] [blame] | 260 | limit %(start)s, %(page_len)s """.format( |
marination | 1e754b1 | 2019-10-30 18:33:44 +0530 | [diff] [blame] | 261 | columns=columns, |
marination | 3dbef9d | 2019-10-28 15:48:10 +0530 | [diff] [blame] | 262 | scond=searchfields, |
Nabin Hait | c628506 | 2016-03-30 13:10:25 +0530 | [diff] [blame] | 263 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Rushabh Mehta | d5f9ebd | 2018-04-02 23:37:33 +0530 | [diff] [blame] | 264 | mcond=get_match_cond(doctype).replace('%', '%%'), |
| 265 | description_cond = description_cond), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 266 | { |
| 267 | "today": nowdate(), |
| 268 | "txt": "%%%s%%" % txt, |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 269 | "_txt": txt.replace("%", ""), |
Anand Doshi | 22c0d78 | 2013-11-04 16:23:04 +0530 | [diff] [blame] | 270 | "start": start, |
| 271 | "page_len": page_len |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 272 | }, as_dict=as_dict) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 273 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 274 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 275 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 276 | @frappe.validate_and_sanitize_search_inputs |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 277 | def bom(doctype, txt, searchfield, start, page_len, filters): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 278 | conditions = [] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 279 | fields = get_fields("BOM", ["name", "item"]) |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 280 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 281 | return frappe.db.sql("""select {fields} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 282 | from tabBOM |
| 283 | where tabBOM.docstatus=1 |
| 284 | and tabBOM.is_active=1 |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 285 | and tabBOM.`{key}` like %(txt)s |
| 286 | {fcond} {mcond} |
| 287 | order by |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 288 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 289 | idx desc, name |
Nabin Hait | 6221117 | 2016-03-16 16:22:03 +0530 | [diff] [blame] | 290 | limit %(start)s, %(page_len)s """.format( |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 291 | fields=", ".join(fields), |
Mangesh-Khairnar | 6a79691 | 2019-07-08 10:40:40 +0530 | [diff] [blame] | 292 | fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), |
Karthikeyan S | 747c262 | 2019-07-19 22:49:21 +0530 | [diff] [blame] | 293 | mcond=get_match_cond(doctype).replace('%', '%%'), |
| 294 | key=searchfield), |
Mangesh-Khairnar | 6a79691 | 2019-07-08 10:40:40 +0530 | [diff] [blame] | 295 | { |
Karthikeyan S | 747c262 | 2019-07-19 22:49:21 +0530 | [diff] [blame] | 296 | 'txt': '%' + txt + '%', |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 297 | '_txt': txt.replace("%", ""), |
Saurabh | 022ab63 | 2017-11-10 15:06:02 +0530 | [diff] [blame] | 298 | 'start': start or 0, |
| 299 | 'page_len': page_len or 20 |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 300 | }) |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 301 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 302 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 303 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 304 | @frappe.validate_and_sanitize_search_inputs |
Saurabh | 0287559 | 2013-07-08 18:45:55 +0530 | [diff] [blame] | 305 | def get_project_name(doctype, txt, searchfield, start, page_len, filters): |
| 306 | cond = '' |
Nabin Hait | f71011a | 2014-08-21 11:34:31 +0530 | [diff] [blame] | 307 | if filters.get('customer'): |
Suraj Shetty | 6ea3de9 | 2018-09-26 18:15:53 +0530 | [diff] [blame] | 308 | cond = """(`tabProject`.customer = %s or |
rohitwaghchaure | e330472 | 2018-08-27 11:43:57 +0530 | [diff] [blame] | 309 | ifnull(`tabProject`.customer,"")="") and""" %(frappe.db.escape(filters.get("customer"))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 310 | |
Rucha Mahabal | 062d301 | 2021-05-07 13:31:14 +0530 | [diff] [blame] | 311 | fields = get_fields("Project", ["name", "project_name"]) |
| 312 | searchfields = frappe.get_meta("Project").get_search_fields() |
| 313 | searchfields = " or ".join([field + " like %(txt)s" for field in searchfields]) |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 314 | |
| 315 | return frappe.db.sql("""select {fields} from `tabProject` |
Rucha Mahabal | 062d301 | 2021-05-07 13:31:14 +0530 | [diff] [blame] | 316 | where |
| 317 | `tabProject`.status not in ("Completed", "Cancelled") |
Subin Tom | 889140f | 2021-06-22 16:26:19 +0530 | [diff] [blame] | 318 | and {cond} {scond} {match_cond} |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 319 | order by |
| 320 | if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), |
| 321 | idx desc, |
| 322 | `tabProject`.name asc |
| 323 | limit {start}, {page_len}""".format( |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 324 | fields=", ".join(['`tabProject`.{0}'.format(f) for f in fields]), |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 325 | cond=cond, |
Rucha Mahabal | 062d301 | 2021-05-07 13:31:14 +0530 | [diff] [blame] | 326 | scond=searchfields, |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 327 | match_cond=get_match_cond(doctype), |
| 328 | start=start, |
| 329 | page_len=page_len), { |
| 330 | "txt": "%{0}%".format(txt), |
Nabin Hait | df4deba | 2016-03-16 11:16:31 +0530 | [diff] [blame] | 331 | "_txt": txt.replace('%', '') |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 332 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 333 | |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 334 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 335 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 336 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 337 | 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] | 338 | fields = get_fields("Delivery Note", ["name", "customer", "posting_date"]) |
| 339 | |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 340 | return frappe.db.sql(""" |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 341 | select %(fields)s |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 342 | from `tabDelivery Note` |
| 343 | where `tabDelivery Note`.`%(key)s` like %(txt)s and |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 344 | `tabDelivery Note`.docstatus = 1 |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 345 | and status not in ("Stopped", "Closed") %(fcond)s |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 346 | and ( |
| 347 | (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100) |
Deepesh Garg | e2dc102 | 2021-04-14 11:21:11 +0530 | [diff] [blame] | 348 | or (`tabDelivery Note`.grand_total = 0 and `tabDelivery Note`.per_billed < 100) |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 349 | or ( |
| 350 | `tabDelivery Note`.is_return = 1 |
| 351 | and return_against in (select name from `tabDelivery Note` where per_billed < 100) |
| 352 | ) |
| 353 | ) |
rohitwaghchaure | d07a3e1 | 2019-05-15 07:46:28 +0530 | [diff] [blame] | 354 | %(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] | 355 | """ % { |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 356 | "fields": ", ".join(["`tabDelivery Note`.{0}".format(f) for f in fields]), |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 357 | "key": searchfield, |
| 358 | "fcond": get_filters_cond(doctype, filters, []), |
| 359 | "mcond": get_match_cond(doctype), |
rohitwaghchaure | d07a3e1 | 2019-05-15 07:46:28 +0530 | [diff] [blame] | 360 | "start": start, |
| 361 | "page_len": page_len, |
Nabin Hait | 1e2d7b3 | 2017-05-17 13:52:21 +0530 | [diff] [blame] | 362 | "txt": "%(txt)s" |
tundebabzy | f6d738b | 2017-09-18 12:40:09 +0100 | [diff] [blame] | 363 | }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict) |
| 364 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 365 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 366 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 367 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 368 | def get_batch_no(doctype, txt, searchfield, start, page_len, filters): |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 369 | cond = "" |
| 370 | if filters.get("posting_date"): |
Nabin Hait | 7918b92 | 2018-01-31 15:30:03 +0530 | [diff] [blame] | 371 | 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] | 372 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 373 | batch_nos = None |
| 374 | args = { |
| 375 | 'item_code': filters.get("item_code"), |
| 376 | 'warehouse': filters.get("warehouse"), |
| 377 | 'posting_date': filters.get('posting_date'), |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 378 | 'txt': "%{0}%".format(txt), |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 379 | "start": start, |
| 380 | "page_len": page_len |
| 381 | } |
| 382 | |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 383 | having_clause = "having sum(sle.actual_qty) > 0" |
| 384 | if filters.get("is_return"): |
| 385 | having_clause = "" |
| 386 | |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 387 | meta = frappe.get_meta("Batch", cached=True) |
| 388 | searchfields = meta.get_search_fields() |
| 389 | |
| 390 | search_columns = '' |
Deepesh Garg | f58a5ec | 2020-10-05 13:55:53 +0530 | [diff] [blame] | 391 | search_cond = '' |
| 392 | |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 393 | if searchfields: |
| 394 | search_columns = ", " + ", ".join(searchfields) |
Deepesh Garg | 1fae774 | 2020-10-05 12:38:54 +0530 | [diff] [blame] | 395 | search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields]) |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 396 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 397 | if args.get('warehouse'): |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 398 | searchfields = ['batch.' + field for field in searchfields] |
| 399 | if searchfields: |
| 400 | search_columns = ", " + ", ".join(searchfields) |
Deepesh Garg | 1fae774 | 2020-10-05 12:38:54 +0530 | [diff] [blame] | 401 | search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields]) |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 402 | |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 403 | batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, |
| 404 | concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date) |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 405 | {search_columns} |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 406 | from `tabStock Ledger Entry` sle |
| 407 | INNER JOIN `tabBatch` batch on sle.batch_no = batch.name |
| 408 | where |
| 409 | batch.disabled = 0 |
| 410 | and sle.item_code = %(item_code)s |
| 411 | and sle.warehouse = %(warehouse)s |
| 412 | and (sle.batch_no like %(txt)s |
Sun Howwrongbum | 088be37 | 2019-12-24 12:29:25 +0530 | [diff] [blame] | 413 | or batch.expiry_date like %(txt)s |
Deepesh Garg | f58a5ec | 2020-10-05 13:55:53 +0530 | [diff] [blame] | 414 | or batch.manufacturing_date like %(txt)s |
| 415 | {search_cond}) |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 416 | and batch.docstatus < 2 |
| 417 | {cond} |
| 418 | {match_conditions} |
| 419 | group by batch_no {having_clause} |
| 420 | order by batch.expiry_date, sle.batch_no desc |
| 421 | limit %(start)s, %(page_len)s""".format( |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 422 | search_columns = search_columns, |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 423 | cond=cond, |
| 424 | match_conditions=get_match_cond(doctype), |
Deepesh Garg | 1fae774 | 2020-10-05 12:38:54 +0530 | [diff] [blame] | 425 | having_clause = having_clause, |
| 426 | search_cond = search_cond |
Rohit Waghchaure | d8ddd1e | 2019-10-22 14:03:27 +0530 | [diff] [blame] | 427 | ), args) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 428 | |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 429 | return batch_nos |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 430 | else: |
Deepesh Garg | a0d192e | 2020-09-22 13:54:07 +0530 | [diff] [blame] | 431 | return frappe.db.sql("""select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date) |
| 432 | {search_columns} |
| 433 | from `tabBatch` batch |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 434 | where batch.disabled = 0 |
| 435 | and item = %(item_code)s |
sivankar | 621740e | 2018-02-12 14:33:40 +0530 | [diff] [blame] | 436 | and (name like %(txt)s |
Sun Howwrongbum | 088be37 | 2019-12-24 12:29:25 +0530 | [diff] [blame] | 437 | or expiry_date like %(txt)s |
Deepesh Garg | f58a5ec | 2020-10-05 13:55:53 +0530 | [diff] [blame] | 438 | or manufacturing_date like %(txt)s |
| 439 | {search_cond}) |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 440 | and docstatus < 2 |
Neil Trini Lasrado | ebb60f5 | 2015-07-08 14:36:09 +0530 | [diff] [blame] | 441 | {0} |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 442 | {match_conditions} |
Deepesh Garg | f58a5ec | 2020-10-05 13:55:53 +0530 | [diff] [blame] | 443 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 444 | order by expiry_date, name desc |
Deepesh Garg | 1fae774 | 2020-10-05 12:38:54 +0530 | [diff] [blame] | 445 | limit %(start)s, %(page_len)s""".format(cond, search_columns = search_columns, |
| 446 | search_cond = search_cond, match_conditions=get_match_cond(doctype)), args) |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 447 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 448 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 449 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 450 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 451 | def get_account_list(doctype, txt, searchfield, start, page_len, filters): |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 452 | filter_list = [] |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 453 | |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 454 | if isinstance(filters, dict): |
| 455 | for key, val in filters.items(): |
| 456 | if isinstance(val, (list, tuple)): |
| 457 | filter_list.append([doctype, key, val[0], val[1]]) |
| 458 | else: |
| 459 | filter_list.append([doctype, key, "=", val]) |
bhupeshg2 | e2e973f | 2015-04-14 22:15:24 +0530 | [diff] [blame] | 460 | elif isinstance(filters, list): |
| 461 | filter_list.extend(filters) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 462 | |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 463 | if "is_group" not in [d[1] for d in filter_list]: |
| 464 | filter_list.append(["Account", "is_group", "=", "0"]) |
Nabin Hait | e1b2b3e | 2014-06-14 15:26:10 +0530 | [diff] [blame] | 465 | |
| 466 | if searchfield and txt: |
| 467 | filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt]) |
| 468 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 469 | return frappe.desk.reportview.execute("Account", filters = filter_list, |
Nabin Hait | ea4aa04 | 2014-05-28 12:56:28 +0530 | [diff] [blame] | 470 | fields = ["name", "parent_account"], |
| 471 | limit_start=start, limit_page_length=page_len, as_list=True) |
Anand Doshi | faefeaa | 2014-06-24 18:53:04 +0530 | [diff] [blame] | 472 | |
Chinmay D. Pai | aa12109 | 2020-07-01 21:14:32 +0530 | [diff] [blame] | 473 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 474 | @frappe.validate_and_sanitize_search_inputs |
Marica | 299e217 | 2020-04-28 13:00:04 +0530 | [diff] [blame] | 475 | def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters): |
| 476 | return frappe.db.sql("""select distinct bo.name, bo.blanket_order_type, bo.to_date |
| 477 | from `tabBlanket Order` bo, `tabBlanket Order Item` boi |
| 478 | where |
| 479 | boi.parent = bo.name |
| 480 | and boi.item_code = {item_code} |
| 481 | and bo.blanket_order_type = '{blanket_order_type}' |
| 482 | and bo.company = {company} |
| 483 | and bo.docstatus = 1""" |
| 484 | .format(item_code = frappe.db.escape(filters.get("item")), |
| 485 | blanket_order_type = filters.get("blanket_order_type"), |
| 486 | company = frappe.db.escape(filters.get("company")) |
| 487 | )) |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 488 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 489 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 490 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 491 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 492 | def get_income_account(doctype, txt, searchfield, start, page_len, filters): |
| 493 | from erpnext.controllers.queries import get_match_cond |
| 494 | |
| 495 | # income account can be any Credit account, |
| 496 | # but can also be a Asset account with account_type='Income Account' in special circumstances. |
| 497 | # Hence the first condition is an "OR" |
| 498 | if not filters: filters = {} |
| 499 | |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 500 | condition = "" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 501 | if filters.get("company"): |
| 502 | condition += "and tabAccount.company = %(company)s" |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 503 | |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 504 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 505 | where (tabAccount.report_type = "Profit and Loss" |
| 506 | or tabAccount.account_type in ("Income Account", "Temporary")) |
| 507 | and tabAccount.is_group=0 |
| 508 | and tabAccount.`{key}` LIKE %(txt)s |
Rushabh Mehta | 3574b37 | 2016-03-11 14:33:04 +0530 | [diff] [blame] | 509 | {condition} {match_condition} |
| 510 | order by idx desc, name""" |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 511 | .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), { |
Suraj Shetty | 4b404c4 | 2018-09-27 15:39:34 +0530 | [diff] [blame] | 512 | 'txt': '%' + txt + '%', |
Nabin Hait | afd14f6 | 2015-10-19 11:55:28 +0530 | [diff] [blame] | 513 | 'company': filters.get("company", "") |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 514 | }) |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 515 | |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 516 | @frappe.whitelist() |
| 517 | @frappe.validate_and_sanitize_search_inputs |
| 518 | def get_filtered_dimensions(doctype, txt, searchfield, start, page_len, filters): |
| 519 | from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import get_dimension_filter_map |
| 520 | dimension_filters = get_dimension_filter_map() |
| 521 | dimension_filters = dimension_filters.get((filters.get('dimension'),filters.get('account'))) |
Deepesh Garg | 6c17b84 | 2020-11-25 13:42:16 +0530 | [diff] [blame] | 522 | query_filters = [] |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 523 | |
| 524 | meta = frappe.get_meta(doctype) |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 525 | if meta.is_tree: |
Deepesh Garg | 6c17b84 | 2020-11-25 13:42:16 +0530 | [diff] [blame] | 526 | query_filters.append(['is_group', '=', 0]) |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 527 | |
| 528 | if meta.has_field('company'): |
Deepesh Garg | 6c17b84 | 2020-11-25 13:42:16 +0530 | [diff] [blame] | 529 | query_filters.append(['company', '=', filters.get('company')]) |
| 530 | |
| 531 | if txt: |
| 532 | query_filters.append([searchfield, 'LIKE', "%%%s%%" % txt]) |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 533 | |
| 534 | if dimension_filters: |
| 535 | if dimension_filters['allow_or_restrict'] == 'Allow': |
| 536 | query_selector = 'in' |
| 537 | else: |
| 538 | query_selector = 'not in' |
| 539 | |
| 540 | if len(dimension_filters['allowed_dimensions']) == 1: |
| 541 | dimensions = tuple(dimension_filters['allowed_dimensions'] * 2) |
| 542 | else: |
| 543 | dimensions = tuple(dimension_filters['allowed_dimensions']) |
| 544 | |
Deepesh Garg | 6c17b84 | 2020-11-25 13:42:16 +0530 | [diff] [blame] | 545 | query_filters.append(['name', query_selector, dimensions]) |
Deepesh Garg | 96e874b | 2020-11-15 22:43:01 +0530 | [diff] [blame] | 546 | |
Deepesh Garg | 6c17b84 | 2020-11-25 13:42:16 +0530 | [diff] [blame] | 547 | output = frappe.get_all(doctype, filters=query_filters) |
| 548 | result = [d.name for d in output] |
| 549 | |
| 550 | return [(d,) for d in set(result)] |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 551 | |
| 552 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 553 | @frappe.validate_and_sanitize_search_inputs |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 554 | def get_expense_account(doctype, txt, searchfield, start, page_len, filters): |
| 555 | from erpnext.controllers.queries import get_match_cond |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 556 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 557 | if not filters: filters = {} |
| 558 | |
| 559 | condition = "" |
| 560 | if filters.get("company"): |
| 561 | condition += "and tabAccount.company = %(company)s" |
Rushabh Mehta | 203cc96 | 2016-04-07 15:25:43 +0530 | [diff] [blame] | 562 | |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 563 | return frappe.db.sql("""select tabAccount.name from `tabAccount` |
| 564 | where (tabAccount.report_type = "Profit and Loss" |
Mangesh-Khairnar | 5619db2 | 2019-08-21 14:49:24 +0530 | [diff] [blame] | 565 | 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] | 566 | and tabAccount.is_group=0 |
| 567 | and tabAccount.docstatus!=2 |
| 568 | and tabAccount.{key} LIKE %(txt)s |
| 569 | {condition} {match_condition}""" |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 570 | .format(condition=condition, key=searchfield, |
Nabin Hait | 3a15c92 | 2016-03-04 12:30:46 +0530 | [diff] [blame] | 571 | match_condition=get_match_cond(doctype)), { |
Neil Trini Lasrado | 30b97b0 | 2016-03-31 23:10:13 +0530 | [diff] [blame] | 572 | 'company': filters.get("company", ""), |
Suraj Shetty | 4b404c4 | 2018-09-27 15:39:34 +0530 | [diff] [blame] | 573 | 'txt': '%' + txt + '%' |
Maxwell Morais | 3557261 | 2016-07-21 23:42:59 -0300 | [diff] [blame] | 574 | }) |
suyashphadtare | 049a88c | 2017-01-12 17:49:37 +0530 | [diff] [blame] | 575 | |
| 576 | |
| 577 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 578 | @frappe.validate_and_sanitize_search_inputs |
suyashphadtare | 049a88c | 2017-01-12 17:49:37 +0530 | [diff] [blame] | 579 | def warehouse_query(doctype, txt, searchfield, start, page_len, filters): |
| 580 | # Should be used when item code is passed in filters. |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 581 | conditions, bin_conditions = [], [] |
| 582 | filter_dict = get_doctype_wise_filters(filters) |
| 583 | |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 584 | query = """select `tabWarehouse`.name, |
Diksha Jadhav | 182ee5e | 2020-08-18 00:35:04 +0530 | [diff] [blame] | 585 | CONCAT_WS(" : ", "Actual Qty", ifnull(round(`tabBin`.actual_qty, 2), 0 )) actual_qty |
| 586 | from `tabWarehouse` left join `tabBin` |
| 587 | on `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 588 | where |
Diksha Jadhav | 182ee5e | 2020-08-18 00:35:04 +0530 | [diff] [blame] | 589 | `tabWarehouse`.`{key}` like {txt} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 590 | {fcond} {mcond} |
Diksha Jadhav | 182ee5e | 2020-08-18 00:35:04 +0530 | [diff] [blame] | 591 | order by ifnull(`tabBin`.actual_qty, 0) desc |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 592 | limit |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 593 | {start}, {page_len} |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 594 | """.format( |
Diksha Jadhav | 182ee5e | 2020-08-18 00:35:04 +0530 | [diff] [blame] | 595 | bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"),bin_conditions, ignore_permissions=True), |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 596 | key=searchfield, |
suyashphadtare | 34ab136 | 2017-01-31 15:14:44 +0530 | [diff] [blame] | 597 | fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions), |
Rushabh Mehta | 7e506af | 2017-08-24 15:23:33 +0530 | [diff] [blame] | 598 | mcond=get_match_cond(doctype), |
| 599 | start=start, |
| 600 | page_len=page_len, |
| 601 | txt=frappe.db.escape('%{0}%'.format(txt)) |
| 602 | ) |
| 603 | |
| 604 | return frappe.db.sql(query) |
suyashphadtare | 750a067 | 2017-01-18 15:35:01 +0530 | [diff] [blame] | 605 | |
| 606 | |
| 607 | def get_doctype_wise_filters(filters): |
| 608 | # Helper function to seperate filters doctype_wise |
| 609 | filter_dict = defaultdict(list) |
| 610 | for row in filters: |
| 611 | filter_dict[row[0]].append(row) |
| 612 | return filter_dict |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 613 | |
| 614 | |
| 615 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 616 | @frappe.validate_and_sanitize_search_inputs |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 617 | def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters): |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 618 | query = """select batch_id from `tabBatch` |
Doridel Cahanap | 59e4c32 | 2018-08-06 17:15:18 +0800 | [diff] [blame] | 619 | where disabled = 0 |
| 620 | and (expiry_date >= CURDATE() or expiry_date IS NULL) |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 621 | and name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 622 | |
rohitwaghchaure | 9fbed56 | 2018-01-12 16:22:33 +0530 | [diff] [blame] | 623 | if filters and filters.get('item'): |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 624 | query += " and item = {item}".format(item = frappe.db.escape(filters.get('item'))) |
tundebabzy | 2a4fefc | 2017-11-29 06:23:09 +0100 | [diff] [blame] | 625 | |
Sachin Mane | 64f48db | 2018-01-08 17:57:32 +0530 | [diff] [blame] | 626 | return frappe.db.sql(query, filters) |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 627 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 628 | |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 629 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 630 | @frappe.validate_and_sanitize_search_inputs |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 631 | def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters): |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 632 | item_filters = [ |
| 633 | ['manufacturer', 'like', '%' + txt + '%'], |
| 634 | ['item_code', '=', filters.get("item_code")] |
| 635 | ] |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 636 | |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 637 | item_manufacturers = frappe.get_all( |
| 638 | "Item Manufacturer", |
| 639 | fields=["manufacturer", "manufacturer_part_no"], |
| 640 | filters=item_filters, |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 641 | limit_start=start, |
| 642 | limit_page_length=page_len, |
| 643 | as_list=1 |
| 644 | ) |
Marica | bac4b93 | 2019-09-16 19:44:28 +0530 | [diff] [blame] | 645 | return item_manufacturers |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 646 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 647 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 648 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 649 | @frappe.validate_and_sanitize_search_inputs |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 650 | def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters): |
| 651 | query = """ |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 652 | select pr.name |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 653 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem |
| 654 | where pr.docstatus = 1 and pritem.parent = pr.name |
| 655 | and pr.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
| 656 | |
| 657 | if filters and filters.get('item_code'): |
| 658 | query += " and pritem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code'))) |
| 659 | |
| 660 | return frappe.db.sql(query, filters) |
| 661 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 662 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 663 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 664 | @frappe.validate_and_sanitize_search_inputs |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 665 | def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters): |
| 666 | query = """ |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 667 | select pi.name |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 668 | from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem |
| 669 | where pi.docstatus = 1 and piitem.parent = pi.name |
| 670 | and pi.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt))) |
| 671 | |
| 672 | if filters and filters.get('item_code'): |
| 673 | query += " and piitem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code'))) |
| 674 | |
| 675 | return frappe.db.sql(query, filters) |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 676 | |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 677 | |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 678 | @frappe.whitelist() |
Suraj Shetty | 1923ef0 | 2020-08-05 19:42:25 +0530 | [diff] [blame] | 679 | @frappe.validate_and_sanitize_search_inputs |
Rucha Mahabal | 20e5315 | 2021-01-18 14:56:55 +0530 | [diff] [blame] | 680 | def get_healthcare_service_units(doctype, txt, searchfield, start, page_len, filters): |
| 681 | query = """ |
| 682 | select name |
| 683 | from `tabHealthcare Service Unit` |
| 684 | where |
| 685 | is_group = 0 |
| 686 | and company = {company} |
| 687 | and name like {txt}""".format( |
| 688 | company = frappe.db.escape(filters.get('company')), txt = frappe.db.escape('%{0}%'.format(txt))) |
| 689 | |
| 690 | if filters and filters.get('inpatient_record'): |
| 691 | from erpnext.healthcare.doctype.inpatient_medication_entry.inpatient_medication_entry import get_current_healthcare_service_unit |
| 692 | service_unit = get_current_healthcare_service_unit(filters.get('inpatient_record')) |
| 693 | |
| 694 | # if the patient is admitted, then appointments should be allowed against the admission service unit, |
| 695 | # inspite of it being an Inpatient Occupancy service unit |
| 696 | if service_unit: |
| 697 | query += " and (allow_appointments = 1 or name = {service_unit})".format(service_unit = frappe.db.escape(service_unit)) |
| 698 | else: |
| 699 | query += " and allow_appointments = 1" |
| 700 | else: |
| 701 | query += " and allow_appointments = 1" |
| 702 | |
| 703 | return frappe.db.sql(query, filters) |
| 704 | |
| 705 | |
| 706 | @frappe.whitelist() |
| 707 | @frappe.validate_and_sanitize_search_inputs |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 708 | def get_tax_template(doctype, txt, searchfield, start, page_len, filters): |
| 709 | |
| 710 | item_doc = frappe.get_cached_doc('Item', filters.get('item_code')) |
| 711 | item_group = filters.get('item_group') |
| 712 | taxes = item_doc.taxes or [] |
| 713 | |
| 714 | while item_group: |
| 715 | item_group_doc = frappe.get_cached_doc('Item Group', item_group) |
| 716 | taxes += item_group_doc.taxes or [] |
| 717 | item_group = item_group_doc.parent_item_group |
| 718 | |
| 719 | if not taxes: |
| 720 | return frappe.db.sql(""" SELECT name FROM `tabItem Tax Template` """) |
| 721 | else: |
Marica | 0fcb05a | 2020-08-10 14:48:13 +0530 | [diff] [blame] | 722 | valid_from = filters.get('valid_from') |
| 723 | valid_from = valid_from[1] if isinstance(valid_from, list) else valid_from |
| 724 | |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 725 | args = { |
| 726 | 'item_code': filters.get('item_code'), |
Marica | 0fcb05a | 2020-08-10 14:48:13 +0530 | [diff] [blame] | 727 | 'posting_date': valid_from, |
mohammadahmad1990 | 41c0c9f | 2020-06-18 11:18:44 +0500 | [diff] [blame] | 728 | 'tax_category': filters.get('tax_category'), |
| 729 | 'company': filters.get('company') |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | taxes = _get_item_tax_template(args, taxes, for_validate=True) |
| 733 | return [(d,) for d in set(taxes)] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 734 | |
| 735 | |
Ankush Menat | 7eac4a2 | 2021-04-19 10:33:39 +0530 | [diff] [blame] | 736 | def get_fields(doctype, fields=None): |
| 737 | if fields is None: |
| 738 | fields = [] |
Himanshu | d94a38e | 2020-05-18 14:26:26 +0530 | [diff] [blame] | 739 | meta = frappe.get_meta(doctype) |
| 740 | fields.extend(meta.get_search_fields()) |
| 741 | |
| 742 | if meta.title_field and not meta.title_field.strip() in fields: |
| 743 | fields.insert(1, meta.title_field.strip()) |
| 744 | |
| 745 | return unique(fields) |