blob: 799fed99cc7d19071213bda268d8269db2348639 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Saurabh02875592013-07-08 18:45:55 +05303
Chillar Anand915b3432021-09-02 16:44:59 +05304
rohitwaghchaurec59371a2021-06-03 20:02:58 +05305import json
suyashphadtare750a0672017-01-18 15:35:01 +05306from collections import defaultdict
Chillar Anand915b3432021-09-02 16:44:59 +05307
8import frappe
DeeMysterioaa826242021-09-14 13:58:18 +05309from frappe import scrub
Chillar Anand915b3432021-09-02 16:44:59 +053010from frappe.desk.reportview import get_filters_cond, get_match_cond
11from frappe.utils import nowdate, unique
12
13import erpnext
Deepesh Gargef0d26c2020-01-06 15:34:15 +053014from erpnext.stock.get_item_details import _get_item_tax_template
Chillar Anand915b3432021-09-02 16:44:59 +053015
Saurabh02875592013-07-08 18:45:55 +053016
Chinmay D. Paiaa121092020-07-01 21:14:32 +053017# searches for active employees
18@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053019@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +053020def employee_query(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +000021 doctype = "Employee"
Kanchan Chauhan7652b852016-11-16 15:29:01 +053022 conditions = []
Sagar Vora9baa2222022-08-03 05:42:30 +000023 fields = get_fields(doctype, ["name", "employee_name"])
Himanshud94a38e2020-05-18 14:26:26 +053024
Ankush Menat494bd9e2022-03-28 18:52:46 +053025 return frappe.db.sql(
26 """select {fields} from `tabEmployee`
Anurag Mishrafc98abe2021-06-23 11:21:38 +053027 where status in ('Active', 'Suspended')
Anand Doshibd67e872014-04-11 16:51:27 +053028 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053029 and ({key} like %(txt)s
30 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053031 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053032 order by
Conorea28ed12022-06-17 10:47:48 -050033 (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
34 (case when locate(%(_txt)s, employee_name) > 0 then locate(%(_txt)s, employee_name) else 99999 end),
Rushabh Mehta3574b372016-03-11 14:33:04 +053035 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053036 name, employee_name
Conor00ef4992022-06-14 00:19:07 -050037 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +053038 **{
39 "fields": ", ".join(fields),
40 "key": searchfield,
41 "fcond": get_filters_cond(doctype, filters, conditions),
42 "mcond": get_match_cond(doctype),
43 }
44 ),
45 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
46 )
Saurabh02875592013-07-08 18:45:55 +053047
Himanshud94a38e2020-05-18 14:26:26 +053048
49# searches for leads which are not converted
Chinmay D. Paiaa121092020-07-01 21:14:32 +053050@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053051@frappe.validate_and_sanitize_search_inputs
Anand Doshibd67e872014-04-11 16:51:27 +053052def lead_query(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +000053 doctype = "Lead"
54 fields = get_fields(doctype, ["name", "lead_name", "company_name"])
Himanshud94a38e2020-05-18 14:26:26 +053055
Ankush Menat494bd9e2022-03-28 18:52:46 +053056 return frappe.db.sql(
57 """select {fields} from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053058 where docstatus < 2
59 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053060 and ({key} like %(txt)s
61 or lead_name like %(txt)s
62 or company_name like %(txt)s)
63 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053064 order by
Conorea28ed12022-06-17 10:47:48 -050065 (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
66 (case when locate(%(_txt)s, lead_name) > 0 then locate(%(_txt)s, lead_name) else 99999 end),
67 (case when locate(%(_txt)s, company_name) > 0 then locate(%(_txt)s, company_name) else 99999 end),
Rushabh Mehta3574b372016-03-11 14:33:04 +053068 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053069 name, lead_name
Conor00ef4992022-06-14 00:19:07 -050070 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +053071 **{"fields": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
72 ),
73 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
74 )
75
76 # searches for customer
Saurabh02875592013-07-08 18:45:55 +053077
Himanshud94a38e2020-05-18 14:26:26 +053078
Chinmay D. Paiaa121092020-07-01 21:14:32 +053079@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053080@frappe.validate_and_sanitize_search_inputs
Rohit Waghchaure5f849932022-10-24 16:10:47 +053081def customer_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Sagar Vora9baa2222022-08-03 05:42:30 +000082 doctype = "Customer"
KanchanChauhan4b888b92017-07-25 14:03:01 +053083 conditions = []
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053084 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053085
Rohit Waghchaure46d148d2022-10-24 15:48:34 +053086 fields = ["name"]
87 if cust_master_name != "Customer Name":
Rohit Waghchaureb0fc5682022-11-03 11:24:58 +053088 fields.append("customer_name")
Rushabh Mehtab92087c2017-01-13 18:53:11 +053089
Sagar Vora9baa2222022-08-03 05:42:30 +000090 fields = get_fields(doctype, fields)
Sagar Vora9baa2222022-08-03 05:42:30 +000091 searchfields = frappe.get_meta(doctype).get_search_fields()
Ankush Menata9c84f72021-06-11 16:00:48 +053092 searchfields = " or ".join(field + " like %(txt)s" for field in searchfields)
Saurabh02875592013-07-08 18:45:55 +053093
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 return frappe.db.sql(
95 """select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053096 where docstatus < 2
Console Admin86231662017-06-23 20:32:52 +030097 and ({scond}) and disabled=0
KanchanChauhan4b888b92017-07-25 14:03:01 +053098 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053099 order by
Conorea28ed12022-06-17 10:47:48 -0500100 (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
101 (case when locate(%(_txt)s, customer_name) > 0 then locate(%(_txt)s, customer_name) else 99999 end),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530102 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530103 name, customer_name
Conor00ef4992022-06-14 00:19:07 -0500104 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530105 **{
106 "fields": ", ".join(fields),
107 "scond": searchfields,
108 "mcond": get_match_cond(doctype),
109 "fcond": get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
110 }
111 ),
112 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
Rohit Waghchaure5f849932022-10-24 16:10:47 +0530113 as_dict=as_dict,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 )
Saurabh02875592013-07-08 18:45:55 +0530115
Himanshud94a38e2020-05-18 14:26:26 +0530116
Saurabh02875592013-07-08 18:45:55 +0530117# searches for supplier
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530118@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530119@frappe.validate_and_sanitize_search_inputs
Rohit Waghchaure5f849932022-10-24 16:10:47 +0530120def supplier_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Sagar Vora9baa2222022-08-03 05:42:30 +0000121 doctype = "Supplier"
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530122 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Suraj Shetty1923ef02020-08-05 19:42:25 +0530123
Rohit Waghchaure46d148d2022-10-24 15:48:34 +0530124 fields = ["name"]
125 if supp_master_name != "Supplier Name":
Rohit Waghchaureb0fc5682022-11-03 11:24:58 +0530126 fields.append("supplier_name")
Himanshud94a38e2020-05-18 14:26:26 +0530127
Sagar Vora9baa2222022-08-03 05:42:30 +0000128 fields = get_fields(doctype, fields)
Saurabh02875592013-07-08 18:45:55 +0530129
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130 return frappe.db.sql(
131 """select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530132 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530133 and ({key} like %(txt)s
Ankush Menat2221c9e2021-10-27 19:15:44 +0530134 or supplier_name like %(txt)s) and disabled=0
Conorea28ed12022-06-17 10:47:48 -0500135 and (on_hold = 0 or (on_hold = 1 and CURRENT_DATE > release_date))
Anand Doshi48d3b542014-07-09 13:15:03 +0530136 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530137 order by
Conorea28ed12022-06-17 10:47:48 -0500138 (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
139 (case when locate(%(_txt)s, supplier_name) > 0 then locate(%(_txt)s, supplier_name) else 99999 end),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530140 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530141 name, supplier_name
Conor00ef4992022-06-14 00:19:07 -0500142 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530143 **{"field": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
144 ),
145 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
Rohit Waghchaure5f849932022-10-24 16:10:47 +0530146 as_dict=as_dict,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530147 )
Anand Doshibd67e872014-04-11 16:51:27 +0530148
Himanshud94a38e2020-05-18 14:26:26 +0530149
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530150@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530151@frappe.validate_and_sanitize_search_inputs
Nabin Hait9a380ef2013-07-16 17:24:17 +0530152def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +0000153 doctype = "Account"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530154 company_currency = erpnext.get_company_currency(filters.get("company"))
Deepesh Gargfbf6e562020-03-31 10:45:32 +0530155
Suraj Shetty1923ef02020-08-05 19:42:25 +0530156 def get_accounts(with_account_type_filter):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530157 account_type_condition = ""
Suraj Shetty1923ef02020-08-05 19:42:25 +0530158 if with_account_type_filter:
159 account_type_condition = "AND account_type in %(account_types)s"
160
Ankush Menat494bd9e2022-03-28 18:52:46 +0530161 accounts = frappe.db.sql(
162 """
Suraj Shetty1923ef02020-08-05 19:42:25 +0530163 SELECT name, parent_account
164 FROM `tabAccount`
165 WHERE `tabAccount`.docstatus!=2
166 {account_type_condition}
167 AND is_group = 0
168 AND company = %(company)s
Saqib Ansaria1e3ae82022-05-11 13:01:06 +0530169 AND disabled = %(disabled)s
Deepesh Garg57924592022-03-22 18:26:58 +0530170 AND (account_currency = %(currency)s or ifnull(account_currency, '') = '')
Suraj Shetty1923ef02020-08-05 19:42:25 +0530171 AND `{searchfield}` LIKE %(txt)s
prssannade7a2bc2020-09-21 13:57:04 +0530172 {mcond}
Suraj Shetty1923ef02020-08-05 19:42:25 +0530173 ORDER BY idx DESC, name
Conor00ef4992022-06-14 00:19:07 -0500174 LIMIT %(limit)s offset %(offset)s
prssannade7a2bc2020-09-21 13:57:04 +0530175 """.format(
176 account_type_condition=account_type_condition,
177 searchfield=searchfield,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530178 mcond=get_match_cond(doctype),
prssannade7a2bc2020-09-21 13:57:04 +0530179 ),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530180 dict(
181 account_types=filters.get("account_type"),
182 company=filters.get("company"),
Saqib Ansaria1e3ae82022-05-11 13:01:06 +0530183 disabled=filters.get("disabled", 0),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530184 currency=company_currency,
185 txt="%{}%".format(txt),
186 offset=start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530187 limit=page_len,
188 ),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530189 )
190
191 return accounts
192
193 tax_accounts = get_accounts(True)
194
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530195 if not tax_accounts:
Suraj Shetty1923ef02020-08-05 19:42:25 +0530196 tax_accounts = get_accounts(False)
Anand Doshibd67e872014-04-11 16:51:27 +0530197
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530198 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530199
Himanshud94a38e2020-05-18 14:26:26 +0530200
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530201@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530202@frappe.validate_and_sanitize_search_inputs
Rushabh Mehta203cc962016-04-07 15:25:43 +0530203def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Sagar Vora9baa2222022-08-03 05:42:30 +0000204 doctype = "Item"
Saurabh02875592013-07-08 18:45:55 +0530205 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530206
rohitwaghchaurec59371a2021-06-03 20:02:58 +0530207 if isinstance(filters, str):
208 filters = json.loads(filters)
209
Ankush Menat494bd9e2022-03-28 18:52:46 +0530210 # Get searchfields from meta and use in Item Link field query
Sagar Vora9baa2222022-08-03 05:42:30 +0000211 meta = frappe.get_meta(doctype, cached=True)
marination3dbef9d2019-10-28 15:48:10 +0530212 searchfields = meta.get_search_fields()
213
Ankush Menat494bd9e2022-03-28 18:52:46 +0530214 columns = ""
Rohit Waghchaurefd889fd2022-09-28 23:00:45 +0530215 extra_searchfields = [field for field in searchfields if not field in ["name", "description"]]
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530216
217 if extra_searchfields:
Rohit Waghchaurefd889fd2022-09-28 23:00:45 +0530218 columns += ", " + ", ".join(extra_searchfields)
219
220 if "description" in searchfields:
221 columns += """, if(length(tabItem.description) > 40, \
222 concat(substr(tabItem.description, 1, 40), "..."), description) as description"""
marination1e754b12019-10-30 18:33:44 +0530223
Ankush Menat494bd9e2022-03-28 18:52:46 +0530224 searchfields = searchfields + [
225 field
226 for field in [searchfield or "name", "item_code", "item_group", "item_name"]
227 if not field in searchfields
228 ]
marination3dbef9d2019-10-28 15:48:10 +0530229 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
230
DeeMysterioaa826242021-09-14 13:58:18 +0530231 if filters and isinstance(filters, dict):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530232 if filters.get("customer") or filters.get("supplier"):
233 party = filters.get("customer") or filters.get("supplier")
234 item_rules_list = frappe.get_all(
235 "Party Specific Item", filters={"party": party}, fields=["restrict_based_on", "based_on_value"]
236 )
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530237
DeeMysterioaa826242021-09-14 13:58:18 +0530238 filters_dict = {}
239 for rule in item_rules_list:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530240 if rule["restrict_based_on"] == "Item":
241 rule["restrict_based_on"] = "name"
DeeMysterioaa826242021-09-14 13:58:18 +0530242 filters_dict[rule.restrict_based_on] = []
noahjacobca2fb472021-05-12 16:25:07 +0530243
DeeMysterioaa826242021-09-14 13:58:18 +0530244 for rule in item_rules_list:
245 filters_dict[rule.restrict_based_on].append(rule.based_on_value)
noahjacobca2fb472021-05-12 16:25:07 +0530246
DeeMysterioaa826242021-09-14 13:58:18 +0530247 for filter in filters_dict:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530248 filters[scrub(filter)] = ["in", filters_dict[filter]]
DeeMysterioaa826242021-09-14 13:58:18 +0530249
Ankush Menat494bd9e2022-03-28 18:52:46 +0530250 if filters.get("customer"):
251 del filters["customer"]
DeeMysterioaa826242021-09-14 13:58:18 +0530252 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530253 del filters["supplier"]
Ankush Menat41a95e52022-02-03 13:02:13 +0530254 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530255 filters.pop("customer", None)
256 filters.pop("supplier", None)
DeeMysterioaa826242021-09-14 13:58:18 +0530257
Ankush Menat494bd9e2022-03-28 18:52:46 +0530258 description_cond = ""
Sagar Vora9baa2222022-08-03 05:42:30 +0000259 if frappe.db.count(doctype, cache=True) < 50000:
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530260 # scan description only if items are less than 50000
Ankush Menat494bd9e2022-03-28 18:52:46 +0530261 description_cond = "or tabItem.description LIKE %(txt)s"
Rohit Waghchaurefd889fd2022-09-28 23:00:45 +0530262
Ankush Menat494bd9e2022-03-28 18:52:46 +0530263 return frappe.db.sql(
264 """select
Rohit Waghchaurefd889fd2022-09-28 23:00:45 +0530265 tabItem.name {columns}
Anand Doshibd67e872014-04-11 16:51:27 +0530266 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530267 where tabItem.docstatus < 2
Anand Doshi21e09a22015-10-29 12:21:41 +0530268 and tabItem.disabled=0
rohitwaghchaure79789072020-05-21 18:10:13 +0530269 and tabItem.has_variants=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530270 and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
marination3dbef9d2019-10-28 15:48:10 +0530271 and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s)
Rohit Waghchaure2bfb0632019-03-02 21:47:55 +0530272 {description_cond})
Anand Doshi22c0d782013-11-04 16:23:04 +0530273 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530274 order by
275 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
276 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530277 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530278 name, item_name
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530279 limit %(start)s, %(page_len)s """.format(
marination1e754b12019-10-30 18:33:44 +0530280 columns=columns,
marination3dbef9d2019-10-28 15:48:10 +0530281 scond=searchfields,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530282 fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
283 mcond=get_match_cond(doctype).replace("%", "%%"),
284 description_cond=description_cond,
285 ),
286 {
287 "today": nowdate(),
288 "txt": "%%%s%%" % txt,
289 "_txt": txt.replace("%", ""),
290 "start": start,
291 "page_len": page_len,
292 },
293 as_dict=as_dict,
294 )
Saurabh02875592013-07-08 18:45:55 +0530295
Himanshud94a38e2020-05-18 14:26:26 +0530296
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530297@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530298@frappe.validate_and_sanitize_search_inputs
Saurabh022ab632017-11-10 15:06:02 +0530299def bom(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +0000300 doctype = "BOM"
Anand Doshibd67e872014-04-11 16:51:27 +0530301 conditions = []
Sagar Vora9baa2222022-08-03 05:42:30 +0000302 fields = get_fields(doctype, ["name", "item"])
Saurabhf52dc072013-07-10 13:07:49 +0530303
Ankush Menat494bd9e2022-03-28 18:52:46 +0530304 return frappe.db.sql(
305 """select {fields}
Conorea28ed12022-06-17 10:47:48 -0500306 from `tabBOM`
307 where `tabBOM`.docstatus=1
308 and `tabBOM`.is_active=1
309 and `tabBOM`.`{key}` like %(txt)s
Nabin Hait62211172016-03-16 16:22:03 +0530310 {fcond} {mcond}
311 order by
Conorea28ed12022-06-17 10:47:48 -0500312 (case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530313 idx desc, name
Conorea28ed12022-06-17 10:47:48 -0500314 limit %(page_len)s offset %(start)s""".format(
Himanshud94a38e2020-05-18 14:26:26 +0530315 fields=", ".join(fields),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530316 fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
317 mcond=get_match_cond(doctype).replace("%", "%%"),
318 key=searchfield,
319 ),
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530320 {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530321 "txt": "%" + txt + "%",
322 "_txt": txt.replace("%", ""),
323 "start": start or 0,
324 "page_len": page_len or 20,
325 },
326 )
Saurabh02875592013-07-08 18:45:55 +0530327
Himanshud94a38e2020-05-18 14:26:26 +0530328
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530329@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530330@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +0530331def get_project_name(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +0000332 doctype = "Project"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530333 cond = ""
334 if filters and filters.get("customer"):
Suraj Shetty6ea3de92018-09-26 18:15:53 +0530335 cond = """(`tabProject`.customer = %s or
Ankush Menat494bd9e2022-03-28 18:52:46 +0530336 ifnull(`tabProject`.customer,"")="") and""" % (
337 frappe.db.escape(filters.get("customer"))
338 )
Anand Doshibd67e872014-04-11 16:51:27 +0530339
Sagar Vora9baa2222022-08-03 05:42:30 +0000340 fields = get_fields(doctype, ["name", "project_name"])
341 searchfields = frappe.get_meta(doctype).get_search_fields()
Conor74a782d2022-06-17 06:31:27 -0500342 searchfields = " or ".join(["`tabProject`." + field + " like %(txt)s" for field in searchfields])
Himanshud94a38e2020-05-18 14:26:26 +0530343
Ankush Menat494bd9e2022-03-28 18:52:46 +0530344 return frappe.db.sql(
345 """select {fields} from `tabProject`
Rucha Mahabal062d3012021-05-07 13:31:14 +0530346 where
Conor74a782d2022-06-17 06:31:27 -0500347 `tabProject`.status not in ('Completed', 'Cancelled')
Subin Tom889140f2021-06-22 16:26:19 +0530348 and {cond} {scond} {match_cond}
Rushabh Mehta3574b372016-03-11 14:33:04 +0530349 order by
Conorea28ed12022-06-17 10:47:48 -0500350 (case when locate(%(_txt)s, `tabProject`.name) > 0 then locate(%(_txt)s, `tabProject`.name) else 99999 end),
351 `tabProject`.idx desc,
Rushabh Mehta3574b372016-03-11 14:33:04 +0530352 `tabProject`.name asc
Conor00ef4992022-06-14 00:19:07 -0500353 limit {page_len} offset {start}""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530354 fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530355 cond=cond,
Rucha Mahabal062d3012021-05-07 13:31:14 +0530356 scond=searchfields,
Rushabh Mehta3574b372016-03-11 14:33:04 +0530357 match_cond=get_match_cond(doctype),
358 start=start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530359 page_len=page_len,
360 ),
361 {"txt": "%{0}%".format(txt), "_txt": txt.replace("%", "")},
362 )
Anand Doshibd67e872014-04-11 16:51:27 +0530363
tundebabzyf6d738b2017-09-18 12:40:09 +0100364
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530365@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530366@frappe.validate_and_sanitize_search_inputs
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530367def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
Sagar Vora9baa2222022-08-03 05:42:30 +0000368 doctype = "Delivery Note"
369 fields = get_fields(doctype, ["name", "customer", "posting_date"])
Himanshud94a38e2020-05-18 14:26:26 +0530370
Ankush Menat494bd9e2022-03-28 18:52:46 +0530371 return frappe.db.sql(
372 """
Himanshud94a38e2020-05-18 14:26:26 +0530373 select %(fields)s
Anand Doshibd67e872014-04-11 16:51:27 +0530374 from `tabDelivery Note`
375 where `tabDelivery Note`.`%(key)s` like %(txt)s and
tundebabzyf6d738b2017-09-18 12:40:09 +0100376 `tabDelivery Note`.docstatus = 1
Conor74a782d2022-06-17 06:31:27 -0500377 and status not in ('Stopped', 'Closed') %(fcond)s
tundebabzyf6d738b2017-09-18 12:40:09 +0100378 and (
379 (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
Deepesh Garge2dc1022021-04-14 11:21:11 +0530380 or (`tabDelivery Note`.grand_total = 0 and `tabDelivery Note`.per_billed < 100)
tundebabzyf6d738b2017-09-18 12:40:09 +0100381 or (
382 `tabDelivery Note`.is_return = 1
383 and return_against in (select name from `tabDelivery Note` where per_billed < 100)
384 )
385 )
Conor00ef4992022-06-14 00:19:07 -0500386 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(page_len)s offset %(start)s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530387 """
388 % {
389 "fields": ", ".join(["`tabDelivery Note`.{0}".format(f) for f in fields]),
390 "key": searchfield,
391 "fcond": get_filters_cond(doctype, filters, []),
392 "mcond": get_match_cond(doctype),
393 "start": start,
394 "page_len": page_len,
395 "txt": "%(txt)s",
396 },
397 {"txt": ("%%%s%%" % txt)},
398 as_dict=as_dict,
399 )
tundebabzyf6d738b2017-09-18 12:40:09 +0100400
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530401
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530402@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530403@frappe.validate_and_sanitize_search_inputs
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530404def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +0000405 doctype = "Batch"
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530406 cond = ""
407 if filters.get("posting_date"):
Nabin Hait7918b922018-01-31 15:30:03 +0530408 cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530409
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530410 batch_nos = None
411 args = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530412 "item_code": filters.get("item_code"),
413 "warehouse": filters.get("warehouse"),
414 "posting_date": filters.get("posting_date"),
415 "txt": "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530416 "start": start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530417 "page_len": page_len,
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530418 }
419
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530420 having_clause = "having sum(sle.actual_qty) > 0"
421 if filters.get("is_return"):
422 having_clause = ""
423
Sagar Vora9baa2222022-08-03 05:42:30 +0000424 meta = frappe.get_meta(doctype, cached=True)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530425 searchfields = meta.get_search_fields()
426
Ankush Menat494bd9e2022-03-28 18:52:46 +0530427 search_columns = ""
428 search_cond = ""
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530429
Deepesh Garga0d192e2020-09-22 13:54:07 +0530430 if searchfields:
431 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530432 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530433
Ankush Menat494bd9e2022-03-28 18:52:46 +0530434 if args.get("warehouse"):
435 searchfields = ["batch." + field for field in searchfields]
Deepesh Garga0d192e2020-09-22 13:54:07 +0530436 if searchfields:
437 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530438 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530439
Ankush Menat494bd9e2022-03-28 18:52:46 +0530440 batch_nos = frappe.db.sql(
441 """select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom,
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530442 concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530443 {search_columns}
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530444 from `tabStock Ledger Entry` sle
445 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
446 where
447 batch.disabled = 0
Rohit Waghchaurec14aa452021-07-20 18:19:15 +0530448 and sle.is_cancelled = 0
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530449 and sle.item_code = %(item_code)s
450 and sle.warehouse = %(warehouse)s
451 and (sle.batch_no like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530452 or batch.expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530453 or batch.manufacturing_date like %(txt)s
454 {search_cond})
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530455 and batch.docstatus < 2
456 {cond}
457 {match_conditions}
458 group by batch_no {having_clause}
459 order by batch.expiry_date, sle.batch_no desc
Conor00ef4992022-06-14 00:19:07 -0500460 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530461 search_columns=search_columns,
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530462 cond=cond,
463 match_conditions=get_match_cond(doctype),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530464 having_clause=having_clause,
465 search_cond=search_cond,
466 ),
467 args,
468 )
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530469
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530470 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530471 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530472 return frappe.db.sql(
473 """select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530474 {search_columns}
475 from `tabBatch` batch
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800476 where batch.disabled = 0
477 and item = %(item_code)s
sivankar621740e2018-02-12 14:33:40 +0530478 and (name like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530479 or expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530480 or manufacturing_date like %(txt)s
481 {search_cond})
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530482 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530483 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530484 {match_conditions}
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530485
Anand Doshi0dc79f42015-04-06 12:59:34 +0530486 order by expiry_date, name desc
Conor00ef4992022-06-14 00:19:07 -0500487 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530488 cond,
489 search_columns=search_columns,
490 search_cond=search_cond,
491 match_conditions=get_match_cond(doctype),
492 ),
493 args,
494 )
Nabin Haitea4aa042014-05-28 12:56:28 +0530495
Himanshud94a38e2020-05-18 14:26:26 +0530496
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530497@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530498@frappe.validate_and_sanitize_search_inputs
Nabin Haitea4aa042014-05-28 12:56:28 +0530499def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Sagar Vora9baa2222022-08-03 05:42:30 +0000500 doctype = "Account"
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530501 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530502
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530503 if isinstance(filters, dict):
504 for key, val in filters.items():
505 if isinstance(val, (list, tuple)):
506 filter_list.append([doctype, key, val[0], val[1]])
507 else:
508 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530509 elif isinstance(filters, list):
510 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530511
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530512 if "is_group" not in [d[1] for d in filter_list]:
513 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530514
515 if searchfield and txt:
516 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
517
Ankush Menat494bd9e2022-03-28 18:52:46 +0530518 return frappe.desk.reportview.execute(
Sagar Vora9baa2222022-08-03 05:42:30 +0000519 doctype,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530520 filters=filter_list,
521 fields=["name", "parent_account"],
522 limit_start=start,
523 limit_page_length=page_len,
524 as_list=True,
525 )
526
Anand Doshifaefeaa2014-06-24 18:53:04 +0530527
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530528@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530529@frappe.validate_and_sanitize_search_inputs
Marica299e2172020-04-28 13:00:04 +0530530def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530531 return frappe.db.sql(
532 """select distinct bo.name, bo.blanket_order_type, bo.to_date
Marica299e2172020-04-28 13:00:04 +0530533 from `tabBlanket Order` bo, `tabBlanket Order Item` boi
534 where
535 boi.parent = bo.name
536 and boi.item_code = {item_code}
537 and bo.blanket_order_type = '{blanket_order_type}'
538 and bo.company = {company}
Ankush Menat494bd9e2022-03-28 18:52:46 +0530539 and bo.docstatus = 1""".format(
540 item_code=frappe.db.escape(filters.get("item")),
541 blanket_order_type=filters.get("blanket_order_type"),
542 company=frappe.db.escape(filters.get("company")),
543 )
544 )
Nabin Haitafd14f62015-10-19 11:55:28 +0530545
Himanshud94a38e2020-05-18 14:26:26 +0530546
Nabin Haitafd14f62015-10-19 11:55:28 +0530547@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530548@frappe.validate_and_sanitize_search_inputs
Nabin Haitafd14f62015-10-19 11:55:28 +0530549def get_income_account(doctype, txt, searchfield, start, page_len, filters):
550 from erpnext.controllers.queries import get_match_cond
551
552 # income account can be any Credit account,
553 # but can also be a Asset account with account_type='Income Account' in special circumstances.
554 # Hence the first condition is an "OR"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530555 if not filters:
556 filters = {}
Nabin Haitafd14f62015-10-19 11:55:28 +0530557
Sagar Vora9baa2222022-08-03 05:42:30 +0000558 doctype = "Account"
Anand Doshi21e09a22015-10-29 12:21:41 +0530559 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530560 if filters.get("company"):
561 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530562
Ankush Menat494bd9e2022-03-28 18:52:46 +0530563 return frappe.db.sql(
564 """select tabAccount.name from `tabAccount`
Nabin Haitafd14f62015-10-19 11:55:28 +0530565 where (tabAccount.report_type = "Profit and Loss"
566 or tabAccount.account_type in ("Income Account", "Temporary"))
567 and tabAccount.is_group=0
568 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530569 {condition} {match_condition}
Ankush Menat494bd9e2022-03-28 18:52:46 +0530570 order by idx desc, name""".format(
571 condition=condition, match_condition=get_match_cond(doctype), key=searchfield
572 ),
573 {"txt": "%" + txt + "%", "company": filters.get("company", "")},
574 )
575
Nabin Hait3a15c922016-03-04 12:30:46 +0530576
Deepesh Garg96e874b2020-11-15 22:43:01 +0530577@frappe.whitelist()
578@frappe.validate_and_sanitize_search_inputs
Ankush Menat6de71eb2023-04-25 18:33:31 +0530579def get_filtered_dimensions(
580 doctype, txt, searchfield, start, page_len, filters, reference_doctype=None
581):
Chillar Anand915b3432021-09-02 16:44:59 +0530582 from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import (
583 get_dimension_filter_map,
584 )
Ankush Menat494bd9e2022-03-28 18:52:46 +0530585
Deepesh Garg96e874b2020-11-15 22:43:01 +0530586 dimension_filters = get_dimension_filter_map()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530587 dimension_filters = dimension_filters.get((filters.get("dimension"), filters.get("account")))
Deepesh Garg6c17b842020-11-25 13:42:16 +0530588 query_filters = []
mergify[bot]071118f2021-11-30 13:15:20 +0000589 or_filters = []
Ankush Menat494bd9e2022-03-28 18:52:46 +0530590 fields = ["name"]
mergify[bot]071118f2021-11-30 13:15:20 +0000591
592 searchfields = frappe.get_meta(doctype).get_search_fields()
Deepesh Garg96e874b2020-11-15 22:43:01 +0530593
594 meta = frappe.get_meta(doctype)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530595 if meta.is_tree:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530596 query_filters.append(["is_group", "=", 0])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530597
Ankush Menat494bd9e2022-03-28 18:52:46 +0530598 if meta.has_field("disabled"):
599 query_filters.append(["disabled", "!=", 1])
Subin Tom333e44e2021-08-18 16:17:54 +0530600
Ankush Menat494bd9e2022-03-28 18:52:46 +0530601 if meta.has_field("company"):
602 query_filters.append(["company", "=", filters.get("company")])
Deepesh Garg6c17b842020-11-25 13:42:16 +0530603
mergify[bot]071118f2021-11-30 13:15:20 +0000604 for field in searchfields:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530605 or_filters.append([field, "LIKE", "%%%s%%" % txt])
mergify[bot]071118f2021-11-30 13:15:20 +0000606 fields.append(field)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530607
608 if dimension_filters:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530609 if dimension_filters["allow_or_restrict"] == "Allow":
610 query_selector = "in"
Deepesh Garg96e874b2020-11-15 22:43:01 +0530611 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530612 query_selector = "not in"
Deepesh Garg96e874b2020-11-15 22:43:01 +0530613
Ankush Menat494bd9e2022-03-28 18:52:46 +0530614 if len(dimension_filters["allowed_dimensions"]) == 1:
615 dimensions = tuple(dimension_filters["allowed_dimensions"] * 2)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530616 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530617 dimensions = tuple(dimension_filters["allowed_dimensions"])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530618
Ankush Menat494bd9e2022-03-28 18:52:46 +0530619 query_filters.append(["name", query_selector, dimensions])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530620
Ankush Menat494bd9e2022-03-28 18:52:46 +0530621 output = frappe.get_list(
Ankush Menat6de71eb2023-04-25 18:33:31 +0530622 doctype,
623 fields=fields,
624 filters=query_filters,
625 or_filters=or_filters,
626 as_list=1,
627 reference_doctype=reference_doctype,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530628 )
Deepesh Garg6c17b842020-11-25 13:42:16 +0530629
mergify[bot]071118f2021-11-30 13:15:20 +0000630 return [tuple(d) for d in set(output)]
Nabin Hait3a15c922016-03-04 12:30:46 +0530631
Ankush Menat494bd9e2022-03-28 18:52:46 +0530632
Nabin Hait3a15c922016-03-04 12:30:46 +0530633@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530634@frappe.validate_and_sanitize_search_inputs
Nabin Hait3a15c922016-03-04 12:30:46 +0530635def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
636 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530637
Ankush Menat494bd9e2022-03-28 18:52:46 +0530638 if not filters:
639 filters = {}
Nabin Hait3a15c922016-03-04 12:30:46 +0530640
Sagar Vora9baa2222022-08-03 05:42:30 +0000641 doctype = "Account"
Nabin Hait3a15c922016-03-04 12:30:46 +0530642 condition = ""
643 if filters.get("company"):
644 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530645
Ankush Menat494bd9e2022-03-28 18:52:46 +0530646 return frappe.db.sql(
647 """select tabAccount.name from `tabAccount`
Nabin Hait3a15c922016-03-04 12:30:46 +0530648 where (tabAccount.report_type = "Profit and Loss"
Mangesh-Khairnar5619db22019-08-21 14:49:24 +0530649 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed", "Capital Work in Progress"))
Nabin Hait3a15c922016-03-04 12:30:46 +0530650 and tabAccount.is_group=0
651 and tabAccount.docstatus!=2
652 and tabAccount.{key} LIKE %(txt)s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530653 {condition} {match_condition}""".format(
654 condition=condition, key=searchfield, match_condition=get_match_cond(doctype)
655 ),
656 {"company": filters.get("company", ""), "txt": "%" + txt + "%"},
657 )
suyashphadtare049a88c2017-01-12 17:49:37 +0530658
659
660@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530661@frappe.validate_and_sanitize_search_inputs
suyashphadtare049a88c2017-01-12 17:49:37 +0530662def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
663 # Should be used when item code is passed in filters.
Sagar Vora9baa2222022-08-03 05:42:30 +0000664 doctype = "Warehouse"
suyashphadtare750a0672017-01-18 15:35:01 +0530665 conditions, bin_conditions = [], []
666 filter_dict = get_doctype_wise_filters(filters)
667
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530668 query = """select `tabWarehouse`.name,
Conor74a782d2022-06-17 06:31:27 -0500669 CONCAT_WS(' : ', 'Actual Qty', ifnull(round(`tabBin`.actual_qty, 2), 0 )) actual_qty
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530670 from `tabWarehouse` left join `tabBin`
671 on `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions}
suyashphadtare34ab1362017-01-31 15:14:44 +0530672 where
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530673 `tabWarehouse`.`{key}` like {txt}
suyashphadtare34ab1362017-01-31 15:14:44 +0530674 {fcond} {mcond}
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530675 order by ifnull(`tabBin`.actual_qty, 0) desc
suyashphadtare34ab1362017-01-31 15:14:44 +0530676 limit
Conor00ef4992022-06-14 00:19:07 -0500677 {page_len} offset {start}
suyashphadtare34ab1362017-01-31 15:14:44 +0530678 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530679 bin_conditions=get_filters_cond(
680 doctype, filter_dict.get("Bin"), bin_conditions, ignore_permissions=True
681 ),
682 key=searchfield,
683 fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
684 mcond=get_match_cond(doctype),
685 start=start,
686 page_len=page_len,
687 txt=frappe.db.escape("%{0}%".format(txt)),
688 )
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530689
690 return frappe.db.sql(query)
suyashphadtare750a0672017-01-18 15:35:01 +0530691
692
693def get_doctype_wise_filters(filters):
694 # Helper function to seperate filters doctype_wise
695 filter_dict = defaultdict(list)
696 for row in filters:
697 filter_dict[row[0]].append(row)
698 return filter_dict
tundebabzy2a4fefc2017-11-29 06:23:09 +0100699
700
701@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530702@frappe.validate_and_sanitize_search_inputs
tundebabzy2a4fefc2017-11-29 06:23:09 +0100703def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530704 query = """select batch_id from `tabBatch`
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800705 where disabled = 0
Conorb8f728a2022-06-15 01:37:33 -0500706 and (expiry_date >= CURRENT_DATE or expiry_date IS NULL)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530707 and name like {txt}""".format(
708 txt=frappe.db.escape("%{0}%".format(txt))
709 )
tundebabzy2a4fefc2017-11-29 06:23:09 +0100710
Ankush Menat494bd9e2022-03-28 18:52:46 +0530711 if filters and filters.get("item"):
712 query += " and item = {item}".format(item=frappe.db.escape(filters.get("item")))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100713
Sachin Mane64f48db2018-01-08 17:57:32 +0530714 return frappe.db.sql(query, filters)
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530715
Himanshud94a38e2020-05-18 14:26:26 +0530716
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530717@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530718@frappe.validate_and_sanitize_search_inputs
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530719def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters):
Maricabac4b932019-09-16 19:44:28 +0530720 item_filters = [
Ankush Menat494bd9e2022-03-28 18:52:46 +0530721 ["manufacturer", "like", "%" + txt + "%"],
722 ["item_code", "=", filters.get("item_code")],
Maricabac4b932019-09-16 19:44:28 +0530723 ]
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530724
Maricabac4b932019-09-16 19:44:28 +0530725 item_manufacturers = frappe.get_all(
726 "Item Manufacturer",
727 fields=["manufacturer", "manufacturer_part_no"],
728 filters=item_filters,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530729 limit_start=start,
730 limit_page_length=page_len,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530731 as_list=1,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530732 )
Maricabac4b932019-09-16 19:44:28 +0530733 return item_manufacturers
Saqibd9956092019-11-18 11:46:55 +0530734
Himanshud94a38e2020-05-18 14:26:26 +0530735
Saqibd9956092019-11-18 11:46:55 +0530736@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530737@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530738def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters):
739 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530740 select pr.name
Saqibd9956092019-11-18 11:46:55 +0530741 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem
742 where pr.docstatus = 1 and pritem.parent = pr.name
Ankush Menat494bd9e2022-03-28 18:52:46 +0530743 and pr.name like {txt}""".format(
744 txt=frappe.db.escape("%{0}%".format(txt))
745 )
Saqibd9956092019-11-18 11:46:55 +0530746
Ankush Menat494bd9e2022-03-28 18:52:46 +0530747 if filters and filters.get("item_code"):
748 query += " and pritem.item_code = {item_code}".format(
749 item_code=frappe.db.escape(filters.get("item_code"))
750 )
Saqibd9956092019-11-18 11:46:55 +0530751
752 return frappe.db.sql(query, filters)
753
Himanshud94a38e2020-05-18 14:26:26 +0530754
Saqibd9956092019-11-18 11:46:55 +0530755@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530756@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530757def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters):
758 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530759 select pi.name
Saqibd9956092019-11-18 11:46:55 +0530760 from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem
761 where pi.docstatus = 1 and piitem.parent = pi.name
Ankush Menat494bd9e2022-03-28 18:52:46 +0530762 and pi.name like {txt}""".format(
763 txt=frappe.db.escape("%{0}%".format(txt))
764 )
Saqibd9956092019-11-18 11:46:55 +0530765
Ankush Menat494bd9e2022-03-28 18:52:46 +0530766 if filters and filters.get("item_code"):
767 query += " and piitem.item_code = {item_code}".format(
768 item_code=frappe.db.escape(filters.get("item_code"))
769 )
Saqibd9956092019-11-18 11:46:55 +0530770
771 return frappe.db.sql(query, filters)
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530772
Himanshud94a38e2020-05-18 14:26:26 +0530773
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530774@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530775@frappe.validate_and_sanitize_search_inputs
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530776def get_tax_template(doctype, txt, searchfield, start, page_len, filters):
777
Ankush Menat494bd9e2022-03-28 18:52:46 +0530778 item_doc = frappe.get_cached_doc("Item", filters.get("item_code"))
779 item_group = filters.get("item_group")
780 company = filters.get("company")
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530781 taxes = item_doc.taxes or []
782
783 while item_group:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530784 item_group_doc = frappe.get_cached_doc("Item Group", item_group)
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530785 taxes += item_group_doc.taxes or []
786 item_group = item_group_doc.parent_item_group
787
788 if not taxes:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530789 return frappe.get_all(
790 "Item Tax Template", filters={"disabled": 0, "company": company}, as_list=True
791 )
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530792 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530793 valid_from = filters.get("valid_from")
Marica0fcb05a2020-08-10 14:48:13 +0530794 valid_from = valid_from[1] if isinstance(valid_from, list) else valid_from
795
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530796 args = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530797 "item_code": filters.get("item_code"),
798 "posting_date": valid_from,
799 "tax_category": filters.get("tax_category"),
800 "company": company,
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530801 }
802
803 taxes = _get_item_tax_template(args, taxes, for_validate=True)
804 return [(d,) for d in set(taxes)]
Himanshud94a38e2020-05-18 14:26:26 +0530805
806
Ankush Menat7eac4a22021-04-19 10:33:39 +0530807def get_fields(doctype, fields=None):
808 if fields is None:
809 fields = []
Himanshud94a38e2020-05-18 14:26:26 +0530810 meta = frappe.get_meta(doctype)
811 fields.extend(meta.get_search_fields())
812
813 if meta.title_field and not meta.title_field.strip() in fields:
814 fields.insert(1, meta.title_field.strip())
815
816 return unique(fields)