blob: c0636cba45946bb9c5d6bf38eff045a82309d5d2 [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)
24
25 # based_on_cols, based_on_select, based_on_group_by, addl_tables
26 bonc, query_bon, based, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
27 # period_cols, period_select
28 pwc, query_pwc = period_wise_colums_query(filters, trans)
29
30 # group_by_cols
31 grbc = group_wise_column(filters.get("group_by"))
Saurabh0326f542013-06-13 19:17:56 +053032
Nabin Haitb8ebbca2013-06-20 13:03:10 +053033 columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
34 if grbc:
35 columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053036
Nabin Haitb8ebbca2013-06-20 13:03:10 +053037 # conditions
38 details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns,
39 "basedon": based, "grbc": grbc, "sup_tab": sup_tab}
Saurabh0326f542013-06-13 19:17:56 +053040
Saurabh1848b712013-06-14 15:03:45 +053041 return details
42
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
51def get_data(filters, tab, details):
Saurabh0326f542013-06-13 19:17:56 +053052 data = []
53 inc, cond= '',''
Saurabh1848b712013-06-14 15:03:45 +053054 query_details = details["query_bon"] + details["query_pwc"]
Saurabh0326f542013-06-13 19:17:56 +053055
Saurabh1848b712013-06-14 15:03:45 +053056 if details["query_bon"] in ["t1.project_name,", "t2.project_name,"]:
57 cond = 'and '+ details["query_bon"][:-1] +' IS Not NULL'
Saurabh0326f542013-06-13 19:17:56 +053058
59 if filters.get("group_by"):
60 sel_col = ''
Saurabh1848b712013-06-14 15:03:45 +053061 ind = details["columns"].index(details["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
74
Saurabh1848b712013-06-14 15:03:45 +053075 data1 = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053076 where t2.parent = t1.name and t1.company = %s
77 and t1.fiscal_year = %s and t1.docstatus = 1 %s
78 group by %s
79 """ % (query_details, tab[0], tab[1], details["sup_tab"], "%s",
80 "%s", cond, details["basedon"]), (filters.get("company"),
81 filters["fiscal_year"]),
82 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053083
84 for d in range(len(data1)):
85 #to add blanck column
86 dt = data1[d]
87 dt.insert(ind,'')
88 data.append(dt)
89
90 #to get distinct value of col specified by group_by in filter
Saurabh1848b712013-06-14 15:03:45 +053091 row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053092 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
93 and t1.docstatus = 1 and %s = %s
94 """%(sel_col, tab[0], tab[1], details["sup_tab"], "%s", "%s", details["basedon"], "%s"),
95 (filters.get("company"), filters.get("fiscal_year"), data1[d][0]),
96 as_list=1)
Saurabh1848b712013-06-14 15:03:45 +053097
Saurabh0326f542013-06-13 19:17:56 +053098 for i in range(len(row)):
Saurabh1848b712013-06-14 15:03:45 +053099 des = ['' for q in range(len(details["columns"]))]
Saurabh0326f542013-06-13 19:17:56 +0530100
101 #get data for each group_by filter
Saurabh1848b712013-06-14 15:03:45 +0530102 row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530103 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
104 and t1.docstatus = 1 and %s = %s and %s = %s
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530105 """ % (sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"],
Saurabh5b6d03e2013-06-19 12:34:22 +0530106 "%s", "%s", sel_col, "%s", details["basedon"], "%s"),
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530107 (filters.get("company"), filters.get("fiscal_year"),
108 row[i][0], data1[d][0]), as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530109
110 des[ind] = row[i]
Saurabh1848b712013-06-14 15:03:45 +0530111 for j in range(1,len(details["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530112 des[j+inc] = row1[0][j]
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530113
Saurabh0326f542013-06-13 19:17:56 +0530114 data.append(des)
115 else:
Saurabh0326f542013-06-13 19:17:56 +0530116 data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530117 where t2.parent = t1.name and t1.company = %s
118 and t1.fiscal_year = %s and t1.docstatus = 1 %s
119 group by %s
120 """%(query_details, tab[0], tab[1], details["sup_tab"], "%s",
121 "%s", cond,details["basedon"]), (filters.get("company"),
122 filters.get("fiscal_year")),
123 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530124
125 return data
126
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530127def get_mon(dt):
128 return getdate(dt).strftime("%b")
Saurabhd4f21992013-06-19 14:44:44 +0530129
Saurabh1848b712013-06-14 15:03:45 +0530130def period_wise_colums_query(filters, trans):
Saurabh0326f542013-06-13 19:17:56 +0530131 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530132 pwc = []
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530133 ysd = webnotes.conn.get_value("Fiscal year", filters.get("fiscal_year"), "year_start_date")
Saurabh1848b712013-06-14 15:03:45 +0530134
Saurabh0326f542013-06-13 19:17:56 +0530135 if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
136 trans_date = 'posting_date'
137 else:
138 trans_date = 'transaction_date'
139
Saurabh1848b712013-06-14 15:03:45 +0530140 if filters.get("period") == "Monthly":
Saurabh0326f542013-06-13 19:17:56 +0530141 month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
142
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530143 for month_idx in range(ysd.month-1,len(month_name)) + range(0, ysd.month-1):
144 query_details = get_monthly_conditions(month_name, month_idx, trans_date,
145 pwc, query_details)
Saurabh0326f542013-06-13 19:17:56 +0530146
Saurabh1848b712013-06-14 15:03:45 +0530147 elif filters.get("period") == "Quarterly":
Saurabh0326f542013-06-13 19:17:56 +0530148
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530149 first_qsd, second_qsd, third_qsd, fourth_qsd = ysd, add_months(ysd,3), add_months(ysd,6), add_months(ysd,9)
Saurabh0326f542013-06-13 19:17:56 +0530150 first_qed, second_qed, third_qed, fourth_qed = add_days(add_months(first_qsd,3),-1), add_days(add_months(second_qsd,3),-1), add_days(add_months(third_qsd,3),-1), add_days(add_months(fourth_qsd,3),-1)
Saurabh0326f542013-06-13 19:17:56 +0530151 bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
Saurabhd4f21992013-06-19 14:44:44 +0530152
153 pwc = [get_mon(first_qsd)+"-"+get_mon(first_qed)+" (Qty):Float:120", get_mon(first_qsd)+"-"+get_mon(first_qed)+"(Amt):Currency:120",
154 get_mon(second_qsd)+"-"+get_mon(second_qed)+" (Qty):Float:120", get_mon(second_qsd)+"-"+get_mon(second_qed)+" (Amt):Currency:120",
155 get_mon(third_qsd)+"-"+get_mon(third_qed)+" (Qty):Float:120", get_mon(third_qsd)+"-"+get_mon(third_qed)+" (Amt):Currency:120",
156 get_mon(fourth_qsd)+"-"+get_mon(fourth_qed)+" (Qty):Float:120", get_mon(fourth_qsd)+"-"+get_mon(fourth_qed)+" (Amt):Currency:120"]
Saurabh1848b712013-06-14 15:03:45 +0530157
Saurabh0326f542013-06-13 19:17:56 +0530158 for d in bet_dates:
159 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530160 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
161 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530162 """%{"trans": trans_date, "sd": d[0],"ed": d[1]}
163
Saurabh1848b712013-06-14 15:03:45 +0530164 elif filters.get("period") == "Half-yearly":
Saurabh0326f542013-06-13 19:17:56 +0530165
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530166 first_half_start = ysd
Saurabh0326f542013-06-13 19:17:56 +0530167 first_half_end = add_days(add_months(first_half_start,6),-1)
168 second_half_start = add_days(first_half_end,1)
169 second_half_end = add_days(add_months(second_half_start,6),-1)
170
Saurabhd4f21992013-06-19 14:44:44 +0530171 pwc = [get_mon(first_half_start)+"-"+get_mon(first_half_end)+"(Qty):Float:120", get_mon(first_half_start)+"-"+get_mon(first_half_end)+" (Amt):Currency:120",
172 get_mon(second_half_start)+"-"+get_mon(second_half_end)+" (Qty):Float:120", get_mon(second_half_start)+"-"+get_mon(second_half_end)+" (Amt):Currency:120"]
173
Saurabh0326f542013-06-13 19:17:56 +0530174 query_details = """
Saurabh5b6d03e2013-06-19 12:34:22 +0530175 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t2.qty, NULL)),
176 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t1.grand_total, NULL)),
177 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t2.qty, NULL)),
178 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530179 """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start,
180 "she": second_half_end}
181
182 else:
Saurabh1848b712013-06-14 15:03:45 +0530183 pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530184 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
185
Saurabh1848b712013-06-14 15:03:45 +0530186 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530187 return pwc, query_details
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530188
189def get_monthly_conditions(month_list, month_idx, trans_date, pwc, query_details):
190 pwc += [month_list[month_idx] + ' (Qty):Float:120',
191 month_list[month_idx] + ' (Amt):Currency:120']
192
193 query_details += """
194 Sum(IF(MONTH(t1.%(trans_date)s)= %(mon_num)s, t2.qty, NULL)),
195 SUM(IF(MONTH(t1.%(trans_date)s)= %(mon_num)s, t1.grand_total, NULL)),
196 """ % {"trans_date": trans_date, "mon_num": cstr(month_idx+1)}
197
198 return query_details
Saurabh0326f542013-06-13 19:17:56 +0530199
Saurabh1848b712013-06-14 15:03:45 +0530200def basedon_wise_colums_query(based_on, trans):
Saurabh0326f542013-06-13 19:17:56 +0530201 sup_tab = ''
202
203 if based_on == "Item":
204 bon = ["Item:Link/Item:120", "Item Name:Data:120"]
205 query_details = "t2.item_code, t2.item_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530206 based = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +0530207
208 elif based_on == "Item Group":
209 bon = ["Item Group:Link/Item Group:120"]
210 query_details = "t2.item_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530211 based = 't2.item_group'
Saurabh0326f542013-06-13 19:17:56 +0530212
213 elif based_on == "Customer":
214 bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
215 query_details = "t1.customer_name, t1.territory, "
Saurabhbfe9fe72013-06-19 12:41:28 +0530216 based = 't1.customer_name'
Saurabh0326f542013-06-13 19:17:56 +0530217
218 elif based_on == "Customer Group":
219 bon = ["Customer Group:Link/Customer Group"]
220 query_details = "t1.customer_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530221 based = 't1.customer_group'
Saurabh0326f542013-06-13 19:17:56 +0530222
223 elif based_on == 'Supplier':
224 bon = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:120"]
225 query_details = "t1.supplier, t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530226 based = 't1.supplier'
Saurabh24208f52013-06-18 15:45:08 +0530227 sup_tab = '`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530228
229 elif based_on == 'Supplier Type':
230 bon = ["Supplier Type:Link/Supplier Type:120"]
231 query_details = "t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530232 based = 't3.supplier_type'
Saurabh24208f52013-06-18 15:45:08 +0530233 sup_tab ='`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530234
235 elif based_on == "Territory":
236 bon = ["Territory:Link/Territory:120"]
237 query_details = "t1.territory,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530238 based = 't1.territory'
Saurabh0326f542013-06-13 19:17:56 +0530239
240 elif based_on == "Project":
Saurabh0326f542013-06-13 19:17:56 +0530241 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
242 bon = ["Project:Link/Project:120"]
243 query_details = "t1.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530244 based = 't1.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530245 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
246 bon = ["Project:Link/Project:120"]
247 query_details = "t2.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530248 based = 't2.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530249 else:
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530250 webnotes.msgprint("Project-wise data is not available for Quotation", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530251
Saurabhbfe9fe72013-06-19 12:41:28 +0530252 return bon, query_details, based, sup_tab
Saurabh0326f542013-06-13 19:17:56 +0530253
Saurabh1848b712013-06-14 15:03:45 +0530254def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530255 if group_by:
256 return [group_by+":Link/"+group_by+":120"]
257 else:
258 return []