blob: cc3f27724e290183dd20921e5e804bf418dda13f [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 Mehtab92087c2017-01-13 18:53:11 +05306from frappe.desk.reportview import get_match_cond, get_filters_cond
Nabin Hait2ed71ba2015-03-20 15:06:30 +05307from frappe.utils import nowdate
Saurabh02875592013-07-08 18:45:55 +05308
Saurabh02875592013-07-08 18:45:55 +05309
Saurabh02875592013-07-08 18:45:55 +053010 # searches for active employees
11def employee_query(doctype, txt, searchfield, start, page_len, filters):
Kanchan Chauhan7652b852016-11-16 15:29:01 +053012 conditions = []
Anand Doshibd67e872014-04-11 16:51:27 +053013 return frappe.db.sql("""select name, employee_name from `tabEmployee`
14 where status = 'Active'
15 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053016 and ({key} like %(txt)s
17 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053018 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053019 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053020 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
21 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053022 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053023 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053024 limit %(start)s, %(page_len)s""".format(**{
25 'key': searchfield,
Kanchan Chauhan7652b852016-11-16 15:29:01 +053026 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi48d3b542014-07-09 13:15:03 +053027 'mcond': get_match_cond(doctype)
28 }), {
29 'txt': "%%%s%%" % txt,
30 '_txt': txt.replace("%", ""),
31 'start': start,
32 'page_len': page_len
33 })
Saurabh02875592013-07-08 18:45:55 +053034
35 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053036def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053037 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053038 where docstatus < 2
39 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053040 and ({key} like %(txt)s
41 or lead_name like %(txt)s
42 or company_name like %(txt)s)
43 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053044 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053045 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
46 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
47 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053048 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053049 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053050 limit %(start)s, %(page_len)s""".format(**{
51 'key': searchfield,
52 'mcond':get_match_cond(doctype)
53 }), {
54 'txt': "%%%s%%" % txt,
55 '_txt': txt.replace("%", ""),
56 'start': start,
57 'page_len': page_len
58 })
Saurabh02875592013-07-08 18:45:55 +053059
60 # searches for customer
61def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053062 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053063
Saurabh02875592013-07-08 18:45:55 +053064 if cust_master_name == "Customer Name":
65 fields = ["name", "customer_group", "territory"]
66 else:
67 fields = ["name", "customer_name", "customer_group", "territory"]
Rushabh Mehtab92087c2017-01-13 18:53:11 +053068
Maxwell Morais35572612016-07-21 23:42:59 -030069 meta = frappe.get_meta("Customer")
70 fields = fields + [f for f in meta.get_search_fields() if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053071
Anand Doshibd67e872014-04-11 16:51:27 +053072 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053073
Anand Doshi48d3b542014-07-09 13:15:03 +053074 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053075 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053076 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +053077 or customer_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +053078 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053079 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053080 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
81 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053082 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +053083 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053084 limit %(start)s, %(page_len)s""".format(**{
85 "fields": fields,
86 "key": searchfield,
87 "mcond": get_match_cond(doctype)
88 }), {
89 'txt': "%%%s%%" % txt,
90 '_txt': txt.replace("%", ""),
91 'start': start,
92 'page_len': page_len
93 })
Saurabh02875592013-07-08 18:45:55 +053094
95# searches for supplier
96def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053097 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +053098 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +053099 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530100 else:
Saurabh02875592013-07-08 18:45:55 +0530101 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530102 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530103
Anand Doshi48d3b542014-07-09 13:15:03 +0530104 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530105 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530106 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530107 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530108 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530109 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530110 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
111 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530112 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530113 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530114 limit %(start)s, %(page_len)s """.format(**{
115 'field': fields,
116 'key': searchfield,
117 'mcond':get_match_cond(doctype)
118 }), {
119 'txt': "%%%s%%" % txt,
120 '_txt': txt.replace("%", ""),
121 'start': start,
122 'page_len': page_len
123 })
Anand Doshibd67e872014-04-11 16:51:27 +0530124
Nabin Hait9a380ef2013-07-16 17:24:17 +0530125def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530126 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
127 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530128 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530129 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530130 and company = %s
131 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530132 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530133 limit %s, %s""" %
134 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
135 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530136 start, page_len]))
137 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530138 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530139 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530140 and company = %s and `%s` LIKE %s limit %s, %s"""
141 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530142 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530143
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530144 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530145
Rushabh Mehta203cc962016-04-07 15:25:43 +0530146def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530147 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530148
Rushabh Mehta203cc962016-04-07 15:25:43 +0530149 return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
Anand Doshibd67e872014-04-11 16:51:27 +0530150 if(length(tabItem.item_name) > 40,
151 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530152 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530153 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530154 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530155 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530156 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530157 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530158 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 +0530159 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800160 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530161 or tabItem.item_name LIKE %(txt)s
162 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530163 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530164 order by
165 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
166 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530167 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530168 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530169 limit %(start)s, %(page_len)s """.format(key=searchfield,
Nabin Haitc6285062016-03-30 13:10:25 +0530170 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
171 mcond=get_match_cond(doctype).replace('%', '%%')),
Anand Doshi22c0d782013-11-04 16:23:04 +0530172 {
173 "today": nowdate(),
174 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530175 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530176 "start": start,
177 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530178 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530179
180def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530181 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530182
Anand Doshibd67e872014-04-11 16:51:27 +0530183 return frappe.db.sql("""select tabBOM.name, tabBOM.item
184 from tabBOM
185 where tabBOM.docstatus=1
186 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530187 and tabBOM.`{key}` like %(txt)s
188 {fcond} {mcond}
189 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530190 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
191 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530192 limit %(start)s, %(page_len)s """.format(
193 fcond=get_filters_cond(doctype, filters, conditions),
194 mcond=get_match_cond(doctype),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530195 key=frappe.db.escape(searchfield)),
Nabin Hait62211172016-03-16 16:22:03 +0530196 {
Rushabh Mehta3574b372016-03-11 14:33:04 +0530197 'txt': "%%%s%%" % frappe.db.escape(txt),
198 '_txt': txt.replace("%", ""),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530199 'start': start,
Nabin Hait62211172016-03-16 16:22:03 +0530200 'page_len': page_len
Rushabh Mehta3574b372016-03-11 14:33:04 +0530201 })
Saurabh02875592013-07-08 18:45:55 +0530202
Saurabh02875592013-07-08 18:45:55 +0530203def get_project_name(doctype, txt, searchfield, start, page_len, filters):
204 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530205 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530206 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530207
208 return frappe.db.sql("""select `tabProject`.name from `tabProject`
209 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530210 and {cond} `tabProject`.name like %(txt)s {match_cond}
211 order by
212 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
213 idx desc,
214 `tabProject`.name asc
215 limit {start}, {page_len}""".format(
216 cond=cond,
217 match_cond=get_match_cond(doctype),
218 start=start,
219 page_len=page_len), {
220 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530221 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530222 })
Anand Doshibd67e872014-04-11 16:51:27 +0530223
Anand Doshi17350b82013-08-01 15:45:23 +0530224def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530225 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530226 from `tabDelivery Note`
227 where `tabDelivery Note`.`%(key)s` like %(txt)s and
shreyas29b565f2016-01-25 17:30:49 +0530228 `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530229 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530230 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
231 limit %(start)s, %(page_len)s""" % {
232 "key": searchfield,
233 "fcond": get_filters_cond(doctype, filters, []),
234 "mcond": get_match_cond(doctype),
235 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530236 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
237
238def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530239 cond = ""
240 if filters.get("posting_date"):
241 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530242
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530243 batch_nos = None
244 args = {
245 'item_code': filters.get("item_code"),
246 'warehouse': filters.get("warehouse"),
247 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530248 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530249 "start": start,
250 "page_len": page_len
251 }
252
Anand Doshi0dc79f42015-04-06 12:59:34 +0530253 if args.get('warehouse'):
254 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
255 from `tabStock Ledger Entry` sle
256 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
257 where
258 sle.item_code = %(item_code)s
259 and sle.warehouse = %(warehouse)s
260 and sle.batch_no like %(txt)s
261 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530262 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530263 {match_conditions}
264 group by batch_no having sum(sle.actual_qty) > 0
265 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530266 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530267
268 if batch_nos:
269 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530270 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530271 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530272 where item = %(item_code)s
273 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530274 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530275 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530276 {match_conditions}
277 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530278 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530279
280def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530281 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530282
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530283 if isinstance(filters, dict):
284 for key, val in filters.items():
285 if isinstance(val, (list, tuple)):
286 filter_list.append([doctype, key, val[0], val[1]])
287 else:
288 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530289 elif isinstance(filters, list):
290 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530291
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530292 if "is_group" not in [d[1] for d in filter_list]:
293 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530294
295 if searchfield and txt:
296 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
297
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530298 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530299 fields = ["name", "parent_account"],
300 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530301
Nabin Haitafd14f62015-10-19 11:55:28 +0530302
303@frappe.whitelist()
304def get_income_account(doctype, txt, searchfield, start, page_len, filters):
305 from erpnext.controllers.queries import get_match_cond
306
307 # income account can be any Credit account,
308 # but can also be a Asset account with account_type='Income Account' in special circumstances.
309 # Hence the first condition is an "OR"
310 if not filters: filters = {}
311
Anand Doshi21e09a22015-10-29 12:21:41 +0530312 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530313 if filters.get("company"):
314 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530315
Nabin Haitafd14f62015-10-19 11:55:28 +0530316 return frappe.db.sql("""select tabAccount.name from `tabAccount`
317 where (tabAccount.report_type = "Profit and Loss"
318 or tabAccount.account_type in ("Income Account", "Temporary"))
319 and tabAccount.is_group=0
320 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530321 {condition} {match_condition}
322 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530323 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530324 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530325 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530326 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530327
328
329@frappe.whitelist()
330def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
331 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530332
Nabin Hait3a15c922016-03-04 12:30:46 +0530333 if not filters: filters = {}
334
335 condition = ""
336 if filters.get("company"):
337 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530338
Nabin Hait3a15c922016-03-04 12:30:46 +0530339 return frappe.db.sql("""select tabAccount.name from `tabAccount`
340 where (tabAccount.report_type = "Profit and Loss"
341 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
342 and tabAccount.is_group=0
343 and tabAccount.docstatus!=2
344 and tabAccount.{key} LIKE %(txt)s
345 {condition} {match_condition}"""
Rushabh Mehta203cc962016-04-07 15:25:43 +0530346 .format(condition=condition, key=frappe.db.escape(searchfield),
Nabin Hait3a15c922016-03-04 12:30:46 +0530347 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530348 'company': filters.get("company", ""),
Nabin Hait3a15c922016-03-04 12:30:46 +0530349 'txt': "%%%s%%" % frappe.db.escape(txt)
Maxwell Morais35572612016-07-21 23:42:59 -0300350 })