2013-08-05 09:29:54 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import webnotes
|
|
|
|
|
|
|
|
def get_filters_cond(doctype, filters, conditions):
|
|
|
|
if filters:
|
|
|
|
if isinstance(filters, dict):
|
2013-07-10 07:37:49 +00:00
|
|
|
filters = filters.items()
|
|
|
|
flt = []
|
|
|
|
for f in filters:
|
2013-08-01 10:15:23 +00:00
|
|
|
if isinstance(f[1], basestring) and f[1][0] == '!':
|
2013-07-10 07:37:49 +00:00
|
|
|
flt.append([doctype, f[0], '!=', f[1][1:]])
|
|
|
|
else:
|
|
|
|
flt.append([doctype, f[0], '=', f[1]])
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
from webnotes.widgets.reportview import build_filter_conditions
|
2013-07-10 09:01:29 +00:00
|
|
|
build_filter_conditions(flt, conditions)
|
2013-07-08 13:15:55 +00:00
|
|
|
cond = ' and ' + ' and '.join(conditions)
|
|
|
|
else:
|
|
|
|
cond = ''
|
|
|
|
return cond
|
|
|
|
|
|
|
|
def get_match_cond(doctype, searchfield = 'name'):
|
|
|
|
from webnotes.widgets.reportview import build_match_conditions
|
2013-07-30 09:10:44 +00:00
|
|
|
cond = build_match_conditions(doctype)
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
if cond:
|
|
|
|
cond = ' and ' + cond
|
|
|
|
else:
|
|
|
|
cond = ''
|
|
|
|
return cond
|
|
|
|
|
|
|
|
# searches for enabled profiles
|
|
|
|
def profile_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
|
2013-07-10 10:36:31 +00:00
|
|
|
from `tabProfile`
|
|
|
|
where ifnull(enabled, 0)=1
|
|
|
|
and docstatus < 2
|
|
|
|
and name not in ('Administrator', 'Guest')
|
|
|
|
and (%(key)s like "%(txt)s"
|
|
|
|
or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s")
|
|
|
|
%(mcond)s
|
2013-07-08 13:15:55 +00:00
|
|
|
order by
|
|
|
|
case when name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
|
|
|
|
then 0 else 1 end,
|
2013-07-10 10:36:31 +00:00
|
|
|
name asc
|
|
|
|
limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
2013-07-08 13:15:55 +00:00
|
|
|
'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
|
|
|
|
|
|
|
|
# searches for active employees
|
|
|
|
def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
|
2013-07-10 10:36:31 +00:00
|
|
|
where status = 'Active'
|
|
|
|
and docstatus < 2
|
|
|
|
and (%(key)s like "%(txt)s"
|
|
|
|
or employee_name like "%(txt)s")
|
|
|
|
%(mcond)s
|
2013-07-08 13:15:55 +00:00
|
|
|
order by
|
|
|
|
case when name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when employee_name like "%(txt)s" then 0 else 1 end,
|
2013-07-10 10:36:31 +00:00
|
|
|
name
|
|
|
|
limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
2013-07-08 13:15:55 +00:00
|
|
|
'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
|
|
|
|
|
|
|
|
# searches for leads which are not converted
|
|
|
|
def lead_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
|
2013-07-10 10:36:31 +00:00
|
|
|
where docstatus < 2
|
|
|
|
and ifnull(status, '') != 'Converted'
|
|
|
|
and (%(key)s like "%(txt)s"
|
|
|
|
or lead_name like "%(txt)s"
|
|
|
|
or company_name like "%(txt)s")
|
|
|
|
%(mcond)s
|
2013-07-08 13:15:55 +00:00
|
|
|
order by
|
|
|
|
case when name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when lead_name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when company_name like "%(txt)s" then 0 else 1 end,
|
2013-07-10 10:36:31 +00:00
|
|
|
lead_name asc
|
|
|
|
limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
2013-07-08 13:15:55 +00:00
|
|
|
'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
|
|
|
|
|
|
|
|
# searches for customer
|
|
|
|
def customer_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
if cust_master_name == "Customer Name":
|
|
|
|
fields = ["name", "customer_group", "territory"]
|
|
|
|
else:
|
|
|
|
fields = ["name", "customer_name", "customer_group", "territory"]
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
fields = ", ".join(fields)
|
|
|
|
|
2013-07-10 10:36:31 +00:00
|
|
|
return webnotes.conn.sql("""select %(field)s from `tabCustomer`
|
|
|
|
where docstatus < 2
|
|
|
|
and (%(key)s like "%(txt)s"
|
|
|
|
or customer_name like "%(txt)s")
|
|
|
|
%(mcond)s
|
2013-07-08 13:15:55 +00:00
|
|
|
order by
|
|
|
|
case when name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when customer_name like "%(txt)s" then 0 else 1 end,
|
2013-07-10 10:36:31 +00:00
|
|
|
name, customer_name
|
|
|
|
limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
|
2013-07-08 13:15:55 +00:00
|
|
|
'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
|
|
|
|
'start': start, 'page_len': page_len})
|
|
|
|
|
|
|
|
# searches for supplier
|
|
|
|
def supplier_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
|
|
|
|
if supp_master_name == "Supplier Name":
|
|
|
|
fields = ["name", "supplier_type"]
|
|
|
|
else:
|
|
|
|
fields = ["name", "supplier_name", "supplier_type"]
|
|
|
|
fields = ", ".join(fields)
|
|
|
|
|
2013-07-10 10:36:31 +00:00
|
|
|
return webnotes.conn.sql("""select %(field)s from `tabSupplier`
|
|
|
|
where docstatus < 2
|
|
|
|
and (%(key)s like "%(txt)s"
|
|
|
|
or supplier_name like "%(txt)s")
|
|
|
|
%(mcond)s
|
2013-07-08 13:15:55 +00:00
|
|
|
order by
|
|
|
|
case when name like "%(txt)s" then 0 else 1 end,
|
|
|
|
case when supplier_name like "%(txt)s" then 0 else 1 end,
|
2013-07-10 10:36:31 +00:00
|
|
|
name, supplier_name
|
|
|
|
limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
|
2013-07-08 13:15:55 +00:00
|
|
|
'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
|
|
|
|
'page_len': page_len})
|
2013-07-16 11:54:17 +00:00
|
|
|
|
|
|
|
def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select name, parent_account, debit_or_credit
|
|
|
|
from tabAccount
|
|
|
|
where tabAccount.docstatus!=2
|
|
|
|
and (account_type in (%s) or
|
|
|
|
(ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) )
|
|
|
|
and group_or_ledger = 'Ledger'
|
|
|
|
and company = %s
|
|
|
|
and `%s` LIKE %s
|
|
|
|
limit %s, %s""" %
|
|
|
|
(", ".join(['%s']*len(filters.get("account_type"))),
|
|
|
|
"%s", "%s", searchfield, "%s", "%s", "%s"),
|
|
|
|
tuple(filters.get("account_type") + [filters.get("debit_or_credit"),
|
|
|
|
filters.get("company"), "%%%s%%" % txt, start, page_len]))
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
def item_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
conditions = []
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
return webnotes.conn.sql("""select tabItem.name,
|
|
|
|
if(length(tabItem.item_name) > 40,
|
|
|
|
concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
|
|
|
|
if(length(tabItem.description) > 40, \
|
|
|
|
concat(substr(tabItem.description, 1, 40), "..."), description) as decription
|
2013-07-10 10:36:31 +00:00
|
|
|
from tabItem
|
2013-07-29 07:37:30 +00:00
|
|
|
where tabItem.docstatus<2
|
2013-07-10 10:36:31 +00:00
|
|
|
and (tabItem.%(key)s LIKE "%(txt)s"
|
|
|
|
or tabItem.item_name LIKE "%(txt)s")
|
|
|
|
%(fcond)s %(mcond)s
|
|
|
|
limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
2013-07-08 13:15:55 +00:00
|
|
|
'fcond': get_filters_cond(doctype, filters, conditions),
|
2013-07-29 07:37:30 +00:00
|
|
|
'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
def bom(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
conditions = []
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
|
|
|
|
from tabBOM
|
2013-07-10 10:36:31 +00:00
|
|
|
where tabBOM.docstatus=1
|
|
|
|
and tabBOM.is_active=1
|
2013-07-11 12:19:18 +00:00
|
|
|
and tabBOM.%(key)s like "%(txt)s"
|
2013-07-10 10:36:31 +00:00
|
|
|
%(fcond)s %(mcond)s
|
|
|
|
limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
|
|
|
'fcond': get_filters_cond(doctype, filters, conditions),
|
|
|
|
'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
def get_project_name(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
cond = ''
|
|
|
|
if filters['customer']:
|
2013-07-09 10:48:52 +00:00
|
|
|
cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
|
2013-07-10 10:36:31 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
|
2013-07-10 10:36:31 +00:00
|
|
|
where `tabProject`.status not in ("Completed", "Cancelled")
|
|
|
|
and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
|
|
|
|
order by `tabProject`.name asc
|
|
|
|
limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
|
2013-07-15 12:22:10 +00:00
|
|
|
'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})
|
|
|
|
|
|
|
|
def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select ref_currency from `tabItem Price`
|
2013-08-09 12:41:35 +00:00
|
|
|
where price_list = %s and buying_or_selling = %s
|
2013-07-15 12:22:10 +00:00
|
|
|
and `%s` like %s order by ref_currency asc limit %s, %s""" %
|
|
|
|
("%s", "%s", searchfield, "%s", "%s", "%s"),
|
2013-08-09 12:41:35 +00:00
|
|
|
(filters["price_list"], filters['buying_or_selling'], "%%%s%%" % txt,
|
2013-08-01 10:15:23 +00:00
|
|
|
start, page_len))
|
|
|
|
|
|
|
|
def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
return webnotes.conn.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
|
|
|
|
from `tabDelivery Note`
|
|
|
|
where `tabDelivery Note`.`%(key)s` like %(txt)s %(fcond)s and
|
|
|
|
(ifnull((select sum(qty) from `tabDelivery Note Item` where
|
|
|
|
`tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
|
|
|
|
ifnull((select sum(qty) from `tabSales Invoice Item` where
|
|
|
|
`tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
|
|
|
|
%(mcond)s order by `tabDelivery Note`.`%(key)s` asc
|
|
|
|
limit %(start)s, %(page_len)s""" % {
|
|
|
|
"key": searchfield,
|
|
|
|
"fcond": get_filters_cond(doctype, filters, []),
|
|
|
|
"mcond": get_match_cond(doctype),
|
|
|
|
"start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
|
|
|
|
}, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }, debug=True)
|