2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-09-09 10:45:35 +00:00
|
|
|
from frappe.desk.reportview import get_match_cond
|
2014-03-06 06:58:47 +00:00
|
|
|
from frappe.model.db_query import DatabaseQuery
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
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]])
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-03-06 06:58:47 +00:00
|
|
|
query = DatabaseQuery(doctype)
|
|
|
|
query.filters = flt
|
|
|
|
query.conditions = conditions
|
2014-05-07 13:48:15 +00:00
|
|
|
query.build_filter_conditions(flt, conditions)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
|
|
|
cond = ' and ' + ' and '.join(query.conditions)
|
2013-07-08 13:15:55 +00:00
|
|
|
else:
|
|
|
|
cond = ''
|
|
|
|
return cond
|
|
|
|
|
|
|
|
# searches for active employees
|
|
|
|
def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-04-11 11:21:27 +00:00
|
|
|
return frappe.db.sql("""select name, employee_name from `tabEmployee`
|
|
|
|
where status = 'Active'
|
|
|
|
and docstatus < 2
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or employee_name like %(txt)s)
|
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2014-07-09 07:45:03 +00:00
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
|
2014-04-16 09:51:46 +00:00
|
|
|
name, employee_name
|
2014-07-09 07:45:03 +00:00
|
|
|
limit %(start)s, %(page_len)s""".format(**{
|
|
|
|
'key': searchfield,
|
|
|
|
'mcond': get_match_cond(doctype)
|
|
|
|
}), {
|
|
|
|
'txt': "%%%s%%" % txt,
|
|
|
|
'_txt': txt.replace("%", ""),
|
|
|
|
'start': start,
|
|
|
|
'page_len': page_len
|
|
|
|
})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
# searches for leads which are not converted
|
2014-04-11 11:21:27 +00:00
|
|
|
def lead_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
|
|
|
and ifnull(status, '') != 'Converted'
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or lead_name like %(txt)s
|
|
|
|
or company_name like %(txt)s)
|
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2014-07-09 07:45:03 +00:00
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
|
|
|
|
if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
|
2014-04-16 09:51:46 +00:00
|
|
|
name, lead_name
|
2014-07-09 07:45:03 +00:00
|
|
|
limit %(start)s, %(page_len)s""".format(**{
|
|
|
|
'key': searchfield,
|
|
|
|
'mcond':get_match_cond(doctype)
|
|
|
|
}), {
|
|
|
|
'txt': "%%%s%%" % txt,
|
|
|
|
'_txt': txt.replace("%", ""),
|
|
|
|
'start': start,
|
|
|
|
'page_len': page_len
|
|
|
|
})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
# searches for customer
|
|
|
|
def customer_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-02-14 10:17:51 +00:00
|
|
|
cust_master_name = frappe.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
|
|
|
|
2014-04-11 11:21:27 +00:00
|
|
|
fields = ", ".join(fields)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2014-07-09 07:45:03 +00:00
|
|
|
return frappe.db.sql("""select {fields} from `tabCustomer`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or customer_name like %(txt)s)
|
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2014-07-09 07:45:03 +00:00
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
|
2014-04-11 11:21:27 +00:00
|
|
|
name, customer_name
|
2014-07-09 07:45:03 +00:00
|
|
|
limit %(start)s, %(page_len)s""".format(**{
|
|
|
|
"fields": fields,
|
|
|
|
"key": searchfield,
|
|
|
|
"mcond": get_match_cond(doctype)
|
|
|
|
}), {
|
|
|
|
'txt': "%%%s%%" % txt,
|
|
|
|
'_txt': txt.replace("%", ""),
|
|
|
|
'start': start,
|
|
|
|
'page_len': page_len
|
|
|
|
})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
# searches for supplier
|
|
|
|
def supplier_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-02-14 10:17:51 +00:00
|
|
|
supp_master_name = frappe.defaults.get_user_default("supp_master_name")
|
2014-04-11 11:21:27 +00:00
|
|
|
if supp_master_name == "Supplier Name":
|
2013-07-08 13:15:55 +00:00
|
|
|
fields = ["name", "supplier_type"]
|
2014-04-11 11:21:27 +00:00
|
|
|
else:
|
2013-07-08 13:15:55 +00:00
|
|
|
fields = ["name", "supplier_name", "supplier_type"]
|
2014-04-11 11:21:27 +00:00
|
|
|
fields = ", ".join(fields)
|
2013-07-08 13:15:55 +00:00
|
|
|
|
2014-07-09 07:45:03 +00:00
|
|
|
return frappe.db.sql("""select {field} from `tabSupplier`
|
2014-04-11 11:21:27 +00:00
|
|
|
where docstatus < 2
|
2014-07-09 07:45:03 +00:00
|
|
|
and ({key} like %(txt)s
|
|
|
|
or supplier_name like %(txt)s)
|
|
|
|
{mcond}
|
2014-04-11 11:21:27 +00:00
|
|
|
order by
|
2014-07-09 07:45:03 +00:00
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
|
2014-04-11 11:21:27 +00:00
|
|
|
name, supplier_name
|
2014-07-09 07:45:03 +00:00
|
|
|
limit %(start)s, %(page_len)s """.format(**{
|
|
|
|
'field': fields,
|
|
|
|
'key': searchfield,
|
|
|
|
'mcond':get_match_cond(doctype)
|
|
|
|
}), {
|
|
|
|
'txt': "%%%s%%" % txt,
|
|
|
|
'_txt': txt.replace("%", ""),
|
|
|
|
'start': start,
|
|
|
|
'page_len': page_len
|
|
|
|
})
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2013-07-16 11:54:17 +00:00
|
|
|
def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-04-11 11:21:27 +00:00
|
|
|
tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
|
|
|
|
where tabAccount.docstatus!=2
|
2014-03-21 05:44:49 +00:00
|
|
|
and account_type in (%s)
|
2013-07-16 11:54:17 +00:00
|
|
|
and group_or_ledger = 'Ledger'
|
|
|
|
and company = %s
|
|
|
|
and `%s` LIKE %s
|
2014-04-11 11:21:27 +00:00
|
|
|
limit %s, %s""" %
|
|
|
|
(", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
|
|
|
|
tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
|
2014-03-21 05:44:49 +00:00
|
|
|
start, page_len]))
|
|
|
|
if not tax_accounts:
|
2014-04-11 11:21:27 +00:00
|
|
|
tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
|
|
|
|
where tabAccount.docstatus!=2 and group_or_ledger = 'Ledger'
|
|
|
|
and company = %s and `%s` LIKE %s limit %s, %s"""
|
|
|
|
% ("%s", searchfield, "%s", "%s", "%s"),
|
2014-03-21 05:44:49 +00:00
|
|
|
(filters.get("company"), "%%%s%%" % txt, start, page_len))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-03-21 05:44:49 +00:00
|
|
|
return tax_accounts
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
def item_query(doctype, txt, searchfield, start, page_len, filters):
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.utils import nowdate
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2013-07-08 13:15:55 +00:00
|
|
|
conditions = []
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2014-04-11 11:21:27 +00:00
|
|
|
return frappe.db.sql("""select tabItem.name,
|
|
|
|
if(length(tabItem.item_name) > 40,
|
|
|
|
concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
|
2013-07-08 13:15:55 +00:00
|
|
|
if(length(tabItem.description) > 40, \
|
2013-11-04 10:53:04 +00:00
|
|
|
concat(substr(tabItem.description, 1, 40), "..."), description) as decription
|
2014-04-11 11:21:27 +00:00
|
|
|
from tabItem
|
2013-11-04 10:53:04 +00:00
|
|
|
where tabItem.docstatus < 2
|
2014-06-23 06:50:12 +00:00
|
|
|
and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
|
2013-11-04 10:53:04 +00:00
|
|
|
and (tabItem.`{key}` LIKE %(txt)s
|
2014-07-21 12:21:14 +00:00
|
|
|
or tabItem.item_name LIKE %(txt)s
|
|
|
|
or tabItem.description LIKE %(txt)s)
|
2013-11-04 10:53:04 +00:00
|
|
|
{fcond} {mcond}
|
2014-04-16 09:51:46 +00:00
|
|
|
order by
|
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
|
|
|
|
name, item_name
|
2013-11-04 10:53:04 +00:00
|
|
|
limit %(start)s, %(page_len)s """.format(key=searchfield,
|
|
|
|
fcond=get_filters_cond(doctype, filters, conditions),
|
2014-04-11 11:21:27 +00:00
|
|
|
mcond=get_match_cond(doctype)),
|
2013-11-04 10:53:04 +00:00
|
|
|
{
|
|
|
|
"today": nowdate(),
|
|
|
|
"txt": "%%%s%%" % txt,
|
2014-04-16 09:51:46 +00:00
|
|
|
"_txt": txt.replace("%", ""),
|
2013-11-04 10:53:04 +00:00
|
|
|
"start": start,
|
|
|
|
"page_len": page_len
|
|
|
|
})
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
def bom(doctype, txt, searchfield, start, page_len, filters):
|
2014-04-11 11:21:27 +00:00
|
|
|
conditions = []
|
|
|
|
|
|
|
|
return frappe.db.sql("""select tabBOM.name, tabBOM.item
|
|
|
|
from tabBOM
|
|
|
|
where tabBOM.docstatus=1
|
|
|
|
and tabBOM.is_active=1
|
|
|
|
and tabBOM.%(key)s like "%(txt)s"
|
|
|
|
%(fcond)s %(mcond)s
|
|
|
|
limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
|
|
|
|
'fcond': get_filters_cond(doctype, filters, conditions),
|
2014-03-06 05:48:37 +00:00
|
|
|
'mcond':get_match_cond(doctype), '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 = ''
|
2014-08-21 06:04:31 +00:00
|
|
|
if filters.get('customer'):
|
2013-07-09 10:48:52 +00:00
|
|
|
cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
|
2014-04-11 11:21:27 +00:00
|
|
|
|
|
|
|
return frappe.db.sql("""select `tabProject`.name from `tabProject`
|
|
|
|
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,
|
2014-03-06 05:48:37 +00:00
|
|
|
'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2013-08-01 10:15:23 +00:00
|
|
|
def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
|
2014-02-26 07:05:33 +00:00
|
|
|
return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
|
2014-04-11 11:21:27 +00:00
|
|
|
from `tabDelivery Note`
|
|
|
|
where `tabDelivery Note`.`%(key)s` like %(txt)s and
|
2013-09-03 10:51:01 +00:00
|
|
|
`tabDelivery Note`.docstatus = 1 %(fcond)s and
|
2014-04-11 11:21:27 +00:00
|
|
|
(ifnull((select sum(qty) from `tabDelivery Note Item` where
|
2013-08-01 10:15:23 +00:00
|
|
|
`tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
|
2014-04-11 11:21:27 +00:00
|
|
|
ifnull((select sum(qty) from `tabSales Invoice Item` where
|
2013-09-03 10:51:01 +00:00
|
|
|
`tabSales Invoice Item`.docstatus = 1 and
|
2013-08-01 10:15:23 +00:00
|
|
|
`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"
|
2013-10-18 06:59:11 +00:00
|
|
|
}, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
|
|
|
|
|
|
|
|
def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.controllers.queries import get_match_cond
|
2013-10-18 06:59:11 +00:00
|
|
|
|
|
|
|
if filters.has_key('warehouse'):
|
2014-04-11 11:21:27 +00:00
|
|
|
return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
|
|
|
|
where item_code = '%(item_code)s'
|
|
|
|
and warehouse = '%(warehouse)s'
|
|
|
|
and batch_no like '%(txt)s'
|
|
|
|
and exists(select * from `tabBatch`
|
|
|
|
where name = sle.batch_no
|
|
|
|
and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
|
|
|
|
and docstatus != 2)
|
2013-10-18 06:59:11 +00:00
|
|
|
%(mcond)s
|
2014-04-11 11:21:27 +00:00
|
|
|
group by batch_no having sum(actual_qty) > 0
|
|
|
|
order by batch_no desc
|
|
|
|
limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
|
|
|
|
'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
|
|
|
|
'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
|
2013-10-18 06:59:11 +00:00
|
|
|
'start': start, 'page_len': page_len})
|
|
|
|
else:
|
2014-04-11 11:21:27 +00:00
|
|
|
return frappe.db.sql("""select name from tabBatch
|
|
|
|
where docstatus != 2
|
|
|
|
and item = '%(item_code)s'
|
2013-10-18 06:59:11 +00:00
|
|
|
and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
|
2014-04-11 11:21:27 +00:00
|
|
|
and name like '%(txt)s'
|
|
|
|
%(mcond)s
|
|
|
|
order by name desc
|
|
|
|
limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
|
|
|
|
'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
|
|
|
|
'mcond':get_match_cond(doctype),'start': start,
|
2013-11-05 06:40:08 +00:00
|
|
|
'page_len': page_len})
|
2014-05-28 07:26:28 +00:00
|
|
|
|
|
|
|
def get_account_list(doctype, txt, searchfield, start, page_len, filters):
|
2014-06-14 09:56:10 +00:00
|
|
|
filter_list = []
|
|
|
|
|
2014-05-28 07:26:28 +00:00
|
|
|
if isinstance(filters, dict):
|
2014-06-14 09:56:10 +00:00
|
|
|
for key, val in filters.items():
|
|
|
|
if isinstance(val, (list, tuple)):
|
|
|
|
filter_list.append([doctype, key, val[0], val[1]])
|
|
|
|
else:
|
|
|
|
filter_list.append([doctype, key, "=", val])
|
|
|
|
|
|
|
|
if "group_or_ledger" not in [d[1] for d in filter_list]:
|
|
|
|
filter_list.append(["Account", "group_or_ledger", "=", "Ledger"])
|
|
|
|
|
|
|
|
if searchfield and txt:
|
|
|
|
filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
|
2014-05-28 07:26:28 +00:00
|
|
|
|
2014-09-09 10:45:35 +00:00
|
|
|
return frappe.desk.reportview.execute("Account", filters = filter_list,
|
2014-05-28 07:26:28 +00:00
|
|
|
fields = ["name", "parent_account"],
|
|
|
|
limit_start=start, limit_page_length=page_len, as_list=True)
|
2014-06-24 13:23:04 +00:00
|
|
|
|