blob: 76db44705f665f10c261dde4492a664be2f9c4b9 [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:
Saurabhbfe9fe72013-06-19 12:41:28 +053030 bonc, query_bon, based, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
Saurabh1848b712013-06-14 15:03:45 +053031 pwc, query_pwc = period_wise_colums_query(filters, trans)
32 grbc = group_wise_column(filters.get("group_by"))
33
Saurabhbfe9fe72013-06-19 12:41:28 +053034 columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh1848b712013-06-14 15:03:45 +053035 if grbc:
Saurabhbfe9fe72013-06-19 12:41:28 +053036 columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +053037
Saurabhbfe9fe72013-06-19 12:41:28 +053038 details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": based,
Saurabh1848b712013-06-14 15:03:45 +053039 "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 = []
Saurabhbfe9fe72013-06-19 12:41:28 +0530124 ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year` where name = '%s'
125 """%filters.get("fiscal_year"))[0][0]
Saurabh1848b712013-06-14 15:03:45 +0530126
127 year_start_date = ysd.strftime('%Y-%m-%d')
128 start_month = cint(year_start_date.split('-')[1])
129
Saurabh0326f542013-06-13 19:17:56 +0530130 if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
131 trans_date = 'posting_date'
132 else:
133 trans_date = 'transaction_date'
134
Saurabh1848b712013-06-14 15:03:45 +0530135 if filters.get("period") == "Monthly":
Saurabh0326f542013-06-13 19:17:56 +0530136 month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
137
138 for month in range(start_month-1,len(month_name)):
139 pwc.append(month_name[month]+' (Qty):Float:120')
140 pwc.append(month_name[month]+' (Amt):Currency:120')
141
142 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530143 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
144 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530145 """%{"trans": trans_date,"mon_num": cstr(month+1)}
146
147 for month in range(0, start_month-1):
148 pwc.append(month_name[month]+' (Qty):Float:120')
149 pwc.append(month_name[month]+' (Amt):Currency:120')
150
151 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530152 Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
153 SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530154 """%{"trans": trans_date, "mon_num": cstr(month+1)}
155
Saurabh1848b712013-06-14 15:03:45 +0530156 elif filters.get("period") == "Quarterly":
157 pwc = ["Q1 (Qty):Float:120", "Q1 (Amt):Currency:120", "Q2 (Qty):Float:120", "Q2 (Amt):Currency:120",
158 "Q3 (Qty):Float:120", "Q3 (Amt):Currency:120", "Q4 (Qty):Float:120", "Q4 (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530159
160 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)
161 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 +0530162 bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
Saurabh1848b712013-06-14 15:03:45 +0530163
Saurabh0326f542013-06-13 19:17:56 +0530164 for d in bet_dates:
165 query_details += """
Saurabh5b6d03e2013-06-19 12:34:22 +0530166 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)),
167 SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530168 """%{"trans": trans_date, "sd": d[0],"ed": d[1]}
169
Saurabh1848b712013-06-14 15:03:45 +0530170 elif filters.get("period") == "Half-yearly":
171 pwc = ["Fisrt Half (Qty):Float:120", "Fisrt Half (Amt):Currency:120", "Second Half (Qty):Float:120",
172 "Second Half (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530173
174 first_half_start = year_start_date
175 first_half_end = add_days(add_months(first_half_start,6),-1)
176 second_half_start = add_days(first_half_end,1)
177 second_half_end = add_days(add_months(second_half_start,6),-1)
178
179 query_details = """
Saurabh5b6d03e2013-06-19 12:34:22 +0530180 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t2.qty, NULL)),
181 SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t1.grand_total, NULL)),
182 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t2.qty, NULL)),
183 SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t1.grand_total, NULL)),
Saurabh0326f542013-06-13 19:17:56 +0530184 """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start,
185 "she": second_half_end}
186
187 else:
Saurabh1848b712013-06-14 15:03:45 +0530188 pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
Saurabh0326f542013-06-13 19:17:56 +0530189 query_details = " SUM(t2.qty), SUM(t1.grand_total),"
190
Saurabh1848b712013-06-14 15:03:45 +0530191 query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
Saurabh0326f542013-06-13 19:17:56 +0530192 return pwc, query_details
193
Saurabh1848b712013-06-14 15:03:45 +0530194def basedon_wise_colums_query(based_on, trans):
Saurabh0326f542013-06-13 19:17:56 +0530195 sup_tab = ''
196
197 if based_on == "Item":
198 bon = ["Item:Link/Item:120", "Item Name:Data:120"]
199 query_details = "t2.item_code, t2.item_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530200 based = 't2.item_code'
Saurabh0326f542013-06-13 19:17:56 +0530201
202 elif based_on == "Item Group":
203 bon = ["Item Group:Link/Item Group:120"]
204 query_details = "t2.item_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530205 based = 't2.item_group'
Saurabh0326f542013-06-13 19:17:56 +0530206
207 elif based_on == "Customer":
208 bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
209 query_details = "t1.customer_name, t1.territory, "
Saurabhbfe9fe72013-06-19 12:41:28 +0530210 based = 't1.customer_name'
Saurabh0326f542013-06-13 19:17:56 +0530211
212 elif based_on == "Customer Group":
213 bon = ["Customer Group:Link/Customer Group"]
214 query_details = "t1.customer_group,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530215 based = 't1.customer_group'
Saurabh0326f542013-06-13 19:17:56 +0530216
217 elif based_on == 'Supplier':
218 bon = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:120"]
219 query_details = "t1.supplier, t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530220 based = 't1.supplier'
Saurabh24208f52013-06-18 15:45:08 +0530221 sup_tab = '`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530222
223 elif based_on == 'Supplier Type':
224 bon = ["Supplier Type:Link/Supplier Type:120"]
225 query_details = "t3.supplier_type,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530226 based = 't3.supplier_type'
Saurabh24208f52013-06-18 15:45:08 +0530227 sup_tab ='`tabSupplier` t3',
Saurabh0326f542013-06-13 19:17:56 +0530228
229 elif based_on == "Territory":
230 bon = ["Territory:Link/Territory:120"]
231 query_details = "t1.territory,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530232 based = 't1.territory'
Saurabh0326f542013-06-13 19:17:56 +0530233
234 elif based_on == "Project":
235
236 if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
237 bon = ["Project:Link/Project:120"]
238 query_details = "t1.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530239 based = 't1.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530240
241 elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
242 bon = ["Project:Link/Project:120"]
243 query_details = "t2.project_name,"
Saurabhbfe9fe72013-06-19 12:41:28 +0530244 based = 't2.project_name'
Saurabh0326f542013-06-13 19:17:56 +0530245
246 else:
Saurabh1848b712013-06-14 15:03:45 +0530247 webnotes.msgprint("Information Not Available", raise_exception=1)
Saurabh0326f542013-06-13 19:17:56 +0530248
Saurabhbfe9fe72013-06-19 12:41:28 +0530249 return bon, query_details, based, sup_tab
Saurabh0326f542013-06-13 19:17:56 +0530250
Saurabh1848b712013-06-14 15:03:45 +0530251def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530252 if group_by:
253 return [group_by+":Link/"+group_by+":120"]
254 else:
255 return []