blob: 0debe4aced69ba059921381fc968c9b30504bf9a [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
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Rushabh Mehtac0bb4532014-09-09 16:15:35 +05306from frappe.desk.reportview import get_match_cond
Rushabh Mehta2e7ad892014-03-06 12:28:47 +05307from frappe.model.db_query import DatabaseQuery
Nabin Hait2ed71ba2015-03-20 15:06:30 +05308from frappe.utils import nowdate
Saurabh02875592013-07-08 18:45:55 +05309
10def get_filters_cond(doctype, filters, conditions):
11 if filters:
Anand Doshi1fef2fa2015-09-14 18:27:21 +053012 flt = filters
Saurabh02875592013-07-08 18:45:55 +053013 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053014 filters = filters.items()
15 flt = []
16 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053017 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053018 flt.append([doctype, f[0], '!=', f[1][1:]])
19 else:
20 flt.append([doctype, f[0], '=', f[1]])
Anand Doshibd67e872014-04-11 16:51:27 +053021
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053022 query = DatabaseQuery(doctype)
23 query.filters = flt
24 query.conditions = conditions
Nabin Hait23649c62014-05-07 19:18:15 +053025 query.build_filter_conditions(flt, conditions)
Anand Doshibd67e872014-04-11 16:51:27 +053026
27 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053028 else:
29 cond = ''
30 return cond
31
Saurabh02875592013-07-08 18:45:55 +053032 # searches for active employees
33def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +053034 return frappe.db.sql("""select name, employee_name from `tabEmployee`
35 where status = 'Active'
36 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053037 and ({key} like %(txt)s
38 or employee_name like %(txt)s)
39 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053040 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053041 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
42 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053043 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053044 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053045 limit %(start)s, %(page_len)s""".format(**{
46 'key': searchfield,
47 'mcond': get_match_cond(doctype)
48 }), {
49 'txt': "%%%s%%" % txt,
50 '_txt': txt.replace("%", ""),
51 'start': start,
52 'page_len': page_len
53 })
Saurabh02875592013-07-08 18:45:55 +053054
55 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053056def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053057 return frappe.db.sql("""select name, lead_name, company_name 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
Anand Doshi48d3b542014-07-09 13:15:03 +053065 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
66 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
67 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053068 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053069 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053070 limit %(start)s, %(page_len)s""".format(**{
71 'key': searchfield,
72 'mcond':get_match_cond(doctype)
73 }), {
74 'txt': "%%%s%%" % txt,
75 '_txt': txt.replace("%", ""),
76 'start': start,
77 'page_len': page_len
78 })
Saurabh02875592013-07-08 18:45:55 +053079
80 # searches for customer
81def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053082 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053083
Saurabh02875592013-07-08 18:45:55 +053084 if cust_master_name == "Customer Name":
85 fields = ["name", "customer_group", "territory"]
86 else:
87 fields = ["name", "customer_name", "customer_group", "territory"]
Maxwell Morais35572612016-07-21 23:42:59 -030088
89 meta = frappe.get_meta("Customer")
90 fields = fields + [f for f in meta.get_search_fields() if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053091
Anand Doshibd67e872014-04-11 16:51:27 +053092 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053093
Anand Doshi48d3b542014-07-09 13:15:03 +053094 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053095 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053096 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +053097 or customer_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +053098 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053099 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530100 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
101 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530102 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530103 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530104 limit %(start)s, %(page_len)s""".format(**{
105 "fields": fields,
106 "key": searchfield,
107 "mcond": get_match_cond(doctype)
108 }), {
109 'txt': "%%%s%%" % txt,
110 '_txt': txt.replace("%", ""),
111 'start': start,
112 'page_len': page_len
113 })
Saurabh02875592013-07-08 18:45:55 +0530114
115# searches for supplier
116def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530117 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530118 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530119 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530120 else:
Saurabh02875592013-07-08 18:45:55 +0530121 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530122 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530123
Anand Doshi48d3b542014-07-09 13:15:03 +0530124 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530125 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530126 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530127 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530128 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530129 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530130 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
131 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530132 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530133 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530134 limit %(start)s, %(page_len)s """.format(**{
135 'field': fields,
136 'key': searchfield,
137 'mcond':get_match_cond(doctype)
138 }), {
139 'txt': "%%%s%%" % txt,
140 '_txt': txt.replace("%", ""),
141 'start': start,
142 'page_len': page_len
143 })
Anand Doshibd67e872014-04-11 16:51:27 +0530144
Nabin Hait9a380ef2013-07-16 17:24:17 +0530145def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530146 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
147 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530148 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530149 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530150 and company = %s
151 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530152 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530153 limit %s, %s""" %
154 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
155 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530156 start, page_len]))
157 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530158 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530159 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530160 and company = %s and `%s` LIKE %s limit %s, %s"""
161 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530162 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530163
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530164 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530165
Rushabh Mehta203cc962016-04-07 15:25:43 +0530166def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530167 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530168
Rushabh Mehta203cc962016-04-07 15:25:43 +0530169 return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
Anand Doshibd67e872014-04-11 16:51:27 +0530170 if(length(tabItem.item_name) > 40,
171 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530172 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530173 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530174 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530175 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530176 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530177 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530178 and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
Anand Doshi22c0d782013-11-04 16:23:04 +0530179 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800180 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530181 or tabItem.item_name LIKE %(txt)s
182 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530183 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530184 order by
185 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
186 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530187 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530188 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530189 limit %(start)s, %(page_len)s """.format(key=searchfield,
Nabin Haitc6285062016-03-30 13:10:25 +0530190 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
191 mcond=get_match_cond(doctype).replace('%', '%%')),
Anand Doshi22c0d782013-11-04 16:23:04 +0530192 {
193 "today": nowdate(),
194 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530195 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530196 "start": start,
197 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530198 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530199
200def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530201 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530202
Anand Doshibd67e872014-04-11 16:51:27 +0530203 return frappe.db.sql("""select tabBOM.name, tabBOM.item
204 from tabBOM
205 where tabBOM.docstatus=1
206 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530207 and tabBOM.`{key}` like %(txt)s
208 {fcond} {mcond}
209 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530210 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
211 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530212 limit %(start)s, %(page_len)s """.format(
213 fcond=get_filters_cond(doctype, filters, conditions),
214 mcond=get_match_cond(doctype),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530215 key=frappe.db.escape(searchfield)),
Nabin Hait62211172016-03-16 16:22:03 +0530216 {
Rushabh Mehta3574b372016-03-11 14:33:04 +0530217 'txt': "%%%s%%" % frappe.db.escape(txt),
218 '_txt': txt.replace("%", ""),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530219 'start': start,
Nabin Hait62211172016-03-16 16:22:03 +0530220 'page_len': page_len
Rushabh Mehta3574b372016-03-11 14:33:04 +0530221 })
Saurabh02875592013-07-08 18:45:55 +0530222
Saurabh02875592013-07-08 18:45:55 +0530223def get_project_name(doctype, txt, searchfield, start, page_len, filters):
224 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530225 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530226 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530227
228 return frappe.db.sql("""select `tabProject`.name from `tabProject`
229 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530230 and {cond} `tabProject`.name like %(txt)s {match_cond}
231 order by
232 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
233 idx desc,
234 `tabProject`.name asc
235 limit {start}, {page_len}""".format(
236 cond=cond,
237 match_cond=get_match_cond(doctype),
238 start=start,
239 page_len=page_len), {
240 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530241 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530242 })
Anand Doshibd67e872014-04-11 16:51:27 +0530243
Anand Doshi17350b82013-08-01 15:45:23 +0530244def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530245 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530246 from `tabDelivery Note`
247 where `tabDelivery Note`.`%(key)s` like %(txt)s and
shreyas29b565f2016-01-25 17:30:49 +0530248 `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530249 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530250 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
251 limit %(start)s, %(page_len)s""" % {
252 "key": searchfield,
253 "fcond": get_filters_cond(doctype, filters, []),
254 "mcond": get_match_cond(doctype),
255 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530256 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
257
258def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530259 cond = ""
260 if filters.get("posting_date"):
261 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530262
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530263 batch_nos = None
264 args = {
265 'item_code': filters.get("item_code"),
266 'warehouse': filters.get("warehouse"),
267 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530268 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530269 "start": start,
270 "page_len": page_len
271 }
272
Anand Doshi0dc79f42015-04-06 12:59:34 +0530273 if args.get('warehouse'):
274 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
275 from `tabStock Ledger Entry` sle
276 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
277 where
278 sle.item_code = %(item_code)s
279 and sle.warehouse = %(warehouse)s
280 and sle.batch_no like %(txt)s
281 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530282 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530283 {match_conditions}
284 group by batch_no having sum(sle.actual_qty) > 0
285 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530286 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530287
288 if batch_nos:
289 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530290 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530291 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530292 where item = %(item_code)s
293 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530294 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530295 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530296 {match_conditions}
297 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530298 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530299
300def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530301 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530302
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530303 if isinstance(filters, dict):
304 for key, val in filters.items():
305 if isinstance(val, (list, tuple)):
306 filter_list.append([doctype, key, val[0], val[1]])
307 else:
308 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530309 elif isinstance(filters, list):
310 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530311
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530312 if "is_group" not in [d[1] for d in filter_list]:
313 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530314
315 if searchfield and txt:
316 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
317
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530318 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530319 fields = ["name", "parent_account"],
320 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530321
Nabin Haitafd14f62015-10-19 11:55:28 +0530322
323@frappe.whitelist()
324def get_income_account(doctype, txt, searchfield, start, page_len, filters):
325 from erpnext.controllers.queries import get_match_cond
326
327 # income account can be any Credit account,
328 # but can also be a Asset account with account_type='Income Account' in special circumstances.
329 # Hence the first condition is an "OR"
330 if not filters: filters = {}
331
Anand Doshi21e09a22015-10-29 12:21:41 +0530332 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530333 if filters.get("company"):
334 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530335
Nabin Haitafd14f62015-10-19 11:55:28 +0530336 return frappe.db.sql("""select tabAccount.name from `tabAccount`
337 where (tabAccount.report_type = "Profit and Loss"
338 or tabAccount.account_type in ("Income Account", "Temporary"))
339 and tabAccount.is_group=0
340 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530341 {condition} {match_condition}
342 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530343 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530344 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530345 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530346 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530347
348
349@frappe.whitelist()
350def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
351 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530352
Nabin Hait3a15c922016-03-04 12:30:46 +0530353 if not filters: filters = {}
354
355 condition = ""
356 if filters.get("company"):
357 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530358
Nabin Hait3a15c922016-03-04 12:30:46 +0530359 return frappe.db.sql("""select tabAccount.name from `tabAccount`
360 where (tabAccount.report_type = "Profit and Loss"
361 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
362 and tabAccount.is_group=0
363 and tabAccount.docstatus!=2
364 and tabAccount.{key} LIKE %(txt)s
365 {condition} {match_condition}"""
Rushabh Mehta203cc962016-04-07 15:25:43 +0530366 .format(condition=condition, key=frappe.db.escape(searchfield),
Nabin Hait3a15c922016-03-04 12:30:46 +0530367 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530368 'company': filters.get("company", ""),
Nabin Hait3a15c922016-03-04 12:30:46 +0530369 'txt': "%%%s%%" % frappe.db.escape(txt)
Maxwell Morais35572612016-07-21 23:42:59 -0300370 })