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