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