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