blob: 98390ff2d21cad58cce9f624c829c51cc54646fd [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):
Kanchan Chauhan7652b852016-11-16 15:29:01 +053035 conditions = []
Anand Doshibd67e872014-04-11 16:51:27 +053036 return frappe.db.sql("""select name, employee_name from `tabEmployee`
37 where status = 'Active'
38 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053039 and ({key} like %(txt)s
40 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053041 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053042 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053043 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
44 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053045 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053046 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053047 limit %(start)s, %(page_len)s""".format(**{
48 'key': searchfield,
Kanchan Chauhan7652b852016-11-16 15:29:01 +053049 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi48d3b542014-07-09 13:15:03 +053050 'mcond': get_match_cond(doctype)
51 }), {
52 'txt': "%%%s%%" % txt,
53 '_txt': txt.replace("%", ""),
54 'start': start,
55 'page_len': page_len
56 })
Saurabh02875592013-07-08 18:45:55 +053057
58 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053059def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053060 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053061 where docstatus < 2
62 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053063 and ({key} like %(txt)s
64 or lead_name like %(txt)s
65 or company_name like %(txt)s)
66 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053067 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053068 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
69 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
70 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053071 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053072 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053073 limit %(start)s, %(page_len)s""".format(**{
74 'key': searchfield,
75 'mcond':get_match_cond(doctype)
76 }), {
77 'txt': "%%%s%%" % txt,
78 '_txt': txt.replace("%", ""),
79 'start': start,
80 'page_len': page_len
81 })
Saurabh02875592013-07-08 18:45:55 +053082
83 # searches for customer
84def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053085 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053086
Saurabh02875592013-07-08 18:45:55 +053087 if cust_master_name == "Customer Name":
88 fields = ["name", "customer_group", "territory"]
89 else:
90 fields = ["name", "customer_name", "customer_group", "territory"]
Maxwell Morais35572612016-07-21 23:42:59 -030091
92 meta = frappe.get_meta("Customer")
93 fields = fields + [f for f in meta.get_search_fields() if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053094
Anand Doshibd67e872014-04-11 16:51:27 +053095 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053096
Anand Doshi48d3b542014-07-09 13:15:03 +053097 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053098 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053099 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530100 or customer_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530101 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530102 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530103 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
104 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530105 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530106 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530107 limit %(start)s, %(page_len)s""".format(**{
108 "fields": fields,
109 "key": searchfield,
110 "mcond": get_match_cond(doctype)
111 }), {
112 'txt': "%%%s%%" % txt,
113 '_txt': txt.replace("%", ""),
114 'start': start,
115 'page_len': page_len
116 })
Saurabh02875592013-07-08 18:45:55 +0530117
118# searches for supplier
119def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530120 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530121 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530122 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530123 else:
Saurabh02875592013-07-08 18:45:55 +0530124 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530125 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530126
Anand Doshi48d3b542014-07-09 13:15:03 +0530127 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530128 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530129 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530130 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530131 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530132 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530133 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
134 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530135 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530136 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530137 limit %(start)s, %(page_len)s """.format(**{
138 'field': fields,
139 'key': searchfield,
140 'mcond':get_match_cond(doctype)
141 }), {
142 'txt': "%%%s%%" % txt,
143 '_txt': txt.replace("%", ""),
144 'start': start,
145 'page_len': page_len
146 })
Anand Doshibd67e872014-04-11 16:51:27 +0530147
Nabin Hait9a380ef2013-07-16 17:24:17 +0530148def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530149 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
150 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530151 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530152 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530153 and company = %s
154 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530155 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530156 limit %s, %s""" %
157 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
158 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530159 start, page_len]))
160 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530161 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530162 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530163 and company = %s and `%s` LIKE %s limit %s, %s"""
164 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530165 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530166
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530167 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530168
Rushabh Mehta203cc962016-04-07 15:25:43 +0530169def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530170 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530171
Rushabh Mehta203cc962016-04-07 15:25:43 +0530172 return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
Anand Doshibd67e872014-04-11 16:51:27 +0530173 if(length(tabItem.item_name) > 40,
174 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530175 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530176 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530177 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530178 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530179 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530180 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530181 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 +0530182 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800183 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530184 or tabItem.item_name LIKE %(txt)s
185 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530186 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530187 order by
188 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
189 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530190 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530191 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530192 limit %(start)s, %(page_len)s """.format(key=searchfield,
Nabin Haitc6285062016-03-30 13:10:25 +0530193 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
194 mcond=get_match_cond(doctype).replace('%', '%%')),
Anand Doshi22c0d782013-11-04 16:23:04 +0530195 {
196 "today": nowdate(),
197 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530198 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530199 "start": start,
200 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530201 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530202
203def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530204 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530205
Anand Doshibd67e872014-04-11 16:51:27 +0530206 return frappe.db.sql("""select tabBOM.name, tabBOM.item
207 from tabBOM
208 where tabBOM.docstatus=1
209 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530210 and tabBOM.`{key}` like %(txt)s
211 {fcond} {mcond}
212 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530213 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
214 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530215 limit %(start)s, %(page_len)s """.format(
216 fcond=get_filters_cond(doctype, filters, conditions),
217 mcond=get_match_cond(doctype),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530218 key=frappe.db.escape(searchfield)),
Nabin Hait62211172016-03-16 16:22:03 +0530219 {
Rushabh Mehta3574b372016-03-11 14:33:04 +0530220 'txt': "%%%s%%" % frappe.db.escape(txt),
221 '_txt': txt.replace("%", ""),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530222 'start': start,
Nabin Hait62211172016-03-16 16:22:03 +0530223 'page_len': page_len
Rushabh Mehta3574b372016-03-11 14:33:04 +0530224 })
Saurabh02875592013-07-08 18:45:55 +0530225
Saurabh02875592013-07-08 18:45:55 +0530226def get_project_name(doctype, txt, searchfield, start, page_len, filters):
227 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530228 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530229 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530230
231 return frappe.db.sql("""select `tabProject`.name from `tabProject`
232 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530233 and {cond} `tabProject`.name like %(txt)s {match_cond}
234 order by
235 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
236 idx desc,
237 `tabProject`.name asc
238 limit {start}, {page_len}""".format(
239 cond=cond,
240 match_cond=get_match_cond(doctype),
241 start=start,
242 page_len=page_len), {
243 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530244 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530245 })
Anand Doshibd67e872014-04-11 16:51:27 +0530246
Anand Doshi17350b82013-08-01 15:45:23 +0530247def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530248 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530249 from `tabDelivery Note`
250 where `tabDelivery Note`.`%(key)s` like %(txt)s and
shreyas29b565f2016-01-25 17:30:49 +0530251 `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530252 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530253 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
254 limit %(start)s, %(page_len)s""" % {
255 "key": searchfield,
256 "fcond": get_filters_cond(doctype, filters, []),
257 "mcond": get_match_cond(doctype),
258 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530259 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
260
261def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530262 cond = ""
263 if filters.get("posting_date"):
264 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530265
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530266 batch_nos = None
267 args = {
268 'item_code': filters.get("item_code"),
269 'warehouse': filters.get("warehouse"),
270 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530271 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530272 "start": start,
273 "page_len": page_len
274 }
275
Anand Doshi0dc79f42015-04-06 12:59:34 +0530276 if args.get('warehouse'):
277 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
278 from `tabStock Ledger Entry` sle
279 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
280 where
281 sle.item_code = %(item_code)s
282 and sle.warehouse = %(warehouse)s
283 and sle.batch_no like %(txt)s
284 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530285 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530286 {match_conditions}
287 group by batch_no having sum(sle.actual_qty) > 0
288 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530289 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530290
291 if batch_nos:
292 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530293 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530294 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530295 where item = %(item_code)s
296 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530297 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530298 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530299 {match_conditions}
300 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530301 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530302
303def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530304 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530305
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530306 if isinstance(filters, dict):
307 for key, val in filters.items():
308 if isinstance(val, (list, tuple)):
309 filter_list.append([doctype, key, val[0], val[1]])
310 else:
311 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530312 elif isinstance(filters, list):
313 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530314
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530315 if "is_group" not in [d[1] for d in filter_list]:
316 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530317
318 if searchfield and txt:
319 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
320
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530321 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530322 fields = ["name", "parent_account"],
323 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530324
Nabin Haitafd14f62015-10-19 11:55:28 +0530325
326@frappe.whitelist()
327def get_income_account(doctype, txt, searchfield, start, page_len, filters):
328 from erpnext.controllers.queries import get_match_cond
329
330 # income account can be any Credit account,
331 # but can also be a Asset account with account_type='Income Account' in special circumstances.
332 # Hence the first condition is an "OR"
333 if not filters: filters = {}
334
Anand Doshi21e09a22015-10-29 12:21:41 +0530335 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530336 if filters.get("company"):
337 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530338
Nabin Haitafd14f62015-10-19 11:55:28 +0530339 return frappe.db.sql("""select tabAccount.name from `tabAccount`
340 where (tabAccount.report_type = "Profit and Loss"
341 or tabAccount.account_type in ("Income Account", "Temporary"))
342 and tabAccount.is_group=0
343 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530344 {condition} {match_condition}
345 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530346 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530347 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530348 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530349 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530350
351
352@frappe.whitelist()
353def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
354 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530355
Nabin Hait3a15c922016-03-04 12:30:46 +0530356 if not filters: filters = {}
357
358 condition = ""
359 if filters.get("company"):
360 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530361
Nabin Hait3a15c922016-03-04 12:30:46 +0530362 return frappe.db.sql("""select tabAccount.name from `tabAccount`
363 where (tabAccount.report_type = "Profit and Loss"
364 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
365 and tabAccount.is_group=0
366 and tabAccount.docstatus!=2
367 and tabAccount.{key} LIKE %(txt)s
368 {condition} {match_condition}"""
Rushabh Mehta203cc962016-04-07 15:25:43 +0530369 .format(condition=condition, key=frappe.db.escape(searchfield),
Nabin Hait3a15c922016-03-04 12:30:46 +0530370 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530371 'company': filters.get("company", ""),
Nabin Hait3a15c922016-03-04 12:30:46 +0530372 'txt': "%%%s%%" % frappe.db.escape(txt)
Maxwell Morais35572612016-07-21 23:42:59 -0300373 })