blob: c2d5e0090ccef89b707cee6136efe9d0ff73594b [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Saurabh0326f542013-06-13 19:17:56 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Rushabh Mehta9f0d6252014-04-14 19:20:45 +05306from frappe.utils import getdate
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05307from frappe import _
Saurabh0326f542013-06-13 19:17:56 +05308
Saurabh1848b712013-06-14 15:03:45 +05309def get_columns(filters, trans):
Nabin Haitb8ebbca2013-06-20 13:03:10 +053010 validate_filters(filters)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053011
Saurabhf8f68c52013-06-20 18:52:31 +053012 # get conditions for based_on filter cond
Nabin Haitb7438892014-06-05 16:14:28 +053013 based_on_details = based_wise_columns_query(filters.get("based_on"), trans)
Saurabhf8f68c52013-06-20 18:52:31 +053014 # get conditions for periodic filter cond
Nabin Haitb7438892014-06-05 16:14:28 +053015 period_cols, period_select = period_wise_columns_query(filters, trans)
Saurabhf8f68c52013-06-20 18:52:31 +053016 # get conditions for grouping filter cond
17 group_by_cols = group_wise_column(filters.get("group_by"))
Saurabh0326f542013-06-13 19:17:56 +053018
81552433qqcom951da0c2014-09-11 16:15:27 +080019 columns = based_on_details["based_on_cols"] + period_cols + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053020 if group_by_cols:
Saurabh2b02f142013-06-21 10:46:26 +053021 columns = based_on_details["based_on_cols"] + group_by_cols + period_cols + \
81552433qqcom951da0c2014-09-11 16:15:27 +080022 [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053023
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053024 conditions = {"based_on_select": based_on_details["based_on_select"], "period_wise_select": period_select,
Saurabh36cb3ca2013-06-21 16:25:28 +053025 "columns": columns, "group_by": based_on_details["based_on_group_by"], "grbc": group_by_cols, "trans": trans,
Nabin Haita6b597a2014-08-11 11:54:21 +053026 "addl_tables": based_on_details["addl_tables"], "addl_tables_relational_cond": based_on_details.get("addl_tables_relational_cond", "")}
Saurabh0326f542013-06-13 19:17:56 +053027
Saurabhf8f68c52013-06-20 18:52:31 +053028 return conditions
Saurabh1848b712013-06-14 15:03:45 +053029
Nabin Haitb8ebbca2013-06-20 13:03:10 +053030def validate_filters(filters):
31 for f in ["Fiscal Year", "Based On", "Period", "Company"]:
32 if not filters.get(f.lower().replace(" ", "_")):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053033 frappe.throw(_("{0} is mandatory").format(f))
34
Nabin Haitb8ebbca2013-06-20 13:03:10 +053035 if filters.get("based_on") == filters.get("group_by"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053036 frappe.throw(_("'Based On' and 'Group By' can not be same"))
Nabin Haitb8ebbca2013-06-20 13:03:10 +053037
Saurabhf8f68c52013-06-20 18:52:31 +053038def get_data(filters, conditions):
Saurabh0326f542013-06-13 19:17:56 +053039 data = []
40 inc, cond= '',''
Saurabhf8f68c52013-06-20 18:52:31 +053041 query_details = conditions["based_on_select"] + conditions["period_wise_select"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053042
Saurabhf8f68c52013-06-20 18:52:31 +053043 if conditions["based_on_select"] in ["t1.project_name,", "t2.project_name,"]:
44 cond = 'and '+ conditions["based_on_select"][:-1] +' IS Not NULL'
Saurabh0326f542013-06-13 19:17:56 +053045
46 if filters.get("group_by"):
47 sel_col = ''
Saurabhf8f68c52013-06-20 18:52:31 +053048 ind = conditions["columns"].index(conditions["grbc"][0])
Saurabh0326f542013-06-13 19:17:56 +053049
50 if filters.get("group_by") == 'Item':
51 sel_col = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +053052 elif filters.get("group_by") == 'Customer':
53 sel_col = 't1.customer'
Saurabh0326f542013-06-13 19:17:56 +053054 elif filters.get("group_by") == 'Supplier':
55 sel_col = 't1.supplier'
56
57 if filters.get('based_on') in ['Item','Customer','Supplier']:
58 inc = 2
59 else :
60 inc = 1
Anand Doshie9baaa62014-02-26 12:35:33 +053061 data1 = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053062 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
Nabin Haita6b597a2014-08-11 11:54:21 +053063 t1.docstatus = 1 %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053064 group by %s
65 """ % (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], "%s",
Nabin Haita6b597a2014-08-11 11:54:21 +053066 "%s", conditions.get("addl_tables_relational_cond"), cond, conditions["group_by"]), (filters.get("company"),
Saurabhf8f68c52013-06-20 18:52:31 +053067 filters["fiscal_year"]),as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053068
69 for d in range(len(data1)):
70 #to add blanck column
71 dt = data1[d]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053072 dt.insert(ind,'')
Saurabh0326f542013-06-13 19:17:56 +053073 data.append(dt)
74
75 #to get distinct value of col specified by group_by in filter
Anand Doshie9baaa62014-02-26 12:35:33 +053076 row = frappe.db.sql("""select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053077 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
Nabin Haita6b597a2014-08-11 11:54:21 +053078 and t1.docstatus = 1 and %s = %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053079 """ %
80 (sel_col, conditions["trans"], conditions["trans"], conditions["addl_tables"],
Nabin Haita6b597a2014-08-11 11:54:21 +053081 "%s", "%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond")),
Nabin Hait4555d3b2013-06-21 16:27:34 +053082 (filters.get("company"), filters.get("fiscal_year"), data1[d][0]), as_list=1)
Saurabh1848b712013-06-14 15:03:45 +053083
Saurabh0326f542013-06-13 19:17:56 +053084 for i in range(len(row)):
Saurabhf8f68c52013-06-20 18:52:31 +053085 des = ['' for q in range(len(conditions["columns"]))]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053086
87 #get data for group_by filter
Anand Doshie9baaa62014-02-26 12:35:33 +053088 row1 = frappe.db.sql(""" select %s , %s from `tab%s` t1, `tab%s Item` t2 %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053089 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
Nabin Haita6b597a2014-08-11 11:54:21 +053090 and t1.docstatus = 1 and %s = %s and %s = %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053091 """ %
92 (sel_col, conditions["period_wise_select"], conditions["trans"],
93 conditions["trans"], conditions["addl_tables"], "%s", "%s", sel_col,
Nabin Haita6b597a2014-08-11 11:54:21 +053094 "%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond")),
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053095 (filters.get("company"), filters.get("fiscal_year"), row[i][0],
Nabin Hait4555d3b2013-06-21 16:27:34 +053096 data1[d][0]), as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053097
98 des[ind] = row[i]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053099 for j in range(1,len(conditions["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530100 des[j+inc] = row1[0][j]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530101
Saurabh0326f542013-06-13 19:17:56 +0530102 data.append(des)
103 else:
Anand Doshie9baaa62014-02-26 12:35:33 +0530104 data = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530105 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
Nabin Haita6b597a2014-08-11 11:54:21 +0530106 t1.docstatus = 1 %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530107 group by %s
108 """ %
109 (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"],
Nabin Haita6b597a2014-08-11 11:54:21 +0530110 "%s", "%s", cond, conditions.get("addl_tables_relational_cond", ""), conditions["group_by"]),
Nabin Hait4555d3b2013-06-21 16:27:34 +0530111 (filters.get("company"), filters.get("fiscal_year")), as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530112
113 return data
114
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530115def get_mon(dt):
116 return getdate(dt).strftime("%b")
Saurabhd4f21992013-06-19 14:44:44 +0530117
Nabin Haitb7438892014-06-05 16:14:28 +0530118def period_wise_columns_query(filters, trans):
Saurabh0326f542013-06-13 19:17:56 +0530119 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530120 pwc = []
Saurabhf8f68c52013-06-20 18:52:31 +0530121 bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year"))
Saurabh1848b712013-06-14 15:03:45 +0530122
Saurabh0326f542013-06-13 19:17:56 +0530123 if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
124 trans_date = 'posting_date'
125 else:
126 trans_date = 'transaction_date'
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530127
Saurabhf8f68c52013-06-20 18:52:31 +0530128 if filters.get("period") != 'Yearly':
129 for dt in bet_dates:
130 get_period_wise_columns(dt, filters.get("period"), pwc)
131 query_details = get_period_wise_query(dt, trans_date, query_details)
Saurabh0326f542013-06-13 19:17:56 +0530132 else:
81552433qqcom951da0c2014-09-11 16:15:27 +0800133 pwc = [_(filters.get("fiscal_year")) + " ("+_("Qty") + "):Float:120",
134 _(filters.get("fiscal_year")) + " ("+ _("Amt") + "):Currency:120"]
Nabin Haitb7438892014-06-05 16:14:28 +0530135 query_details = " SUM(t2.qty), SUM(t2.base_amount),"
Saurabh0326f542013-06-13 19:17:56 +0530136
Nabin Haitb7438892014-06-05 16:14:28 +0530137 query_details += 'SUM(t2.qty), SUM(t2.base_amount)'
Saurabh0326f542013-06-13 19:17:56 +0530138 return pwc, query_details
139
Saurabhf8f68c52013-06-20 18:52:31 +0530140def get_period_wise_columns(bet_dates, period, pwc):
141 if period == 'Monthly':
81552433qqcom951da0c2014-09-11 16:15:27 +0800142 pwc += [_(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120",
143 _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120"]
Saurabhf8f68c52013-06-20 18:52:31 +0530144 else:
81552433qqcom951da0c2014-09-11 16:15:27 +0800145 pwc += [_(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Qty") + "):Float:120",
146 _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Amt") + "):Currency:120"]
Saurabhf8f68c52013-06-20 18:52:31 +0530147
148def get_period_wise_query(bet_dates, trans_date, query_details):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530149 query_details += """SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
Nabin Haitb7438892014-06-05 16:14:28 +0530150 SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.base_amount, NULL)),
Nabin Hait4555d3b2013-06-21 16:27:34 +0530151 """ % {"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]}
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530152 return query_details
Saurabh0326f542013-06-13 19:17:56 +0530153
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530154@frappe.whitelist(allow_guest=True)
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530155def get_period_date_ranges(period, fiscal_year=None, year_start_date=None):
156 from dateutil.relativedelta import relativedelta
Saurabhf8f68c52013-06-20 18:52:31 +0530157
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530158 if not year_start_date:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530159 year_start_date, year_end_date = frappe.db.get_value("Fiscal Year",
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530160 fiscal_year, ["year_start_date", "year_end_date"])
Saurabhf8f68c52013-06-20 18:52:31 +0530161
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530162 increment = {
163 "Monthly": 1,
164 "Quarterly": 3,
165 "Half-Yearly": 6,
166 "Yearly": 12
167 }.get(period)
Saurabhf8f68c52013-06-20 18:52:31 +0530168
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530169 period_date_ranges = []
170 for i in xrange(1, 13, increment):
171 period_end_date = getdate(year_start_date) + relativedelta(months=increment, days=-1)
172 if period_end_date > getdate(year_end_date):
173 period_end_date = year_end_date
174 period_date_ranges.append([year_start_date, period_end_date])
175 year_start_date = period_end_date + relativedelta(days=1)
176 if period_end_date == year_end_date:
177 break
178
179 return period_date_ranges
Saurabhf8f68c52013-06-20 18:52:31 +0530180
181def get_period_month_ranges(period, fiscal_year):
182 from dateutil.relativedelta import relativedelta
183 period_month_ranges = []
184
185 for start_date, end_date in get_period_date_ranges(period, fiscal_year):
186 months_in_this_period = []
187 while start_date <= end_date:
188 months_in_this_period.append(start_date.strftime("%B"))
189 start_date += relativedelta(months=1)
190 period_month_ranges.append(months_in_this_period)
191
192 return period_month_ranges
193
Nabin Haitb7438892014-06-05 16:14:28 +0530194def based_wise_columns_query(based_on, trans):
Saurabh2b02f142013-06-21 10:46:26 +0530195 based_on_details = {}
Saurabh0326f542013-06-13 19:17:56 +0530196
Saurabh2b02f142013-06-21 10:46:26 +0530197 # based_on_cols, based_on_select, based_on_group_by, addl_tables
Saurabh0326f542013-06-13 19:17:56 +0530198 if based_on == "Item":
Saurabh2b02f142013-06-21 10:46:26 +0530199 based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530200 based_on_details["based_on_select"] = "t2.item_code, t2.item_name,"
Saurabh2b02f142013-06-21 10:46:26 +0530201 based_on_details["based_on_group_by"] = 't2.item_code'
202 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530203
204 elif based_on == "Item Group":
Saurabh2b02f142013-06-21 10:46:26 +0530205 based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530206 based_on_details["based_on_select"] = "t2.item_group,"
Saurabh2b02f142013-06-21 10:46:26 +0530207 based_on_details["based_on_group_by"] = 't2.item_group'
208 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530209
210 elif based_on == "Customer":
Saurabh2b02f142013-06-21 10:46:26 +0530211 based_on_details["based_on_cols"] = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
212 based_on_details["based_on_select"] = "t1.customer_name, t1.territory, "
213 based_on_details["based_on_group_by"] = 't1.customer_name'
214 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530215
216 elif based_on == "Customer Group":
Saurabh2b02f142013-06-21 10:46:26 +0530217 based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"]
218 based_on_details["based_on_select"] = "t1.customer_group,"
219 based_on_details["based_on_group_by"] = 't1.customer_group'
220 based_on_details["addl_tables"] = ''
221
Saurabh0326f542013-06-13 19:17:56 +0530222 elif based_on == 'Supplier':
Saurabh2b02f142013-06-21 10:46:26 +0530223 based_on_details["based_on_cols"] = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:140"]
224 based_on_details["based_on_select"] = "t1.supplier, t3.supplier_type,"
225 based_on_details["based_on_group_by"] = 't1.supplier'
226 based_on_details["addl_tables"] = ',`tabSupplier` t3'
Nabin Haita6b597a2014-08-11 11:54:21 +0530227 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530228
Saurabh0326f542013-06-13 19:17:56 +0530229 elif based_on == 'Supplier Type':
Saurabh2b02f142013-06-21 10:46:26 +0530230 based_on_details["based_on_cols"] = ["Supplier Type:Link/Supplier Type:140"]
231 based_on_details["based_on_select"] = "t3.supplier_type,"
232 based_on_details["based_on_group_by"] = 't3.supplier_type'
Nabin Haita6b597a2014-08-11 11:54:21 +0530233 based_on_details["addl_tables"] = ',`tabSupplier` t3'
234 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Saurabh0326f542013-06-13 19:17:56 +0530235
236 elif based_on == "Territory":
Saurabh2b02f142013-06-21 10:46:26 +0530237 based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"]
238 based_on_details["based_on_select"] = "t1.territory,"
239 based_on_details["based_on_group_by"] = 't1.territory'
240 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530241
242 elif based_on == "Project":
Saurabh0326f542013-06-13 19:17:56 +0530243 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
Saurabh2b02f142013-06-21 10:46:26 +0530244 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
245 based_on_details["based_on_select"] = "t1.project_name,"
246 based_on_details["based_on_group_by"] = 't1.project_name'
247 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530248 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
Saurabh2b02f142013-06-21 10:46:26 +0530249 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
250 based_on_details["based_on_select"] = "t2.project_name,"
251 based_on_details["based_on_group_by"] = 't2.project_name'
252 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530253 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530254 frappe.throw(_("Project-wise data is not available for Quotation"))
Saurabh0326f542013-06-13 19:17:56 +0530255
Saurabh2b02f142013-06-21 10:46:26 +0530256 return based_on_details
Saurabh0326f542013-06-13 19:17:56 +0530257
Saurabh1848b712013-06-14 15:03:45 +0530258def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530259 if group_by:
260 return [group_by+":Link/"+group_by+":120"]
261 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530262 return []