blob: 8130af925a658f1125bc1819dc4b1df4f987dddb [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
suyashphadtare750a0672017-01-18 15:35:01 +05308from collections import defaultdict
Saurabh02875592013-07-08 18:45:55 +05309
Saurabh02875592013-07-08 18:45:55 +053010
Saurabh02875592013-07-08 18:45:55 +053011 # searches for active employees
12def employee_query(doctype, txt, searchfield, start, page_len, filters):
Kanchan Chauhan7652b852016-11-16 15:29:01 +053013 conditions = []
Anand Doshibd67e872014-04-11 16:51:27 +053014 return frappe.db.sql("""select name, employee_name from `tabEmployee`
15 where status = 'Active'
16 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053017 and ({key} like %(txt)s
18 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053019 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053020 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053021 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
22 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053023 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053024 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053025 limit %(start)s, %(page_len)s""".format(**{
26 'key': searchfield,
Kanchan Chauhan7652b852016-11-16 15:29:01 +053027 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi48d3b542014-07-09 13:15:03 +053028 'mcond': get_match_cond(doctype)
29 }), {
30 'txt': "%%%s%%" % txt,
31 '_txt': txt.replace("%", ""),
32 'start': start,
33 'page_len': page_len
34 })
Saurabh02875592013-07-08 18:45:55 +053035
36 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053037def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053038 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053039 where docstatus < 2
40 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053041 and ({key} like %(txt)s
42 or lead_name like %(txt)s
43 or company_name like %(txt)s)
44 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053045 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053046 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
47 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
48 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053049 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053050 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053051 limit %(start)s, %(page_len)s""".format(**{
52 'key': searchfield,
53 'mcond':get_match_cond(doctype)
54 }), {
55 'txt': "%%%s%%" % txt,
56 '_txt': txt.replace("%", ""),
57 'start': start,
58 'page_len': page_len
59 })
Saurabh02875592013-07-08 18:45:55 +053060
61 # searches for customer
62def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053063 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053064
Saurabh02875592013-07-08 18:45:55 +053065 if cust_master_name == "Customer Name":
66 fields = ["name", "customer_group", "territory"]
67 else:
68 fields = ["name", "customer_name", "customer_group", "territory"]
Rushabh Mehtab92087c2017-01-13 18:53:11 +053069
Maxwell Morais35572612016-07-21 23:42:59 -030070 meta = frappe.get_meta("Customer")
Console Admin86231662017-06-23 20:32:52 +030071 searchfields = meta.get_search_fields()
72 searchfields = searchfields + [f for f in [searchfield or "name", "customer_name"] \
73 if not f in searchfields]
74 fields = fields + [f for f in searchfields if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053075
Anand Doshibd67e872014-04-11 16:51:27 +053076 fields = ", ".join(fields)
Console Admin86231662017-06-23 20:32:52 +030077 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
Saurabh02875592013-07-08 18:45:55 +053078
Anand Doshi48d3b542014-07-09 13:15:03 +053079 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053080 where docstatus < 2
Console Admin86231662017-06-23 20:32:52 +030081 and ({scond}) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +053082 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053083 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053084 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
85 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053086 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +053087 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053088 limit %(start)s, %(page_len)s""".format(**{
89 "fields": fields,
Console Admin86231662017-06-23 20:32:52 +030090 "scond": searchfields,
Anand Doshi48d3b542014-07-09 13:15:03 +053091 "mcond": get_match_cond(doctype)
92 }), {
93 'txt': "%%%s%%" % txt,
94 '_txt': txt.replace("%", ""),
95 'start': start,
96 'page_len': page_len
97 })
Saurabh02875592013-07-08 18:45:55 +053098
99# searches for supplier
100def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530101 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530102 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530103 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530104 else:
Saurabh02875592013-07-08 18:45:55 +0530105 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530106 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530107
Anand Doshi48d3b542014-07-09 13:15:03 +0530108 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530109 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530110 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530111 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530112 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530113 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530114 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
115 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530116 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530117 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530118 limit %(start)s, %(page_len)s """.format(**{
119 'field': fields,
120 'key': searchfield,
121 'mcond':get_match_cond(doctype)
122 }), {
123 'txt': "%%%s%%" % txt,
124 '_txt': txt.replace("%", ""),
125 'start': start,
126 'page_len': page_len
127 })
Anand Doshibd67e872014-04-11 16:51:27 +0530128
Nabin Hait9a380ef2013-07-16 17:24:17 +0530129def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530130 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
131 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530132 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530133 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530134 and company = %s
135 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530136 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530137 limit %s, %s""" %
138 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
139 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530140 start, page_len]))
141 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530142 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530143 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530144 and company = %s and `%s` LIKE %s limit %s, %s"""
145 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530146 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530147
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530148 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530149
Rushabh Mehta203cc962016-04-07 15:25:43 +0530150def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530151 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530152
Rushabh Mehta203cc962016-04-07 15:25:43 +0530153 return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
Anand Doshibd67e872014-04-11 16:51:27 +0530154 if(length(tabItem.item_name) > 40,
155 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530156 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530157 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530158 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530159 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530160 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530161 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530162 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 +0530163 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800164 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530165 or tabItem.item_name LIKE %(txt)s
166 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530167 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530168 order by
169 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
170 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530171 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530172 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530173 limit %(start)s, %(page_len)s """.format(key=searchfield,
Nabin Haitc6285062016-03-30 13:10:25 +0530174 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
175 mcond=get_match_cond(doctype).replace('%', '%%')),
Anand Doshi22c0d782013-11-04 16:23:04 +0530176 {
177 "today": nowdate(),
178 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530179 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530180 "start": start,
181 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530182 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530183
184def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530185 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530186
Anand Doshibd67e872014-04-11 16:51:27 +0530187 return frappe.db.sql("""select tabBOM.name, tabBOM.item
188 from tabBOM
189 where tabBOM.docstatus=1
190 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530191 and tabBOM.`{key}` like %(txt)s
192 {fcond} {mcond}
193 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530194 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
195 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530196 limit %(start)s, %(page_len)s """.format(
197 fcond=get_filters_cond(doctype, filters, conditions),
198 mcond=get_match_cond(doctype),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530199 key=frappe.db.escape(searchfield)),
Nabin Hait62211172016-03-16 16:22:03 +0530200 {
Rushabh Mehta3574b372016-03-11 14:33:04 +0530201 'txt': "%%%s%%" % frappe.db.escape(txt),
202 '_txt': txt.replace("%", ""),
Rushabh Mehta203cc962016-04-07 15:25:43 +0530203 'start': start,
Nabin Hait62211172016-03-16 16:22:03 +0530204 'page_len': page_len
Rushabh Mehta3574b372016-03-11 14:33:04 +0530205 })
Saurabh02875592013-07-08 18:45:55 +0530206
Saurabh02875592013-07-08 18:45:55 +0530207def get_project_name(doctype, txt, searchfield, start, page_len, filters):
208 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530209 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530210 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530211
212 return frappe.db.sql("""select `tabProject`.name from `tabProject`
213 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530214 and {cond} `tabProject`.name like %(txt)s {match_cond}
215 order by
216 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
217 idx desc,
218 `tabProject`.name asc
219 limit {start}, {page_len}""".format(
220 cond=cond,
221 match_cond=get_match_cond(doctype),
222 start=start,
223 page_len=page_len), {
224 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530225 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530226 })
Anand Doshibd67e872014-04-11 16:51:27 +0530227
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530228def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
229 return frappe.db.sql("""
230 select `tabDelivery Note`.name, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date
Anand Doshibd67e872014-04-11 16:51:27 +0530231 from `tabDelivery Note`
232 where `tabDelivery Note`.`%(key)s` like %(txt)s and
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530233 `tabDelivery Note`.docstatus = 1 and `tabDelivery Note`.is_return = 0
234 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530235 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530236 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530237 """ % {
238 "key": searchfield,
239 "fcond": get_filters_cond(doctype, filters, []),
240 "mcond": get_match_cond(doctype),
241 "txt": "%(txt)s"
242 }, { "txt": ("%%%s%%" % txt) }, as_dict=as_dict)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530243
244def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530245 cond = ""
246 if filters.get("posting_date"):
247 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530248
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530249 batch_nos = None
250 args = {
251 'item_code': filters.get("item_code"),
252 'warehouse': filters.get("warehouse"),
253 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530254 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530255 "start": start,
256 "page_len": page_len
257 }
258
Anand Doshi0dc79f42015-04-06 12:59:34 +0530259 if args.get('warehouse'):
260 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
261 from `tabStock Ledger Entry` sle
262 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
263 where
264 sle.item_code = %(item_code)s
265 and sle.warehouse = %(warehouse)s
266 and sle.batch_no like %(txt)s
267 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530268 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530269 {match_conditions}
270 group by batch_no having sum(sle.actual_qty) > 0
271 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530272 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530273
274 if batch_nos:
275 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530276 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530277 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530278 where item = %(item_code)s
279 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530280 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530281 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530282 {match_conditions}
283 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530284 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530285
286def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530287 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530288
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530289 if isinstance(filters, dict):
290 for key, val in filters.items():
291 if isinstance(val, (list, tuple)):
292 filter_list.append([doctype, key, val[0], val[1]])
293 else:
294 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530295 elif isinstance(filters, list):
296 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530297
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530298 if "is_group" not in [d[1] for d in filter_list]:
299 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530300
301 if searchfield and txt:
302 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
303
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530304 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530305 fields = ["name", "parent_account"],
306 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530307
Nabin Haitafd14f62015-10-19 11:55:28 +0530308
309@frappe.whitelist()
310def get_income_account(doctype, txt, searchfield, start, page_len, filters):
311 from erpnext.controllers.queries import get_match_cond
312
313 # income account can be any Credit account,
314 # but can also be a Asset account with account_type='Income Account' in special circumstances.
315 # Hence the first condition is an "OR"
316 if not filters: filters = {}
317
Anand Doshi21e09a22015-10-29 12:21:41 +0530318 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530319 if filters.get("company"):
320 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530321
Nabin Haitafd14f62015-10-19 11:55:28 +0530322 return frappe.db.sql("""select tabAccount.name from `tabAccount`
323 where (tabAccount.report_type = "Profit and Loss"
324 or tabAccount.account_type in ("Income Account", "Temporary"))
325 and tabAccount.is_group=0
326 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530327 {condition} {match_condition}
328 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530329 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530330 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530331 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530332 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530333
334
335@frappe.whitelist()
336def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
337 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530338
Nabin Hait3a15c922016-03-04 12:30:46 +0530339 if not filters: filters = {}
340
341 condition = ""
342 if filters.get("company"):
343 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530344
Nabin Hait3a15c922016-03-04 12:30:46 +0530345 return frappe.db.sql("""select tabAccount.name from `tabAccount`
346 where (tabAccount.report_type = "Profit and Loss"
347 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
348 and tabAccount.is_group=0
349 and tabAccount.docstatus!=2
350 and tabAccount.{key} LIKE %(txt)s
351 {condition} {match_condition}"""
Rushabh Mehta203cc962016-04-07 15:25:43 +0530352 .format(condition=condition, key=frappe.db.escape(searchfield),
Nabin Hait3a15c922016-03-04 12:30:46 +0530353 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530354 'company': filters.get("company", ""),
Nabin Hait3a15c922016-03-04 12:30:46 +0530355 'txt': "%%%s%%" % frappe.db.escape(txt)
Maxwell Morais35572612016-07-21 23:42:59 -0300356 })
suyashphadtare049a88c2017-01-12 17:49:37 +0530357
358
359@frappe.whitelist()
360def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
361 # Should be used when item code is passed in filters.
suyashphadtare750a0672017-01-18 15:35:01 +0530362 conditions, bin_conditions = [], []
363 filter_dict = get_doctype_wise_filters(filters)
364
365 sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin`
suyashphadtare34ab1362017-01-31 15:14:44 +0530366 where `tabBin`.warehouse = `tabWarehouse`.name
367 {bin_conditions} """.format(
Nabin Hait4e6ff8c2017-05-09 15:09:10 +0530368 bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"),
369 bin_conditions, ignore_permissions=True))
suyashphadtare750a0672017-01-18 15:35:01 +0530370
371 response = frappe.db.sql("""select `tabWarehouse`.name,
suyashphadtare34ab1362017-01-31 15:14:44 +0530372 CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty
373 from `tabWarehouse`
374 where
375 `tabWarehouse`.`{key}` like %(txt)s
376 {fcond} {mcond}
377 order by
378 `tabWarehouse`.name desc
379 limit
380 %(start)s, %(page_len)s
381 """.format(
382 sub_query=sub_query,
383 key=frappe.db.escape(searchfield),
384 fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
385 mcond=get_match_cond(doctype)
386 ),
387 {
388 "txt": "%%%s%%" % frappe.db.escape(txt),
389 "start": start,
390 "page_len": page_len
391 })
suyashphadtare049a88c2017-01-12 17:49:37 +0530392 return response
suyashphadtare750a0672017-01-18 15:35:01 +0530393
394
395def get_doctype_wise_filters(filters):
396 # Helper function to seperate filters doctype_wise
397 filter_dict = defaultdict(list)
398 for row in filters:
399 filter_dict[row[0]].append(row)
400 return filter_dict