blob: 6da496ba174ce62c91a78d9195382042b606e551 [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:
Nabin Hait8693b5c2016-09-04 10:31:41 +053020 value = frappe.db.escape(f[1]) if isinstance(f[1], basestring) else f[1]
21 flt.append([doctype, f[0], '=', value])
Anand Doshibd67e872014-04-11 16:51:27 +053022
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053023 query = DatabaseQuery(doctype)
24 query.filters = flt
25 query.conditions = conditions
Nabin Hait23649c62014-05-07 19:18:15 +053026 query.build_filter_conditions(flt, conditions)
Anand Doshibd67e872014-04-11 16:51:27 +053027
28 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053029 else:
30 cond = ''
31 return cond
32
Saurabh02875592013-07-08 18:45:55 +053033 # searches for active employees
34def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +053035 return frappe.db.sql("""select name, employee_name from `tabEmployee`
36 where status = 'Active'
37 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053038 and ({key} like %(txt)s
39 or employee_name like %(txt)s)
40 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053041 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053042 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
43 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053044 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053045 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053046 limit %(start)s, %(page_len)s""".format(**{
47 'key': searchfield,
48 'mcond': get_match_cond(doctype)
49 }), {
50 'txt': "%%%s%%" % txt,
51 '_txt': txt.replace("%", ""),
52 'start': start,
53 'page_len': page_len
54 })
Saurabh02875592013-07-08 18:45:55 +053055
56 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053057def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053058 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053059 where docstatus < 2
60 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053061 and ({key} like %(txt)s
62 or lead_name like %(txt)s
63 or company_name like %(txt)s)
64 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053065 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053066 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
67 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
68 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053069 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053070 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053071 limit %(start)s, %(page_len)s""".format(**{
72 'key': searchfield,
73 'mcond':get_match_cond(doctype)
74 }), {
75 'txt': "%%%s%%" % txt,
76 '_txt': txt.replace("%", ""),
77 'start': start,
78 'page_len': page_len
79 })
Saurabh02875592013-07-08 18:45:55 +053080
81 # searches for customer
82def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053083 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053084
Saurabh02875592013-07-08 18:45:55 +053085 if cust_master_name == "Customer Name":
86 fields = ["name", "customer_group", "territory"]
87 else:
88 fields = ["name", "customer_name", "customer_group", "territory"]
Maxwell Morais35572612016-07-21 23:42:59 -030089
90 meta = frappe.get_meta("Customer")
91 fields = fields + [f for f in meta.get_search_fields() if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053092
Anand Doshibd67e872014-04-11 16:51:27 +053093 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053094
Anand Doshi48d3b542014-07-09 13:15:03 +053095 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053096 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053097 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +053098 or customer_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +053099 {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(**{
106 "fields": fields,
107 "key": searchfield,
108 "mcond": get_match_cond(doctype)
109 }), {
110 'txt': "%%%s%%" % txt,
111 '_txt': txt.replace("%", ""),
112 'start': start,
113 'page_len': page_len
114 })
Saurabh02875592013-07-08 18:45:55 +0530115
116# searches for supplier
117def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530118 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530119 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530120 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530121 else:
Saurabh02875592013-07-08 18:45:55 +0530122 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530123 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530124
Anand Doshi48d3b542014-07-09 13:15:03 +0530125 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530126 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530127 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530128 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530129 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530130 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530131 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
132 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530133 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530134 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530135 limit %(start)s, %(page_len)s """.format(**{
136 'field': fields,
137 'key': searchfield,
138 'mcond':get_match_cond(doctype)
139 }), {
140 'txt': "%%%s%%" % txt,
141 '_txt': txt.replace("%", ""),
142 'start': start,
143 'page_len': page_len
144 })
Anand Doshibd67e872014-04-11 16:51:27 +0530145
Nabin Hait9a380ef2013-07-16 17:24:17 +0530146def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530147 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
148 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530149 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530150 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530151 and company = %s
152 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530153 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530154 limit %s, %s""" %
155 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
156 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530157 start, page_len]))
158 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530159 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530160 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530161 and company = %s and `%s` LIKE %s limit %s, %s"""
162 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530163 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530164
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530165 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530166
Rushabh Mehta203cc962016-04-07 15:25:43 +0530167def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530168 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530169
Rushabh Mehta203cc962016-04-07 15:25:43 +0530170 return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
Anand Doshibd67e872014-04-11 16:51:27 +0530171 if(length(tabItem.item_name) > 40,
172 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530173 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530174 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530175 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530176 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530177 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530178 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530179 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 +0530180 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800181 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530182 or tabItem.item_name LIKE %(txt)s
183 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530184 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530185 order by
186 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
187 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530188 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530189 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530190 limit %(start)s, %(page_len)s """.format(key=searchfield,
Nabin Haitc6285062016-03-30 13:10:25 +0530191 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
192 mcond=get_match_cond(doctype).replace('%', '%%')),
Anand Doshi22c0d782013-11-04 16:23:04 +0530193 {
194 "today": nowdate(),
195 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530196 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530197 "start": start,
198 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530199 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530200
201def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530202 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530203
Anand Doshibd67e872014-04-11 16:51:27 +0530204 return frappe.db.sql("""select tabBOM.name, tabBOM.item
205 from tabBOM
206 where tabBOM.docstatus=1
207 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530208 and tabBOM.`{key}` like %(txt)s
209 {fcond} {mcond}
210 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530211 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
212 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530213 limit %(start)s, %(page_len)s """.format(
214 fcond=get_filters_cond(doctype, filters, conditions),
215 mcond=get_match_cond(doctype),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530216 key=frappe.db.escape(searchfield)),
Nabin Hait62211172016-03-16 16:22:03 +0530217 {
Rushabh Mehta3574b372016-03-11 14:33:04 +0530218 'txt': "%%%s%%" % frappe.db.escape(txt),
219 '_txt': txt.replace("%", ""),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530220 'start': start,
Nabin Hait62211172016-03-16 16:22:03 +0530221 'page_len': page_len
Rushabh Mehta3574b372016-03-11 14:33:04 +0530222 })
Saurabh02875592013-07-08 18:45:55 +0530223
Saurabh02875592013-07-08 18:45:55 +0530224def get_project_name(doctype, txt, searchfield, start, page_len, filters):
225 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530226 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530227 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530228
229 return frappe.db.sql("""select `tabProject`.name from `tabProject`
230 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530231 and {cond} `tabProject`.name like %(txt)s {match_cond}
232 order by
233 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
234 idx desc,
235 `tabProject`.name asc
236 limit {start}, {page_len}""".format(
237 cond=cond,
238 match_cond=get_match_cond(doctype),
239 start=start,
240 page_len=page_len), {
241 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530242 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530243 })
Anand Doshibd67e872014-04-11 16:51:27 +0530244
Anand Doshi17350b82013-08-01 15:45:23 +0530245def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530246 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530247 from `tabDelivery Note`
248 where `tabDelivery Note`.`%(key)s` like %(txt)s and
shreyas29b565f2016-01-25 17:30:49 +0530249 `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530250 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530251 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
252 limit %(start)s, %(page_len)s""" % {
253 "key": searchfield,
254 "fcond": get_filters_cond(doctype, filters, []),
255 "mcond": get_match_cond(doctype),
256 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530257 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
258
259def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530260 cond = ""
261 if filters.get("posting_date"):
262 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530263
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530264 batch_nos = None
265 args = {
266 'item_code': filters.get("item_code"),
267 'warehouse': filters.get("warehouse"),
268 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530269 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530270 "start": start,
271 "page_len": page_len
272 }
273
Anand Doshi0dc79f42015-04-06 12:59:34 +0530274 if args.get('warehouse'):
275 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
276 from `tabStock Ledger Entry` sle
277 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
278 where
279 sle.item_code = %(item_code)s
280 and sle.warehouse = %(warehouse)s
281 and sle.batch_no like %(txt)s
282 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530283 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530284 {match_conditions}
285 group by batch_no having sum(sle.actual_qty) > 0
286 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530287 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530288
289 if batch_nos:
290 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530291 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530292 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530293 where item = %(item_code)s
294 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530295 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530296 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530297 {match_conditions}
298 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530299 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530300
301def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530302 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530303
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530304 if isinstance(filters, dict):
305 for key, val in filters.items():
306 if isinstance(val, (list, tuple)):
307 filter_list.append([doctype, key, val[0], val[1]])
308 else:
309 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530310 elif isinstance(filters, list):
311 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530312
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530313 if "is_group" not in [d[1] for d in filter_list]:
314 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530315
316 if searchfield and txt:
317 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
318
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530319 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530320 fields = ["name", "parent_account"],
321 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530322
Nabin Haitafd14f62015-10-19 11:55:28 +0530323
324@frappe.whitelist()
325def get_income_account(doctype, txt, searchfield, start, page_len, filters):
326 from erpnext.controllers.queries import get_match_cond
327
328 # income account can be any Credit account,
329 # but can also be a Asset account with account_type='Income Account' in special circumstances.
330 # Hence the first condition is an "OR"
331 if not filters: filters = {}
332
Anand Doshi21e09a22015-10-29 12:21:41 +0530333 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530334 if filters.get("company"):
335 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530336
Nabin Haitafd14f62015-10-19 11:55:28 +0530337 return frappe.db.sql("""select tabAccount.name from `tabAccount`
338 where (tabAccount.report_type = "Profit and Loss"
339 or tabAccount.account_type in ("Income Account", "Temporary"))
340 and tabAccount.is_group=0
341 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530342 {condition} {match_condition}
343 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530344 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530345 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530346 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530347 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530348
349
350@frappe.whitelist()
351def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
352 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530353
Nabin Hait3a15c922016-03-04 12:30:46 +0530354 if not filters: filters = {}
355
356 condition = ""
357 if filters.get("company"):
358 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530359
Nabin Hait3a15c922016-03-04 12:30:46 +0530360 return frappe.db.sql("""select tabAccount.name from `tabAccount`
361 where (tabAccount.report_type = "Profit and Loss"
362 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
363 and tabAccount.is_group=0
364 and tabAccount.docstatus!=2
365 and tabAccount.{key} LIKE %(txt)s
366 {condition} {match_condition}"""
Rushabh Mehta203cc962016-04-07 15:25:43 +0530367 .format(condition=condition, key=frappe.db.escape(searchfield),
Nabin Hait3a15c922016-03-04 12:30:46 +0530368 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530369 'company': filters.get("company", ""),
Nabin Hait3a15c922016-03-04 12:30:46 +0530370 'txt': "%%%s%%" % frappe.db.escape(txt)
Maxwell Morais35572612016-07-21 23:42:59 -0300371 })