blob: 5ba314ecf06ca02b1d1be0a4a48ee85425efc388 [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):
Kanchan Chauhan7652b852016-11-16 15:29:01 +053021 conditions = []
Himanshud94a38e2020-05-18 14:26:26 +053022 fields = get_fields("Employee", ["name", "employee_name"])
23
Ankush Menat494bd9e2022-03-28 18:52:46 +053024 return frappe.db.sql(
25 """select {fields} from `tabEmployee`
Anurag Mishrafc98abe2021-06-23 11:21:38 +053026 where status in ('Active', 'Suspended')
Anand Doshibd67e872014-04-11 16:51:27 +053027 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053028 and ({key} like %(txt)s
29 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053030 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053031 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053032 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
33 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053034 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053035 name, employee_name
Conor00ef4992022-06-14 00:19:07 -050036 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +053037 **{
38 "fields": ", ".join(fields),
39 "key": searchfield,
40 "fcond": get_filters_cond(doctype, filters, conditions),
41 "mcond": get_match_cond(doctype),
42 }
43 ),
44 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
45 )
Saurabh02875592013-07-08 18:45:55 +053046
Himanshud94a38e2020-05-18 14:26:26 +053047
48# searches for leads which are not converted
Chinmay D. Paiaa121092020-07-01 21:14:32 +053049@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053050@frappe.validate_and_sanitize_search_inputs
Anand Doshibd67e872014-04-11 16:51:27 +053051def lead_query(doctype, txt, searchfield, start, page_len, filters):
Himanshud94a38e2020-05-18 14:26:26 +053052 fields = get_fields("Lead", ["name", "lead_name", "company_name"])
53
Ankush Menat494bd9e2022-03-28 18:52:46 +053054 return frappe.db.sql(
55 """select {fields} from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053056 where docstatus < 2
57 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053058 and ({key} like %(txt)s
59 or lead_name like %(txt)s
60 or company_name like %(txt)s)
61 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053062 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053063 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
64 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
65 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053066 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053067 name, lead_name
Conor00ef4992022-06-14 00:19:07 -050068 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 **{"fields": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
70 ),
71 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
72 )
73
74 # searches for customer
Saurabh02875592013-07-08 18:45:55 +053075
Himanshud94a38e2020-05-18 14:26:26 +053076
Chinmay D. Paiaa121092020-07-01 21:14:32 +053077@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053078@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +053079def customer_query(doctype, txt, searchfield, start, page_len, filters):
KanchanChauhan4b888b92017-07-25 14:03:01 +053080 conditions = []
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053081 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053082
Saurabh02875592013-07-08 18:45:55 +053083 if cust_master_name == "Customer Name":
84 fields = ["name", "customer_group", "territory"]
85 else:
86 fields = ["name", "customer_name", "customer_group", "territory"]
Rushabh Mehtab92087c2017-01-13 18:53:11 +053087
Himanshud94a38e2020-05-18 14:26:26 +053088 fields = get_fields("Customer", fields)
Saurabhf52dc072013-07-10 13:07:49 +053089
Himanshud94a38e2020-05-18 14:26:26 +053090 searchfields = frappe.get_meta("Customer").get_search_fields()
Ankush Menata9c84f72021-06-11 16:00:48 +053091 searchfields = " or ".join(field + " like %(txt)s" for field in searchfields)
Saurabh02875592013-07-08 18:45:55 +053092
Ankush Menat494bd9e2022-03-28 18:52:46 +053093 return frappe.db.sql(
94 """select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053095 where docstatus < 2
Console Admin86231662017-06-23 20:32:52 +030096 and ({scond}) and disabled=0
KanchanChauhan4b888b92017-07-25 14:03:01 +053097 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053098 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053099 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
100 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530101 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530102 name, customer_name
Conor00ef4992022-06-14 00:19:07 -0500103 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104 **{
105 "fields": ", ".join(fields),
106 "scond": searchfields,
107 "mcond": get_match_cond(doctype),
108 "fcond": get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
109 }
110 ),
111 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
112 )
Saurabh02875592013-07-08 18:45:55 +0530113
Himanshud94a38e2020-05-18 14:26:26 +0530114
Saurabh02875592013-07-08 18:45:55 +0530115# searches for supplier
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530116@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530117@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +0530118def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530119 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Suraj Shetty1923ef02020-08-05 19:42:25 +0530120
Anand Doshibd67e872014-04-11 16:51:27 +0530121 if supp_master_name == "Supplier Name":
Zlash652e080982018-04-19 18:37:53 +0530122 fields = ["name", "supplier_group"]
Anand Doshibd67e872014-04-11 16:51:27 +0530123 else:
Zlash652e080982018-04-19 18:37:53 +0530124 fields = ["name", "supplier_name", "supplier_group"]
Himanshud94a38e2020-05-18 14:26:26 +0530125
126 fields = get_fields("Supplier", fields)
Saurabh02875592013-07-08 18:45:55 +0530127
Ankush Menat494bd9e2022-03-28 18:52:46 +0530128 return frappe.db.sql(
129 """select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530130 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530131 and ({key} like %(txt)s
Ankush Menat2221c9e2021-10-27 19:15:44 +0530132 or supplier_name like %(txt)s) and disabled=0
133 and (on_hold = 0 or (on_hold = 1 and CURDATE() > release_date))
Anand Doshi48d3b542014-07-09 13:15:03 +0530134 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530135 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530136 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
137 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530138 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530139 name, supplier_name
Conor00ef4992022-06-14 00:19:07 -0500140 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530141 **{"field": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
142 ),
143 {"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
144 )
Anand Doshibd67e872014-04-11 16:51:27 +0530145
Himanshud94a38e2020-05-18 14:26:26 +0530146
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530147@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530148@frappe.validate_and_sanitize_search_inputs
Nabin Hait9a380ef2013-07-16 17:24:17 +0530149def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530150 company_currency = erpnext.get_company_currency(filters.get("company"))
Deepesh Gargfbf6e562020-03-31 10:45:32 +0530151
Suraj Shetty1923ef02020-08-05 19:42:25 +0530152 def get_accounts(with_account_type_filter):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530153 account_type_condition = ""
Suraj Shetty1923ef02020-08-05 19:42:25 +0530154 if with_account_type_filter:
155 account_type_condition = "AND account_type in %(account_types)s"
156
Ankush Menat494bd9e2022-03-28 18:52:46 +0530157 accounts = frappe.db.sql(
158 """
Suraj Shetty1923ef02020-08-05 19:42:25 +0530159 SELECT name, parent_account
160 FROM `tabAccount`
161 WHERE `tabAccount`.docstatus!=2
162 {account_type_condition}
163 AND is_group = 0
164 AND company = %(company)s
Saqib Ansaria1e3ae82022-05-11 13:01:06 +0530165 AND disabled = %(disabled)s
Deepesh Garg57924592022-03-22 18:26:58 +0530166 AND (account_currency = %(currency)s or ifnull(account_currency, '') = '')
Suraj Shetty1923ef02020-08-05 19:42:25 +0530167 AND `{searchfield}` LIKE %(txt)s
prssannade7a2bc2020-09-21 13:57:04 +0530168 {mcond}
Suraj Shetty1923ef02020-08-05 19:42:25 +0530169 ORDER BY idx DESC, name
Conor00ef4992022-06-14 00:19:07 -0500170 LIMIT %(limit)s offset %(offset)s
prssannade7a2bc2020-09-21 13:57:04 +0530171 """.format(
172 account_type_condition=account_type_condition,
173 searchfield=searchfield,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530174 mcond=get_match_cond(doctype),
prssannade7a2bc2020-09-21 13:57:04 +0530175 ),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530176 dict(
177 account_types=filters.get("account_type"),
178 company=filters.get("company"),
Saqib Ansaria1e3ae82022-05-11 13:01:06 +0530179 disabled=filters.get("disabled", 0),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530180 currency=company_currency,
181 txt="%{}%".format(txt),
182 offset=start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530183 limit=page_len,
184 ),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530185 )
186
187 return accounts
188
189 tax_accounts = get_accounts(True)
190
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530191 if not tax_accounts:
Suraj Shetty1923ef02020-08-05 19:42:25 +0530192 tax_accounts = get_accounts(False)
Anand Doshibd67e872014-04-11 16:51:27 +0530193
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530194 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530195
Himanshud94a38e2020-05-18 14:26:26 +0530196
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530197@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530198@frappe.validate_and_sanitize_search_inputs
Rushabh Mehta203cc962016-04-07 15:25:43 +0530199def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530200 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530201
rohitwaghchaurec59371a2021-06-03 20:02:58 +0530202 if isinstance(filters, str):
203 filters = json.loads(filters)
204
Ankush Menat494bd9e2022-03-28 18:52:46 +0530205 # Get searchfields from meta and use in Item Link field query
marination1e754b12019-10-30 18:33:44 +0530206 meta = frappe.get_meta("Item", cached=True)
marination3dbef9d2019-10-28 15:48:10 +0530207 searchfields = meta.get_search_fields()
208
Ankush Menat34f52832021-11-09 11:55:33 +0530209 # these are handled separately
210 ignored_search_fields = ("item_name", "description")
211 for ignored_field in ignored_search_fields:
212 if ignored_field in searchfields:
213 searchfields.remove(ignored_field)
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530214
Ankush Menat494bd9e2022-03-28 18:52:46 +0530215 columns = ""
216 extra_searchfields = [
217 field
218 for field in searchfields
219 if not field in ["name", "item_group", "description", "item_name"]
220 ]
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530221
222 if extra_searchfields:
223 columns = ", " + ", ".join(extra_searchfields)
marination1e754b12019-10-30 18:33:44 +0530224
Ankush Menat494bd9e2022-03-28 18:52:46 +0530225 searchfields = searchfields + [
226 field
227 for field in [searchfield or "name", "item_code", "item_group", "item_name"]
228 if not field in searchfields
229 ]
marination3dbef9d2019-10-28 15:48:10 +0530230 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
231
DeeMysterioaa826242021-09-14 13:58:18 +0530232 if filters and isinstance(filters, dict):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530233 if filters.get("customer") or filters.get("supplier"):
234 party = filters.get("customer") or filters.get("supplier")
235 item_rules_list = frappe.get_all(
236 "Party Specific Item", filters={"party": party}, fields=["restrict_based_on", "based_on_value"]
237 )
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530238
DeeMysterioaa826242021-09-14 13:58:18 +0530239 filters_dict = {}
240 for rule in item_rules_list:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530241 if rule["restrict_based_on"] == "Item":
242 rule["restrict_based_on"] = "name"
DeeMysterioaa826242021-09-14 13:58:18 +0530243 filters_dict[rule.restrict_based_on] = []
noahjacobca2fb472021-05-12 16:25:07 +0530244
DeeMysterioaa826242021-09-14 13:58:18 +0530245 for rule in item_rules_list:
246 filters_dict[rule.restrict_based_on].append(rule.based_on_value)
noahjacobca2fb472021-05-12 16:25:07 +0530247
DeeMysterioaa826242021-09-14 13:58:18 +0530248 for filter in filters_dict:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530249 filters[scrub(filter)] = ["in", filters_dict[filter]]
DeeMysterioaa826242021-09-14 13:58:18 +0530250
Ankush Menat494bd9e2022-03-28 18:52:46 +0530251 if filters.get("customer"):
252 del filters["customer"]
DeeMysterioaa826242021-09-14 13:58:18 +0530253 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530254 del filters["supplier"]
Ankush Menat41a95e52022-02-03 13:02:13 +0530255 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530256 filters.pop("customer", None)
257 filters.pop("supplier", None)
DeeMysterioaa826242021-09-14 13:58:18 +0530258
Ankush Menat494bd9e2022-03-28 18:52:46 +0530259 description_cond = ""
260 if frappe.db.count("Item", cache=True) < 50000:
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530261 # scan description only if items are less than 50000
Ankush Menat494bd9e2022-03-28 18:52:46 +0530262 description_cond = "or tabItem.description LIKE %(txt)s"
263 return frappe.db.sql(
264 """select
Ankush Menat34f52832021-11-09 11:55:33 +0530265 tabItem.name, tabItem.item_name, tabItem.item_group,
Saurabh02875592013-07-08 18:45:55 +0530266 if(length(tabItem.description) > 40, \
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530267 concat(substr(tabItem.description, 1, 40), "..."), description) as description
marination1e754b12019-10-30 18:33:44 +0530268 {columns}
Anand Doshibd67e872014-04-11 16:51:27 +0530269 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530270 where tabItem.docstatus < 2
Anand Doshi21e09a22015-10-29 12:21:41 +0530271 and tabItem.disabled=0
rohitwaghchaure79789072020-05-21 18:10:13 +0530272 and tabItem.has_variants=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530273 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 +0530274 and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s)
Rohit Waghchaure2bfb0632019-03-02 21:47:55 +0530275 {description_cond})
Anand Doshi22c0d782013-11-04 16:23:04 +0530276 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530277 order by
278 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
279 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530280 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530281 name, item_name
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530282 limit %(start)s, %(page_len)s """.format(
marination1e754b12019-10-30 18:33:44 +0530283 columns=columns,
marination3dbef9d2019-10-28 15:48:10 +0530284 scond=searchfields,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530285 fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
286 mcond=get_match_cond(doctype).replace("%", "%%"),
287 description_cond=description_cond,
288 ),
289 {
290 "today": nowdate(),
291 "txt": "%%%s%%" % txt,
292 "_txt": txt.replace("%", ""),
293 "start": start,
294 "page_len": page_len,
295 },
296 as_dict=as_dict,
297 )
Saurabh02875592013-07-08 18:45:55 +0530298
Himanshud94a38e2020-05-18 14:26:26 +0530299
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530300@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530301@frappe.validate_and_sanitize_search_inputs
Saurabh022ab632017-11-10 15:06:02 +0530302def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530303 conditions = []
Himanshud94a38e2020-05-18 14:26:26 +0530304 fields = get_fields("BOM", ["name", "item"])
Saurabhf52dc072013-07-10 13:07:49 +0530305
Ankush Menat494bd9e2022-03-28 18:52:46 +0530306 return frappe.db.sql(
307 """select {fields}
Anand Doshibd67e872014-04-11 16:51:27 +0530308 from tabBOM
309 where tabBOM.docstatus=1
310 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530311 and tabBOM.`{key}` like %(txt)s
312 {fcond} {mcond}
313 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530314 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
315 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530316 limit %(start)s, %(page_len)s """.format(
Himanshud94a38e2020-05-18 14:26:26 +0530317 fields=", ".join(fields),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530318 fcond=get_filters_cond(doctype, filters, conditions).replace("%", "%%"),
319 mcond=get_match_cond(doctype).replace("%", "%%"),
320 key=searchfield,
321 ),
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530322 {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530323 "txt": "%" + txt + "%",
324 "_txt": txt.replace("%", ""),
325 "start": start or 0,
326 "page_len": page_len or 20,
327 },
328 )
Saurabh02875592013-07-08 18:45:55 +0530329
Himanshud94a38e2020-05-18 14:26:26 +0530330
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530331@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530332@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +0530333def get_project_name(doctype, txt, searchfield, start, page_len, filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530334 cond = ""
335 if filters and filters.get("customer"):
Suraj Shetty6ea3de92018-09-26 18:15:53 +0530336 cond = """(`tabProject`.customer = %s or
Ankush Menat494bd9e2022-03-28 18:52:46 +0530337 ifnull(`tabProject`.customer,"")="") and""" % (
338 frappe.db.escape(filters.get("customer"))
339 )
Anand Doshibd67e872014-04-11 16:51:27 +0530340
Rucha Mahabal062d3012021-05-07 13:31:14 +0530341 fields = get_fields("Project", ["name", "project_name"])
342 searchfields = frappe.get_meta("Project").get_search_fields()
Conor74a782d2022-06-17 06:31:27 -0500343 searchfields = " or ".join(["`tabProject`." + field + " like %(txt)s" for field in searchfields])
Himanshud94a38e2020-05-18 14:26:26 +0530344
Ankush Menat494bd9e2022-03-28 18:52:46 +0530345 return frappe.db.sql(
346 """select {fields} from `tabProject`
Rucha Mahabal062d3012021-05-07 13:31:14 +0530347 where
Conor74a782d2022-06-17 06:31:27 -0500348 `tabProject`.status not in ('Completed', 'Cancelled')
Subin Tom889140f2021-06-22 16:26:19 +0530349 and {cond} {scond} {match_cond}
Rushabh Mehta3574b372016-03-11 14:33:04 +0530350 order by
351 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
352 idx desc,
353 `tabProject`.name asc
Conor00ef4992022-06-14 00:19:07 -0500354 limit {page_len} offset {start}""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530355 fields=", ".join(["`tabProject`.{0}".format(f) for f in fields]),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530356 cond=cond,
Rucha Mahabal062d3012021-05-07 13:31:14 +0530357 scond=searchfields,
Rushabh Mehta3574b372016-03-11 14:33:04 +0530358 match_cond=get_match_cond(doctype),
359 start=start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530360 page_len=page_len,
361 ),
362 {"txt": "%{0}%".format(txt), "_txt": txt.replace("%", "")},
363 )
Anand Doshibd67e872014-04-11 16:51:27 +0530364
tundebabzyf6d738b2017-09-18 12:40:09 +0100365
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530366@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530367@frappe.validate_and_sanitize_search_inputs
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530368def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
Himanshud94a38e2020-05-18 14:26:26 +0530369 fields = get_fields("Delivery Note", ["name", "customer", "posting_date"])
370
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):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530405 cond = ""
406 if filters.get("posting_date"):
Nabin Hait7918b922018-01-31 15:30:03 +0530407 cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530408
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530409 batch_nos = None
410 args = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530411 "item_code": filters.get("item_code"),
412 "warehouse": filters.get("warehouse"),
413 "posting_date": filters.get("posting_date"),
414 "txt": "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530415 "start": start,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530416 "page_len": page_len,
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530417 }
418
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530419 having_clause = "having sum(sle.actual_qty) > 0"
420 if filters.get("is_return"):
421 having_clause = ""
422
Deepesh Garga0d192e2020-09-22 13:54:07 +0530423 meta = frappe.get_meta("Batch", cached=True)
424 searchfields = meta.get_search_fields()
425
Ankush Menat494bd9e2022-03-28 18:52:46 +0530426 search_columns = ""
427 search_cond = ""
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530428
Deepesh Garga0d192e2020-09-22 13:54:07 +0530429 if searchfields:
430 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530431 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530432
Ankush Menat494bd9e2022-03-28 18:52:46 +0530433 if args.get("warehouse"):
434 searchfields = ["batch." + field for field in searchfields]
Deepesh Garga0d192e2020-09-22 13:54:07 +0530435 if searchfields:
436 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530437 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530438
Ankush Menat494bd9e2022-03-28 18:52:46 +0530439 batch_nos = frappe.db.sql(
440 """select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom,
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530441 concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530442 {search_columns}
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530443 from `tabStock Ledger Entry` sle
444 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
445 where
446 batch.disabled = 0
Rohit Waghchaurec14aa452021-07-20 18:19:15 +0530447 and sle.is_cancelled = 0
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530448 and sle.item_code = %(item_code)s
449 and sle.warehouse = %(warehouse)s
450 and (sle.batch_no like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530451 or batch.expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530452 or batch.manufacturing_date like %(txt)s
453 {search_cond})
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530454 and batch.docstatus < 2
455 {cond}
456 {match_conditions}
457 group by batch_no {having_clause}
458 order by batch.expiry_date, sle.batch_no desc
Conor00ef4992022-06-14 00:19:07 -0500459 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530460 search_columns=search_columns,
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530461 cond=cond,
462 match_conditions=get_match_cond(doctype),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530463 having_clause=having_clause,
464 search_cond=search_cond,
465 ),
466 args,
467 )
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530468
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530469 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530470 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530471 return frappe.db.sql(
472 """select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530473 {search_columns}
474 from `tabBatch` batch
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800475 where batch.disabled = 0
476 and item = %(item_code)s
sivankar621740e2018-02-12 14:33:40 +0530477 and (name like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530478 or expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530479 or manufacturing_date like %(txt)s
480 {search_cond})
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530481 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530482 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530483 {match_conditions}
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530484
Anand Doshi0dc79f42015-04-06 12:59:34 +0530485 order by expiry_date, name desc
Conor00ef4992022-06-14 00:19:07 -0500486 limit %(page_len)s offset %(start)s""".format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530487 cond,
488 search_columns=search_columns,
489 search_cond=search_cond,
490 match_conditions=get_match_cond(doctype),
491 ),
492 args,
493 )
Nabin Haitea4aa042014-05-28 12:56:28 +0530494
Himanshud94a38e2020-05-18 14:26:26 +0530495
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530496@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530497@frappe.validate_and_sanitize_search_inputs
Nabin Haitea4aa042014-05-28 12:56:28 +0530498def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530499 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530500
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530501 if isinstance(filters, dict):
502 for key, val in filters.items():
503 if isinstance(val, (list, tuple)):
504 filter_list.append([doctype, key, val[0], val[1]])
505 else:
506 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530507 elif isinstance(filters, list):
508 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530509
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530510 if "is_group" not in [d[1] for d in filter_list]:
511 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530512
513 if searchfield and txt:
514 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
515
Ankush Menat494bd9e2022-03-28 18:52:46 +0530516 return frappe.desk.reportview.execute(
517 "Account",
518 filters=filter_list,
519 fields=["name", "parent_account"],
520 limit_start=start,
521 limit_page_length=page_len,
522 as_list=True,
523 )
524
Anand Doshifaefeaa2014-06-24 18:53:04 +0530525
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530526@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530527@frappe.validate_and_sanitize_search_inputs
Marica299e2172020-04-28 13:00:04 +0530528def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530529 return frappe.db.sql(
530 """select distinct bo.name, bo.blanket_order_type, bo.to_date
Marica299e2172020-04-28 13:00:04 +0530531 from `tabBlanket Order` bo, `tabBlanket Order Item` boi
532 where
533 boi.parent = bo.name
534 and boi.item_code = {item_code}
535 and bo.blanket_order_type = '{blanket_order_type}'
536 and bo.company = {company}
Ankush Menat494bd9e2022-03-28 18:52:46 +0530537 and bo.docstatus = 1""".format(
538 item_code=frappe.db.escape(filters.get("item")),
539 blanket_order_type=filters.get("blanket_order_type"),
540 company=frappe.db.escape(filters.get("company")),
541 )
542 )
Nabin Haitafd14f62015-10-19 11:55:28 +0530543
Himanshud94a38e2020-05-18 14:26:26 +0530544
Nabin Haitafd14f62015-10-19 11:55:28 +0530545@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530546@frappe.validate_and_sanitize_search_inputs
Nabin Haitafd14f62015-10-19 11:55:28 +0530547def get_income_account(doctype, txt, searchfield, start, page_len, filters):
548 from erpnext.controllers.queries import get_match_cond
549
550 # income account can be any Credit account,
551 # but can also be a Asset account with account_type='Income Account' in special circumstances.
552 # Hence the first condition is an "OR"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530553 if not filters:
554 filters = {}
Nabin Haitafd14f62015-10-19 11:55:28 +0530555
Anand Doshi21e09a22015-10-29 12:21:41 +0530556 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530557 if filters.get("company"):
558 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530559
Ankush Menat494bd9e2022-03-28 18:52:46 +0530560 return frappe.db.sql(
561 """select tabAccount.name from `tabAccount`
Nabin Haitafd14f62015-10-19 11:55:28 +0530562 where (tabAccount.report_type = "Profit and Loss"
563 or tabAccount.account_type in ("Income Account", "Temporary"))
564 and tabAccount.is_group=0
565 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530566 {condition} {match_condition}
Ankush Menat494bd9e2022-03-28 18:52:46 +0530567 order by idx desc, name""".format(
568 condition=condition, match_condition=get_match_cond(doctype), key=searchfield
569 ),
570 {"txt": "%" + txt + "%", "company": filters.get("company", "")},
571 )
572
Nabin Hait3a15c922016-03-04 12:30:46 +0530573
Deepesh Garg96e874b2020-11-15 22:43:01 +0530574@frappe.whitelist()
575@frappe.validate_and_sanitize_search_inputs
576def get_filtered_dimensions(doctype, txt, searchfield, start, page_len, filters):
Chillar Anand915b3432021-09-02 16:44:59 +0530577 from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import (
578 get_dimension_filter_map,
579 )
Ankush Menat494bd9e2022-03-28 18:52:46 +0530580
Deepesh Garg96e874b2020-11-15 22:43:01 +0530581 dimension_filters = get_dimension_filter_map()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530582 dimension_filters = dimension_filters.get((filters.get("dimension"), filters.get("account")))
Deepesh Garg6c17b842020-11-25 13:42:16 +0530583 query_filters = []
mergify[bot]071118f2021-11-30 13:15:20 +0000584 or_filters = []
Ankush Menat494bd9e2022-03-28 18:52:46 +0530585 fields = ["name"]
mergify[bot]071118f2021-11-30 13:15:20 +0000586
587 searchfields = frappe.get_meta(doctype).get_search_fields()
Deepesh Garg96e874b2020-11-15 22:43:01 +0530588
589 meta = frappe.get_meta(doctype)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530590 if meta.is_tree:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530591 query_filters.append(["is_group", "=", 0])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530592
Ankush Menat494bd9e2022-03-28 18:52:46 +0530593 if meta.has_field("disabled"):
594 query_filters.append(["disabled", "!=", 1])
Subin Tom333e44e2021-08-18 16:17:54 +0530595
Ankush Menat494bd9e2022-03-28 18:52:46 +0530596 if meta.has_field("company"):
597 query_filters.append(["company", "=", filters.get("company")])
Deepesh Garg6c17b842020-11-25 13:42:16 +0530598
mergify[bot]071118f2021-11-30 13:15:20 +0000599 for field in searchfields:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530600 or_filters.append([field, "LIKE", "%%%s%%" % txt])
mergify[bot]071118f2021-11-30 13:15:20 +0000601 fields.append(field)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530602
603 if dimension_filters:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530604 if dimension_filters["allow_or_restrict"] == "Allow":
605 query_selector = "in"
Deepesh Garg96e874b2020-11-15 22:43:01 +0530606 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530607 query_selector = "not in"
Deepesh Garg96e874b2020-11-15 22:43:01 +0530608
Ankush Menat494bd9e2022-03-28 18:52:46 +0530609 if len(dimension_filters["allowed_dimensions"]) == 1:
610 dimensions = tuple(dimension_filters["allowed_dimensions"] * 2)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530611 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530612 dimensions = tuple(dimension_filters["allowed_dimensions"])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530613
Ankush Menat494bd9e2022-03-28 18:52:46 +0530614 query_filters.append(["name", query_selector, dimensions])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530615
Ankush Menat494bd9e2022-03-28 18:52:46 +0530616 output = frappe.get_list(
617 doctype, fields=fields, filters=query_filters, or_filters=or_filters, as_list=1
618 )
Deepesh Garg6c17b842020-11-25 13:42:16 +0530619
mergify[bot]071118f2021-11-30 13:15:20 +0000620 return [tuple(d) for d in set(output)]
Nabin Hait3a15c922016-03-04 12:30:46 +0530621
Ankush Menat494bd9e2022-03-28 18:52:46 +0530622
Nabin Hait3a15c922016-03-04 12:30:46 +0530623@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530624@frappe.validate_and_sanitize_search_inputs
Nabin Hait3a15c922016-03-04 12:30:46 +0530625def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
626 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530627
Ankush Menat494bd9e2022-03-28 18:52:46 +0530628 if not filters:
629 filters = {}
Nabin Hait3a15c922016-03-04 12:30:46 +0530630
631 condition = ""
632 if filters.get("company"):
633 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530634
Ankush Menat494bd9e2022-03-28 18:52:46 +0530635 return frappe.db.sql(
636 """select tabAccount.name from `tabAccount`
Nabin Hait3a15c922016-03-04 12:30:46 +0530637 where (tabAccount.report_type = "Profit and Loss"
Mangesh-Khairnar5619db22019-08-21 14:49:24 +0530638 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 +0530639 and tabAccount.is_group=0
640 and tabAccount.docstatus!=2
641 and tabAccount.{key} LIKE %(txt)s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530642 {condition} {match_condition}""".format(
643 condition=condition, key=searchfield, match_condition=get_match_cond(doctype)
644 ),
645 {"company": filters.get("company", ""), "txt": "%" + txt + "%"},
646 )
suyashphadtare049a88c2017-01-12 17:49:37 +0530647
648
649@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530650@frappe.validate_and_sanitize_search_inputs
suyashphadtare049a88c2017-01-12 17:49:37 +0530651def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
652 # Should be used when item code is passed in filters.
suyashphadtare750a0672017-01-18 15:35:01 +0530653 conditions, bin_conditions = [], []
654 filter_dict = get_doctype_wise_filters(filters)
655
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530656 query = """select `tabWarehouse`.name,
Conor74a782d2022-06-17 06:31:27 -0500657 CONCAT_WS(' : ', 'Actual Qty', ifnull(round(`tabBin`.actual_qty, 2), 0 )) actual_qty
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530658 from `tabWarehouse` left join `tabBin`
659 on `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions}
suyashphadtare34ab1362017-01-31 15:14:44 +0530660 where
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530661 `tabWarehouse`.`{key}` like {txt}
suyashphadtare34ab1362017-01-31 15:14:44 +0530662 {fcond} {mcond}
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530663 order by ifnull(`tabBin`.actual_qty, 0) desc
suyashphadtare34ab1362017-01-31 15:14:44 +0530664 limit
Conor00ef4992022-06-14 00:19:07 -0500665 {page_len} offset {start}
suyashphadtare34ab1362017-01-31 15:14:44 +0530666 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530667 bin_conditions=get_filters_cond(
668 doctype, filter_dict.get("Bin"), bin_conditions, ignore_permissions=True
669 ),
670 key=searchfield,
671 fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
672 mcond=get_match_cond(doctype),
673 start=start,
674 page_len=page_len,
675 txt=frappe.db.escape("%{0}%".format(txt)),
676 )
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530677
678 return frappe.db.sql(query)
suyashphadtare750a0672017-01-18 15:35:01 +0530679
680
681def get_doctype_wise_filters(filters):
682 # Helper function to seperate filters doctype_wise
683 filter_dict = defaultdict(list)
684 for row in filters:
685 filter_dict[row[0]].append(row)
686 return filter_dict
tundebabzy2a4fefc2017-11-29 06:23:09 +0100687
688
689@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530690@frappe.validate_and_sanitize_search_inputs
tundebabzy2a4fefc2017-11-29 06:23:09 +0100691def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530692 query = """select batch_id from `tabBatch`
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800693 where disabled = 0
Conorb8f728a2022-06-15 01:37:33 -0500694 and (expiry_date >= CURRENT_DATE or expiry_date IS NULL)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530695 and name like {txt}""".format(
696 txt=frappe.db.escape("%{0}%".format(txt))
697 )
tundebabzy2a4fefc2017-11-29 06:23:09 +0100698
Ankush Menat494bd9e2022-03-28 18:52:46 +0530699 if filters and filters.get("item"):
700 query += " and item = {item}".format(item=frappe.db.escape(filters.get("item")))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100701
Sachin Mane64f48db2018-01-08 17:57:32 +0530702 return frappe.db.sql(query, filters)
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530703
Himanshud94a38e2020-05-18 14:26:26 +0530704
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530705@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530706@frappe.validate_and_sanitize_search_inputs
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530707def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters):
Maricabac4b932019-09-16 19:44:28 +0530708 item_filters = [
Ankush Menat494bd9e2022-03-28 18:52:46 +0530709 ["manufacturer", "like", "%" + txt + "%"],
710 ["item_code", "=", filters.get("item_code")],
Maricabac4b932019-09-16 19:44:28 +0530711 ]
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530712
Maricabac4b932019-09-16 19:44:28 +0530713 item_manufacturers = frappe.get_all(
714 "Item Manufacturer",
715 fields=["manufacturer", "manufacturer_part_no"],
716 filters=item_filters,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530717 limit_start=start,
718 limit_page_length=page_len,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530719 as_list=1,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530720 )
Maricabac4b932019-09-16 19:44:28 +0530721 return item_manufacturers
Saqibd9956092019-11-18 11:46:55 +0530722
Himanshud94a38e2020-05-18 14:26:26 +0530723
Saqibd9956092019-11-18 11:46:55 +0530724@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530725@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530726def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters):
727 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530728 select pr.name
Saqibd9956092019-11-18 11:46:55 +0530729 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem
730 where pr.docstatus = 1 and pritem.parent = pr.name
Ankush Menat494bd9e2022-03-28 18:52:46 +0530731 and pr.name like {txt}""".format(
732 txt=frappe.db.escape("%{0}%".format(txt))
733 )
Saqibd9956092019-11-18 11:46:55 +0530734
Ankush Menat494bd9e2022-03-28 18:52:46 +0530735 if filters and filters.get("item_code"):
736 query += " and pritem.item_code = {item_code}".format(
737 item_code=frappe.db.escape(filters.get("item_code"))
738 )
Saqibd9956092019-11-18 11:46:55 +0530739
740 return frappe.db.sql(query, filters)
741
Himanshud94a38e2020-05-18 14:26:26 +0530742
Saqibd9956092019-11-18 11:46:55 +0530743@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530744@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530745def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters):
746 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530747 select pi.name
Saqibd9956092019-11-18 11:46:55 +0530748 from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem
749 where pi.docstatus = 1 and piitem.parent = pi.name
Ankush Menat494bd9e2022-03-28 18:52:46 +0530750 and pi.name like {txt}""".format(
751 txt=frappe.db.escape("%{0}%".format(txt))
752 )
Saqibd9956092019-11-18 11:46:55 +0530753
Ankush Menat494bd9e2022-03-28 18:52:46 +0530754 if filters and filters.get("item_code"):
755 query += " and piitem.item_code = {item_code}".format(
756 item_code=frappe.db.escape(filters.get("item_code"))
757 )
Saqibd9956092019-11-18 11:46:55 +0530758
759 return frappe.db.sql(query, filters)
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530760
Himanshud94a38e2020-05-18 14:26:26 +0530761
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530762@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530763@frappe.validate_and_sanitize_search_inputs
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530764def get_tax_template(doctype, txt, searchfield, start, page_len, filters):
765
Ankush Menat494bd9e2022-03-28 18:52:46 +0530766 item_doc = frappe.get_cached_doc("Item", filters.get("item_code"))
767 item_group = filters.get("item_group")
768 company = filters.get("company")
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530769 taxes = item_doc.taxes or []
770
771 while item_group:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530772 item_group_doc = frappe.get_cached_doc("Item Group", item_group)
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530773 taxes += item_group_doc.taxes or []
774 item_group = item_group_doc.parent_item_group
775
776 if not taxes:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530777 return frappe.get_all(
778 "Item Tax Template", filters={"disabled": 0, "company": company}, as_list=True
779 )
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530780 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530781 valid_from = filters.get("valid_from")
Marica0fcb05a2020-08-10 14:48:13 +0530782 valid_from = valid_from[1] if isinstance(valid_from, list) else valid_from
783
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530784 args = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530785 "item_code": filters.get("item_code"),
786 "posting_date": valid_from,
787 "tax_category": filters.get("tax_category"),
788 "company": company,
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530789 }
790
791 taxes = _get_item_tax_template(args, taxes, for_validate=True)
792 return [(d,) for d in set(taxes)]
Himanshud94a38e2020-05-18 14:26:26 +0530793
794
Ankush Menat7eac4a22021-04-19 10:33:39 +0530795def get_fields(doctype, fields=None):
796 if fields is None:
797 fields = []
Himanshud94a38e2020-05-18 14:26:26 +0530798 meta = frappe.get_meta(doctype)
799 fields.extend(meta.get_search_fields())
800
801 if meta.title_field and not meta.title_field.strip() in fields:
802 fields.insert(1, meta.title_field.strip())
803
804 return unique(fields)