blob: b00afe8d908458c2d71bdb23761e07b5bdbb39d5 [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
Saurabh5b6d03e2013-06-19 12:34:22 +053069 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
Saurabh5b6d03e2013-06-19 12:34:22 +053085 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
86 and t1.docstatus = 1 and %s = %s
87 """%(sel_col, tab[0], tab[1], details["sup_tab"], "%s", "%s", details["basedon"], "%s"),
88 (filters.get("company"), filters.get("fiscal_year"), data1[d][0]),
89 as_list=1)
Saurabh1848b712013-06-14 15:03:45 +053090
Saurabh0326f542013-06-13 19:17:56 +053091 for i in range(len(row)):
Saurabh1848b712013-06-14 15:03:45 +053092 des = ['' for q in range(len(details["columns"]))]
Saurabh0326f542013-06-13 19:17:56 +053093
94 #get data for each group_by filter
Saurabh1848b712013-06-14 15:03:45 +053095 row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +053096 where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s
97 and t1.docstatus = 1 and %s = %s and %s = %s
98 """%(sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"],
99 "%s", "%s", sel_col, "%s", details["basedon"], "%s"),
100 (filters.get("company"), filters.get("fiscal_year"), row[i][0], data1[d][0]),
101 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530102
103 des[ind] = row[i]
Saurabh1848b712013-06-14 15:03:45 +0530104 for j in range(1,len(details["columns"])-inc):
Saurabh0326f542013-06-13 19:17:56 +0530105 des[j+inc] = row1[0][j]
106 data.append(des)
107 else:
108
109 data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
Saurabh5b6d03e2013-06-19 12:34:22 +0530110 where t2.parent = t1.name and t1.company = %s
111 and t1.fiscal_year = %s and t1.docstatus = 1 %s
112 group by %s
113 """%(query_details, tab[0], tab[1], details["sup_tab"], "%s",
114 "%s", cond,details["basedon"]), (filters.get("company"),
115 filters.get("fiscal_year")),
116 as_list=1)
Saurabh0326f542013-06-13 19:17:56 +0530117
118 return data
119
Saurabh1848b712013-06-14 15:03:45 +0530120def period_wise_colums_query(filters, trans):
121
Saurabh0326f542013-06-13 19:17:56 +0530122 query_details = ''
Saurabh1848b712013-06-14 15:03:45 +0530123 pwc = []
124 ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year`
125 where name = '%s'
126 """%filters.get("fiscal_year"))[0][0]
127
128 year_start_date = ysd.strftime('%Y-%m-%d')
129 start_month = cint(year_start_date.split('-')[1])
130
Saurabh0326f542013-06-13 19:17:56 +0530131 if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
132 trans_date = 'posting_date'
133 else:
134 trans_date = 'transaction_date'
135
Saurabh1848b712013-06-14 15:03:45 +0530136 if filters.get("period") == "Monthly":
Saurabh0326f542013-06-13 19:17:56 +0530137 month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
138
139 for month in range(start_month-1,len(month_name)):
140 pwc.append(month_name[month]+' (Qty):Float:120')
141 pwc.append(month_name[month]+' (Amt):Currency:120')
142
143 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530144 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
145 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530146 """%{"trans": trans_date,"mon_num": cstr(month+1)}
147
148 for month in range(0, start_month-1):
149 pwc.append(month_name[month]+' (Qty):Float:120')
150 pwc.append(month_name[month]+' (Amt):Currency:120')
151
152 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530153 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
154 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530155 """%{"trans": trans_date, "mon_num": cstr(month+1)}
156
Saurabh1848b712013-06-14 15:03:45 +0530157 elif filters.get("period") == "Quarterly":
158 pwc = ["Q1 (Qty):Float:120", "Q1 (Amt):Currency:120", "Q2 (Qty):Float:120", "Q2 (Amt):Currency:120",
159 "Q3 (Qty):Float:120", "Q3 (Amt):Currency:120", "Q4 (Qty):Float:120", "Q4 (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530160
161 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)
162 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 +0530163 bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
Saurabh1848b712013-06-14 15:03:45 +0530164
Saurabh0326f542013-06-13 19:17:56 +0530165 for d in bet_dates:
166 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530167 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
168 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530169 """%{"trans": trans_date, "sd": d[0],"ed": d[1]}
170
Saurabh1848b712013-06-14 15:03:45 +0530171 elif filters.get("period") == "Half-yearly":
172 pwc = ["Fisrt Half (Qty):Float:120", "Fisrt Half (Amt):Currency:120", "Second Half (Qty):Float:120",
173 "Second Half (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530174
175 first_half_start = year_start_date
176 first_half_end = add_days(add_months(first_half_start,6),-1)
177 second_half_start = add_days(first_half_end,1)
178 second_half_end = add_days(add_months(second_half_start,6),-1)
179
180 query_details = """
Saurabh5b6d03e2013-06-19 12:34:22 +0530181 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t2.qty, NULL)),
182 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t1.grand_total, NULL)),
183 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t2.qty, NULL)),
184 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530185 """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start,
186 "she": second_half_end}
187
188 else:
Saurabh1848b712013-06-14 15:03:45 +0530189 pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530190 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
191
Saurabh1848b712013-06-14 15:03:45 +0530192 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530193 return pwc, query_details
194
Saurabh1848b712013-06-14 15:03:45 +0530195def basedon_wise_colums_query(based_on, trans):
Saurabh0326f542013-06-13 19:17:56 +0530196 sup_tab = ''
197
198 if based_on == "Item":
199 bon = ["Item:Link/Item:120", "Item Name:Data:120"]
200 query_details = "t2.item_code, t2.item_name,"
201 basedon = 't2.item_code'
202
203 elif based_on == "Item Group":
204 bon = ["Item Group:Link/Item Group:120"]
205 query_details = "t2.item_group,"
206 basedon = 't2.item_group'
207
208 elif based_on == "Customer":
209 bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
210 query_details = "t1.customer_name, t1.territory, "
211 basedon = 't1.customer_name'
212
213 elif based_on == "Customer Group":
214 bon = ["Customer Group:Link/Customer Group"]
215 query_details = "t1.customer_group,"
216 basedon = 't1.customer_group'
217
218 elif based_on == 'Supplier':
219 bon = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:120"]
220 query_details = "t1.supplier, t3.supplier_type,"
221 basedon = 't1.supplier'
Saurabh24208f52013-06-18 15:45:08 +0530222 sup_tab = '`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530223
224 elif based_on == 'Supplier Type':
225 bon = ["Supplier Type:Link/Supplier Type:120"]
226 query_details = "t3.supplier_type,"
227 basedon = 't3.supplier_type'
Saurabh24208f52013-06-18 15:45:08 +0530228 sup_tab ='`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530229
230 elif based_on == "Territory":
231 bon = ["Territory:Link/Territory:120"]
232 query_details = "t1.territory,"
233 basedon = 't1.territory'
234
235 elif based_on == "Project":
236
237 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
238 bon = ["Project:Link/Project:120"]
239 query_details = "t1.project_name,"
240 basedon = 't1.project_name'
241
242 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
243 bon = ["Project:Link/Project:120"]
244 query_details = "t2.project_name,"
245 basedon = 't2.project_name'
246
247 else:
Saurabh1848b712013-06-14 15:03:45 +0530248 webnotes.msgprint("Information Not Available", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530249
250 return bon, query_details, basedon, sup_tab
251
Saurabh1848b712013-06-14 15:03:45 +0530252def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530253 if group_by:
254 return [group_by+":Link/"+group_by+":120"]
255 else:
256 return []