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