blob: fc1ce833ba557d01674db3aa0bc93ba1e298061a [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),
Anand Doshi652bc072014-04-16 15:21:46 +053043 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053044 limit %(start)s, %(page_len)s""".format(**{
45 'key': searchfield,
46 'mcond': get_match_cond(doctype)
47 }), {
48 'txt': "%%%s%%" % txt,
49 '_txt': txt.replace("%", ""),
50 'start': start,
51 'page_len': page_len
52 })
Saurabh02875592013-07-08 18:45:55 +053053
54 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053055def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053056 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053057 where docstatus < 2
58 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053059 and ({key} like %(txt)s
60 or lead_name like %(txt)s
61 or company_name like %(txt)s)
62 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053063 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053064 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
65 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
66 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Anand Doshi652bc072014-04-16 15:21:46 +053067 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053068 limit %(start)s, %(page_len)s""".format(**{
69 'key': searchfield,
70 'mcond':get_match_cond(doctype)
71 }), {
72 'txt': "%%%s%%" % txt,
73 '_txt': txt.replace("%", ""),
74 'start': start,
75 'page_len': page_len
76 })
Saurabh02875592013-07-08 18:45:55 +053077
78 # searches for customer
79def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053080 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053081
Saurabh02875592013-07-08 18:45:55 +053082 if cust_master_name == "Customer Name":
83 fields = ["name", "customer_group", "territory"]
84 else:
85 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053086
Anand Doshibd67e872014-04-11 16:51:27 +053087 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +053088
Anand Doshi48d3b542014-07-09 13:15:03 +053089 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053090 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053091 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +053092 or customer_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +053093 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053094 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053095 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
96 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +053097 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053098 limit %(start)s, %(page_len)s""".format(**{
99 "fields": fields,
100 "key": searchfield,
101 "mcond": get_match_cond(doctype)
102 }), {
103 'txt': "%%%s%%" % txt,
104 '_txt': txt.replace("%", ""),
105 'start': start,
106 'page_len': page_len
107 })
Saurabh02875592013-07-08 18:45:55 +0530108
109# searches for supplier
110def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530111 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530112 if supp_master_name == "Supplier Name":
Saurabh02875592013-07-08 18:45:55 +0530113 fields = ["name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530114 else:
Saurabh02875592013-07-08 18:45:55 +0530115 fields = ["name", "supplier_name", "supplier_type"]
Anand Doshibd67e872014-04-11 16:51:27 +0530116 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530117
Anand Doshi48d3b542014-07-09 13:15:03 +0530118 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530119 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530120 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530121 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530122 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530123 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530124 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
125 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Anand Doshibd67e872014-04-11 16:51:27 +0530126 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530127 limit %(start)s, %(page_len)s """.format(**{
128 'field': fields,
129 'key': searchfield,
130 'mcond':get_match_cond(doctype)
131 }), {
132 'txt': "%%%s%%" % txt,
133 '_txt': txt.replace("%", ""),
134 'start': start,
135 'page_len': page_len
136 })
Anand Doshibd67e872014-04-11 16:51:27 +0530137
Nabin Hait9a380ef2013-07-16 17:24:17 +0530138def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530139 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
140 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530141 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530142 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530143 and company = %s
144 and `%s` LIKE %s
Anand Doshibd67e872014-04-11 16:51:27 +0530145 limit %s, %s""" %
146 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
147 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530148 start, page_len]))
149 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530150 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530151 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530152 and company = %s and `%s` LIKE %s limit %s, %s"""
153 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530154 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530155
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530156 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530157
158def item_query(doctype, txt, searchfield, start, page_len, filters):
159 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530160
RicardoJohann3fb53402016-01-20 14:32:59 +0800161 return frappe.db.sql("""select tabItem.name,tabItem.item_group,
Anand Doshibd67e872014-04-11 16:51:27 +0530162 if(length(tabItem.item_name) > 40,
163 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Saurabh02875592013-07-08 18:45:55 +0530164 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530165 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Anand Doshibd67e872014-04-11 16:51:27 +0530166 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530167 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530168 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530169 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530170 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 +0530171 and (tabItem.`{key}` LIKE %(txt)s
ShashaQin425ab6b2016-03-02 13:07:58 +0800172 or tabItem.item_group LIKE %(txt)s
Anand Doshi7fcb3c92014-07-21 17:51:14 +0530173 or tabItem.item_name LIKE %(txt)s
174 or tabItem.description LIKE %(txt)s)
Anand Doshi22c0d782013-11-04 16:23:04 +0530175 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530176 order by
177 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
178 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
179 name, item_name
Anand Doshi22c0d782013-11-04 16:23:04 +0530180 limit %(start)s, %(page_len)s """.format(key=searchfield,
181 fcond=get_filters_cond(doctype, filters, conditions),
Anand Doshibd67e872014-04-11 16:51:27 +0530182 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530183 {
184 "today": nowdate(),
185 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530186 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530187 "start": start,
188 "page_len": page_len
189 })
Saurabh02875592013-07-08 18:45:55 +0530190
191def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530192 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530193
Anand Doshibd67e872014-04-11 16:51:27 +0530194 return frappe.db.sql("""select tabBOM.name, tabBOM.item
195 from tabBOM
196 where tabBOM.docstatus=1
197 and tabBOM.is_active=1
198 and tabBOM.%(key)s like "%(txt)s"
199 %(fcond)s %(mcond)s
Nabin Hait93cdee42015-06-27 12:51:00 +0530200 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % frappe.db.escape(txt),
Anand Doshibd67e872014-04-11 16:51:27 +0530201 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530202 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530203
Saurabh02875592013-07-08 18:45:55 +0530204def get_project_name(doctype, txt, searchfield, start, page_len, filters):
205 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530206 if filters.get('customer'):
Saurabhab462d22013-07-09 16:18:52 +0530207 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Anand Doshibd67e872014-04-11 16:51:27 +0530208
209 return frappe.db.sql("""select `tabProject`.name from `tabProject`
210 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530211 and {cond} `tabProject`.name like %s {match_cond}
Anand Doshibd67e872014-04-11 16:51:27 +0530212 order by `tabProject`.name asc
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530213 limit {start}, {page_len}""".format(cond=cond, match_cond=get_match_cond(doctype),
214 start=start, page_len=page_len), "%{0}%".format(txt))
Anand Doshibd67e872014-04-11 16:51:27 +0530215
Anand Doshi17350b82013-08-01 15:45:23 +0530216def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530217 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshibd67e872014-04-11 16:51:27 +0530218 from `tabDelivery Note`
219 where `tabDelivery Note`.`%(key)s` like %(txt)s and
shreyas29b565f2016-01-25 17:30:49 +0530220 `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s
shreyas2563f402016-01-25 17:44:09 +0530221 and (`tabDelivery Note`.per_billed < 100 or `tabDelivery Note`.grand_total = 0)
Anand Doshi17350b82013-08-01 15:45:23 +0530222 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
223 limit %(start)s, %(page_len)s""" % {
224 "key": searchfield,
225 "fcond": get_filters_cond(doctype, filters, []),
226 "mcond": get_match_cond(doctype),
227 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530228 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
229
230def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530231 cond = ""
232 if filters.get("posting_date"):
233 cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530234
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530235 batch_nos = None
236 args = {
237 'item_code': filters.get("item_code"),
238 'warehouse': filters.get("warehouse"),
239 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530240 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530241 "start": start,
242 "page_len": page_len
243 }
244
Anand Doshi0dc79f42015-04-06 12:59:34 +0530245 if args.get('warehouse'):
246 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
247 from `tabStock Ledger Entry` sle
248 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
249 where
250 sle.item_code = %(item_code)s
251 and sle.warehouse = %(warehouse)s
252 and sle.batch_no like %(txt)s
253 and batch.docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530254 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530255 {match_conditions}
256 group by batch_no having sum(sle.actual_qty) > 0
257 order by batch.expiry_date, sle.batch_no desc
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530258 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530259
260 if batch_nos:
261 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530262 else:
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530263 return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
Anand Doshi0dc79f42015-04-06 12:59:34 +0530264 where item = %(item_code)s
265 and name like %(txt)s
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530266 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530267 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530268 {match_conditions}
269 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530270 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530271
272def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530273 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530274
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530275 if isinstance(filters, dict):
276 for key, val in filters.items():
277 if isinstance(val, (list, tuple)):
278 filter_list.append([doctype, key, val[0], val[1]])
279 else:
280 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530281 elif isinstance(filters, list):
282 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530283
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530284 if "is_group" not in [d[1] for d in filter_list]:
285 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530286
287 if searchfield and txt:
288 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
289
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530290 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530291 fields = ["name", "parent_account"],
292 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530293
Nabin Haitafd14f62015-10-19 11:55:28 +0530294
295@frappe.whitelist()
296def get_income_account(doctype, txt, searchfield, start, page_len, filters):
297 from erpnext.controllers.queries import get_match_cond
298
299 # income account can be any Credit account,
300 # but can also be a Asset account with account_type='Income Account' in special circumstances.
301 # Hence the first condition is an "OR"
302 if not filters: filters = {}
303
Anand Doshi21e09a22015-10-29 12:21:41 +0530304 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530305 if filters.get("company"):
306 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530307
Nabin Haitafd14f62015-10-19 11:55:28 +0530308 return frappe.db.sql("""select tabAccount.name from `tabAccount`
309 where (tabAccount.report_type = "Profit and Loss"
310 or tabAccount.account_type in ("Income Account", "Temporary"))
311 and tabAccount.is_group=0
312 and tabAccount.`{key}` LIKE %(txt)s
313 {condition} {match_condition}"""
314 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Anand Doshi21e09a22015-10-29 12:21:41 +0530315 'txt': "%%%s%%" % frappe.db.escape(txt),
Nabin Haitafd14f62015-10-19 11:55:28 +0530316 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530317 })