blob: 8905591193e117aa96ca6997b55488339b4bcd29 [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
20
Saurabh1848b712013-06-14 15:03:45 +053021def get_columns(filters, trans):
Saurabh0326f542013-06-13 19:17:56 +053022
Saurabh1848b712013-06-14 15:03:45 +053023 if not (filters.get("period") and filters.get("based_on")):
Saurabh0326f542013-06-13 19:17:56 +053024 webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
25
Saurabh1848b712013-06-14 15:03:45 +053026 elif filters.get("based_on") == filters.get("group_by"):
Saurabh0326f542013-06-13 19:17:56 +053027 webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1)
28
29 else:
Saurabh1848b712013-06-14 15:03:45 +053030 bon, query_bon, basedon, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
31 pwc, query_pwc = period_wise_colums_query(filters, trans)
32 grbc = group_wise_column(filters.get("group_by"))
33
Saurabh0326f542013-06-13 19:17:56 +053034 columns = bon + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh1848b712013-06-14 15:03:45 +053035 if grbc:
36 columns = bon + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053037
Saurabh1848b712013-06-14 15:03:45 +053038 details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": basedon,
39 "grbc": grbc, "sup_tab": sup_tab}
Saurabh0326f542013-06-13 19:17:56 +053040
Saurabh1848b712013-06-14 15:03:45 +053041 return details
42
43def get_data(filters, tab, details):
Saurabh0326f542013-06-13 19:17:56 +053044
Saurabh0326f542013-06-13 19:17:56 +053045 data = []
46 inc, cond= '',''
Saurabh1848b712013-06-14 15:03:45 +053047 query_details = details["query_bon"] + details["query_pwc"]
Saurabh0326f542013-06-13 19:17:56 +053048
Saurabh1848b712013-06-14 15:03:45 +053049 if details["query_bon"] in ["t1.project_name,", "t2.project_name,"]:
50 cond = 'and '+ details["query_bon"][:-1] +' IS Not NULL'
Saurabh0326f542013-06-13 19:17:56 +053051
52 if filters.get("group_by"):
53 sel_col = ''
Saurabh1848b712013-06-14 15:03:45 +053054 ind = details["columns"].index(details["grbc"][0])
Saurabh0326f542013-06-13 19:17:56 +053055
56 if filters.get("group_by") == 'Item':
57 sel_col = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +053058 elif filters.get("group_by") == 'Customer':
59 sel_col = 't1.customer'
Saurabh0326f542013-06-13 19:17:56 +053060 elif filters.get("group_by") == 'Supplier':
61 sel_col = 't1.supplier'
62
63 if filters.get('based_on') in ['Item','Customer','Supplier']:
64 inc = 2
65 else :
66 inc = 1
67
Saurabh1848b712013-06-14 15:03:45 +053068 data1 = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
69 where t2.parent = t1.name and t1.company = %s
70 and t1.fiscal_year = %s and t1.docstatus = 1 %s
71 group by %s
72 """ % (query_details, tab[0], tab[1], details["sup_tab"], "%s",
73 "%s", cond, details["basedon"]), (filters.get("company"),
74 filters["fiscal_year"]),
75 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +053076
77 for d in range(len(data1)):
78 #to add blanck column
79 dt = data1[d]
80 dt.insert(ind,'')
81 data.append(dt)
82
83 #to get distinct value of col specified by group_by in filter
Saurabh1848b712013-06-14 15:03:45 +053084 row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2 %s
85 where t2.parent = t1.name and t1.company = %s
Saurabh24208f52013-06-18 15:45:08 +053086 and t1.fiscal_year = %s and t1.docstatus = 1
Saurabh1848b712013-06-14 15:03:45 +053087 and %s = %s
88 """%(sel_col, tab[0], tab[1], details["sup_tab"], "%s",
89 "%s", details["basedon"], "%s"),
90 (filters.get("company"), filters.get("fiscal_year"),
91 data1[d][0]),
92 as_list=1)
93
Saurabh0326f542013-06-13 19:17:56 +053094 for i in range(len(row)):
Saurabh1848b712013-06-14 15:03:45 +053095 des = ['' for q in range(len(details["columns"]))]
Saurabh0326f542013-06-13 19:17:56 +053096
97 #get data for each group_by filter
Saurabh1848b712013-06-14 15:03:45 +053098 row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s
99 where t2.parent = t1.name and t1.company = %s
100 and t1.fiscal_year = %s and t1.docstatus = 1
101 and %s = %s and %s = %s
102 """%(sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"],
103 "%s", "%s", sel_col, "%s", details["basedon"], "%s"),
104 (filters.get("company"), filters.get("fiscal_year"), row[i][0],
105 data1[d][0]),
106 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530107
108 des[ind] = row[i]
Saurabh1848b712013-06-14 15:03:45 +0530109 for j in range(1,len(details["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530110 des[j+inc] = row1[0][j]
111 data.append(des)
112 else:
113
114 data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh1848b712013-06-14 15:03:45 +0530115 where t2.parent = t1.name and t1.company = %s
116 and t1.fiscal_year = %s and t1.docstatus = 1
117 %s group by %s
118 """%(query_details, tab[0], tab[1], details["sup_tab"], "%s",
119 "%s", cond,details["basedon"]), (filters.get("company"),
120 filters.get("fiscal_year")),
121 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530122
123 return data
124
Saurabh1848b712013-06-14 15:03:45 +0530125def period_wise_colums_query(filters, trans):
126
Saurabh0326f542013-06-13 19:17:56 +0530127 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530128 pwc = []
129 ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year`
130 where name = '%s'
131 """%filters.get("fiscal_year"))[0][0]
132
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 += """
149 Sum(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t2.qty ELSE NULL END),
150 SUM(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t1.grand_total ELSE NULL END),
151 """%{"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 += """
158 Sum(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t2.qty ELSE NULL END),
159 SUM(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t1.grand_total ELSE NULL END),
160 """%{"trans": trans_date, "mon_num": cstr(month+1)}
161
Saurabh1848b712013-06-14 15:03:45 +0530162 elif filters.get("period") == "Quarterly":
163 pwc = ["Q1 (Qty):Float:120", "Q1 (Amt):Currency:120", "Q2 (Qty):Float:120", "Q2 (Amt):Currency:120",
164 "Q3 (Qty):Float:120", "Q3 (Amt):Currency:120", "Q4 (Qty):Float:120", "Q4 (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530165
166 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)
167 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 +0530168 bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
Saurabh1848b712013-06-14 15:03:45 +0530169
Saurabh0326f542013-06-13 19:17:56 +0530170 for d in bet_dates:
171 query_details += """
172 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s' THEN t2.qty ELSE NULL END),
173 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s' THEN t1.grand_total ELSE NULL END),
174 """%{"trans": trans_date, "sd": d[0],"ed": d[1]}
175
Saurabh1848b712013-06-14 15:03:45 +0530176 elif filters.get("period") == "Half-yearly":
177 pwc = ["Fisrt Half (Qty):Float:120", "Fisrt Half (Amt):Currency:120", "Second Half (Qty):Float:120",
178 "Second Half (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530179
180 first_half_start = year_start_date
181 first_half_end = add_days(add_months(first_half_start,6),-1)
182 second_half_start = add_days(first_half_end,1)
183 second_half_end = add_days(add_months(second_half_start,6),-1)
184
185 query_details = """
186 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t2.qty ELSE NULL END),
187 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t1.grand_total ELSE NULL END),
188 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s' THEN t2.qty ELSE NULL END),
189 SUM(CASE WHEN t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s' THEN t1.grand_total ELSE NULL END),
190 """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start,
191 "she": second_half_end}
192
193 else:
Saurabh1848b712013-06-14 15:03:45 +0530194 pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530195 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
196
Saurabh1848b712013-06-14 15:03:45 +0530197 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530198 return pwc, query_details
199
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,"
206 basedon = 't2.item_code'
207
208 elif based_on == "Item Group":
209 bon = ["Item Group:Link/Item Group:120"]
210 query_details = "t2.item_group,"
211 basedon = 't2.item_group'
212
213 elif based_on == "Customer":
214 bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
215 query_details = "t1.customer_name, t1.territory, "
216 basedon = 't1.customer_name'
217
218 elif based_on == "Customer Group":
219 bon = ["Customer Group:Link/Customer Group"]
220 query_details = "t1.customer_group,"
221 basedon = 't1.customer_group'
222
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,"
226 basedon = '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,"
232 basedon = '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,"
238 basedon = 't1.territory'
239
240 elif based_on == "Project":
241
242 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
243 bon = ["Project:Link/Project:120"]
244 query_details = "t1.project_name,"
245 basedon = 't1.project_name'
246
247 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
248 bon = ["Project:Link/Project:120"]
249 query_details = "t2.project_name,"
250 basedon = 't2.project_name'
251
252 else:
Saurabh1848b712013-06-14 15:03:45 +0530253 webnotes.msgprint("Information Not Available", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530254
255 return bon, query_details, basedon, sup_tab
256
Saurabh1848b712013-06-14 15:03:45 +0530257def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530258 if group_by:
259 return [group_by+":Link/"+group_by+":120"]
260 else:
261 return []