blob: 76a7d1a95ffbc3e163f72228274d7cac0c91e788 [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
24 return frappe.db.sql("""select {fields} from `tabEmployee`
Anurag Mishrafc98abe2021-06-23 11:21:38 +053025 where status in ('Active', 'Suspended')
Anand Doshibd67e872014-04-11 16:51:27 +053026 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053027 and ({key} like %(txt)s
28 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053029 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053030 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053031 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
32 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053033 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053034 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053035 limit %(start)s, %(page_len)s""".format(**{
Himanshud94a38e2020-05-18 14:26:26 +053036 'fields': ", ".join(fields),
Anand Doshi48d3b542014-07-09 13:15:03 +053037 'key': searchfield,
Kanchan Chauhan7652b852016-11-16 15:29:01 +053038 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi48d3b542014-07-09 13:15:03 +053039 'mcond': get_match_cond(doctype)
40 }), {
41 'txt': "%%%s%%" % txt,
42 '_txt': txt.replace("%", ""),
43 'start': start,
44 '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
54 return frappe.db.sql("""select {fields} from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053055 where docstatus < 2
56 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053057 and ({key} like %(txt)s
58 or lead_name like %(txt)s
59 or company_name like %(txt)s)
60 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053061 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053062 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
63 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
64 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053065 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053066 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053067 limit %(start)s, %(page_len)s""".format(**{
Himanshud94a38e2020-05-18 14:26:26 +053068 'fields': ", ".join(fields),
Anand Doshi48d3b542014-07-09 13:15:03 +053069 'key': searchfield,
70 'mcond':get_match_cond(doctype)
71 }), {
72 'txt': "%%%s%%" % txt,
73 '_txt': txt.replace("%", ""),
74 'start': start,
75 'page_len': page_len
76 })
Saurabh02875592013-07-08 18:45:55 +053077
Himanshud94a38e2020-05-18 14:26:26 +053078
Saurabh02875592013-07-08 18:45:55 +053079 # searches for customer
Chinmay D. Paiaa121092020-07-01 21:14:32 +053080@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +053081@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +053082def customer_query(doctype, txt, searchfield, start, page_len, filters):
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
Saurabh02875592013-07-08 18:45:55 +053086 if cust_master_name == "Customer Name":
87 fields = ["name", "customer_group", "territory"]
88 else:
89 fields = ["name", "customer_name", "customer_group", "territory"]
Rushabh Mehtab92087c2017-01-13 18:53:11 +053090
Himanshud94a38e2020-05-18 14:26:26 +053091 fields = get_fields("Customer", fields)
Saurabhf52dc072013-07-10 13:07:49 +053092
Himanshud94a38e2020-05-18 14:26:26 +053093 searchfields = frappe.get_meta("Customer").get_search_fields()
Ankush Menata9c84f72021-06-11 16:00:48 +053094 searchfields = " or ".join(field + " like %(txt)s" for field in searchfields)
Saurabh02875592013-07-08 18:45:55 +053095
Anand Doshi48d3b542014-07-09 13:15:03 +053096 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053097 where docstatus < 2
Console Admin86231662017-06-23 20:32:52 +030098 and ({scond}) and disabled=0
KanchanChauhan4b888b92017-07-25 14:03:01 +053099 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530100 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530101 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
102 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530103 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530104 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530105 limit %(start)s, %(page_len)s""".format(**{
Himanshud94a38e2020-05-18 14:26:26 +0530106 "fields": ", ".join(fields),
Console Admin86231662017-06-23 20:32:52 +0300107 "scond": searchfields,
KanchanChauhan4b888b92017-07-25 14:03:01 +0530108 "mcond": get_match_cond(doctype),
109 "fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Anand Doshi48d3b542014-07-09 13:15:03 +0530110 }), {
111 'txt': "%%%s%%" % txt,
112 '_txt': txt.replace("%", ""),
113 'start': start,
114 'page_len': page_len
115 })
Saurabh02875592013-07-08 18:45:55 +0530116
Himanshud94a38e2020-05-18 14:26:26 +0530117
Saurabh02875592013-07-08 18:45:55 +0530118# searches for supplier
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530119@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530120@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +0530121def supplier_query(doctype, txt, searchfield, start, page_len, filters):
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
Anand Doshibd67e872014-04-11 16:51:27 +0530124 if supp_master_name == "Supplier Name":
Zlash652e080982018-04-19 18:37:53 +0530125 fields = ["name", "supplier_group"]
Anand Doshibd67e872014-04-11 16:51:27 +0530126 else:
Zlash652e080982018-04-19 18:37:53 +0530127 fields = ["name", "supplier_name", "supplier_group"]
Himanshud94a38e2020-05-18 14:26:26 +0530128
129 fields = get_fields("Supplier", fields)
Saurabh02875592013-07-08 18:45:55 +0530130
Anand Doshi48d3b542014-07-09 13:15:03 +0530131 return frappe.db.sql("""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
135 and (on_hold = 0 or (on_hold = 1 and CURDATE() > release_date))
Anand Doshi48d3b542014-07-09 13:15:03 +0530136 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530137 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530138 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
139 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530140 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530141 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530142 limit %(start)s, %(page_len)s """.format(**{
Himanshud94a38e2020-05-18 14:26:26 +0530143 'field': ', '.join(fields),
Anand Doshi48d3b542014-07-09 13:15:03 +0530144 'key': searchfield,
145 'mcond':get_match_cond(doctype)
146 }), {
147 'txt': "%%%s%%" % txt,
148 '_txt': txt.replace("%", ""),
149 'start': start,
150 'page_len': page_len
151 })
Anand Doshibd67e872014-04-11 16:51:27 +0530152
Himanshud94a38e2020-05-18 14:26:26 +0530153
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530154@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530155@frappe.validate_and_sanitize_search_inputs
Nabin Hait9a380ef2013-07-16 17:24:17 +0530156def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Deepesh Gargfbf6e562020-03-31 10:45:32 +0530157 company_currency = erpnext.get_company_currency(filters.get('company'))
158
Suraj Shetty1923ef02020-08-05 19:42:25 +0530159 def get_accounts(with_account_type_filter):
160 account_type_condition = ''
161 if with_account_type_filter:
162 account_type_condition = "AND account_type in %(account_types)s"
163
164 accounts = frappe.db.sql("""
165 SELECT name, parent_account
166 FROM `tabAccount`
167 WHERE `tabAccount`.docstatus!=2
168 {account_type_condition}
169 AND is_group = 0
170 AND company = %(company)s
171 AND account_currency = %(currency)s
172 AND `{searchfield}` LIKE %(txt)s
prssannade7a2bc2020-09-21 13:57:04 +0530173 {mcond}
Suraj Shetty1923ef02020-08-05 19:42:25 +0530174 ORDER BY idx DESC, name
175 LIMIT %(offset)s, %(limit)s
prssannade7a2bc2020-09-21 13:57:04 +0530176 """.format(
177 account_type_condition=account_type_condition,
178 searchfield=searchfield,
179 mcond=get_match_cond(doctype)
180 ),
Suraj Shetty1923ef02020-08-05 19:42:25 +0530181 dict(
182 account_types=filters.get("account_type"),
183 company=filters.get("company"),
184 currency=company_currency,
185 txt="%{}%".format(txt),
186 offset=start,
187 limit=page_len
188 )
189 )
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):
Saurabh02875592013-07-08 18:45:55 +0530204 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530205
rohitwaghchaurec59371a2021-06-03 20:02:58 +0530206 if isinstance(filters, str):
207 filters = json.loads(filters)
208
marination3dbef9d2019-10-28 15:48:10 +0530209 #Get searchfields from meta and use in Item Link field query
marination1e754b12019-10-30 18:33:44 +0530210 meta = frappe.get_meta("Item", cached=True)
marination3dbef9d2019-10-28 15:48:10 +0530211 searchfields = meta.get_search_fields()
212
Ankush Menat34f52832021-11-09 11:55:33 +0530213 # these are handled separately
214 ignored_search_fields = ("item_name", "description")
215 for ignored_field in ignored_search_fields:
216 if ignored_field in searchfields:
217 searchfields.remove(ignored_field)
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530218
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530219 columns = ''
220 extra_searchfields = [field for field in searchfields
Ankush Menat34f52832021-11-09 11:55:33 +0530221 if not field in ["name", "item_group", "description", "item_name"]]
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530222
223 if extra_searchfields:
224 columns = ", " + ", ".join(extra_searchfields)
marination1e754b12019-10-30 18:33:44 +0530225
226 searchfields = searchfields + [field for field in[searchfield or "name", "item_code", "item_group", "item_name"]
227 if not field in searchfields]
marination3dbef9d2019-10-28 15:48:10 +0530228 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
229
DeeMysterioaa826242021-09-14 13:58:18 +0530230 if filters and isinstance(filters, dict):
231 if filters.get('customer') or filters.get('supplier'):
232 party = filters.get('customer') or filters.get('supplier')
233 item_rules_list = frappe.get_all('Party Specific Item',
234 filters = {'party': party}, fields = ['restrict_based_on', 'based_on_value'])
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530235
DeeMysterioaa826242021-09-14 13:58:18 +0530236 filters_dict = {}
237 for rule in item_rules_list:
238 if rule['restrict_based_on'] == 'Item':
239 rule['restrict_based_on'] = 'name'
240 filters_dict[rule.restrict_based_on] = []
noahjacobca2fb472021-05-12 16:25:07 +0530241
DeeMysterioaa826242021-09-14 13:58:18 +0530242 for rule in item_rules_list:
243 filters_dict[rule.restrict_based_on].append(rule.based_on_value)
noahjacobca2fb472021-05-12 16:25:07 +0530244
DeeMysterioaa826242021-09-14 13:58:18 +0530245 for filter in filters_dict:
246 filters[scrub(filter)] = ['in', filters_dict[filter]]
247
248 if filters.get('customer'):
249 del filters['customer']
250 else:
251 del filters['supplier']
252
Rohit Waghchaure721b4132021-06-02 14:13:09 +0530253
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530254 description_cond = ''
255 if frappe.db.count('Item', cache=True) < 50000:
256 # scan description only if items are less than 50000
257 description_cond = 'or tabItem.description LIKE %(txt)s'
Ankush Menat34f52832021-11-09 11:55:33 +0530258 return frappe.db.sql("""select
259 tabItem.name, tabItem.item_name, tabItem.item_group,
Saurabh02875592013-07-08 18:45:55 +0530260 if(length(tabItem.description) > 40, \
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530261 concat(substr(tabItem.description, 1, 40), "..."), description) as description
marination1e754b12019-10-30 18:33:44 +0530262 {columns}
Anand Doshibd67e872014-04-11 16:51:27 +0530263 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530264 where tabItem.docstatus < 2
Anand Doshi21e09a22015-10-29 12:21:41 +0530265 and tabItem.disabled=0
rohitwaghchaure79789072020-05-21 18:10:13 +0530266 and tabItem.has_variants=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530267 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 +0530268 and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s)
Rohit Waghchaure2bfb0632019-03-02 21:47:55 +0530269 {description_cond})
Anand Doshi22c0d782013-11-04 16:23:04 +0530270 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530271 order by
272 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
273 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530274 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530275 name, item_name
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530276 limit %(start)s, %(page_len)s """.format(
marination1e754b12019-10-30 18:33:44 +0530277 columns=columns,
marination3dbef9d2019-10-28 15:48:10 +0530278 scond=searchfields,
Nabin Haitc6285062016-03-30 13:10:25 +0530279 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530280 mcond=get_match_cond(doctype).replace('%', '%%'),
281 description_cond = description_cond),
Anand Doshi22c0d782013-11-04 16:23:04 +0530282 {
283 "today": nowdate(),
284 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530285 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530286 "start": start,
287 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530288 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530289
Himanshud94a38e2020-05-18 14:26:26 +0530290
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530291@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530292@frappe.validate_and_sanitize_search_inputs
Saurabh022ab632017-11-10 15:06:02 +0530293def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530294 conditions = []
Himanshud94a38e2020-05-18 14:26:26 +0530295 fields = get_fields("BOM", ["name", "item"])
Saurabhf52dc072013-07-10 13:07:49 +0530296
Himanshud94a38e2020-05-18 14:26:26 +0530297 return frappe.db.sql("""select {fields}
Anand Doshibd67e872014-04-11 16:51:27 +0530298 from tabBOM
299 where tabBOM.docstatus=1
300 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530301 and tabBOM.`{key}` like %(txt)s
302 {fcond} {mcond}
303 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530304 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
305 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530306 limit %(start)s, %(page_len)s """.format(
Himanshud94a38e2020-05-18 14:26:26 +0530307 fields=", ".join(fields),
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530308 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Karthikeyan S747c2622019-07-19 22:49:21 +0530309 mcond=get_match_cond(doctype).replace('%', '%%'),
310 key=searchfield),
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530311 {
Karthikeyan S747c2622019-07-19 22:49:21 +0530312 'txt': '%' + txt + '%',
Rushabh Mehta3574b372016-03-11 14:33:04 +0530313 '_txt': txt.replace("%", ""),
Saurabh022ab632017-11-10 15:06:02 +0530314 'start': start or 0,
315 'page_len': page_len or 20
Rushabh Mehta3574b372016-03-11 14:33:04 +0530316 })
Saurabh02875592013-07-08 18:45:55 +0530317
Himanshud94a38e2020-05-18 14:26:26 +0530318
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530319@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530320@frappe.validate_and_sanitize_search_inputs
Saurabh02875592013-07-08 18:45:55 +0530321def get_project_name(doctype, txt, searchfield, start, page_len, filters):
322 cond = ''
Ankush Menat62fc5442021-09-10 12:46:35 +0530323 if filters and filters.get('customer'):
Suraj Shetty6ea3de92018-09-26 18:15:53 +0530324 cond = """(`tabProject`.customer = %s or
rohitwaghchauree3304722018-08-27 11:43:57 +0530325 ifnull(`tabProject`.customer,"")="") and""" %(frappe.db.escape(filters.get("customer")))
Anand Doshibd67e872014-04-11 16:51:27 +0530326
Rucha Mahabal062d3012021-05-07 13:31:14 +0530327 fields = get_fields("Project", ["name", "project_name"])
328 searchfields = frappe.get_meta("Project").get_search_fields()
329 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
Himanshud94a38e2020-05-18 14:26:26 +0530330
331 return frappe.db.sql("""select {fields} from `tabProject`
Rucha Mahabal062d3012021-05-07 13:31:14 +0530332 where
333 `tabProject`.status not in ("Completed", "Cancelled")
Subin Tom889140f2021-06-22 16:26:19 +0530334 and {cond} {scond} {match_cond}
Rushabh Mehta3574b372016-03-11 14:33:04 +0530335 order by
336 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
337 idx desc,
338 `tabProject`.name asc
339 limit {start}, {page_len}""".format(
Himanshud94a38e2020-05-18 14:26:26 +0530340 fields=", ".join(['`tabProject`.{0}'.format(f) for f in fields]),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530341 cond=cond,
Rucha Mahabal062d3012021-05-07 13:31:14 +0530342 scond=searchfields,
Rushabh Mehta3574b372016-03-11 14:33:04 +0530343 match_cond=get_match_cond(doctype),
344 start=start,
345 page_len=page_len), {
346 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530347 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530348 })
Anand Doshibd67e872014-04-11 16:51:27 +0530349
tundebabzyf6d738b2017-09-18 12:40:09 +0100350
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530351@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530352@frappe.validate_and_sanitize_search_inputs
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530353def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
Himanshud94a38e2020-05-18 14:26:26 +0530354 fields = get_fields("Delivery Note", ["name", "customer", "posting_date"])
355
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530356 return frappe.db.sql("""
Himanshud94a38e2020-05-18 14:26:26 +0530357 select %(fields)s
Anand Doshibd67e872014-04-11 16:51:27 +0530358 from `tabDelivery Note`
359 where `tabDelivery Note`.`%(key)s` like %(txt)s and
tundebabzyf6d738b2017-09-18 12:40:09 +0100360 `tabDelivery Note`.docstatus = 1
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530361 and status not in ("Stopped", "Closed") %(fcond)s
tundebabzyf6d738b2017-09-18 12:40:09 +0100362 and (
363 (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
Deepesh Garge2dc1022021-04-14 11:21:11 +0530364 or (`tabDelivery Note`.grand_total = 0 and `tabDelivery Note`.per_billed < 100)
tundebabzyf6d738b2017-09-18 12:40:09 +0100365 or (
366 `tabDelivery Note`.is_return = 1
367 and return_against in (select name from `tabDelivery Note` where per_billed < 100)
368 )
369 )
rohitwaghchaured07a3e12019-05-15 07:46:28 +0530370 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(start)s, %(page_len)s
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530371 """ % {
Himanshud94a38e2020-05-18 14:26:26 +0530372 "fields": ", ".join(["`tabDelivery Note`.{0}".format(f) for f in fields]),
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530373 "key": searchfield,
374 "fcond": get_filters_cond(doctype, filters, []),
375 "mcond": get_match_cond(doctype),
rohitwaghchaured07a3e12019-05-15 07:46:28 +0530376 "start": start,
377 "page_len": page_len,
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530378 "txt": "%(txt)s"
tundebabzyf6d738b2017-09-18 12:40:09 +0100379 }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict)
380
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530381
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530382@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530383@frappe.validate_and_sanitize_search_inputs
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530384def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530385 cond = ""
386 if filters.get("posting_date"):
Nabin Hait7918b922018-01-31 15:30:03 +0530387 cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530388
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530389 batch_nos = None
390 args = {
391 'item_code': filters.get("item_code"),
392 'warehouse': filters.get("warehouse"),
393 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530394 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530395 "start": start,
396 "page_len": page_len
397 }
398
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530399 having_clause = "having sum(sle.actual_qty) > 0"
400 if filters.get("is_return"):
401 having_clause = ""
402
Deepesh Garga0d192e2020-09-22 13:54:07 +0530403 meta = frappe.get_meta("Batch", cached=True)
404 searchfields = meta.get_search_fields()
405
406 search_columns = ''
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530407 search_cond = ''
408
Deepesh Garga0d192e2020-09-22 13:54:07 +0530409 if searchfields:
410 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530411 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530412
Anand Doshi0dc79f42015-04-06 12:59:34 +0530413 if args.get('warehouse'):
Deepesh Garga0d192e2020-09-22 13:54:07 +0530414 searchfields = ['batch.' + field for field in searchfields]
415 if searchfields:
416 search_columns = ", " + ", ".join(searchfields)
Deepesh Garg1fae7742020-10-05 12:38:54 +0530417 search_cond = " or " + " or ".join([field + " like %(txt)s" for field in searchfields])
Deepesh Garga0d192e2020-09-22 13:54:07 +0530418
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530419 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom,
420 concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date)
Deepesh Garga0d192e2020-09-22 13:54:07 +0530421 {search_columns}
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530422 from `tabStock Ledger Entry` sle
423 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
424 where
425 batch.disabled = 0
Rohit Waghchaurec14aa452021-07-20 18:19:15 +0530426 and sle.is_cancelled = 0
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530427 and sle.item_code = %(item_code)s
428 and sle.warehouse = %(warehouse)s
429 and (sle.batch_no like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530430 or batch.expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530431 or batch.manufacturing_date like %(txt)s
432 {search_cond})
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530433 and batch.docstatus < 2
434 {cond}
435 {match_conditions}
436 group by batch_no {having_clause}
437 order by batch.expiry_date, sle.batch_no desc
438 limit %(start)s, %(page_len)s""".format(
Deepesh Garga0d192e2020-09-22 13:54:07 +0530439 search_columns = search_columns,
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530440 cond=cond,
441 match_conditions=get_match_cond(doctype),
Deepesh Garg1fae7742020-10-05 12:38:54 +0530442 having_clause = having_clause,
443 search_cond = search_cond
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530444 ), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530445
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530446 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530447 else:
Deepesh Garga0d192e2020-09-22 13:54:07 +0530448 return frappe.db.sql("""select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date)
449 {search_columns}
450 from `tabBatch` batch
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800451 where batch.disabled = 0
452 and item = %(item_code)s
sivankar621740e2018-02-12 14:33:40 +0530453 and (name like %(txt)s
Sun Howwrongbum088be372019-12-24 12:29:25 +0530454 or expiry_date like %(txt)s
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530455 or manufacturing_date like %(txt)s
456 {search_cond})
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530457 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530458 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530459 {match_conditions}
Deepesh Gargf58a5ec2020-10-05 13:55:53 +0530460
Anand Doshi0dc79f42015-04-06 12:59:34 +0530461 order by expiry_date, name desc
Deepesh Garg1fae7742020-10-05 12:38:54 +0530462 limit %(start)s, %(page_len)s""".format(cond, search_columns = search_columns,
463 search_cond = search_cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530464
Himanshud94a38e2020-05-18 14:26:26 +0530465
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530466@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530467@frappe.validate_and_sanitize_search_inputs
Nabin Haitea4aa042014-05-28 12:56:28 +0530468def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530469 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530470
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530471 if isinstance(filters, dict):
472 for key, val in filters.items():
473 if isinstance(val, (list, tuple)):
474 filter_list.append([doctype, key, val[0], val[1]])
475 else:
476 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530477 elif isinstance(filters, list):
478 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530479
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530480 if "is_group" not in [d[1] for d in filter_list]:
481 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530482
483 if searchfield and txt:
484 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
485
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530486 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530487 fields = ["name", "parent_account"],
488 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530489
Chinmay D. Paiaa121092020-07-01 21:14:32 +0530490@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530491@frappe.validate_and_sanitize_search_inputs
Marica299e2172020-04-28 13:00:04 +0530492def get_blanket_orders(doctype, txt, searchfield, start, page_len, filters):
493 return frappe.db.sql("""select distinct bo.name, bo.blanket_order_type, bo.to_date
494 from `tabBlanket Order` bo, `tabBlanket Order Item` boi
495 where
496 boi.parent = bo.name
497 and boi.item_code = {item_code}
498 and bo.blanket_order_type = '{blanket_order_type}'
499 and bo.company = {company}
500 and bo.docstatus = 1"""
501 .format(item_code = frappe.db.escape(filters.get("item")),
502 blanket_order_type = filters.get("blanket_order_type"),
503 company = frappe.db.escape(filters.get("company"))
504 ))
Nabin Haitafd14f62015-10-19 11:55:28 +0530505
Himanshud94a38e2020-05-18 14:26:26 +0530506
Nabin Haitafd14f62015-10-19 11:55:28 +0530507@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530508@frappe.validate_and_sanitize_search_inputs
Nabin Haitafd14f62015-10-19 11:55:28 +0530509def get_income_account(doctype, txt, searchfield, start, page_len, filters):
510 from erpnext.controllers.queries import get_match_cond
511
512 # income account can be any Credit account,
513 # but can also be a Asset account with account_type='Income Account' in special circumstances.
514 # Hence the first condition is an "OR"
515 if not filters: filters = {}
516
Anand Doshi21e09a22015-10-29 12:21:41 +0530517 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530518 if filters.get("company"):
519 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530520
Nabin Haitafd14f62015-10-19 11:55:28 +0530521 return frappe.db.sql("""select tabAccount.name from `tabAccount`
522 where (tabAccount.report_type = "Profit and Loss"
523 or tabAccount.account_type in ("Income Account", "Temporary"))
524 and tabAccount.is_group=0
525 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530526 {condition} {match_condition}
527 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530528 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Suraj Shetty4b404c42018-09-27 15:39:34 +0530529 'txt': '%' + txt + '%',
Nabin Haitafd14f62015-10-19 11:55:28 +0530530 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530531 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530532
Deepesh Garg96e874b2020-11-15 22:43:01 +0530533@frappe.whitelist()
534@frappe.validate_and_sanitize_search_inputs
535def get_filtered_dimensions(doctype, txt, searchfield, start, page_len, filters):
Chillar Anand915b3432021-09-02 16:44:59 +0530536 from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import (
537 get_dimension_filter_map,
538 )
Deepesh Garg96e874b2020-11-15 22:43:01 +0530539 dimension_filters = get_dimension_filter_map()
540 dimension_filters = dimension_filters.get((filters.get('dimension'),filters.get('account')))
Deepesh Garg6c17b842020-11-25 13:42:16 +0530541 query_filters = []
Deepesh Garg96e874b2020-11-15 22:43:01 +0530542
543 meta = frappe.get_meta(doctype)
Deepesh Garg96e874b2020-11-15 22:43:01 +0530544 if meta.is_tree:
Deepesh Garg6c17b842020-11-25 13:42:16 +0530545 query_filters.append(['is_group', '=', 0])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530546
Subin Tom333e44e2021-08-18 16:17:54 +0530547 if meta.has_field('disabled'):
548 query_filters.append(['disabled', '!=', 1])
549
Deepesh Garg96e874b2020-11-15 22:43:01 +0530550 if meta.has_field('company'):
Deepesh Garg6c17b842020-11-25 13:42:16 +0530551 query_filters.append(['company', '=', filters.get('company')])
552
553 if txt:
554 query_filters.append([searchfield, 'LIKE', "%%%s%%" % txt])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530555
556 if dimension_filters:
557 if dimension_filters['allow_or_restrict'] == 'Allow':
558 query_selector = 'in'
559 else:
560 query_selector = 'not in'
561
562 if len(dimension_filters['allowed_dimensions']) == 1:
563 dimensions = tuple(dimension_filters['allowed_dimensions'] * 2)
564 else:
565 dimensions = tuple(dimension_filters['allowed_dimensions'])
566
Deepesh Garg6c17b842020-11-25 13:42:16 +0530567 query_filters.append(['name', query_selector, dimensions])
Deepesh Garg96e874b2020-11-15 22:43:01 +0530568
Deepesh Gargaa9e78b2021-10-29 12:45:19 +0530569 output = frappe.get_list(doctype, filters=query_filters)
Deepesh Garg6c17b842020-11-25 13:42:16 +0530570 result = [d.name for d in output]
571
572 return [(d,) for d in set(result)]
Nabin Hait3a15c922016-03-04 12:30:46 +0530573
574@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530575@frappe.validate_and_sanitize_search_inputs
Nabin Hait3a15c922016-03-04 12:30:46 +0530576def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
577 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530578
Nabin Hait3a15c922016-03-04 12:30:46 +0530579 if not filters: filters = {}
580
581 condition = ""
582 if filters.get("company"):
583 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530584
Nabin Hait3a15c922016-03-04 12:30:46 +0530585 return frappe.db.sql("""select tabAccount.name from `tabAccount`
586 where (tabAccount.report_type = "Profit and Loss"
Mangesh-Khairnar5619db22019-08-21 14:49:24 +0530587 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 +0530588 and tabAccount.is_group=0
589 and tabAccount.docstatus!=2
590 and tabAccount.{key} LIKE %(txt)s
591 {condition} {match_condition}"""
Suraj Shettybfc195d2018-09-21 10:20:52 +0530592 .format(condition=condition, key=searchfield,
Nabin Hait3a15c922016-03-04 12:30:46 +0530593 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530594 'company': filters.get("company", ""),
Suraj Shetty4b404c42018-09-27 15:39:34 +0530595 'txt': '%' + txt + '%'
Maxwell Morais35572612016-07-21 23:42:59 -0300596 })
suyashphadtare049a88c2017-01-12 17:49:37 +0530597
598
599@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530600@frappe.validate_and_sanitize_search_inputs
suyashphadtare049a88c2017-01-12 17:49:37 +0530601def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
602 # Should be used when item code is passed in filters.
suyashphadtare750a0672017-01-18 15:35:01 +0530603 conditions, bin_conditions = [], []
604 filter_dict = get_doctype_wise_filters(filters)
605
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530606 query = """select `tabWarehouse`.name,
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530607 CONCAT_WS(" : ", "Actual Qty", ifnull(round(`tabBin`.actual_qty, 2), 0 )) actual_qty
608 from `tabWarehouse` left join `tabBin`
609 on `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions}
suyashphadtare34ab1362017-01-31 15:14:44 +0530610 where
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530611 `tabWarehouse`.`{key}` like {txt}
suyashphadtare34ab1362017-01-31 15:14:44 +0530612 {fcond} {mcond}
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530613 order by ifnull(`tabBin`.actual_qty, 0) desc
suyashphadtare34ab1362017-01-31 15:14:44 +0530614 limit
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530615 {start}, {page_len}
suyashphadtare34ab1362017-01-31 15:14:44 +0530616 """.format(
Diksha Jadhav182ee5e2020-08-18 00:35:04 +0530617 bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"),bin_conditions, ignore_permissions=True),
Suraj Shettybfc195d2018-09-21 10:20:52 +0530618 key=searchfield,
suyashphadtare34ab1362017-01-31 15:14:44 +0530619 fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530620 mcond=get_match_cond(doctype),
621 start=start,
622 page_len=page_len,
623 txt=frappe.db.escape('%{0}%'.format(txt))
624 )
625
626 return frappe.db.sql(query)
suyashphadtare750a0672017-01-18 15:35:01 +0530627
628
629def get_doctype_wise_filters(filters):
630 # Helper function to seperate filters doctype_wise
631 filter_dict = defaultdict(list)
632 for row in filters:
633 filter_dict[row[0]].append(row)
634 return filter_dict
tundebabzy2a4fefc2017-11-29 06:23:09 +0100635
636
637@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530638@frappe.validate_and_sanitize_search_inputs
tundebabzy2a4fefc2017-11-29 06:23:09 +0100639def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530640 query = """select batch_id from `tabBatch`
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800641 where disabled = 0
642 and (expiry_date >= CURDATE() or expiry_date IS NULL)
Suraj Shettybfc195d2018-09-21 10:20:52 +0530643 and name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100644
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530645 if filters and filters.get('item'):
Suraj Shettybfc195d2018-09-21 10:20:52 +0530646 query += " and item = {item}".format(item = frappe.db.escape(filters.get('item')))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100647
Sachin Mane64f48db2018-01-08 17:57:32 +0530648 return frappe.db.sql(query, filters)
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530649
Himanshud94a38e2020-05-18 14:26:26 +0530650
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530651@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530652@frappe.validate_and_sanitize_search_inputs
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530653def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters):
Maricabac4b932019-09-16 19:44:28 +0530654 item_filters = [
655 ['manufacturer', 'like', '%' + txt + '%'],
656 ['item_code', '=', filters.get("item_code")]
657 ]
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530658
Maricabac4b932019-09-16 19:44:28 +0530659 item_manufacturers = frappe.get_all(
660 "Item Manufacturer",
661 fields=["manufacturer", "manufacturer_part_no"],
662 filters=item_filters,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530663 limit_start=start,
664 limit_page_length=page_len,
665 as_list=1
666 )
Maricabac4b932019-09-16 19:44:28 +0530667 return item_manufacturers
Saqibd9956092019-11-18 11:46:55 +0530668
Himanshud94a38e2020-05-18 14:26:26 +0530669
Saqibd9956092019-11-18 11:46:55 +0530670@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530671@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530672def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters):
673 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530674 select pr.name
Saqibd9956092019-11-18 11:46:55 +0530675 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem
676 where pr.docstatus = 1 and pritem.parent = pr.name
677 and pr.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
678
679 if filters and filters.get('item_code'):
680 query += " and pritem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code')))
681
682 return frappe.db.sql(query, filters)
683
Himanshud94a38e2020-05-18 14:26:26 +0530684
Saqibd9956092019-11-18 11:46:55 +0530685@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530686@frappe.validate_and_sanitize_search_inputs
Saqibd9956092019-11-18 11:46:55 +0530687def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters):
688 query = """
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530689 select pi.name
Saqibd9956092019-11-18 11:46:55 +0530690 from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem
691 where pi.docstatus = 1 and piitem.parent = pi.name
692 and pi.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
693
694 if filters and filters.get('item_code'):
695 query += " and piitem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code')))
696
697 return frappe.db.sql(query, filters)
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530698
Himanshud94a38e2020-05-18 14:26:26 +0530699
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530700@frappe.whitelist()
Suraj Shetty1923ef02020-08-05 19:42:25 +0530701@frappe.validate_and_sanitize_search_inputs
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530702def get_tax_template(doctype, txt, searchfield, start, page_len, filters):
703
704 item_doc = frappe.get_cached_doc('Item', filters.get('item_code'))
705 item_group = filters.get('item_group')
706 taxes = item_doc.taxes or []
707
708 while item_group:
709 item_group_doc = frappe.get_cached_doc('Item Group', item_group)
710 taxes += item_group_doc.taxes or []
711 item_group = item_group_doc.parent_item_group
712
713 if not taxes:
714 return frappe.db.sql(""" SELECT name FROM `tabItem Tax Template` """)
715 else:
Marica0fcb05a2020-08-10 14:48:13 +0530716 valid_from = filters.get('valid_from')
717 valid_from = valid_from[1] if isinstance(valid_from, list) else valid_from
718
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530719 args = {
720 'item_code': filters.get('item_code'),
Marica0fcb05a2020-08-10 14:48:13 +0530721 'posting_date': valid_from,
mohammadahmad199041c0c9f2020-06-18 11:18:44 +0500722 'tax_category': filters.get('tax_category'),
723 'company': filters.get('company')
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530724 }
725
726 taxes = _get_item_tax_template(args, taxes, for_validate=True)
727 return [(d,) for d in set(taxes)]
Himanshud94a38e2020-05-18 14:26:26 +0530728
729
Ankush Menat7eac4a22021-04-19 10:33:39 +0530730def get_fields(doctype, fields=None):
731 if fields is None:
732 fields = []
Himanshud94a38e2020-05-18 14:26:26 +0530733 meta = frappe.get_meta(doctype)
734 fields.extend(meta.get_search_fields())
735
736 if meta.title_field and not meta.title_field.strip() in fields:
737 fields.insert(1, meta.title_field.strip())
738
739 return unique(fields)