##// END OF EJS Templates
CSV export added to timelog report (#1009)....
Jean-Philippe Lang -
r1323:5d3454853956
parent child
Show More
@@ -1,242 +1,245
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 layout 'base'
19 layout 'base'
20 menu_item :issues
20 menu_item :issues
21 before_filter :find_project, :authorize
21 before_filter :find_project, :authorize
22
22
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :issues
27 helper :issues
28 include TimelogHelper
28 include TimelogHelper
29
29
30 def report
30 def report
31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
32 :klass => Project,
32 :klass => Project,
33 :label => :label_project},
33 :label => :label_project},
34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
35 :klass => Version,
35 :klass => Version,
36 :label => :label_version},
36 :label => :label_version},
37 'category' => {:sql => "#{Issue.table_name}.category_id",
37 'category' => {:sql => "#{Issue.table_name}.category_id",
38 :klass => IssueCategory,
38 :klass => IssueCategory,
39 :label => :field_category},
39 :label => :field_category},
40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
41 :klass => User,
41 :klass => User,
42 :label => :label_member},
42 :label => :label_member},
43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
44 :klass => Tracker,
44 :klass => Tracker,
45 :label => :label_tracker},
45 :label => :label_tracker},
46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
47 :klass => Enumeration,
47 :klass => Enumeration,
48 :label => :label_activity},
48 :label => :label_activity},
49 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
49 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
50 :klass => Issue,
50 :klass => Issue,
51 :label => :label_issue}
51 :label => :label_issue}
52 }
52 }
53
53
54 @criterias = params[:criterias] || []
54 @criterias = params[:criterias] || []
55 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
55 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
56 @criterias.uniq!
56 @criterias.uniq!
57 @criterias = @criterias[0,3]
57 @criterias = @criterias[0,3]
58
58
59 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
59 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
60
60
61 retrieve_date_range
61 retrieve_date_range
62
62
63 unless @criterias.empty?
63 unless @criterias.empty?
64 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
64 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
65 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
65 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
66
66
67 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
67 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
68 sql << " FROM #{TimeEntry.table_name}"
68 sql << " FROM #{TimeEntry.table_name}"
69 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
69 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
70 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
70 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
71 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
71 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
72 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
72 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
73 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
73 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
74 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
74 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
75
75
76 @hours = ActiveRecord::Base.connection.select_all(sql)
76 @hours = ActiveRecord::Base.connection.select_all(sql)
77
77
78 @hours.each do |row|
78 @hours.each do |row|
79 case @columns
79 case @columns
80 when 'year'
80 when 'year'
81 row['year'] = row['tyear']
81 row['year'] = row['tyear']
82 when 'month'
82 when 'month'
83 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
83 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
84 when 'week'
84 when 'week'
85 row['week'] = "#{row['tyear']}-#{row['tweek']}"
85 row['week'] = "#{row['tyear']}-#{row['tweek']}"
86 when 'day'
86 when 'day'
87 row['day'] = "#{row['spent_on']}"
87 row['day'] = "#{row['spent_on']}"
88 end
88 end
89 end
89 end
90
90
91 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
91 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
92
92
93 @periods = []
93 @periods = []
94 # Date#at_beginning_of_ not supported in Rails 1.2.x
94 # Date#at_beginning_of_ not supported in Rails 1.2.x
95 date_from = @from.to_time
95 date_from = @from.to_time
96 # 100 columns max
96 # 100 columns max
97 while date_from <= @to.to_time && @periods.length < 100
97 while date_from <= @to.to_time && @periods.length < 100
98 case @columns
98 case @columns
99 when 'year'
99 when 'year'
100 @periods << "#{date_from.year}"
100 @periods << "#{date_from.year}"
101 date_from = (date_from + 1.year).at_beginning_of_year
101 date_from = (date_from + 1.year).at_beginning_of_year
102 when 'month'
102 when 'month'
103 @periods << "#{date_from.year}-#{date_from.month}"
103 @periods << "#{date_from.year}-#{date_from.month}"
104 date_from = (date_from + 1.month).at_beginning_of_month
104 date_from = (date_from + 1.month).at_beginning_of_month
105 when 'week'
105 when 'week'
106 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
106 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
107 date_from = (date_from + 7.day).at_beginning_of_week
107 date_from = (date_from + 7.day).at_beginning_of_week
108 when 'day'
108 when 'day'
109 @periods << "#{date_from.to_date}"
109 @periods << "#{date_from.to_date}"
110 date_from = date_from + 1.day
110 date_from = date_from + 1.day
111 end
111 end
112 end
112 end
113 end
113 end
114
114
115 render :layout => false if request.xhr?
115 respond_to do |format|
116 format.html { render :layout => !request.xhr? }
117 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
118 end
116 end
119 end
117
120
118 def details
121 def details
119 sort_init 'spent_on', 'desc'
122 sort_init 'spent_on', 'desc'
120 sort_update
123 sort_update
121
124
122 cond = ARCondition.new
125 cond = ARCondition.new
123 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
126 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
124 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
127 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
125
128
126 retrieve_date_range
129 retrieve_date_range
127 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
130 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
128
131
129 TimeEntry.visible_by(User.current) do
132 TimeEntry.visible_by(User.current) do
130 respond_to do |format|
133 respond_to do |format|
131 format.html {
134 format.html {
132 # Paginate results
135 # Paginate results
133 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
136 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
134 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
137 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
135 @entries = TimeEntry.find(:all,
138 @entries = TimeEntry.find(:all,
136 :include => [:project, :activity, :user, {:issue => :tracker}],
139 :include => [:project, :activity, :user, {:issue => :tracker}],
137 :conditions => cond.conditions,
140 :conditions => cond.conditions,
138 :order => sort_clause,
141 :order => sort_clause,
139 :limit => @entry_pages.items_per_page,
142 :limit => @entry_pages.items_per_page,
140 :offset => @entry_pages.current.offset)
143 :offset => @entry_pages.current.offset)
141 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
144 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
142
145
143 render :layout => !request.xhr?
146 render :layout => !request.xhr?
144 }
147 }
145 format.csv {
148 format.csv {
146 # Export all entries
149 # Export all entries
147 @entries = TimeEntry.find(:all,
150 @entries = TimeEntry.find(:all,
148 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
151 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
149 :conditions => cond.conditions,
152 :conditions => cond.conditions,
150 :order => sort_clause)
153 :order => sort_clause)
151 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
154 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
152 }
155 }
153 end
156 end
154 end
157 end
155 end
158 end
156
159
157 def edit
160 def edit
158 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
161 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
159 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
162 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
160 @time_entry.attributes = params[:time_entry]
163 @time_entry.attributes = params[:time_entry]
161 if request.post? and @time_entry.save
164 if request.post? and @time_entry.save
162 flash[:notice] = l(:notice_successful_update)
165 flash[:notice] = l(:notice_successful_update)
163 redirect_to :action => 'details', :project_id => @time_entry.project
166 redirect_to :action => 'details', :project_id => @time_entry.project
164 return
167 return
165 end
168 end
166 @activities = Enumeration::get_values('ACTI')
169 @activities = Enumeration::get_values('ACTI')
167 end
170 end
168
171
169 def destroy
172 def destroy
170 render_404 and return unless @time_entry
173 render_404 and return unless @time_entry
171 render_403 and return unless @time_entry.editable_by?(User.current)
174 render_403 and return unless @time_entry.editable_by?(User.current)
172 @time_entry.destroy
175 @time_entry.destroy
173 flash[:notice] = l(:notice_successful_delete)
176 flash[:notice] = l(:notice_successful_delete)
174 redirect_to :back
177 redirect_to :back
175 rescue RedirectBackError
178 rescue RedirectBackError
176 redirect_to :action => 'details', :project_id => @time_entry.project
179 redirect_to :action => 'details', :project_id => @time_entry.project
177 end
180 end
178
181
179 private
182 private
180 def find_project
183 def find_project
181 if params[:id]
184 if params[:id]
182 @time_entry = TimeEntry.find(params[:id])
185 @time_entry = TimeEntry.find(params[:id])
183 @project = @time_entry.project
186 @project = @time_entry.project
184 elsif params[:issue_id]
187 elsif params[:issue_id]
185 @issue = Issue.find(params[:issue_id])
188 @issue = Issue.find(params[:issue_id])
186 @project = @issue.project
189 @project = @issue.project
187 elsif params[:project_id]
190 elsif params[:project_id]
188 @project = Project.find(params[:project_id])
191 @project = Project.find(params[:project_id])
189 else
192 else
190 render_404
193 render_404
191 return false
194 return false
192 end
195 end
193 rescue ActiveRecord::RecordNotFound
196 rescue ActiveRecord::RecordNotFound
194 render_404
197 render_404
195 end
198 end
196
199
197 # Retrieves the date range based on predefined ranges or specific from/to param dates
200 # Retrieves the date range based on predefined ranges or specific from/to param dates
198 def retrieve_date_range
201 def retrieve_date_range
199 @free_period = false
202 @free_period = false
200 @from, @to = nil, nil
203 @from, @to = nil, nil
201
204
202 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
205 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
203 case params[:period].to_s
206 case params[:period].to_s
204 when 'today'
207 when 'today'
205 @from = @to = Date.today
208 @from = @to = Date.today
206 when 'yesterday'
209 when 'yesterday'
207 @from = @to = Date.today - 1
210 @from = @to = Date.today - 1
208 when 'current_week'
211 when 'current_week'
209 @from = Date.today - (Date.today.cwday - 1)%7
212 @from = Date.today - (Date.today.cwday - 1)%7
210 @to = @from + 6
213 @to = @from + 6
211 when 'last_week'
214 when 'last_week'
212 @from = Date.today - 7 - (Date.today.cwday - 1)%7
215 @from = Date.today - 7 - (Date.today.cwday - 1)%7
213 @to = @from + 6
216 @to = @from + 6
214 when '7_days'
217 when '7_days'
215 @from = Date.today - 7
218 @from = Date.today - 7
216 @to = Date.today
219 @to = Date.today
217 when 'current_month'
220 when 'current_month'
218 @from = Date.civil(Date.today.year, Date.today.month, 1)
221 @from = Date.civil(Date.today.year, Date.today.month, 1)
219 @to = (@from >> 1) - 1
222 @to = (@from >> 1) - 1
220 when 'last_month'
223 when 'last_month'
221 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
224 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
222 @to = (@from >> 1) - 1
225 @to = (@from >> 1) - 1
223 when '30_days'
226 when '30_days'
224 @from = Date.today - 30
227 @from = Date.today - 30
225 @to = Date.today
228 @to = Date.today
226 when 'current_year'
229 when 'current_year'
227 @from = Date.civil(Date.today.year, 1, 1)
230 @from = Date.civil(Date.today.year, 1, 1)
228 @to = Date.civil(Date.today.year, 12, 31)
231 @to = Date.civil(Date.today.year, 12, 31)
229 end
232 end
230 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
233 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
231 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
234 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
232 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
235 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
233 @free_period = true
236 @free_period = true
234 else
237 else
235 # default
238 # default
236 end
239 end
237
240
238 @from, @to = @to, @from if @from && @to && @from > @to
241 @from, @to = @to, @from if @from && @to && @from > @to
239 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
242 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
240 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
243 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
241 end
244 end
242 end
245 end
@@ -1,79 +1,131
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module TimelogHelper
18 module TimelogHelper
19 def select_hours(data, criteria, value)
19 def select_hours(data, criteria, value)
20 data.select {|row| row[criteria] == value}
20 data.select {|row| row[criteria] == value}
21 end
21 end
22
22
23 def sum_hours(data)
23 def sum_hours(data)
24 sum = 0
24 sum = 0
25 data.each do |row|
25 data.each do |row|
26 sum += row['hours'].to_f
26 sum += row['hours'].to_f
27 end
27 end
28 sum
28 sum
29 end
29 end
30
30
31 def options_for_period_select(value)
31 def options_for_period_select(value)
32 options_for_select([[l(:label_all_time), 'all'],
32 options_for_select([[l(:label_all_time), 'all'],
33 [l(:label_today), 'today'],
33 [l(:label_today), 'today'],
34 [l(:label_yesterday), 'yesterday'],
34 [l(:label_yesterday), 'yesterday'],
35 [l(:label_this_week), 'current_week'],
35 [l(:label_this_week), 'current_week'],
36 [l(:label_last_week), 'last_week'],
36 [l(:label_last_week), 'last_week'],
37 [l(:label_last_n_days, 7), '7_days'],
37 [l(:label_last_n_days, 7), '7_days'],
38 [l(:label_this_month), 'current_month'],
38 [l(:label_this_month), 'current_month'],
39 [l(:label_last_month), 'last_month'],
39 [l(:label_last_month), 'last_month'],
40 [l(:label_last_n_days, 30), '30_days'],
40 [l(:label_last_n_days, 30), '30_days'],
41 [l(:label_this_year), 'current_year']],
41 [l(:label_this_year), 'current_year']],
42 value)
42 value)
43 end
43 end
44
44
45 def entries_to_csv(entries)
45 def entries_to_csv(entries)
46 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
46 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
47 export = StringIO.new
47 export = StringIO.new
48 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
48 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
49 # csv header fields
49 # csv header fields
50 headers = [l(:field_spent_on),
50 headers = [l(:field_spent_on),
51 l(:field_user),
51 l(:field_user),
52 l(:field_activity),
52 l(:field_activity),
53 l(:field_project),
53 l(:field_project),
54 l(:field_issue),
54 l(:field_issue),
55 l(:field_tracker),
55 l(:field_tracker),
56 l(:field_subject),
56 l(:field_subject),
57 l(:field_hours),
57 l(:field_hours),
58 l(:field_comments)
58 l(:field_comments)
59 ]
59 ]
60 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
60 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
61 # csv lines
61 # csv lines
62 entries.each do |entry|
62 entries.each do |entry|
63 fields = [l_date(entry.spent_on),
63 fields = [l_date(entry.spent_on),
64 entry.user,
64 entry.user,
65 entry.activity,
65 entry.activity,
66 entry.project,
66 entry.project,
67 (entry.issue ? entry.issue.id : nil),
67 (entry.issue ? entry.issue.id : nil),
68 (entry.issue ? entry.issue.tracker : nil),
68 (entry.issue ? entry.issue.tracker : nil),
69 (entry.issue ? entry.issue.subject : nil),
69 (entry.issue ? entry.issue.subject : nil),
70 entry.hours,
70 entry.hours,
71 entry.comments
71 entry.comments
72 ]
72 ]
73 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
73 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
74 end
74 end
75 end
75 end
76 export.rewind
76 export.rewind
77 export
77 export
78 end
78 end
79
80 def report_to_csv(criterias, periods, hours)
81 export = StringIO.new
82 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
83 # Column headers
84 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
85 headers += periods
86 headers << l(:label_total)
87 csv << headers.collect {|c| to_utf8(c) }
88 # Content
89 report_criteria_to_csv(csv, criterias, periods, hours)
90 # Total row
91 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
92 total = 0
93 periods.each do |period|
94 sum = sum_hours(select_hours(hours, @columns, period.to_s))
95 total += sum
96 row << (sum > 0 ? "%.2f" % sum : '')
97 end
98 row << "%.2f" %total
99 csv << row
100 end
101 export.rewind
102 export
103 end
104
105 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
106 hours.collect {|h| h[criterias[level]]}.uniq.each do |value|
107 hours_for_value = select_hours(hours, criterias[level], value)
108 next if hours_for_value.empty?
109 row = [''] * level
110 row << to_utf8(value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value))
111 row += [''] * (criterias.length - level - 1)
112 total = 0
113 periods.each do |period|
114 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
115 total += sum
116 row << (sum > 0 ? "%.2f" % sum : '')
117 end
118 row << "%.2f" %total
119 csv << row
120
121 if criterias.length > level + 1
122 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
123 end
124 end
125 end
126
127 def to_utf8(s)
128 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
129 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
130 end
79 end
131 end
@@ -1,67 +1,72
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
3 </div>
3 </div>
4
4
5 <h2><%= l(:label_spent_time) %></h2>
5 <h2><%= l(:label_spent_time) %></h2>
6
6
7 <% form_remote_tag(:url => {}, :update => 'content') do %>
7 <% form_remote_tag(:url => {}, :update => 'content') do %>
8 <% @criterias.each do |criteria| %>
8 <% @criterias.each do |criteria| %>
9 <%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
9 <%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
10 <% end %>
10 <% end %>
11 <%= hidden_field_tag 'project_id', params[:project_id] %>
11 <%= hidden_field_tag 'project_id', params[:project_id] %>
12 <%= render :partial => 'date_range' %>
12 <%= render :partial => 'date_range' %>
13
13
14 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
14 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
15 [l(:label_month), 'month'],
15 [l(:label_month), 'month'],
16 [l(:label_week), 'week'],
16 [l(:label_week), 'week'],
17 [l(:label_day_plural).titleize, 'day']], @columns),
17 [l(:label_day_plural).titleize, 'day']], @columns),
18 :onchange => "this.form.onsubmit();" %>
18 :onchange => "this.form.onsubmit();" %>
19
19
20 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
20 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
21 :onchange => "this.form.onsubmit();",
21 :onchange => "this.form.onsubmit();",
22 :style => 'width: 200px',
22 :style => 'width: 200px',
23 :id => nil,
23 :id => nil,
24 :disabled => (@criterias.length >= 3)) %>
24 :disabled => (@criterias.length >= 3)) %>
25 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
25 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
26 :update => 'content'
26 :update => 'content'
27 }, :class => 'icon icon-reload' %></p>
27 }, :class => 'icon icon-reload' %></p>
28 <% end %>
28 <% end %>
29
29
30 <% unless @criterias.empty? %>
30 <% unless @criterias.empty? %>
31 <div class="total-hours">
31 <div class="total-hours">
32 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
32 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
33 </div>
33 </div>
34
34
35 <% unless @hours.empty? %>
35 <% unless @hours.empty? %>
36 <table class="list" id="time-report">
36 <table class="list" id="time-report">
37 <thead>
37 <thead>
38 <tr>
38 <tr>
39 <% @criterias.each do |criteria| %>
39 <% @criterias.each do |criteria| %>
40 <th><%= l(@available_criterias[criteria][:label]) %></th>
40 <th><%= l(@available_criterias[criteria][:label]) %></th>
41 <% end %>
41 <% end %>
42 <% columns_width = (40 / (@periods.length+1)).to_i %>
42 <% columns_width = (40 / (@periods.length+1)).to_i %>
43 <% @periods.each do |period| %>
43 <% @periods.each do |period| %>
44 <th class="period" width="<%= columns_width %>%"><%= period %></th>
44 <th class="period" width="<%= columns_width %>%"><%= period %></th>
45 <% end %>
45 <% end %>
46 <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
46 <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
47 </tr>
47 </tr>
48 </thead>
48 </thead>
49 <tbody>
49 <tbody>
50 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
50 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
51 <tr class="total">
51 <tr class="total">
52 <td><%= l(:label_total) %></td>
52 <td><%= l(:label_total) %></td>
53 <%= '<td></td>' * (@criterias.size - 1) %>
53 <%= '<td></td>' * (@criterias.size - 1) %>
54 <% total = 0 -%>
54 <% total = 0 -%>
55 <% @periods.each do |period| -%>
55 <% @periods.each do |period| -%>
56 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
56 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
57 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
57 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
58 <% end -%>
58 <% end -%>
59 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
59 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
60 </tr>
60 </tr>
61 </tbody>
61 </tbody>
62 </table>
62 </table>
63
64 <p class="other-formats">
65 <%= l(:label_export_to) %>
66 <span><%= link_to 'CSV', params.merge({:format => 'csv'}), :class => 'csv' %></span>
67 </p>
63 <% end %>
68 <% end %>
64 <% end %>
69 <% end %>
65
70
66 <% html_title l(:label_spent_time), l(:label_report) %>
71 <% html_title l(:label_spent_time), l(:label_report) %>
67
72
@@ -1,180 +1,191
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'timelog_controller'
19 require 'timelog_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class TimelogController; def rescue_action(e) raise e end; end
22 class TimelogController; def rescue_action(e) raise e end; end
23
23
24 class TimelogControllerTest < Test::Unit::TestCase
24 class TimelogControllerTest < Test::Unit::TestCase
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
26
26
27 def setup
27 def setup
28 @controller = TimelogController.new
28 @controller = TimelogController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 end
31 end
32
32
33 def test_create
33 def test_create
34 @request.session[:user_id] = 3
34 @request.session[:user_id] = 3
35 post :edit, :project_id => 1,
35 post :edit, :project_id => 1,
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
37 :activity_id => '10',
37 :activity_id => '10',
38 :spent_on => '2008-03-14',
38 :spent_on => '2008-03-14',
39 :issue_id => '1',
39 :issue_id => '1',
40 :hours => '7.3'}
40 :hours => '7.3'}
41 assert_redirected_to 'projects/ecookbook/timelog/details'
41 assert_redirected_to 'projects/ecookbook/timelog/details'
42
42
43 i = Issue.find(1)
43 i = Issue.find(1)
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
45 assert_not_nil t
45 assert_not_nil t
46 assert_equal 7.3, t.hours
46 assert_equal 7.3, t.hours
47 assert_equal 3, t.user_id
47 assert_equal 3, t.user_id
48 assert_equal i, t.issue
48 assert_equal i, t.issue
49 assert_equal i.project, t.project
49 assert_equal i.project, t.project
50 end
50 end
51
51
52 def test_update
52 def test_update
53 entry = TimeEntry.find(1)
53 entry = TimeEntry.find(1)
54 assert_equal 1, entry.issue_id
54 assert_equal 1, entry.issue_id
55 assert_equal 2, entry.user_id
55 assert_equal 2, entry.user_id
56
56
57 @request.session[:user_id] = 1
57 @request.session[:user_id] = 1
58 post :edit, :id => 1,
58 post :edit, :id => 1,
59 :time_entry => {:issue_id => '2',
59 :time_entry => {:issue_id => '2',
60 :hours => '8'}
60 :hours => '8'}
61 assert_redirected_to 'projects/ecookbook/timelog/details'
61 assert_redirected_to 'projects/ecookbook/timelog/details'
62 entry.reload
62 entry.reload
63
63
64 assert_equal 8, entry.hours
64 assert_equal 8, entry.hours
65 assert_equal 2, entry.issue_id
65 assert_equal 2, entry.issue_id
66 assert_equal 2, entry.user_id
66 assert_equal 2, entry.user_id
67 end
67 end
68
68
69 def destroy
69 def destroy
70 @request.session[:user_id] = 2
70 @request.session[:user_id] = 2
71 post :destroy, :id => 1
71 post :destroy, :id => 1
72 assert_redirected_to 'projects/ecookbook/timelog/details'
72 assert_redirected_to 'projects/ecookbook/timelog/details'
73 assert_nil TimeEntry.find_by_id(1)
73 assert_nil TimeEntry.find_by_id(1)
74 end
74 end
75
75
76 def test_report_no_criteria
76 def test_report_no_criteria
77 get :report, :project_id => 1
77 get :report, :project_id => 1
78 assert_response :success
78 assert_response :success
79 assert_template 'report'
79 assert_template 'report'
80 end
80 end
81
81
82 def test_report_all_time
82 def test_report_all_time
83 get :report, :project_id => 1, :criterias => ['project', 'issue']
83 get :report, :project_id => 1, :criterias => ['project', 'issue']
84 assert_response :success
84 assert_response :success
85 assert_template 'report'
85 assert_template 'report'
86 assert_not_nil assigns(:total_hours)
86 assert_not_nil assigns(:total_hours)
87 assert_equal "162.90", "%.2f" % assigns(:total_hours)
87 assert_equal "162.90", "%.2f" % assigns(:total_hours)
88 end
88 end
89
89
90 def test_report_all_time_by_day
90 def test_report_all_time_by_day
91 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
91 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
92 assert_response :success
92 assert_response :success
93 assert_template 'report'
93 assert_template 'report'
94 assert_not_nil assigns(:total_hours)
94 assert_not_nil assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
96 assert_tag :tag => 'th', :content => '2007-03-12'
96 assert_tag :tag => 'th', :content => '2007-03-12'
97 end
97 end
98
98
99 def test_report_one_criteria
99 def test_report_one_criteria
100 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
100 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
101 assert_response :success
101 assert_response :success
102 assert_template 'report'
102 assert_template 'report'
103 assert_not_nil assigns(:total_hours)
103 assert_not_nil assigns(:total_hours)
104 assert_equal "8.65", "%.2f" % assigns(:total_hours)
104 assert_equal "8.65", "%.2f" % assigns(:total_hours)
105 end
105 end
106
106
107 def test_report_two_criterias
107 def test_report_two_criterias
108 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
108 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
109 assert_response :success
109 assert_response :success
110 assert_template 'report'
110 assert_template 'report'
111 assert_not_nil assigns(:total_hours)
111 assert_not_nil assigns(:total_hours)
112 assert_equal "162.90", "%.2f" % assigns(:total_hours)
112 assert_equal "162.90", "%.2f" % assigns(:total_hours)
113 end
113 end
114
114
115 def test_report_one_criteria_no_result
115 def test_report_one_criteria_no_result
116 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
116 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
117 assert_response :success
117 assert_response :success
118 assert_template 'report'
118 assert_template 'report'
119 assert_not_nil assigns(:total_hours)
119 assert_not_nil assigns(:total_hours)
120 assert_equal "0.00", "%.2f" % assigns(:total_hours)
120 assert_equal "0.00", "%.2f" % assigns(:total_hours)
121 end
121 end
122
123 def test_report_csv_export
124 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
125 assert_response :success
126 assert_equal 'text/csv', @response.content_type
127 lines = @response.body.chomp.split("\n")
128 # Headers
129 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
130 # Total row
131 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
132 end
122
133
123 def test_details_at_project_level
134 def test_details_at_project_level
124 get :details, :project_id => 1
135 get :details, :project_id => 1
125 assert_response :success
136 assert_response :success
126 assert_template 'details'
137 assert_template 'details'
127 assert_not_nil assigns(:entries)
138 assert_not_nil assigns(:entries)
128 assert_equal 4, assigns(:entries).size
139 assert_equal 4, assigns(:entries).size
129 # project and subproject
140 # project and subproject
130 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
141 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
131 assert_not_nil assigns(:total_hours)
142 assert_not_nil assigns(:total_hours)
132 assert_equal "162.90", "%.2f" % assigns(:total_hours)
143 assert_equal "162.90", "%.2f" % assigns(:total_hours)
133 # display all time by default
144 # display all time by default
134 assert_equal '2007-03-11'.to_date, assigns(:from)
145 assert_equal '2007-03-11'.to_date, assigns(:from)
135 assert_equal '2007-04-22'.to_date, assigns(:to)
146 assert_equal '2007-04-22'.to_date, assigns(:to)
136 end
147 end
137
148
138 def test_details_at_project_level_with_date_range
149 def test_details_at_project_level_with_date_range
139 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
150 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
140 assert_response :success
151 assert_response :success
141 assert_template 'details'
152 assert_template 'details'
142 assert_not_nil assigns(:entries)
153 assert_not_nil assigns(:entries)
143 assert_equal 3, assigns(:entries).size
154 assert_equal 3, assigns(:entries).size
144 assert_not_nil assigns(:total_hours)
155 assert_not_nil assigns(:total_hours)
145 assert_equal "12.90", "%.2f" % assigns(:total_hours)
156 assert_equal "12.90", "%.2f" % assigns(:total_hours)
146 assert_equal '2007-03-20'.to_date, assigns(:from)
157 assert_equal '2007-03-20'.to_date, assigns(:from)
147 assert_equal '2007-04-30'.to_date, assigns(:to)
158 assert_equal '2007-04-30'.to_date, assigns(:to)
148 end
159 end
149
160
150 def test_details_at_project_level_with_period
161 def test_details_at_project_level_with_period
151 get :details, :project_id => 1, :period => '7_days'
162 get :details, :project_id => 1, :period => '7_days'
152 assert_response :success
163 assert_response :success
153 assert_template 'details'
164 assert_template 'details'
154 assert_not_nil assigns(:entries)
165 assert_not_nil assigns(:entries)
155 assert_not_nil assigns(:total_hours)
166 assert_not_nil assigns(:total_hours)
156 assert_equal Date.today - 7, assigns(:from)
167 assert_equal Date.today - 7, assigns(:from)
157 assert_equal Date.today, assigns(:to)
168 assert_equal Date.today, assigns(:to)
158 end
169 end
159
170
160 def test_details_at_issue_level
171 def test_details_at_issue_level
161 get :details, :issue_id => 1
172 get :details, :issue_id => 1
162 assert_response :success
173 assert_response :success
163 assert_template 'details'
174 assert_template 'details'
164 assert_not_nil assigns(:entries)
175 assert_not_nil assigns(:entries)
165 assert_equal 2, assigns(:entries).size
176 assert_equal 2, assigns(:entries).size
166 assert_not_nil assigns(:total_hours)
177 assert_not_nil assigns(:total_hours)
167 assert_equal 154.25, assigns(:total_hours)
178 assert_equal 154.25, assigns(:total_hours)
168 # display all time by default
179 # display all time by default
169 assert_equal '2007-03-11'.to_date, assigns(:from)
180 assert_equal '2007-03-11'.to_date, assigns(:from)
170 assert_equal '2007-04-22'.to_date, assigns(:to)
181 assert_equal '2007-04-22'.to_date, assigns(:to)
171 end
182 end
172
183
173 def test_details_csv_export
184 def test_details_csv_export
174 get :details, :project_id => 1, :format => 'csv'
185 get :details, :project_id => 1, :format => 'csv'
175 assert_response :success
186 assert_response :success
176 assert_equal 'text/csv', @response.content_type
187 assert_equal 'text/csv', @response.content_type
177 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
188 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
178 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
189 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
179 end
190 end
180 end
191 end
General Comments 0
You need to be logged in to leave comments. Login now