##// END OF EJS Templates
Makes timelog report work at issue level (#2935)....
Jean-Philippe Lang -
r2779:41cbd239c44e
parent child
Show More
@@ -1,293 +1,301
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 menu_item :issues
19 menu_item :issues
20 before_filter :find_project, :authorize, :only => [:edit, :destroy]
20 before_filter :find_project, :authorize, :only => [:edit, :destroy]
21 before_filter :find_optional_project, :only => [:report, :details]
21 before_filter :find_optional_project, :only => [:report, :details]
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 helper :custom_fields
29 helper :custom_fields
30 include CustomFieldsHelper
30 include CustomFieldsHelper
31
31
32 def report
32 def report
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
34 :klass => Project,
34 :klass => Project,
35 :label => :label_project},
35 :label => :label_project},
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
37 :klass => Version,
37 :klass => Version,
38 :label => :label_version},
38 :label => :label_version},
39 'category' => {:sql => "#{Issue.table_name}.category_id",
39 'category' => {:sql => "#{Issue.table_name}.category_id",
40 :klass => IssueCategory,
40 :klass => IssueCategory,
41 :label => :field_category},
41 :label => :field_category},
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
43 :klass => User,
43 :klass => User,
44 :label => :label_member},
44 :label => :label_member},
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
46 :klass => Tracker,
46 :klass => Tracker,
47 :label => :label_tracker},
47 :label => :label_tracker},
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
49 :klass => TimeEntryActivity,
49 :klass => TimeEntryActivity,
50 :label => :label_activity},
50 :label => :label_activity},
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
52 :klass => Issue,
52 :klass => Issue,
53 :label => :label_issue}
53 :label => :label_issue}
54 }
54 }
55
55
56 # Add list and boolean custom fields as available criterias
56 # Add list and boolean custom fields as available criterias
57 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
57 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
58 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
58 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
59 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
59 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
60 :format => cf.field_format,
60 :format => cf.field_format,
61 :label => cf.name}
61 :label => cf.name}
62 end if @project
62 end if @project
63
63
64 # Add list and boolean time entry custom fields
64 # Add list and boolean time entry custom fields
65 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
65 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
66 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
66 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
67 :format => cf.field_format,
67 :format => cf.field_format,
68 :label => cf.name}
68 :label => cf.name}
69 end
69 end
70
70
71 @criterias = params[:criterias] || []
71 @criterias = params[:criterias] || []
72 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
72 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
73 @criterias.uniq!
73 @criterias.uniq!
74 @criterias = @criterias[0,3]
74 @criterias = @criterias[0,3]
75
75
76 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
76 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
77
77
78 retrieve_date_range
78 retrieve_date_range
79
79
80 unless @criterias.empty?
80 unless @criterias.empty?
81 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
81 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
82 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
82 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
83 sql_condition = ''
83
84
85 if @project.nil?
86 sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
87 elsif @issue.nil?
88 sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
89 else
90 sql_condition = "#{TimeEntry.table_name}.issue_id = #{@issue.id}"
91 end
92
84 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
93 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
85 sql << " FROM #{TimeEntry.table_name}"
94 sql << " FROM #{TimeEntry.table_name}"
86 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
95 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
87 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
96 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
88 sql << " WHERE"
97 sql << " WHERE"
89 sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project
98 sql << " (%s) AND" % sql_condition
90 sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries)
91 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
99 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
92 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
100 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
93
101
94 @hours = ActiveRecord::Base.connection.select_all(sql)
102 @hours = ActiveRecord::Base.connection.select_all(sql)
95
103
96 @hours.each do |row|
104 @hours.each do |row|
97 case @columns
105 case @columns
98 when 'year'
106 when 'year'
99 row['year'] = row['tyear']
107 row['year'] = row['tyear']
100 when 'month'
108 when 'month'
101 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
109 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
102 when 'week'
110 when 'week'
103 row['week'] = "#{row['tyear']}-#{row['tweek']}"
111 row['week'] = "#{row['tyear']}-#{row['tweek']}"
104 when 'day'
112 when 'day'
105 row['day'] = "#{row['spent_on']}"
113 row['day'] = "#{row['spent_on']}"
106 end
114 end
107 end
115 end
108
116
109 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
117 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
110
118
111 @periods = []
119 @periods = []
112 # Date#at_beginning_of_ not supported in Rails 1.2.x
120 # Date#at_beginning_of_ not supported in Rails 1.2.x
113 date_from = @from.to_time
121 date_from = @from.to_time
114 # 100 columns max
122 # 100 columns max
115 while date_from <= @to.to_time && @periods.length < 100
123 while date_from <= @to.to_time && @periods.length < 100
116 case @columns
124 case @columns
117 when 'year'
125 when 'year'
118 @periods << "#{date_from.year}"
126 @periods << "#{date_from.year}"
119 date_from = (date_from + 1.year).at_beginning_of_year
127 date_from = (date_from + 1.year).at_beginning_of_year
120 when 'month'
128 when 'month'
121 @periods << "#{date_from.year}-#{date_from.month}"
129 @periods << "#{date_from.year}-#{date_from.month}"
122 date_from = (date_from + 1.month).at_beginning_of_month
130 date_from = (date_from + 1.month).at_beginning_of_month
123 when 'week'
131 when 'week'
124 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
132 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
125 date_from = (date_from + 7.day).at_beginning_of_week
133 date_from = (date_from + 7.day).at_beginning_of_week
126 when 'day'
134 when 'day'
127 @periods << "#{date_from.to_date}"
135 @periods << "#{date_from.to_date}"
128 date_from = date_from + 1.day
136 date_from = date_from + 1.day
129 end
137 end
130 end
138 end
131 end
139 end
132
140
133 respond_to do |format|
141 respond_to do |format|
134 format.html { render :layout => !request.xhr? }
142 format.html { render :layout => !request.xhr? }
135 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
143 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
136 end
144 end
137 end
145 end
138
146
139 def details
147 def details
140 sort_init 'spent_on', 'desc'
148 sort_init 'spent_on', 'desc'
141 sort_update 'spent_on' => 'spent_on',
149 sort_update 'spent_on' => 'spent_on',
142 'user' => 'user_id',
150 'user' => 'user_id',
143 'activity' => 'activity_id',
151 'activity' => 'activity_id',
144 'project' => "#{Project.table_name}.name",
152 'project' => "#{Project.table_name}.name",
145 'issue' => 'issue_id',
153 'issue' => 'issue_id',
146 'hours' => 'hours'
154 'hours' => 'hours'
147
155
148 cond = ARCondition.new
156 cond = ARCondition.new
149 if @project.nil?
157 if @project.nil?
150 cond << Project.allowed_to_condition(User.current, :view_time_entries)
158 cond << Project.allowed_to_condition(User.current, :view_time_entries)
151 elsif @issue.nil?
159 elsif @issue.nil?
152 cond << @project.project_condition(Setting.display_subprojects_issues?)
160 cond << @project.project_condition(Setting.display_subprojects_issues?)
153 else
161 else
154 cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
162 cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
155 end
163 end
156
164
157 retrieve_date_range
165 retrieve_date_range
158 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
166 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
159
167
160 TimeEntry.visible_by(User.current) do
168 TimeEntry.visible_by(User.current) do
161 respond_to do |format|
169 respond_to do |format|
162 format.html {
170 format.html {
163 # Paginate results
171 # Paginate results
164 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
172 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
165 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
173 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
166 @entries = TimeEntry.find(:all,
174 @entries = TimeEntry.find(:all,
167 :include => [:project, :activity, :user, {:issue => :tracker}],
175 :include => [:project, :activity, :user, {:issue => :tracker}],
168 :conditions => cond.conditions,
176 :conditions => cond.conditions,
169 :order => sort_clause,
177 :order => sort_clause,
170 :limit => @entry_pages.items_per_page,
178 :limit => @entry_pages.items_per_page,
171 :offset => @entry_pages.current.offset)
179 :offset => @entry_pages.current.offset)
172 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
180 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
173
181
174 render :layout => !request.xhr?
182 render :layout => !request.xhr?
175 }
183 }
176 format.atom {
184 format.atom {
177 entries = TimeEntry.find(:all,
185 entries = TimeEntry.find(:all,
178 :include => [:project, :activity, :user, {:issue => :tracker}],
186 :include => [:project, :activity, :user, {:issue => :tracker}],
179 :conditions => cond.conditions,
187 :conditions => cond.conditions,
180 :order => "#{TimeEntry.table_name}.created_on DESC",
188 :order => "#{TimeEntry.table_name}.created_on DESC",
181 :limit => Setting.feeds_limit.to_i)
189 :limit => Setting.feeds_limit.to_i)
182 render_feed(entries, :title => l(:label_spent_time))
190 render_feed(entries, :title => l(:label_spent_time))
183 }
191 }
184 format.csv {
192 format.csv {
185 # Export all entries
193 # Export all entries
186 @entries = TimeEntry.find(:all,
194 @entries = TimeEntry.find(:all,
187 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
195 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
188 :conditions => cond.conditions,
196 :conditions => cond.conditions,
189 :order => sort_clause)
197 :order => sort_clause)
190 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
198 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
191 }
199 }
192 end
200 end
193 end
201 end
194 end
202 end
195
203
196 def edit
204 def edit
197 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
205 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
198 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
206 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
199 @time_entry.attributes = params[:time_entry]
207 @time_entry.attributes = params[:time_entry]
200
208
201 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
209 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
202
210
203 if request.post? and @time_entry.save
211 if request.post? and @time_entry.save
204 flash[:notice] = l(:notice_successful_update)
212 flash[:notice] = l(:notice_successful_update)
205 redirect_back_or_default :action => 'details', :project_id => @time_entry.project
213 redirect_back_or_default :action => 'details', :project_id => @time_entry.project
206 return
214 return
207 end
215 end
208 end
216 end
209
217
210 def destroy
218 def destroy
211 render_404 and return unless @time_entry
219 render_404 and return unless @time_entry
212 render_403 and return unless @time_entry.editable_by?(User.current)
220 render_403 and return unless @time_entry.editable_by?(User.current)
213 @time_entry.destroy
221 @time_entry.destroy
214 flash[:notice] = l(:notice_successful_delete)
222 flash[:notice] = l(:notice_successful_delete)
215 redirect_to :back
223 redirect_to :back
216 rescue ::ActionController::RedirectBackError
224 rescue ::ActionController::RedirectBackError
217 redirect_to :action => 'details', :project_id => @time_entry.project
225 redirect_to :action => 'details', :project_id => @time_entry.project
218 end
226 end
219
227
220 private
228 private
221 def find_project
229 def find_project
222 if params[:id]
230 if params[:id]
223 @time_entry = TimeEntry.find(params[:id])
231 @time_entry = TimeEntry.find(params[:id])
224 @project = @time_entry.project
232 @project = @time_entry.project
225 elsif params[:issue_id]
233 elsif params[:issue_id]
226 @issue = Issue.find(params[:issue_id])
234 @issue = Issue.find(params[:issue_id])
227 @project = @issue.project
235 @project = @issue.project
228 elsif params[:project_id]
236 elsif params[:project_id]
229 @project = Project.find(params[:project_id])
237 @project = Project.find(params[:project_id])
230 else
238 else
231 render_404
239 render_404
232 return false
240 return false
233 end
241 end
234 rescue ActiveRecord::RecordNotFound
242 rescue ActiveRecord::RecordNotFound
235 render_404
243 render_404
236 end
244 end
237
245
238 def find_optional_project
246 def find_optional_project
239 if !params[:issue_id].blank?
247 if !params[:issue_id].blank?
240 @issue = Issue.find(params[:issue_id])
248 @issue = Issue.find(params[:issue_id])
241 @project = @issue.project
249 @project = @issue.project
242 elsif !params[:project_id].blank?
250 elsif !params[:project_id].blank?
243 @project = Project.find(params[:project_id])
251 @project = Project.find(params[:project_id])
244 end
252 end
245 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
253 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
246 end
254 end
247
255
248 # Retrieves the date range based on predefined ranges or specific from/to param dates
256 # Retrieves the date range based on predefined ranges or specific from/to param dates
249 def retrieve_date_range
257 def retrieve_date_range
250 @free_period = false
258 @free_period = false
251 @from, @to = nil, nil
259 @from, @to = nil, nil
252
260
253 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
261 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
254 case params[:period].to_s
262 case params[:period].to_s
255 when 'today'
263 when 'today'
256 @from = @to = Date.today
264 @from = @to = Date.today
257 when 'yesterday'
265 when 'yesterday'
258 @from = @to = Date.today - 1
266 @from = @to = Date.today - 1
259 when 'current_week'
267 when 'current_week'
260 @from = Date.today - (Date.today.cwday - 1)%7
268 @from = Date.today - (Date.today.cwday - 1)%7
261 @to = @from + 6
269 @to = @from + 6
262 when 'last_week'
270 when 'last_week'
263 @from = Date.today - 7 - (Date.today.cwday - 1)%7
271 @from = Date.today - 7 - (Date.today.cwday - 1)%7
264 @to = @from + 6
272 @to = @from + 6
265 when '7_days'
273 when '7_days'
266 @from = Date.today - 7
274 @from = Date.today - 7
267 @to = Date.today
275 @to = Date.today
268 when 'current_month'
276 when 'current_month'
269 @from = Date.civil(Date.today.year, Date.today.month, 1)
277 @from = Date.civil(Date.today.year, Date.today.month, 1)
270 @to = (@from >> 1) - 1
278 @to = (@from >> 1) - 1
271 when 'last_month'
279 when 'last_month'
272 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
280 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
273 @to = (@from >> 1) - 1
281 @to = (@from >> 1) - 1
274 when '30_days'
282 when '30_days'
275 @from = Date.today - 30
283 @from = Date.today - 30
276 @to = Date.today
284 @to = Date.today
277 when 'current_year'
285 when 'current_year'
278 @from = Date.civil(Date.today.year, 1, 1)
286 @from = Date.civil(Date.today.year, 1, 1)
279 @to = Date.civil(Date.today.year, 12, 31)
287 @to = Date.civil(Date.today.year, 12, 31)
280 end
288 end
281 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
289 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
282 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
290 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
283 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
291 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
284 @free_period = true
292 @free_period = true
285 else
293 else
286 # default
294 # default
287 end
295 end
288
296
289 @from, @to = @to, @from if @from && @to && @from > @to
297 @from, @to = @to, @from if @from && @to && @from > @to
290 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1
298 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1
291 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today)
299 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today)
292 end
300 end
293 end
301 end
@@ -1,35 +1,35
1 <fieldset id="date-range" class="collapsible">
1 <fieldset id="date-range" class="collapsible">
2 <legend onclick="toggleFieldset(this);"><%= l(:label_date_range) %></legend>
2 <legend onclick="toggleFieldset(this);"><%= l(:label_date_range) %></legend>
3 <div>
3 <div>
4 <p>
4 <p>
5 <%= radio_button_tag 'period_type', '1', !@free_period %>
5 <%= radio_button_tag 'period_type', '1', !@free_period %>
6 <%= select_tag 'period', options_for_period_select(params[:period]),
6 <%= select_tag 'period', options_for_period_select(params[:period]),
7 :onchange => 'this.form.onsubmit();',
7 :onchange => 'this.form.onsubmit();',
8 :onfocus => '$("period_type_1").checked = true;' %>
8 :onfocus => '$("period_type_1").checked = true;' %>
9 </p>
9 </p>
10 <p>
10 <p>
11 <%= radio_button_tag 'period_type', '2', @free_period %>
11 <%= radio_button_tag 'period_type', '2', @free_period %>
12 <span onclick="$('period_type_2').checked = true;">
12 <span onclick="$('period_type_2').checked = true;">
13 <%= l(:label_date_from_to, :start => (text_field_tag('from', @from, :size => 10) + calendar_for('from')),
13 <%= l(:label_date_from_to, :start => (text_field_tag('from', @from, :size => 10) + calendar_for('from')),
14 :end => (text_field_tag('to', @to, :size => 10) + calendar_for('to'))) %>
14 :end => (text_field_tag('to', @to, :size => 10) + calendar_for('to'))) %>
15 </span>
15 </span>
16 </p>
16 </p>
17 </div>
17 </div>
18 </fieldset>
18 </fieldset>
19 <p class="buttons">
19 <p class="buttons">
20 <%= link_to_remote l(:button_apply),
20 <%= link_to_remote l(:button_apply),
21 { :url => { },
21 { :url => { },
22 :update => "content",
22 :update => "content",
23 :with => "Form.serialize('query_form')"
23 :with => "Form.serialize('query_form')"
24 }, :class => 'icon icon-checked' %>
24 }, :class => 'icon icon-checked' %>
25 </p>
25 </p>
26
26
27 <div class="tabs">
27 <div class="tabs">
28 <% url_params = @free_period ? { :from => @from, :to => @to } : { :period => params[:period] } %>
28 <% url_params = @free_period ? { :from => @from, :to => @to } : { :period => params[:period] } %>
29 <ul>
29 <ul>
30 <li><%= link_to(l(:label_details), url_params.merge({:controller => 'timelog', :action => 'details', :project_id => @project }),
30 <li><%= link_to(l(:label_details), url_params.merge({:controller => 'timelog', :action => 'details', :project_id => @project, :issue_id => @issue }),
31 :class => (@controller.action_name == 'details' ? 'selected' : nil)) %></li>
31 :class => (@controller.action_name == 'details' ? 'selected' : nil)) %></li>
32 <li><%= link_to(l(:label_report), url_params.merge({:controller => 'timelog', :action => 'report', :project_id => @project}),
32 <li><%= link_to(l(:label_report), url_params.merge({:controller => 'timelog', :action => 'report', :project_id => @project, :issue_id => @issue}),
33 :class => (@controller.action_name == 'report' ? 'selected' : nil)) %></li>
33 :class => (@controller.action_name == 'report' ? 'selected' : nil)) %></li>
34 </ul>
34 </ul>
35 </div>
35 </div>
@@ -1,75 +1,76
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-add' %>
2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time-add' %>
3 </div>
3 </div>
4
4
5 <%= render_timelog_breadcrumb %>
5 <%= render_timelog_breadcrumb %>
6
6
7 <h2><%= l(:label_spent_time) %></h2>
7 <h2><%= l(:label_spent_time) %></h2>
8
8
9 <% form_remote_tag(:url => {}, :html => {:method => :get, :id => 'query_form'}, :method => :get, :update => 'content') do %>
9 <% form_remote_tag(:url => {}, :html => {:method => :get, :id => 'query_form'}, :method => :get, :update => 'content') do %>
10 <% @criterias.each do |criteria| %>
10 <% @criterias.each do |criteria| %>
11 <%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
11 <%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
12 <% end %>
12 <% end %>
13 <%# TODO: get rid of the project_id field, that should already be in the URL %>
13 <%# TODO: get rid of the project_id field, that should already be in the URL %>
14 <%= hidden_field_tag('project_id', params[:project_id]) if @project %>
14 <%= hidden_field_tag('project_id', params[:project_id]) if @project %>
15 <%= hidden_field_tag('issue_id', params[:issue_id]) if @issue %>
15 <%= render :partial => 'date_range' %>
16 <%= render :partial => 'date_range' %>
16
17
17 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
18 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
18 [l(:label_month), 'month'],
19 [l(:label_month), 'month'],
19 [l(:label_week), 'week'],
20 [l(:label_week), 'week'],
20 [l(:label_day_plural).titleize, 'day']], @columns),
21 [l(:label_day_plural).titleize, 'day']], @columns),
21 :onchange => "this.form.onsubmit();" %>
22 :onchange => "this.form.onsubmit();" %>
22
23
23 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l_or_humanize(@available_criterias[k][:label]), k]}),
24 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l_or_humanize(@available_criterias[k][:label]), k]}),
24 :onchange => "this.form.onsubmit();",
25 :onchange => "this.form.onsubmit();",
25 :style => 'width: 200px',
26 :style => 'width: 200px',
26 :id => nil,
27 :id => nil,
27 :disabled => (@criterias.length >= 3)) %>
28 :disabled => (@criterias.length >= 3)) %>
28 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
29 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
29 :method => :get,
30 :method => :get,
30 :update => 'content'
31 :update => 'content'
31 }, :class => 'icon icon-reload' %></p>
32 }, :class => 'icon icon-reload' %></p>
32 <% end %>
33 <% end %>
33
34
34 <% unless @criterias.empty? %>
35 <% unless @criterias.empty? %>
35 <div class="total-hours">
36 <div class="total-hours">
36 <p><%= l(:label_total) %>: <%= html_hours(l_hours(@total_hours)) %></p>
37 <p><%= l(:label_total) %>: <%= html_hours(l_hours(@total_hours)) %></p>
37 </div>
38 </div>
38
39
39 <% unless @hours.empty? %>
40 <% unless @hours.empty? %>
40 <table class="list" id="time-report">
41 <table class="list" id="time-report">
41 <thead>
42 <thead>
42 <tr>
43 <tr>
43 <% @criterias.each do |criteria| %>
44 <% @criterias.each do |criteria| %>
44 <th><%= l_or_humanize(@available_criterias[criteria][:label]) %></th>
45 <th><%= l_or_humanize(@available_criterias[criteria][:label]) %></th>
45 <% end %>
46 <% end %>
46 <% columns_width = (40 / (@periods.length+1)).to_i %>
47 <% columns_width = (40 / (@periods.length+1)).to_i %>
47 <% @periods.each do |period| %>
48 <% @periods.each do |period| %>
48 <th class="period" width="<%= columns_width %>%"><%= period %></th>
49 <th class="period" width="<%= columns_width %>%"><%= period %></th>
49 <% end %>
50 <% end %>
50 <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
51 <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
51 </tr>
52 </tr>
52 </thead>
53 </thead>
53 <tbody>
54 <tbody>
54 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
55 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
55 <tr class="total">
56 <tr class="total">
56 <td><%= l(:label_total) %></td>
57 <td><%= l(:label_total) %></td>
57 <%= '<td></td>' * (@criterias.size - 1) %>
58 <%= '<td></td>' * (@criterias.size - 1) %>
58 <% total = 0 -%>
59 <% total = 0 -%>
59 <% @periods.each do |period| -%>
60 <% @periods.each do |period| -%>
60 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
61 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
61 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
62 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
62 <% end -%>
63 <% end -%>
63 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
64 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
64 </tr>
65 </tr>
65 </tbody>
66 </tbody>
66 </table>
67 </table>
67
68
68 <% other_formats_links do |f| %>
69 <% other_formats_links do |f| %>
69 <%= f.link_to 'CSV', :url => params %>
70 <%= f.link_to 'CSV', :url => params %>
70 <% end %>
71 <% end %>
71 <% end %>
72 <% end %>
72 <% end %>
73 <% end %>
73
74
74 <% html_title l(:label_spent_time), l(:label_report) %>
75 <% html_title l(:label_spent_time), l(:label_report) %>
75
76
@@ -1,396 +1,404
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 < ActionController::TestCase
24 class TimelogControllerTest < ActionController::TestCase
25 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
25 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
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_edit_routing
33 def test_edit_routing
34 assert_routing(
34 assert_routing(
35 {:method => :get, :path => '/issues/567/time_entries/new'},
35 {:method => :get, :path => '/issues/567/time_entries/new'},
36 :controller => 'timelog', :action => 'edit', :issue_id => '567'
36 :controller => 'timelog', :action => 'edit', :issue_id => '567'
37 )
37 )
38 assert_routing(
38 assert_routing(
39 {:method => :get, :path => '/projects/ecookbook/time_entries/new'},
39 {:method => :get, :path => '/projects/ecookbook/time_entries/new'},
40 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook'
40 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook'
41 )
41 )
42 assert_routing(
42 assert_routing(
43 {:method => :get, :path => '/projects/ecookbook/issues/567/time_entries/new'},
43 {:method => :get, :path => '/projects/ecookbook/issues/567/time_entries/new'},
44 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook', :issue_id => '567'
44 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook', :issue_id => '567'
45 )
45 )
46
46
47 #TODO: change new form to POST to issue_time_entries_path instead of to edit action
47 #TODO: change new form to POST to issue_time_entries_path instead of to edit action
48 #TODO: change edit form to PUT to time_entry_path
48 #TODO: change edit form to PUT to time_entry_path
49 assert_routing(
49 assert_routing(
50 {:method => :get, :path => '/time_entries/22/edit'},
50 {:method => :get, :path => '/time_entries/22/edit'},
51 :controller => 'timelog', :action => 'edit', :id => '22'
51 :controller => 'timelog', :action => 'edit', :id => '22'
52 )
52 )
53 end
53 end
54
54
55 def test_get_edit
55 def test_get_edit
56 @request.session[:user_id] = 3
56 @request.session[:user_id] = 3
57 get :edit, :project_id => 1
57 get :edit, :project_id => 1
58 assert_response :success
58 assert_response :success
59 assert_template 'edit'
59 assert_template 'edit'
60 # Default activity selected
60 # Default activity selected
61 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
61 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
62 :content => 'Development'
62 :content => 'Development'
63 end
63 end
64
64
65 def test_get_edit_existing_time
65 def test_get_edit_existing_time
66 @request.session[:user_id] = 2
66 @request.session[:user_id] = 2
67 get :edit, :id => 2, :project_id => nil
67 get :edit, :id => 2, :project_id => nil
68 assert_response :success
68 assert_response :success
69 assert_template 'edit'
69 assert_template 'edit'
70 # Default activity selected
70 # Default activity selected
71 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/timelog/edit/2' }
71 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/timelog/edit/2' }
72 end
72 end
73
73
74 def test_post_edit
74 def test_post_edit
75 # TODO: should POST to issues’ time log instead of project. change form
75 # TODO: should POST to issues’ time log instead of project. change form
76 # and routing
76 # and routing
77 @request.session[:user_id] = 3
77 @request.session[:user_id] = 3
78 post :edit, :project_id => 1,
78 post :edit, :project_id => 1,
79 :time_entry => {:comments => 'Some work on TimelogControllerTest',
79 :time_entry => {:comments => 'Some work on TimelogControllerTest',
80 # Not the default activity
80 # Not the default activity
81 :activity_id => '11',
81 :activity_id => '11',
82 :spent_on => '2008-03-14',
82 :spent_on => '2008-03-14',
83 :issue_id => '1',
83 :issue_id => '1',
84 :hours => '7.3'}
84 :hours => '7.3'}
85 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
85 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
86
86
87 i = Issue.find(1)
87 i = Issue.find(1)
88 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
88 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
89 assert_not_nil t
89 assert_not_nil t
90 assert_equal 11, t.activity_id
90 assert_equal 11, t.activity_id
91 assert_equal 7.3, t.hours
91 assert_equal 7.3, t.hours
92 assert_equal 3, t.user_id
92 assert_equal 3, t.user_id
93 assert_equal i, t.issue
93 assert_equal i, t.issue
94 assert_equal i.project, t.project
94 assert_equal i.project, t.project
95 end
95 end
96
96
97 def test_update
97 def test_update
98 entry = TimeEntry.find(1)
98 entry = TimeEntry.find(1)
99 assert_equal 1, entry.issue_id
99 assert_equal 1, entry.issue_id
100 assert_equal 2, entry.user_id
100 assert_equal 2, entry.user_id
101
101
102 @request.session[:user_id] = 1
102 @request.session[:user_id] = 1
103 post :edit, :id => 1,
103 post :edit, :id => 1,
104 :time_entry => {:issue_id => '2',
104 :time_entry => {:issue_id => '2',
105 :hours => '8'}
105 :hours => '8'}
106 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
106 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
107 entry.reload
107 entry.reload
108
108
109 assert_equal 8, entry.hours
109 assert_equal 8, entry.hours
110 assert_equal 2, entry.issue_id
110 assert_equal 2, entry.issue_id
111 assert_equal 2, entry.user_id
111 assert_equal 2, entry.user_id
112 end
112 end
113
113
114 def test_destroy_routing
114 def test_destroy_routing
115 #TODO: use DELETE to time_entry_path
115 #TODO: use DELETE to time_entry_path
116 assert_routing(
116 assert_routing(
117 {:method => :post, :path => '/time_entries/55/destroy'},
117 {:method => :post, :path => '/time_entries/55/destroy'},
118 :controller => 'timelog', :action => 'destroy', :id => '55'
118 :controller => 'timelog', :action => 'destroy', :id => '55'
119 )
119 )
120 end
120 end
121
121
122 def test_destroy
122 def test_destroy
123 @request.session[:user_id] = 2
123 @request.session[:user_id] = 2
124 post :destroy, :id => 1
124 post :destroy, :id => 1
125 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
125 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
126 assert_nil TimeEntry.find_by_id(1)
126 assert_nil TimeEntry.find_by_id(1)
127 end
127 end
128
128
129 def test_report_routing
129 def test_report_routing
130 assert_routing(
130 assert_routing(
131 {:method => :get, :path => '/projects/567/time_entries/report'},
131 {:method => :get, :path => '/projects/567/time_entries/report'},
132 :controller => 'timelog', :action => 'report', :project_id => '567'
132 :controller => 'timelog', :action => 'report', :project_id => '567'
133 )
133 )
134 assert_routing(
134 assert_routing(
135 {:method => :get, :path => '/projects/567/time_entries/report.csv'},
135 {:method => :get, :path => '/projects/567/time_entries/report.csv'},
136 :controller => 'timelog', :action => 'report', :project_id => '567', :format => 'csv'
136 :controller => 'timelog', :action => 'report', :project_id => '567', :format => 'csv'
137 )
137 )
138 end
138 end
139
139
140 def test_report_no_criteria
140 def test_report_no_criteria
141 get :report, :project_id => 1
141 get :report, :project_id => 1
142 assert_response :success
142 assert_response :success
143 assert_template 'report'
143 assert_template 'report'
144 end
144 end
145
145
146 def test_report_routing_for_all_projects
146 def test_report_routing_for_all_projects
147 assert_routing(
147 assert_routing(
148 {:method => :get, :path => '/time_entries/report'},
148 {:method => :get, :path => '/time_entries/report'},
149 :controller => 'timelog', :action => 'report'
149 :controller => 'timelog', :action => 'report'
150 )
150 )
151 end
151 end
152
152
153 def test_report_all_projects
153 def test_report_all_projects
154 get :report
154 get :report
155 assert_response :success
155 assert_response :success
156 assert_template 'report'
156 assert_template 'report'
157 end
157 end
158
158
159 def test_report_all_projects_denied
159 def test_report_all_projects_denied
160 r = Role.anonymous
160 r = Role.anonymous
161 r.permissions.delete(:view_time_entries)
161 r.permissions.delete(:view_time_entries)
162 r.permissions_will_change!
162 r.permissions_will_change!
163 r.save
163 r.save
164 get :report
164 get :report
165 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
165 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
166 end
166 end
167
167
168 def test_report_all_projects_one_criteria
168 def test_report_all_projects_one_criteria
169 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
169 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
170 assert_response :success
170 assert_response :success
171 assert_template 'report'
171 assert_template 'report'
172 assert_not_nil assigns(:total_hours)
172 assert_not_nil assigns(:total_hours)
173 assert_equal "8.65", "%.2f" % assigns(:total_hours)
173 assert_equal "8.65", "%.2f" % assigns(:total_hours)
174 end
174 end
175
175
176 def test_report_all_time
176 def test_report_all_time
177 get :report, :project_id => 1, :criterias => ['project', 'issue']
177 get :report, :project_id => 1, :criterias => ['project', 'issue']
178 assert_response :success
178 assert_response :success
179 assert_template 'report'
179 assert_template 'report'
180 assert_not_nil assigns(:total_hours)
180 assert_not_nil assigns(:total_hours)
181 assert_equal "162.90", "%.2f" % assigns(:total_hours)
181 assert_equal "162.90", "%.2f" % assigns(:total_hours)
182 end
182 end
183
183
184 def test_report_all_time_by_day
184 def test_report_all_time_by_day
185 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
185 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
186 assert_response :success
186 assert_response :success
187 assert_template 'report'
187 assert_template 'report'
188 assert_not_nil assigns(:total_hours)
188 assert_not_nil assigns(:total_hours)
189 assert_equal "162.90", "%.2f" % assigns(:total_hours)
189 assert_equal "162.90", "%.2f" % assigns(:total_hours)
190 assert_tag :tag => 'th', :content => '2007-03-12'
190 assert_tag :tag => 'th', :content => '2007-03-12'
191 end
191 end
192
192
193 def test_report_one_criteria
193 def test_report_one_criteria
194 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
194 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
195 assert_response :success
195 assert_response :success
196 assert_template 'report'
196 assert_template 'report'
197 assert_not_nil assigns(:total_hours)
197 assert_not_nil assigns(:total_hours)
198 assert_equal "8.65", "%.2f" % assigns(:total_hours)
198 assert_equal "8.65", "%.2f" % assigns(:total_hours)
199 end
199 end
200
200
201 def test_report_two_criterias
201 def test_report_two_criterias
202 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
202 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
203 assert_response :success
203 assert_response :success
204 assert_template 'report'
204 assert_template 'report'
205 assert_not_nil assigns(:total_hours)
205 assert_not_nil assigns(:total_hours)
206 assert_equal "162.90", "%.2f" % assigns(:total_hours)
206 assert_equal "162.90", "%.2f" % assigns(:total_hours)
207 end
207 end
208
208
209 def test_report_at_issue_level
210 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
211 assert_response :success
212 assert_template 'report'
213 assert_not_nil assigns(:total_hours)
214 assert_equal "154.25", "%.2f" % assigns(:total_hours)
215 end
216
209 def test_report_custom_field_criteria
217 def test_report_custom_field_criteria
210 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
218 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
211 assert_response :success
219 assert_response :success
212 assert_template 'report'
220 assert_template 'report'
213 assert_not_nil assigns(:total_hours)
221 assert_not_nil assigns(:total_hours)
214 assert_not_nil assigns(:criterias)
222 assert_not_nil assigns(:criterias)
215 assert_equal 2, assigns(:criterias).size
223 assert_equal 2, assigns(:criterias).size
216 assert_equal "162.90", "%.2f" % assigns(:total_hours)
224 assert_equal "162.90", "%.2f" % assigns(:total_hours)
217 # Custom field column
225 # Custom field column
218 assert_tag :tag => 'th', :content => 'Database'
226 assert_tag :tag => 'th', :content => 'Database'
219 # Custom field row
227 # Custom field row
220 assert_tag :tag => 'td', :content => 'MySQL',
228 assert_tag :tag => 'td', :content => 'MySQL',
221 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
229 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
222 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
230 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
223 :content => '1' }}
231 :content => '1' }}
224 end
232 end
225
233
226 def test_report_one_criteria_no_result
234 def test_report_one_criteria_no_result
227 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
235 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
228 assert_response :success
236 assert_response :success
229 assert_template 'report'
237 assert_template 'report'
230 assert_not_nil assigns(:total_hours)
238 assert_not_nil assigns(:total_hours)
231 assert_equal "0.00", "%.2f" % assigns(:total_hours)
239 assert_equal "0.00", "%.2f" % assigns(:total_hours)
232 end
240 end
233
241
234 def test_report_all_projects_csv_export
242 def test_report_all_projects_csv_export
235 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
243 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
236 assert_response :success
244 assert_response :success
237 assert_equal 'text/csv', @response.content_type
245 assert_equal 'text/csv', @response.content_type
238 lines = @response.body.chomp.split("\n")
246 lines = @response.body.chomp.split("\n")
239 # Headers
247 # Headers
240 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
248 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
241 # Total row
249 # Total row
242 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
250 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
243 end
251 end
244
252
245 def test_report_csv_export
253 def test_report_csv_export
246 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
254 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
247 assert_response :success
255 assert_response :success
248 assert_equal 'text/csv', @response.content_type
256 assert_equal 'text/csv', @response.content_type
249 lines = @response.body.chomp.split("\n")
257 lines = @response.body.chomp.split("\n")
250 # Headers
258 # Headers
251 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
259 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
252 # Total row
260 # Total row
253 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
261 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
254 end
262 end
255
263
256 def test_details_all_projects
264 def test_details_all_projects
257 get :details
265 get :details
258 assert_response :success
266 assert_response :success
259 assert_template 'details'
267 assert_template 'details'
260 assert_not_nil assigns(:total_hours)
268 assert_not_nil assigns(:total_hours)
261 assert_equal "162.90", "%.2f" % assigns(:total_hours)
269 assert_equal "162.90", "%.2f" % assigns(:total_hours)
262 end
270 end
263
271
264 def test_project_details_routing
272 def test_project_details_routing
265 assert_routing(
273 assert_routing(
266 {:method => :get, :path => '/projects/567/time_entries'},
274 {:method => :get, :path => '/projects/567/time_entries'},
267 :controller => 'timelog', :action => 'details', :project_id => '567'
275 :controller => 'timelog', :action => 'details', :project_id => '567'
268 )
276 )
269 end
277 end
270
278
271 def test_details_at_project_level
279 def test_details_at_project_level
272 get :details, :project_id => 1
280 get :details, :project_id => 1
273 assert_response :success
281 assert_response :success
274 assert_template 'details'
282 assert_template 'details'
275 assert_not_nil assigns(:entries)
283 assert_not_nil assigns(:entries)
276 assert_equal 4, assigns(:entries).size
284 assert_equal 4, assigns(:entries).size
277 # project and subproject
285 # project and subproject
278 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
286 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
279 assert_not_nil assigns(:total_hours)
287 assert_not_nil assigns(:total_hours)
280 assert_equal "162.90", "%.2f" % assigns(:total_hours)
288 assert_equal "162.90", "%.2f" % assigns(:total_hours)
281 # display all time by default
289 # display all time by default
282 assert_equal '2007-03-11'.to_date, assigns(:from)
290 assert_equal '2007-03-11'.to_date, assigns(:from)
283 assert_equal '2007-04-22'.to_date, assigns(:to)
291 assert_equal '2007-04-22'.to_date, assigns(:to)
284 end
292 end
285
293
286 def test_details_at_project_level_with_date_range
294 def test_details_at_project_level_with_date_range
287 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
295 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
288 assert_response :success
296 assert_response :success
289 assert_template 'details'
297 assert_template 'details'
290 assert_not_nil assigns(:entries)
298 assert_not_nil assigns(:entries)
291 assert_equal 3, assigns(:entries).size
299 assert_equal 3, assigns(:entries).size
292 assert_not_nil assigns(:total_hours)
300 assert_not_nil assigns(:total_hours)
293 assert_equal "12.90", "%.2f" % assigns(:total_hours)
301 assert_equal "12.90", "%.2f" % assigns(:total_hours)
294 assert_equal '2007-03-20'.to_date, assigns(:from)
302 assert_equal '2007-03-20'.to_date, assigns(:from)
295 assert_equal '2007-04-30'.to_date, assigns(:to)
303 assert_equal '2007-04-30'.to_date, assigns(:to)
296 end
304 end
297
305
298 def test_details_at_project_level_with_period
306 def test_details_at_project_level_with_period
299 get :details, :project_id => 1, :period => '7_days'
307 get :details, :project_id => 1, :period => '7_days'
300 assert_response :success
308 assert_response :success
301 assert_template 'details'
309 assert_template 'details'
302 assert_not_nil assigns(:entries)
310 assert_not_nil assigns(:entries)
303 assert_not_nil assigns(:total_hours)
311 assert_not_nil assigns(:total_hours)
304 assert_equal Date.today - 7, assigns(:from)
312 assert_equal Date.today - 7, assigns(:from)
305 assert_equal Date.today, assigns(:to)
313 assert_equal Date.today, assigns(:to)
306 end
314 end
307
315
308 def test_issue_details_routing
316 def test_issue_details_routing
309 assert_routing(
317 assert_routing(
310 {:method => :get, :path => 'time_entries'},
318 {:method => :get, :path => 'time_entries'},
311 :controller => 'timelog', :action => 'details'
319 :controller => 'timelog', :action => 'details'
312 )
320 )
313 assert_routing(
321 assert_routing(
314 {:method => :get, :path => '/issues/234/time_entries'},
322 {:method => :get, :path => '/issues/234/time_entries'},
315 :controller => 'timelog', :action => 'details', :issue_id => '234'
323 :controller => 'timelog', :action => 'details', :issue_id => '234'
316 )
324 )
317 # TODO: issue detail page shouldnt link to project_issue_time_entries_path but to normal issues one
325 # TODO: issue detail page shouldnt link to project_issue_time_entries_path but to normal issues one
318 # doesnt seem to have effect on resulting page so controller can be left untouched
326 # doesnt seem to have effect on resulting page so controller can be left untouched
319 assert_routing(
327 assert_routing(
320 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries'},
328 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries'},
321 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123'
329 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123'
322 )
330 )
323 end
331 end
324
332
325 def test_details_at_issue_level
333 def test_details_at_issue_level
326 get :details, :issue_id => 1
334 get :details, :issue_id => 1
327 assert_response :success
335 assert_response :success
328 assert_template 'details'
336 assert_template 'details'
329 assert_not_nil assigns(:entries)
337 assert_not_nil assigns(:entries)
330 assert_equal 2, assigns(:entries).size
338 assert_equal 2, assigns(:entries).size
331 assert_not_nil assigns(:total_hours)
339 assert_not_nil assigns(:total_hours)
332 assert_equal 154.25, assigns(:total_hours)
340 assert_equal 154.25, assigns(:total_hours)
333 # display all time by default
341 # display all time by default
334 assert_equal '2007-03-11'.to_date, assigns(:from)
342 assert_equal '2007-03-11'.to_date, assigns(:from)
335 assert_equal '2007-04-22'.to_date, assigns(:to)
343 assert_equal '2007-04-22'.to_date, assigns(:to)
336 end
344 end
337
345
338 def test_details_formatted_routing
346 def test_details_formatted_routing
339 assert_routing(
347 assert_routing(
340 {:method => :get, :path => 'time_entries.atom'},
348 {:method => :get, :path => 'time_entries.atom'},
341 :controller => 'timelog', :action => 'details', :format => 'atom'
349 :controller => 'timelog', :action => 'details', :format => 'atom'
342 )
350 )
343 assert_routing(
351 assert_routing(
344 {:method => :get, :path => 'time_entries.csv'},
352 {:method => :get, :path => 'time_entries.csv'},
345 :controller => 'timelog', :action => 'details', :format => 'csv'
353 :controller => 'timelog', :action => 'details', :format => 'csv'
346 )
354 )
347 end
355 end
348
356
349 def test_details_for_project_formatted_routing
357 def test_details_for_project_formatted_routing
350 assert_routing(
358 assert_routing(
351 {:method => :get, :path => '/projects/567/time_entries.atom'},
359 {:method => :get, :path => '/projects/567/time_entries.atom'},
352 :controller => 'timelog', :action => 'details', :format => 'atom', :project_id => '567'
360 :controller => 'timelog', :action => 'details', :format => 'atom', :project_id => '567'
353 )
361 )
354 assert_routing(
362 assert_routing(
355 {:method => :get, :path => '/projects/567/time_entries.csv'},
363 {:method => :get, :path => '/projects/567/time_entries.csv'},
356 :controller => 'timelog', :action => 'details', :format => 'csv', :project_id => '567'
364 :controller => 'timelog', :action => 'details', :format => 'csv', :project_id => '567'
357 )
365 )
358 end
366 end
359
367
360 def test_details_for_issue_formatted_routing
368 def test_details_for_issue_formatted_routing
361 assert_routing(
369 assert_routing(
362 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.atom'},
370 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.atom'},
363 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'atom'
371 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'atom'
364 )
372 )
365 assert_routing(
373 assert_routing(
366 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.csv'},
374 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.csv'},
367 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'csv'
375 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'csv'
368 )
376 )
369 end
377 end
370
378
371 def test_details_atom_feed
379 def test_details_atom_feed
372 get :details, :project_id => 1, :format => 'atom'
380 get :details, :project_id => 1, :format => 'atom'
373 assert_response :success
381 assert_response :success
374 assert_equal 'application/atom+xml', @response.content_type
382 assert_equal 'application/atom+xml', @response.content_type
375 assert_not_nil assigns(:items)
383 assert_not_nil assigns(:items)
376 assert assigns(:items).first.is_a?(TimeEntry)
384 assert assigns(:items).first.is_a?(TimeEntry)
377 end
385 end
378
386
379 def test_details_all_projects_csv_export
387 def test_details_all_projects_csv_export
380 Setting.date_format = '%m/%d/%Y'
388 Setting.date_format = '%m/%d/%Y'
381 get :details, :format => 'csv'
389 get :details, :format => 'csv'
382 assert_response :success
390 assert_response :success
383 assert_equal 'text/csv', @response.content_type
391 assert_equal 'text/csv', @response.content_type
384 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
392 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
385 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
393 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
386 end
394 end
387
395
388 def test_details_csv_export
396 def test_details_csv_export
389 Setting.date_format = '%m/%d/%Y'
397 Setting.date_format = '%m/%d/%Y'
390 get :details, :project_id => 1, :format => 'csv'
398 get :details, :project_id => 1, :format => 'csv'
391 assert_response :success
399 assert_response :success
392 assert_equal 'text/csv', @response.content_type
400 assert_equal 'text/csv', @response.content_type
393 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
401 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
394 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
402 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
395 end
403 end
396 end
404 end
General Comments 0
You need to be logged in to leave comments. Login now