2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe 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
|
2017-01-13 13:23:11 +00:00
|
|
|
from frappe.desk.reportview import get_match_cond, get_filters_cond
|
2015-03-20 09:36:30 +00:00
|
|
|
from frappe.utils import nowdate
|
2017-01-18 10:05:01 +00:00
|
|
|
from collections import defaultdict
|
2013-07-08 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# searches for active employees
|
|
|
|
def employee_query(doctype, txt, searchfield, start, page_len, filters):
|
2016-11-16 09:59:01 +00:00
|
|
|
conditions = []
|
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)
|
2016-11-16 09:59:01 +00:00
|
|
|
{fcond} {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),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
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,
|
2016-11-16 09:59:01 +00:00
|
|
|
'fcond': get_filters_cond(doctype, filters, conditions),
|
2014-07-09 07:45:03 +00:00
|
|
|
'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),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
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):
|
2017-07-25 08:33:01 +00:00
|
|
|
conditions = []
|
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"]
|
2017-01-13 13:23:11 +00:00
|
|
|
|
2016-07-22 02:42:59 +00:00
|
|
|
meta = frappe.get_meta("Customer")
|
2017-06-23 17:32:52 +00:00
|
|
|
searchfields = meta.get_search_fields()
|
|
|
|
searchfields = searchfields + [f for f in [searchfield or "name", "customer_name"] \
|
|
|
|
if not f in searchfields]
|
|
|
|
fields = fields + [f for f in searchfields if not f in fields]
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2014-04-11 11:21:27 +00:00
|
|
|
fields = ", ".join(fields)
|
2017-06-23 17:32:52 +00:00
|
|
|
searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
|
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
|
2017-06-23 17:32:52 +00:00
|
|
|
and ({scond}) and disabled=0
|
2017-07-25 08:33:01 +00:00
|
|
|
{fcond} {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),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
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,
|
2017-06-23 17:32:52 +00:00
|
|
|
"scond": searchfields,
|
2017-07-25 08:33:01 +00:00
|
|
|
"mcond": get_match_cond(doctype),
|
|
|
|
"fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
|
2014-07-09 07:45:03 +00:00
|
|
|
}), {
|
|
|
|
'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
|
2016-01-25 12:00:49 +00:00
|
|
|
or supplier_name like %(txt)s) and disabled=0
|
2014-07-09 07:45:03 +00:00
|
|
|
{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),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
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)
|
2015-04-23 07:44:17 +00:00
|
|
|
and is_group = 0
|
2013-07-16 11:54:17 +00:00
|
|
|
and company = %s
|
|
|
|
and `%s` LIKE %s
|
2016-03-11 09:03:04 +00:00
|
|
|
order by idx desc, name
|
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
|
2015-04-23 07:44:17 +00:00
|
|
|
where tabAccount.docstatus!=2 and is_group = 0
|
2014-04-11 11:21:27 +00:00
|
|
|
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
|
|
|
|
2016-04-07 09:55:43 +00:00
|
|
|
def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
|
2013-07-08 13:15:55 +00:00
|
|
|
conditions = []
|
2013-07-10 07:37:49 +00:00
|
|
|
|
2016-04-07 09:55:43 +00:00
|
|
|
return frappe.db.sql("""select tabItem.name, tabItem.item_group, tabItem.image,
|
2014-04-11 11:21:27 +00:00
|
|
|
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
|
2015-11-16 13:35:46 +00:00
|
|
|
and tabItem.has_variants=0
|
2015-10-29 06:51:41 +00:00
|
|
|
and tabItem.disabled=0
|
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
|
2016-03-02 05:07:58 +00:00
|
|
|
or tabItem.item_group 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),
|
2016-03-11 09:03:04 +00:00
|
|
|
idx desc,
|
2014-04-16 09:51:46 +00:00
|
|
|
name, item_name
|
2013-11-04 10:53:04 +00:00
|
|
|
limit %(start)s, %(page_len)s """.format(key=searchfield,
|
2016-03-30 07:40:25 +00:00
|
|
|
fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
|
|
|
|
mcond=get_match_cond(doctype).replace('%', '%%')),
|
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
|
2016-04-07 09:55:43 +00:00
|
|
|
}, as_dict=as_dict)
|
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
|
2016-03-16 10:52:03 +00:00
|
|
|
and tabBOM.`{key}` like %(txt)s
|
|
|
|
{fcond} {mcond}
|
|
|
|
order by
|
2016-03-11 09:03:04 +00:00
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
idx desc, name
|
2016-03-16 10:52:03 +00:00
|
|
|
limit %(start)s, %(page_len)s """.format(
|
|
|
|
fcond=get_filters_cond(doctype, filters, conditions),
|
|
|
|
mcond=get_match_cond(doctype),
|
2016-04-07 09:55:43 +00:00
|
|
|
key=frappe.db.escape(searchfield)),
|
2016-03-16 10:52:03 +00:00
|
|
|
{
|
2016-03-11 09:03:04 +00:00
|
|
|
'txt': "%%%s%%" % frappe.db.escape(txt),
|
|
|
|
'_txt': txt.replace("%", ""),
|
2016-04-07 09:55:43 +00:00
|
|
|
'start': start,
|
2016-03-16 10:52:03 +00:00
|
|
|
'page_len': page_len
|
2016-03-11 09:03:04 +00:00
|
|
|
})
|
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")
|
2016-03-11 09:03:04 +00:00
|
|
|
and {cond} `tabProject`.name like %(txt)s {match_cond}
|
|
|
|
order by
|
|
|
|
if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
|
|
|
|
idx desc,
|
|
|
|
`tabProject`.name asc
|
|
|
|
limit {start}, {page_len}""".format(
|
|
|
|
cond=cond,
|
|
|
|
match_cond=get_match_cond(doctype),
|
|
|
|
start=start,
|
|
|
|
page_len=page_len), {
|
|
|
|
"txt": "%{0}%".format(txt),
|
2016-03-16 05:46:31 +00:00
|
|
|
"_txt": txt.replace('%', '')
|
2016-03-11 09:03:04 +00:00
|
|
|
})
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2017-09-18 11:40:09 +00:00
|
|
|
|
2017-05-17 08:22:21 +00:00
|
|
|
def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
|
|
|
|
return frappe.db.sql("""
|
|
|
|
select `tabDelivery Note`.name, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date
|
2014-04-11 11:21:27 +00:00
|
|
|
from `tabDelivery Note`
|
|
|
|
where `tabDelivery Note`.`%(key)s` like %(txt)s and
|
2017-09-18 11:40:09 +00:00
|
|
|
`tabDelivery Note`.docstatus = 1
|
2017-05-17 08:22:21 +00:00
|
|
|
and status not in ("Stopped", "Closed") %(fcond)s
|
2017-09-18 11:40:09 +00:00
|
|
|
and (
|
|
|
|
(`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
|
|
|
|
or `tabDelivery Note`.grand_total = 0
|
|
|
|
or (
|
|
|
|
`tabDelivery Note`.is_return = 1
|
|
|
|
and return_against in (select name from `tabDelivery Note` where per_billed < 100)
|
|
|
|
)
|
|
|
|
)
|
2013-08-01 10:15:23 +00:00
|
|
|
%(mcond)s order by `tabDelivery Note`.`%(key)s` asc
|
2017-05-17 08:22:21 +00:00
|
|
|
""" % {
|
|
|
|
"key": searchfield,
|
|
|
|
"fcond": get_filters_cond(doctype, filters, []),
|
|
|
|
"mcond": get_match_cond(doctype),
|
|
|
|
"txt": "%(txt)s"
|
2017-09-18 11:40:09 +00:00
|
|
|
}, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict)
|
|
|
|
|
2013-10-18 06:59:11 +00:00
|
|
|
|
|
|
|
def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
|
2015-07-08 09:06:09 +00:00
|
|
|
cond = ""
|
|
|
|
if filters.get("posting_date"):
|
|
|
|
cond = "and (ifnull(batch.expiry_date, '')='' or batch.expiry_date >= %(posting_date)s)"
|
2015-08-26 05:20:16 +00:00
|
|
|
|
2015-03-20 09:36:30 +00:00
|
|
|
batch_nos = None
|
|
|
|
args = {
|
|
|
|
'item_code': filters.get("item_code"),
|
|
|
|
'warehouse': filters.get("warehouse"),
|
|
|
|
'posting_date': filters.get('posting_date'),
|
2015-04-06 07:29:34 +00:00
|
|
|
'txt': "%{0}%".format(txt),
|
2015-03-20 09:36:30 +00:00
|
|
|
"start": start,
|
|
|
|
"page_len": page_len
|
|
|
|
}
|
2013-10-18 06:59:11 +00:00
|
|
|
|
2015-04-06 07:29:34 +00:00
|
|
|
if args.get('warehouse'):
|
|
|
|
batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, batch.expiry_date
|
|
|
|
from `tabStock Ledger Entry` sle
|
|
|
|
INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
|
|
|
|
where
|
|
|
|
sle.item_code = %(item_code)s
|
|
|
|
and sle.warehouse = %(warehouse)s
|
|
|
|
and sle.batch_no like %(txt)s
|
|
|
|
and batch.docstatus < 2
|
2015-07-08 09:06:09 +00:00
|
|
|
{0}
|
2015-04-06 07:29:34 +00:00
|
|
|
{match_conditions}
|
|
|
|
group by batch_no having sum(sle.actual_qty) > 0
|
|
|
|
order by batch.expiry_date, sle.batch_no desc
|
2015-07-08 09:06:09 +00:00
|
|
|
limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
|
2015-03-20 09:36:30 +00:00
|
|
|
|
|
|
|
if batch_nos:
|
|
|
|
return batch_nos
|
2013-10-18 06:59:11 +00:00
|
|
|
else:
|
2015-07-08 09:06:09 +00:00
|
|
|
return frappe.db.sql("""select name, expiry_date from `tabBatch` batch
|
2015-04-06 07:29:34 +00:00
|
|
|
where item = %(item_code)s
|
|
|
|
and name like %(txt)s
|
2015-03-20 09:36:30 +00:00
|
|
|
and docstatus < 2
|
2015-07-08 09:06:09 +00:00
|
|
|
{0}
|
2015-04-06 07:29:34 +00:00
|
|
|
{match_conditions}
|
|
|
|
order by expiry_date, name desc
|
2015-09-02 05:25:32 +00:00
|
|
|
limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
|
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])
|
2015-04-14 16:45:24 +00:00
|
|
|
elif isinstance(filters, list):
|
|
|
|
filter_list.extend(filters)
|
2014-06-14 09:56:10 +00:00
|
|
|
|
2015-04-23 07:44:17 +00:00
|
|
|
if "is_group" not in [d[1] for d in filter_list]:
|
|
|
|
filter_list.append(["Account", "is_group", "=", "0"])
|
2014-06-14 09:56:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-19 06:25:28 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_income_account(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
from erpnext.controllers.queries import get_match_cond
|
|
|
|
|
|
|
|
# income account can be any Credit account,
|
|
|
|
# but can also be a Asset account with account_type='Income Account' in special circumstances.
|
|
|
|
# Hence the first condition is an "OR"
|
|
|
|
if not filters: filters = {}
|
|
|
|
|
2015-10-29 06:51:41 +00:00
|
|
|
condition = ""
|
2015-10-19 06:25:28 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
condition += "and tabAccount.company = %(company)s"
|
2015-10-29 06:51:41 +00:00
|
|
|
|
2015-10-19 06:25:28 +00:00
|
|
|
return frappe.db.sql("""select tabAccount.name from `tabAccount`
|
|
|
|
where (tabAccount.report_type = "Profit and Loss"
|
|
|
|
or tabAccount.account_type in ("Income Account", "Temporary"))
|
|
|
|
and tabAccount.is_group=0
|
|
|
|
and tabAccount.`{key}` LIKE %(txt)s
|
2016-03-11 09:03:04 +00:00
|
|
|
{condition} {match_condition}
|
|
|
|
order by idx desc, name"""
|
2015-10-19 06:25:28 +00:00
|
|
|
.format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
|
2015-10-29 06:51:41 +00:00
|
|
|
'txt': "%%%s%%" % frappe.db.escape(txt),
|
2015-10-19 06:25:28 +00:00
|
|
|
'company': filters.get("company", "")
|
2015-10-29 06:51:41 +00:00
|
|
|
})
|
2016-03-04 07:00:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
from erpnext.controllers.queries import get_match_cond
|
2016-04-07 09:55:43 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
if not filters: filters = {}
|
|
|
|
|
|
|
|
condition = ""
|
|
|
|
if filters.get("company"):
|
|
|
|
condition += "and tabAccount.company = %(company)s"
|
2016-04-07 09:55:43 +00:00
|
|
|
|
2016-03-04 07:00:46 +00:00
|
|
|
return frappe.db.sql("""select tabAccount.name from `tabAccount`
|
|
|
|
where (tabAccount.report_type = "Profit and Loss"
|
|
|
|
or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary"))
|
|
|
|
and tabAccount.is_group=0
|
|
|
|
and tabAccount.docstatus!=2
|
|
|
|
and tabAccount.{key} LIKE %(txt)s
|
|
|
|
{condition} {match_condition}"""
|
2016-04-07 09:55:43 +00:00
|
|
|
.format(condition=condition, key=frappe.db.escape(searchfield),
|
2016-03-04 07:00:46 +00:00
|
|
|
match_condition=get_match_cond(doctype)), {
|
2016-03-31 17:40:13 +00:00
|
|
|
'company': filters.get("company", ""),
|
2016-03-04 07:00:46 +00:00
|
|
|
'txt': "%%%s%%" % frappe.db.escape(txt)
|
2016-07-22 02:42:59 +00:00
|
|
|
})
|
2017-01-12 12:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
|
|
|
|
# Should be used when item code is passed in filters.
|
2017-01-18 10:05:01 +00:00
|
|
|
conditions, bin_conditions = [], []
|
|
|
|
filter_dict = get_doctype_wise_filters(filters)
|
|
|
|
|
|
|
|
sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin`
|
2017-01-31 09:44:44 +00:00
|
|
|
where `tabBin`.warehouse = `tabWarehouse`.name
|
|
|
|
{bin_conditions} """.format(
|
2017-08-24 09:53:33 +00:00
|
|
|
bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"),
|
2017-05-09 09:39:10 +00:00
|
|
|
bin_conditions, ignore_permissions=True))
|
2017-01-18 10:05:01 +00:00
|
|
|
|
2017-08-24 09:53:33 +00:00
|
|
|
query = """select `tabWarehouse`.name,
|
2017-01-31 09:44:44 +00:00
|
|
|
CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty
|
|
|
|
from `tabWarehouse`
|
|
|
|
where
|
2017-08-24 09:53:33 +00:00
|
|
|
`tabWarehouse`.`{key}` like '{txt}'
|
2017-01-31 09:44:44 +00:00
|
|
|
{fcond} {mcond}
|
|
|
|
order by
|
|
|
|
`tabWarehouse`.name desc
|
|
|
|
limit
|
2017-08-24 09:53:33 +00:00
|
|
|
{start}, {page_len}
|
2017-01-31 09:44:44 +00:00
|
|
|
""".format(
|
|
|
|
sub_query=sub_query,
|
|
|
|
key=frappe.db.escape(searchfield),
|
|
|
|
fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
|
2017-08-24 09:53:33 +00:00
|
|
|
mcond=get_match_cond(doctype),
|
|
|
|
start=start,
|
|
|
|
page_len=page_len,
|
|
|
|
txt=frappe.db.escape('%{0}%'.format(txt))
|
|
|
|
)
|
|
|
|
|
|
|
|
return frappe.db.sql(query)
|
2017-01-18 10:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_doctype_wise_filters(filters):
|
|
|
|
# Helper function to seperate filters doctype_wise
|
|
|
|
filter_dict = defaultdict(list)
|
|
|
|
for row in filters:
|
|
|
|
filter_dict[row[0]].append(row)
|
|
|
|
return filter_dict
|