blob: eaaf4d18f492ab26499e148eafa6b525bf2be839 [file] [log] [blame]
Saurabh0326f542013-06-13 19:17:56 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Nabin Haitb8ebbca2013-06-20 13:03:10 +053019from webnotes.utils import add_days, add_months, cstr, getdate
20from webnotes import _
Saurabh0326f542013-06-13 19:17:56 +053021
Saurabh1848b712013-06-14 15:03:45 +053022def get_columns(filters, trans):
Nabin Haitb8ebbca2013-06-20 13:03:10 +053023 validate_filters(filters)
Saurabh0326f542013-06-13 19:17:56 +053024
Saurabhf8f68c52013-06-20 18:52:31 +053025 # get conditions for based_on filter cond
Saurabh2b02f142013-06-21 10:46:26 +053026 based_on_details = based_wise_colums_query(filters.get("based_on"), trans)
Saurabhf8f68c52013-06-20 18:52:31 +053027 # get conditions for periodic filter cond
28 period_cols, period_select = period_wise_colums_query(filters, trans)
29 # get conditions for grouping filter cond
30 group_by_cols = group_wise_column(filters.get("group_by"))
Saurabh0326f542013-06-13 19:17:56 +053031
Saurabh2b02f142013-06-21 10:46:26 +053032 columns = based_on_details["based_on_cols"] + period_cols + ["Total(Qty):Float:120", "Total(Amt):Currency:120"]
Saurabhf8f68c52013-06-20 18:52:31 +053033 if group_by_cols:
Saurabh2b02f142013-06-21 10:46:26 +053034 columns = based_on_details["based_on_cols"] + group_by_cols + period_cols + \
35 ["Total(Qty):Float:120", "Total(Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053036
Saurabh2b02f142013-06-21 10:46:26 +053037 conditions = {"based_on_select": based_on_details["based_on_select"], "period_wise_select": period_select,
Saurabh36cb3ca2013-06-21 16:25:28 +053038 "columns": columns, "group_by": based_on_details["based_on_group_by"], "grbc": group_by_cols, "trans": trans,
Saurabh2b02f142013-06-21 10:46:26 +053039 "addl_tables": based_on_details["addl_tables"]}
Saurabh0326f542013-06-13 19:17:56 +053040
Saurabhf8f68c52013-06-20 18:52:31 +053041 return conditions
Saurabh0326f542013-06-13 19:17:56 +053042
Nabin Haitb8ebbca2013-06-20 13:03:10 +053043def validate_filters(filters):
44 for f in ["Fiscal Year", "Based On", "Period", "Company"]:
45 if not filters.get(f.lower().replace(" ", "_")):
46 webnotes.msgprint(f + _(" is mandatory"), raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +053047
Nabin Haitb8ebbca2013-06-20 13:03:10 +053048 if filters.get("based_on") == filters.get("group_by"):
49 webnotes.msgprint("'Based On' and 'Group By' can not be same", raise_exception=1)
50
Saurabhf8f68c52013-06-20 18:52:31 +053051def get_data(filters, conditions):
Saurabh0326f542013-06-13 19:17:56 +053052 data = []
53 inc, cond= '',''
Saurabhf8f68c52013-06-20 18:52:31 +053054 query_details = conditions["based_on_select"] + conditions["period_wise_select"]
Saurabh0326f542013-06-13 19:17:56 +053055
Saurabhf8f68c52013-06-20 18:52:31 +053056 if conditions["based_on_select"] in ["t1.project_name,", "t2.project_name,"]:
57 cond = 'and '+ conditions["based_on_select"][:-1] +' IS Not NULL'
Saurabh0326f542013-06-13 19:17:56 +053058
59 if filters.get("group_by"):
60 sel_col = ''
Saurabhf8f68c52013-06-20 18:52:31 +053061 ind = conditions["columns"].index(conditions["grbc"][0])
Saurabh0326f542013-06-13 19:17:56 +053062
63 if filters.get("group_by") == 'Item':
64 sel_col = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +053065 elif filters.get("group_by") == 'Customer':
66 sel_col = 't1.customer'
Saurabh0326f542013-06-13 19:17:56 +053067 elif filters.get("group_by") == 'Supplier':
68 sel_col = 't1.supplier'
69
70 if filters.get('based_on') in ['Item','Customer','Supplier']:
71 inc = 2
72 else :
73 inc = 1
Saurabhf8f68c52013-06-20 18:52:31 +053074 data1 = webnotes.conn.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
Saurabh36cb3ca2013-06-21 16:25:28 +053075 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
76 t1.docstatus = 1 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053077 group by %s
Saurabh2b02f142013-06-21 10:46:26 +053078 """ % (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], "%s",
Saurabhf8f68c52013-06-20 18:52:31 +053079 "%s", cond, conditions["group_by"]), (filters.get("company"),
80 filters["fiscal_year"]),as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053081
82 for d in range(len(data1)):
83 #to add blanck column
84 dt = data1[d]
85 dt.insert(ind,'')
86 data.append(dt)
87
88 #to get distinct value of col specified by group_by in filter
Saurabhf8f68c52013-06-20 18:52:31 +053089 row = webnotes.conn.sql("""select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053090 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
91 and t1.docstatus = 1 and %s = %s
Nabin Hait4555d3b2013-06-21 16:27:34 +053092 """ %
93 (sel_col, conditions["trans"], conditions["trans"], conditions["addl_tables"],
94 "%s", "%s", conditions["group_by"], "%s"),
95 (filters.get("company"), filters.get("fiscal_year"), data1[d][0]), as_list=1)
Saurabh1848b712013-06-14 15:03:45 +053096
Saurabh0326f542013-06-13 19:17:56 +053097 for i in range(len(row)):
Saurabhf8f68c52013-06-20 18:52:31 +053098 des = ['' for q in range(len(conditions["columns"]))]
Saurabh0326f542013-06-13 19:17:56 +053099
Saurabhaf5df662013-06-20 15:00:53 +0530100 #get data for group_by filter
Saurabhf8f68c52013-06-20 18:52:31 +0530101 row1 = webnotes.conn.sql(""" select %s , %s from `tab%s` t1, `tab%s Item` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530102 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
103 and t1.docstatus = 1 and %s = %s and %s = %s
Nabin Hait4555d3b2013-06-21 16:27:34 +0530104 """ %
105 (sel_col, conditions["period_wise_select"], conditions["trans"],
106 conditions["trans"], conditions["addl_tables"], "%s", "%s", sel_col,
107 "%s", conditions["group_by"], "%s"),
108 (filters.get("company"), filters.get("fiscal_year"), row[i][0],
109 data1[d][0]), as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530110
111 des[ind] = row[i]
Saurabhf8f68c52013-06-20 18:52:31 +0530112 for j in range(1,len(conditions["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530113 des[j+inc] = row1[0][j]
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530114
Saurabh0326f542013-06-13 19:17:56 +0530115 data.append(des)
116 else:
Saurabhf8f68c52013-06-20 18:52:31 +0530117 data = webnotes.conn.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s
Saurabh36cb3ca2013-06-21 16:25:28 +0530118 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and
119 t1.docstatus = 1 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530120 group by %s
Nabin Hait4555d3b2013-06-21 16:27:34 +0530121 """ %
122 (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"],
123 "%s", "%s", cond,conditions["group_by"]),
124 (filters.get("company"), filters.get("fiscal_year")), as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530125
126 return data
127
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530128def get_mon(dt):
129 return getdate(dt).strftime("%b")
Saurabhd4f21992013-06-19 14:44:44 +0530130
Saurabh1848b712013-06-14 15:03:45 +0530131def period_wise_colums_query(filters, trans):
Saurabh0326f542013-06-13 19:17:56 +0530132 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530133 pwc = []
Saurabhf8f68c52013-06-20 18:52:31 +0530134 bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year"))
Saurabh1848b712013-06-14 15:03:45 +0530135
Saurabh0326f542013-06-13 19:17:56 +0530136 if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
137 trans_date = 'posting_date'
138 else:
139 trans_date = 'transaction_date'
Saurabh0326f542013-06-13 19:17:56 +0530140
Saurabhf8f68c52013-06-20 18:52:31 +0530141 if filters.get("period") != 'Yearly':
142 for dt in bet_dates:
143 get_period_wise_columns(dt, filters.get("period"), pwc)
144 query_details = get_period_wise_query(dt, trans_date, query_details)
Saurabh0326f542013-06-13 19:17:56 +0530145 else:
Nabin Hait4555d3b2013-06-21 16:27:34 +0530146 pwc = [filters.get("fiscal_year") + " (Qty):Float:120",
147 filters.get("fiscal_year") + " (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530148 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
149
Saurabh1848b712013-06-14 15:03:45 +0530150 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530151 return pwc, query_details
152
Saurabhf8f68c52013-06-20 18:52:31 +0530153def get_period_wise_columns(bet_dates, period, pwc):
154 if period == 'Monthly':
155 pwc += [get_mon(bet_dates[0]) + " (Qty):Float:120",
156 get_mon(bet_dates[0]) + " (Amt):Currency:120"]
157 else:
158 pwc += [get_mon(bet_dates[0]) + "-" + get_mon(bet_dates[1]) + " (Qty):Float:120",
159 get_mon(bet_dates[0]) + "-" + get_mon(bet_dates[1]) + " (Amt):Currency:120"]
160
161def get_period_wise_query(bet_dates, trans_date, query_details):
Saurabhf8f68c52013-06-20 18:52:31 +0530162 query_details += """SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
163 SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
Nabin Hait4555d3b2013-06-21 16:27:34 +0530164 """ % {"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]}
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530165 return query_details
Saurabh0326f542013-06-13 19:17:56 +0530166
Saurabhf8f68c52013-06-20 18:52:31 +0530167def get_period_date_ranges(period, fiscal_year):
168 from dateutil.relativedelta import relativedelta
169
170 year_start_date = webnotes.conn.get_value("Fiscal Year", fiscal_year, "year_start_date")
171 increment = {
172 "Monthly": 1,
173 "Quarterly": 3,
174 "Half-Yearly": 6,
175 "Yearly": 12
176 }.get(period)
177
178 period_date_ranges = []
179 for i in xrange(1, 13, increment):
180 period_end_date = year_start_date + relativedelta(months=increment, days=-1)
181 period_date_ranges.append([year_start_date, period_end_date])
182 year_start_date = period_end_date + relativedelta(days=1)
183
184 return period_date_ranges
185
186def get_period_month_ranges(period, fiscal_year):
187 from dateutil.relativedelta import relativedelta
188 period_month_ranges = []
189
190 for start_date, end_date in get_period_date_ranges(period, fiscal_year):
191 months_in_this_period = []
192 while start_date <= end_date:
193 months_in_this_period.append(start_date.strftime("%B"))
194 start_date += relativedelta(months=1)
195 period_month_ranges.append(months_in_this_period)
196
197 return period_month_ranges
198
199def based_wise_colums_query(based_on, trans):
Saurabh2b02f142013-06-21 10:46:26 +0530200 based_on_details = {}
Saurabh0326f542013-06-13 19:17:56 +0530201
Saurabh2b02f142013-06-21 10:46:26 +0530202 # based_on_cols, based_on_select, based_on_group_by, addl_tables
Saurabh0326f542013-06-13 19:17:56 +0530203 if based_on == "Item":
Saurabh2b02f142013-06-21 10:46:26 +0530204 based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"]
205 based_on_details["based_on_select"] = "t2.item_code, t2.item_name,"
206 based_on_details["based_on_group_by"] = 't2.item_code'
207 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530208
209 elif based_on == "Item Group":
Saurabh2b02f142013-06-21 10:46:26 +0530210 based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"]
211 based_on_details["based_on_select"] = "t2.item_group,"
212 based_on_details["based_on_group_by"] = 't2.item_group'
213 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530214
215 elif based_on == "Customer":
Saurabh2b02f142013-06-21 10:46:26 +0530216 based_on_details["based_on_cols"] = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
217 based_on_details["based_on_select"] = "t1.customer_name, t1.territory, "
218 based_on_details["based_on_group_by"] = 't1.customer_name'
219 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530220
221 elif based_on == "Customer Group":
Saurabh2b02f142013-06-21 10:46:26 +0530222 based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"]
223 based_on_details["based_on_select"] = "t1.customer_group,"
224 based_on_details["based_on_group_by"] = 't1.customer_group'
225 based_on_details["addl_tables"] = ''
226
Saurabh0326f542013-06-13 19:17:56 +0530227 elif based_on == 'Supplier':
Saurabh2b02f142013-06-21 10:46:26 +0530228 based_on_details["based_on_cols"] = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:140"]
229 based_on_details["based_on_select"] = "t1.supplier, t3.supplier_type,"
230 based_on_details["based_on_group_by"] = 't1.supplier'
231 based_on_details["addl_tables"] = ',`tabSupplier` t3'
Saurabh0326f542013-06-13 19:17:56 +0530232
233 elif based_on == 'Supplier Type':
Saurabh2b02f142013-06-21 10:46:26 +0530234 based_on_details["based_on_cols"] = ["Supplier Type:Link/Supplier Type:140"]
235 based_on_details["based_on_select"] = "t3.supplier_type,"
236 based_on_details["based_on_group_by"] = 't3.supplier_type'
237 based_on_details["addl_tables"] =',`tabSupplier` t3'
Saurabh0326f542013-06-13 19:17:56 +0530238
239 elif based_on == "Territory":
Saurabh2b02f142013-06-21 10:46:26 +0530240 based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"]
241 based_on_details["based_on_select"] = "t1.territory,"
242 based_on_details["based_on_group_by"] = 't1.territory'
243 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530244
245 elif based_on == "Project":
Saurabh0326f542013-06-13 19:17:56 +0530246 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
Saurabh2b02f142013-06-21 10:46:26 +0530247 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
248 based_on_details["based_on_select"] = "t1.project_name,"
249 based_on_details["based_on_group_by"] = 't1.project_name'
250 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530251 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
Saurabh2b02f142013-06-21 10:46:26 +0530252 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
253 based_on_details["based_on_select"] = "t2.project_name,"
254 based_on_details["based_on_group_by"] = 't2.project_name'
255 based_on_details["addl_tables"] = ''
Saurabh0326f542013-06-13 19:17:56 +0530256 else:
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530257 webnotes.msgprint("Project-wise data is not available for Quotation", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530258
Saurabh2b02f142013-06-21 10:46:26 +0530259 return based_on_details
Saurabh0326f542013-06-13 19:17:56 +0530260
Saurabh1848b712013-06-14 15:03:45 +0530261def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530262 if group_by:
263 return [group_by+":Link/"+group_by+":120"]
264 else:
265 return []