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-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-04-14 13:50:45 +00:00
|
|
|
from frappe.utils import getdate
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe import _
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2013-06-14 09:33:45 +00:00
|
|
|
def get_columns(filters, trans):
|
2013-06-20 07:33:10 +00:00
|
|
|
validate_filters(filters)
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
# get conditions for based_on filter cond
|
2014-06-05 10:44:28 +00:00
|
|
|
based_on_details = based_wise_columns_query(filters.get("based_on"), trans)
|
2013-06-20 13:22:31 +00:00
|
|
|
# get conditions for periodic filter cond
|
2014-06-05 10:44:28 +00:00
|
|
|
period_cols, period_select = period_wise_columns_query(filters, trans)
|
2013-06-20 13:22:31 +00:00
|
|
|
# get conditions for grouping filter cond
|
|
|
|
group_by_cols = group_wise_column(filters.get("group_by"))
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2014-09-11 08:15:27 +00:00
|
|
|
columns = based_on_details["based_on_cols"] + period_cols + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
|
2014-04-14 13:50:45 +00:00
|
|
|
if group_by_cols:
|
2013-06-21 05:16:26 +00:00
|
|
|
columns = based_on_details["based_on_cols"] + group_by_cols + period_cols + \
|
2014-09-11 08:15:27 +00:00
|
|
|
[_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2014-04-14 13:50:45 +00:00
|
|
|
conditions = {"based_on_select": based_on_details["based_on_select"], "period_wise_select": period_select,
|
2013-06-21 10:55:28 +00:00
|
|
|
"columns": columns, "group_by": based_on_details["based_on_group_by"], "grbc": group_by_cols, "trans": trans,
|
2014-08-11 06:24:21 +00:00
|
|
|
"addl_tables": based_on_details["addl_tables"], "addl_tables_relational_cond": based_on_details.get("addl_tables_relational_cond", "")}
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
return conditions
|
2013-06-14 09:33:45 +00:00
|
|
|
|
2013-06-20 07:33:10 +00:00
|
|
|
def validate_filters(filters):
|
|
|
|
for f in ["Fiscal Year", "Based On", "Period", "Company"]:
|
|
|
|
if not filters.get(f.lower().replace(" ", "_")):
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("{0} is mandatory").format(f))
|
|
|
|
|
2013-06-20 07:33:10 +00:00
|
|
|
if filters.get("based_on") == filters.get("group_by"):
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("'Based On' and 'Group By' can not be same"))
|
2013-06-20 07:33:10 +00:00
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
def get_data(filters, conditions):
|
2013-06-13 13:47:56 +00:00
|
|
|
data = []
|
|
|
|
inc, cond= '',''
|
2013-06-20 13:22:31 +00:00
|
|
|
query_details = conditions["based_on_select"] + conditions["period_wise_select"]
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
if conditions["based_on_select"] in ["t1.project_name,", "t2.project_name,"]:
|
|
|
|
cond = 'and '+ conditions["based_on_select"][:-1] +' IS Not NULL'
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
if filters.get("group_by"):
|
|
|
|
sel_col = ''
|
2013-06-20 13:22:31 +00:00
|
|
|
ind = conditions["columns"].index(conditions["grbc"][0])
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
if filters.get("group_by") == 'Item':
|
|
|
|
sel_col = 't2.item_code'
|
|
|
|
elif filters.get("group_by") == 'Customer':
|
|
|
|
sel_col = 't1.customer'
|
|
|
|
elif filters.get("group_by") == 'Supplier':
|
|
|
|
sel_col = 't1.supplier'
|
|
|
|
|
|
|
|
if filters.get('based_on') in ['Item','Customer','Supplier']:
|
|
|
|
inc = 2
|
|
|
|
else :
|
|
|
|
inc = 1
|
2014-02-26 07:05:33 +00:00
|
|
|
data1 = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
|
2014-04-14 13:50:45 +00:00
|
|
|
where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
|
2014-08-11 06:24:21 +00:00
|
|
|
t1.docstatus = 1 %s %s
|
2014-04-14 13:50:45 +00:00
|
|
|
group by %s
|
|
|
|
""" % (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], "%s",
|
2014-08-11 06:24:21 +00:00
|
|
|
"%s", conditions.get("addl_tables_relational_cond"), cond, conditions["group_by"]), (filters.get("company"),
|
2013-06-20 13:22:31 +00:00
|
|
|
filters["fiscal_year"]),as_list=1)
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
for d in range(len(data1)):
|
|
|
|
#to add blanck column
|
|
|
|
dt = data1[d]
|
2014-04-14 13:50:45 +00:00
|
|
|
dt.insert(ind,'')
|
2013-06-13 13:47:56 +00:00
|
|
|
data.append(dt)
|
|
|
|
|
|
|
|
#to get distinct value of col specified by group_by in filter
|
2014-02-26 07:05:33 +00:00
|
|
|
row = frappe.db.sql("""select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s
|
2014-04-14 13:50:45 +00:00
|
|
|
where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
|
2014-08-11 06:24:21 +00:00
|
|
|
and t1.docstatus = 1 and %s = %s %s
|
2014-04-14 13:50:45 +00:00
|
|
|
""" %
|
|
|
|
(sel_col, conditions["trans"], conditions["trans"], conditions["addl_tables"],
|
2014-08-11 06:24:21 +00:00
|
|
|
"%s", "%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond")),
|
2013-06-21 10:57:34 +00:00
|
|
|
(filters.get("company"), filters.get("fiscal_year"), data1[d][0]), as_list=1)
|
2013-06-14 09:33:45 +00:00
|
|
|
|
2013-06-13 13:47:56 +00:00
|
|
|
for i in range(len(row)):
|
2013-06-20 13:22:31 +00:00
|
|
|
des = ['' for q in range(len(conditions["columns"]))]
|
2014-04-14 13:50:45 +00:00
|
|
|
|
|
|
|
#get data for group_by filter
|
2014-02-26 07:05:33 +00:00
|
|
|
row1 = frappe.db.sql(""" select %s , %s from `tab%s` t1, `tab%s Item` t2 %s
|
2014-04-14 13:50:45 +00:00
|
|
|
where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
|
2014-08-11 06:24:21 +00:00
|
|
|
and t1.docstatus = 1 and %s = %s and %s = %s %s
|
2014-04-14 13:50:45 +00:00
|
|
|
""" %
|
|
|
|
(sel_col, conditions["period_wise_select"], conditions["trans"],
|
|
|
|
conditions["trans"], conditions["addl_tables"], "%s", "%s", sel_col,
|
2014-08-11 06:24:21 +00:00
|
|
|
"%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond")),
|
2014-04-14 13:50:45 +00:00
|
|
|
(filters.get("company"), filters.get("fiscal_year"), row[i][0],
|
2013-06-21 10:57:34 +00:00
|
|
|
data1[d][0]), as_list=1)
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
des[ind] = row[i]
|
2014-04-14 13:50:45 +00:00
|
|
|
for j in range(1,len(conditions["columns"])-inc):
|
2013-06-13 13:47:56 +00:00
|
|
|
des[j+inc] = row1[0][j]
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-06-13 13:47:56 +00:00
|
|
|
data.append(des)
|
|
|
|
else:
|
2014-02-26 07:05:33 +00:00
|
|
|
data = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
|
2014-04-14 13:50:45 +00:00
|
|
|
where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
|
2014-08-11 06:24:21 +00:00
|
|
|
t1.docstatus = 1 %s %s
|
2014-04-14 13:50:45 +00:00
|
|
|
group by %s
|
|
|
|
""" %
|
|
|
|
(query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"],
|
2014-08-11 06:24:21 +00:00
|
|
|
"%s", "%s", cond, conditions.get("addl_tables_relational_cond", ""), conditions["group_by"]),
|
2013-06-21 10:57:34 +00:00
|
|
|
(filters.get("company"), filters.get("fiscal_year")), as_list=1)
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2013-06-20 07:33:10 +00:00
|
|
|
def get_mon(dt):
|
|
|
|
return getdate(dt).strftime("%b")
|
2013-06-19 09:14:44 +00:00
|
|
|
|
2014-06-05 10:44:28 +00:00
|
|
|
def period_wise_columns_query(filters, trans):
|
2013-06-13 13:47:56 +00:00
|
|
|
query_details = ''
|
2013-06-14 09:33:45 +00:00
|
|
|
pwc = []
|
2013-06-20 13:22:31 +00:00
|
|
|
bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year"))
|
2013-06-14 09:33:45 +00:00
|
|
|
|
2013-06-13 13:47:56 +00:00
|
|
|
if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
|
|
|
|
trans_date = 'posting_date'
|
|
|
|
else:
|
|
|
|
trans_date = 'transaction_date'
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
if filters.get("period") != 'Yearly':
|
|
|
|
for dt in bet_dates:
|
|
|
|
get_period_wise_columns(dt, filters.get("period"), pwc)
|
|
|
|
query_details = get_period_wise_query(dt, trans_date, query_details)
|
2013-06-13 13:47:56 +00:00
|
|
|
else:
|
2014-09-11 08:15:27 +00:00
|
|
|
pwc = [_(filters.get("fiscal_year")) + " ("+_("Qty") + "):Float:120",
|
|
|
|
_(filters.get("fiscal_year")) + " ("+ _("Amt") + "):Currency:120"]
|
2014-06-05 10:44:28 +00:00
|
|
|
query_details = " SUM(t2.qty), SUM(t2.base_amount),"
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2014-06-05 10:44:28 +00:00
|
|
|
query_details += 'SUM(t2.qty), SUM(t2.base_amount)'
|
2013-06-13 13:47:56 +00:00
|
|
|
return pwc, query_details
|
|
|
|
|
2013-06-20 13:22:31 +00:00
|
|
|
def get_period_wise_columns(bet_dates, period, pwc):
|
|
|
|
if period == 'Monthly':
|
2014-09-11 08:15:27 +00:00
|
|
|
pwc += [_(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120",
|
|
|
|
_(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120"]
|
2013-06-20 13:22:31 +00:00
|
|
|
else:
|
2014-09-11 08:15:27 +00:00
|
|
|
pwc += [_(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Qty") + "):Float:120",
|
|
|
|
_(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Amt") + "):Currency:120"]
|
2013-06-20 13:22:31 +00:00
|
|
|
|
|
|
|
def get_period_wise_query(bet_dates, trans_date, query_details):
|
2014-04-14 13:50:45 +00:00
|
|
|
query_details += """SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
|
2014-06-05 10:44:28 +00:00
|
|
|
SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.base_amount, NULL)),
|
2013-06-21 10:57:34 +00:00
|
|
|
""" % {"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]}
|
2013-06-20 07:33:10 +00:00
|
|
|
return query_details
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2013-11-25 14:21:18 +00:00
|
|
|
def get_period_date_ranges(period, fiscal_year=None, year_start_date=None):
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
if not year_start_date:
|
2014-04-14 13:50:45 +00:00
|
|
|
year_start_date, year_end_date = frappe.db.get_value("Fiscal Year",
|
2013-11-25 14:21:18 +00:00
|
|
|
fiscal_year, ["year_start_date", "year_end_date"])
|
|
|
|
|
|
|
|
increment = {
|
|
|
|
"Monthly": 1,
|
|
|
|
"Quarterly": 3,
|
|
|
|
"Half-Yearly": 6,
|
|
|
|
"Yearly": 12
|
|
|
|
}.get(period)
|
|
|
|
|
|
|
|
period_date_ranges = []
|
|
|
|
for i in xrange(1, 13, increment):
|
|
|
|
period_end_date = getdate(year_start_date) + relativedelta(months=increment, days=-1)
|
|
|
|
if period_end_date > getdate(year_end_date):
|
|
|
|
period_end_date = year_end_date
|
|
|
|
period_date_ranges.append([year_start_date, period_end_date])
|
|
|
|
year_start_date = period_end_date + relativedelta(days=1)
|
|
|
|
if period_end_date == year_end_date:
|
|
|
|
break
|
|
|
|
|
|
|
|
return period_date_ranges
|
2013-06-20 13:22:31 +00:00
|
|
|
|
|
|
|
def get_period_month_ranges(period, fiscal_year):
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
period_month_ranges = []
|
|
|
|
|
|
|
|
for start_date, end_date in get_period_date_ranges(period, fiscal_year):
|
|
|
|
months_in_this_period = []
|
|
|
|
while start_date <= end_date:
|
|
|
|
months_in_this_period.append(start_date.strftime("%B"))
|
|
|
|
start_date += relativedelta(months=1)
|
|
|
|
period_month_ranges.append(months_in_this_period)
|
|
|
|
|
|
|
|
return period_month_ranges
|
|
|
|
|
2014-06-05 10:44:28 +00:00
|
|
|
def based_wise_columns_query(based_on, trans):
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details = {}
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2013-06-21 05:16:26 +00:00
|
|
|
# based_on_cols, based_on_select, based_on_group_by, addl_tables
|
2013-06-13 13:47:56 +00:00
|
|
|
if based_on == "Item":
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"]
|
2014-04-14 13:50:45 +00:00
|
|
|
based_on_details["based_on_select"] = "t2.item_code, t2.item_name,"
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_group_by"] = 't2.item_code'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
elif based_on == "Item Group":
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"]
|
2014-04-14 13:50:45 +00:00
|
|
|
based_on_details["based_on_select"] = "t2.item_group,"
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_group_by"] = 't2.item_group'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
elif based_on == "Customer":
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
|
|
|
|
based_on_details["based_on_select"] = "t1.customer_name, t1.territory, "
|
|
|
|
based_on_details["based_on_group_by"] = 't1.customer_name'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
elif based_on == "Customer Group":
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"]
|
|
|
|
based_on_details["based_on_select"] = "t1.customer_group,"
|
|
|
|
based_on_details["based_on_group_by"] = 't1.customer_group'
|
|
|
|
based_on_details["addl_tables"] = ''
|
|
|
|
|
2013-06-13 13:47:56 +00:00
|
|
|
elif based_on == 'Supplier':
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:140"]
|
|
|
|
based_on_details["based_on_select"] = "t1.supplier, t3.supplier_type,"
|
|
|
|
based_on_details["based_on_group_by"] = 't1.supplier'
|
|
|
|
based_on_details["addl_tables"] = ',`tabSupplier` t3'
|
2014-08-11 06:24:21 +00:00
|
|
|
based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-06-13 13:47:56 +00:00
|
|
|
elif based_on == 'Supplier Type':
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Supplier Type:Link/Supplier Type:140"]
|
|
|
|
based_on_details["based_on_select"] = "t3.supplier_type,"
|
|
|
|
based_on_details["based_on_group_by"] = 't3.supplier_type'
|
2014-08-11 06:24:21 +00:00
|
|
|
based_on_details["addl_tables"] = ',`tabSupplier` t3'
|
|
|
|
based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
elif based_on == "Territory":
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"]
|
|
|
|
based_on_details["based_on_select"] = "t1.territory,"
|
|
|
|
based_on_details["based_on_group_by"] = 't1.territory'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
|
|
|
|
elif based_on == "Project":
|
|
|
|
if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
|
|
|
|
based_on_details["based_on_select"] = "t1.project_name,"
|
|
|
|
based_on_details["based_on_group_by"] = 't1.project_name'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
|
2013-06-21 05:16:26 +00:00
|
|
|
based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
|
|
|
|
based_on_details["based_on_select"] = "t2.project_name,"
|
|
|
|
based_on_details["based_on_group_by"] = 't2.project_name'
|
|
|
|
based_on_details["addl_tables"] = ''
|
2013-06-13 13:47:56 +00:00
|
|
|
else:
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("Project-wise data is not available for Quotation"))
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2013-06-21 05:16:26 +00:00
|
|
|
return based_on_details
|
2013-06-13 13:47:56 +00:00
|
|
|
|
2013-06-14 09:33:45 +00:00
|
|
|
def group_wise_column(group_by):
|
2013-06-13 13:47:56 +00:00
|
|
|
if group_by:
|
|
|
|
return [group_by+":Link/"+group_by+":120"]
|
|
|
|
else:
|
2014-04-14 13:50:45 +00:00
|
|
|
return []
|