blob: 4edb7845cebf0bf3f4d420fac672fff46e20ed07 [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
19from webnotes.utils import cint, add_days, add_months, cstr
Saurabhd4f21992013-06-19 14:44:44 +053020from datetime import datetime
Saurabh0326f542013-06-13 19:17:56 +053021
Saurabh1848b712013-06-14 15:03:45 +053022def get_columns(filters, trans):
Saurabh0326f542013-06-13 19:17:56 +053023
Saurabh1848b712013-06-14 15:03:45 +053024 if not (filters.get("period") and filters.get("based_on")):
Saurabh0326f542013-06-13 19:17:56 +053025 webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
26
Saurabh1848b712013-06-14 15:03:45 +053027 elif filters.get("based_on") == filters.get("group_by"):
Saurabh0326f542013-06-13 19:17:56 +053028 webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1)
29
30 else:
Saurabhbfe9fe72013-06-19 12:41:28 +053031 bonc, query_bon, based, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
Saurabh1848b712013-06-14 15:03:45 +053032 pwc, query_pwc = period_wise_colums_query(filters, trans)
33 grbc = group_wise_column(filters.get("group_by"))
34
Saurabhbfe9fe72013-06-19 12:41:28 +053035 columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh1848b712013-06-14 15:03:45 +053036 if grbc:
Saurabhbfe9fe72013-06-19 12:41:28 +053037 columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053038
Saurabhbfe9fe72013-06-19 12:41:28 +053039 details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": based,
Saurabh1848b712013-06-14 15:03:45 +053040 "grbc": grbc, "sup_tab": sup_tab}
Saurabh0326f542013-06-13 19:17:56 +053041
Saurabh1848b712013-06-14 15:03:45 +053042 return details
43
44def get_data(filters, tab, details):
Saurabh0326f542013-06-13 19:17:56 +053045
Saurabh0326f542013-06-13 19:17:56 +053046 data = []
47 inc, cond= '',''
Saurabh1848b712013-06-14 15:03:45 +053048 query_details = details["query_bon"] + details["query_pwc"]
Saurabh0326f542013-06-13 19:17:56 +053049
Saurabh1848b712013-06-14 15:03:45 +053050 if details["query_bon"] in ["t1.project_name,", "t2.project_name,"]:
51 cond = 'and '+ details["query_bon"][:-1] +' IS Not NULL'
Saurabh0326f542013-06-13 19:17:56 +053052
53 if filters.get("group_by"):
54 sel_col = ''
Saurabh1848b712013-06-14 15:03:45 +053055 ind = details["columns"].index(details["grbc"][0])
Saurabh0326f542013-06-13 19:17:56 +053056
57 if filters.get("group_by") == 'Item':
58 sel_col = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +053059 elif filters.get("group_by") == 'Customer':
60 sel_col = 't1.customer'
Saurabh0326f542013-06-13 19:17:56 +053061 elif filters.get("group_by") == 'Supplier':
62 sel_col = 't1.supplier'
63
64 if filters.get('based_on') in ['Item','Customer','Supplier']:
65 inc = 2
66 else :
67 inc = 1
68
Saurabh1848b712013-06-14 15:03:45 +053069 data1 = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053070 where t2.parent = t1.name and t1.company = %s
71 and t1.fiscal_year = %s and t1.docstatus = 1 %s
72 group by %s
73 """ % (query_details, tab[0], tab[1], details["sup_tab"], "%s",
74 "%s", cond, details["basedon"]), (filters.get("company"),
75 filters["fiscal_year"]),
76 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053077
78 for d in range(len(data1)):
79 #to add blanck column
80 dt = data1[d]
81 dt.insert(ind,'')
82 data.append(dt)
83
84 #to get distinct value of col specified by group_by in filter
Saurabh1848b712013-06-14 15:03:45 +053085 row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053086 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
87 and t1.docstatus = 1 and %s = %s
88 """%(sel_col, tab[0], tab[1], details["sup_tab"], "%s", "%s", details["basedon"], "%s"),
89 (filters.get("company"), filters.get("fiscal_year"), data1[d][0]),
90 as_list=1)
Saurabh1848b712013-06-14 15:03:45 +053091
Saurabh0326f542013-06-13 19:17:56 +053092 for i in range(len(row)):
Saurabh1848b712013-06-14 15:03:45 +053093 des = ['' for q in range(len(details["columns"]))]
Saurabh0326f542013-06-13 19:17:56 +053094
95 #get data for each group_by filter
Saurabh1848b712013-06-14 15:03:45 +053096 row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053097 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
98 and t1.docstatus = 1 and %s = %s and %s = %s
99 """%(sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"],
100 "%s", "%s", sel_col, "%s", details["basedon"], "%s"),
101 (filters.get("company"), filters.get("fiscal_year"), row[i][0], data1[d][0]),
102 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530103
104 des[ind] = row[i]
Saurabh1848b712013-06-14 15:03:45 +0530105 for j in range(1,len(details["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530106 des[j+inc] = row1[0][j]
107 data.append(des)
108 else:
109
110 data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530111 where t2.parent = t1.name and t1.company = %s
112 and t1.fiscal_year = %s and t1.docstatus = 1 %s
113 group by %s
114 """%(query_details, tab[0], tab[1], details["sup_tab"], "%s",
115 "%s", cond,details["basedon"]), (filters.get("company"),
116 filters.get("fiscal_year")),
117 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530118
119 return data
120
Saurabhd4f21992013-06-19 14:44:44 +0530121def get_mon(date):
122 """convert srting formated date into date and retrieve month abbrevation"""
123 return (datetime.strptime(date, '%Y-%m-%d')).strftime("%b")
124
Saurabh1848b712013-06-14 15:03:45 +0530125def period_wise_colums_query(filters, trans):
Saurabhd4f21992013-06-19 14:44:44 +0530126 from datetime import datetime
Saurabh1848b712013-06-14 15:03:45 +0530127
Saurabh0326f542013-06-13 19:17:56 +0530128 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530129 pwc = []
Saurabhbfe9fe72013-06-19 12:41:28 +0530130 ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year` where name = '%s'
131 """%filters.get("fiscal_year"))[0][0]
Saurabh1848b712013-06-14 15:03:45 +0530132
133 year_start_date = ysd.strftime('%Y-%m-%d')
134 start_month = cint(year_start_date.split('-')[1])
135
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'
140
Saurabh1848b712013-06-14 15:03:45 +0530141 if filters.get("period") == "Monthly":
Saurabh0326f542013-06-13 19:17:56 +0530142 month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
143
144 for month in range(start_month-1,len(month_name)):
145 pwc.append(month_name[month]+' (Qty):Float:120')
146 pwc.append(month_name[month]+' (Amt):Currency:120')
147
148 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530149 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
150 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530151 """%{"trans": trans_date,"mon_num": cstr(month+1)}
152
153 for month in range(0, start_month-1):
154 pwc.append(month_name[month]+' (Qty):Float:120')
155 pwc.append(month_name[month]+' (Amt):Currency:120')
156
157 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530158 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
159 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530160 """%{"trans": trans_date, "mon_num": cstr(month+1)}
161
Saurabh1848b712013-06-14 15:03:45 +0530162 elif filters.get("period") == "Quarterly":
Saurabh0326f542013-06-13 19:17:56 +0530163
164 first_qsd, second_qsd, third_qsd, fourth_qsd = year_start_date, add_months(year_start_date,3), add_months(year_start_date,6), add_months(year_start_date,9)
165 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 +0530166 bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
Saurabhd4f21992013-06-19 14:44:44 +0530167
168 pwc = [get_mon(first_qsd)+"-"+get_mon(first_qed)+" (Qty):Float:120", get_mon(first_qsd)+"-"+get_mon(first_qed)+"(Amt):Currency:120",
169 get_mon(second_qsd)+"-"+get_mon(second_qed)+" (Qty):Float:120", get_mon(second_qsd)+"-"+get_mon(second_qed)+" (Amt):Currency:120",
170 get_mon(third_qsd)+"-"+get_mon(third_qed)+" (Qty):Float:120", get_mon(third_qsd)+"-"+get_mon(third_qed)+" (Amt):Currency:120",
171 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 +0530172
Saurabh0326f542013-06-13 19:17:56 +0530173 for d in bet_dates:
174 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530175 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
176 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530177 """%{"trans": trans_date, "sd": d[0],"ed": d[1]}
178
Saurabh1848b712013-06-14 15:03:45 +0530179 elif filters.get("period") == "Half-yearly":
Saurabh0326f542013-06-13 19:17:56 +0530180
181 first_half_start = year_start_date
182 first_half_end = add_days(add_months(first_half_start,6),-1)
183 second_half_start = add_days(first_half_end,1)
184 second_half_end = add_days(add_months(second_half_start,6),-1)
185
Saurabhd4f21992013-06-19 14:44:44 +0530186 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",
187 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"]
188
Saurabh0326f542013-06-13 19:17:56 +0530189 query_details = """
Saurabh5b6d03e2013-06-19 12:34:22 +0530190 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t2.qty, NULL)),
191 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t1.grand_total, NULL)),
192 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t2.qty, NULL)),
193 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530194 """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start,
195 "she": second_half_end}
196
197 else:
Saurabh1848b712013-06-14 15:03:45 +0530198 pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530199 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
200
Saurabh1848b712013-06-14 15:03:45 +0530201 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530202 return pwc, query_details
203
Saurabh1848b712013-06-14 15:03:45 +0530204def basedon_wise_colums_query(based_on, trans):
Saurabh0326f542013-06-13 19:17:56 +0530205 sup_tab = ''
206
207 if based_on == "Item":
208 bon = ["Item:Link/Item:120", "Item Name:Data:120"]
209 query_details = "t2.item_code, t2.item_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530210 based = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +0530211
212 elif based_on == "Item Group":
213 bon = ["Item Group:Link/Item Group:120"]
214 query_details = "t2.item_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530215 based = 't2.item_group'
Saurabh0326f542013-06-13 19:17:56 +0530216
217 elif based_on == "Customer":
218 bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
219 query_details = "t1.customer_name, t1.territory, "
Saurabhbfe9fe72013-06-19 12:41:28 +0530220 based = 't1.customer_name'
Saurabh0326f542013-06-13 19:17:56 +0530221
222 elif based_on == "Customer Group":
223 bon = ["Customer Group:Link/Customer Group"]
224 query_details = "t1.customer_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530225 based = 't1.customer_group'
Saurabh0326f542013-06-13 19:17:56 +0530226
227 elif based_on == 'Supplier':
228 bon = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:120"]
229 query_details = "t1.supplier, t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530230 based = 't1.supplier'
Saurabh24208f52013-06-18 15:45:08 +0530231 sup_tab = '`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530232
233 elif based_on == 'Supplier Type':
234 bon = ["Supplier Type:Link/Supplier Type:120"]
235 query_details = "t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530236 based = 't3.supplier_type'
Saurabh24208f52013-06-18 15:45:08 +0530237 sup_tab ='`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530238
239 elif based_on == "Territory":
240 bon = ["Territory:Link/Territory:120"]
241 query_details = "t1.territory,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530242 based = 't1.territory'
Saurabh0326f542013-06-13 19:17:56 +0530243
244 elif based_on == "Project":
245
246 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
247 bon = ["Project:Link/Project:120"]
248 query_details = "t1.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530249 based = 't1.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530250
251 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
252 bon = ["Project:Link/Project:120"]
253 query_details = "t2.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530254 based = 't2.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530255
256 else:
Saurabh1848b712013-06-14 15:03:45 +0530257 webnotes.msgprint("Information Not Available", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530258
Saurabhbfe9fe72013-06-19 12:41:28 +0530259 return bon, query_details, based, sup_tab
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 []