##// END OF EJS Templates
Add predefined date ranges to the time report in the same way as the details view (closes #972). It nows defaults to 'All time'....
Jean-Philippe Lang -
r1303:043cb37b161d
parent child
Show More
@@ -0,0 +1,16
1 <fieldset><legend><%= l(:label_date_range) %></legend>
2 <p>
3 <%= radio_button_tag 'period_type', '1', !@free_period %>
4 <%= select_tag 'period', options_for_period_select(params[:period]),
5 :onchange => 'this.form.onsubmit();',
6 :onfocus => '$("period_type_1").checked = true;' %>
7 </p>
8 <p>
9 <%= radio_button_tag 'period_type', '2', @free_period %>
10 <%= l(:label_date_from) %>
11 <%= text_field_tag 'from', @from, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('from') %>
12 <%= l(:label_date_to) %>
13 <%= text_field_tag 'to', @to, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('to') %>
14 <%= submit_tag l(:button_apply), :name => nil, :onclick => '$("period_type_2").checked = true;' %>
15 </p>
16 </fieldset>
@@ -1,241 +1,242
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 }
49 }
50
50
51 @criterias = params[:criterias] || []
51 @criterias = params[:criterias] || []
52 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
52 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
53 @criterias.uniq!
53 @criterias.uniq!
54 @criterias = @criterias[0,3]
54 @criterias = @criterias[0,3]
55
55
56 @columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
56 @columns = (params[:columns] && %w(year month week).include?(params[:columns])) ? params[:columns] : 'month'
57
57
58 if params[:date_from]
58 retrieve_date_range
59 begin; @date_from = params[:date_from].to_date; rescue; end
59 @from ||= TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today
60 end
60 @to ||= TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today
61 if params[:date_to]
62 begin; @date_to = params[:date_to].to_date; rescue; end
63 end
64 @date_from ||= Date.civil(Date.today.year, 1, 1)
65 @date_to ||= (Date.civil(Date.today.year, Date.today.month, 1) >> 1) - 1
66
61
67 unless @criterias.empty?
62 unless @criterias.empty?
68 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
63 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
69 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
64 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
70
65
71 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
66 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
72 sql << " FROM #{TimeEntry.table_name}"
67 sql << " FROM #{TimeEntry.table_name}"
73 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
68 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
74 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
69 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
75 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
70 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
76 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
71 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
77 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
72 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
78 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
73 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
79
74
80 @hours = ActiveRecord::Base.connection.select_all(sql)
75 @hours = ActiveRecord::Base.connection.select_all(sql)
81
76
82 @hours.each do |row|
77 @hours.each do |row|
83 case @columns
78 case @columns
84 when 'year'
79 when 'year'
85 row['year'] = row['tyear']
80 row['year'] = row['tyear']
86 when 'month'
81 when 'month'
87 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
82 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
88 when 'week'
83 when 'week'
89 row['week'] = "#{row['tyear']}-#{row['tweek']}"
84 row['week'] = "#{row['tyear']}-#{row['tweek']}"
90 end
85 end
91 end
86 end
92
87
93 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
88 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
94 end
89
95
90 @periods = []
96 @periods = []
91 # Date#at_beginning_of_ not supported in Rails 1.2.x
97 date_from = @date_from
92 date_from = @from.to_time
98 # 100 columns max
93 # 100 columns max
99 while date_from < @date_to && @periods.length < 100
94 while date_from <= @to.to_time && @periods.length < 100
100 case @columns
95 case @columns
101 when 'year'
96 when 'year'
102 @periods << "#{date_from.year}"
97 @periods << "#{date_from.year}"
103 date_from = date_from >> 12
98 date_from = (date_from + 1.year).at_beginning_of_year
104 when 'month'
99 when 'month'
105 @periods << "#{date_from.year}-#{date_from.month}"
100 @periods << "#{date_from.year}-#{date_from.month}"
106 date_from = date_from >> 1
101 date_from = (date_from + 1.month).at_beginning_of_month
107 when 'week'
102 when 'week'
108 @periods << "#{date_from.year}-#{date_from.cweek}"
103 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
109 date_from = date_from + 7
104 date_from = (date_from + 7.day).at_beginning_of_week
105 end
110 end
106 end
111 end
107 end
112
108
113 render :layout => false if request.xhr?
109 render :layout => false if request.xhr?
114 end
110 end
115
111
116 def details
112 def details
117 sort_init 'spent_on', 'desc'
113 sort_init 'spent_on', 'desc'
118 sort_update
114 sort_update
119
120 @free_period = false
121 @from, @to = nil, nil
122
123 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
124 case params[:period].to_s
125 when 'today'
126 @from = @to = Date.today
127 when 'yesterday'
128 @from = @to = Date.today - 1
129 when 'current_week'
130 @from = Date.today - (Date.today.cwday - 1)%7
131 @to = @from + 6
132 when 'last_week'
133 @from = Date.today - 7 - (Date.today.cwday - 1)%7
134 @to = @from + 6
135 when '7_days'
136 @from = Date.today - 7
137 @to = Date.today
138 when 'current_month'
139 @from = Date.civil(Date.today.year, Date.today.month, 1)
140 @to = (@from >> 1) - 1
141 when 'last_month'
142 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
143 @to = (@from >> 1) - 1
144 when '30_days'
145 @from = Date.today - 30
146 @to = Date.today
147 when 'current_year'
148 @from = Date.civil(Date.today.year, 1, 1)
149 @to = Date.civil(Date.today.year, 12, 31)
150 end
151 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
152 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
153 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
154 @free_period = true
155 else
156 # default
157 end
158
159 @from, @to = @to, @from if @from && @to && @from > @to
160
115
161 cond = ARCondition.new
116 cond = ARCondition.new
162 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
117 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
163 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
118 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
164
119
120 retrieve_date_range
121
165 if @from
122 if @from
166 if @to
123 if @to
167 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
124 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
168 else
125 else
169 cond << ['spent_on >= ?', @from]
126 cond << ['spent_on >= ?', @from]
170 end
127 end
171 elsif @to
128 elsif @to
172 cond << ['spent_on <= ?', @to]
129 cond << ['spent_on <= ?', @to]
173 end
130 end
174
131
175 TimeEntry.visible_by(User.current) do
132 TimeEntry.visible_by(User.current) do
176 respond_to do |format|
133 respond_to do |format|
177 format.html {
134 format.html {
178 # Paginate results
135 # Paginate results
179 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
136 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
180 @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']
181 @entries = TimeEntry.find(:all,
138 @entries = TimeEntry.find(:all,
182 :include => [:project, :activity, :user, {:issue => :tracker}],
139 :include => [:project, :activity, :user, {:issue => :tracker}],
183 :conditions => cond.conditions,
140 :conditions => cond.conditions,
184 :order => sort_clause,
141 :order => sort_clause,
185 :limit => @entry_pages.items_per_page,
142 :limit => @entry_pages.items_per_page,
186 :offset => @entry_pages.current.offset)
143 :offset => @entry_pages.current.offset)
187 @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
188 render :layout => !request.xhr?
145 render :layout => !request.xhr?
189 }
146 }
190 format.csv {
147 format.csv {
191 # Export all entries
148 # Export all entries
192 @entries = TimeEntry.find(:all,
149 @entries = TimeEntry.find(:all,
193 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
150 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
194 :conditions => cond.conditions,
151 :conditions => cond.conditions,
195 :order => sort_clause)
152 :order => sort_clause)
196 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
153 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
197 }
154 }
198 end
155 end
199 end
156 end
200 end
157 end
201
158
202 def edit
159 def edit
203 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
160 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
204 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
161 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
205 @time_entry.attributes = params[:time_entry]
162 @time_entry.attributes = params[:time_entry]
206 if request.post? and @time_entry.save
163 if request.post? and @time_entry.save
207 flash[:notice] = l(:notice_successful_update)
164 flash[:notice] = l(:notice_successful_update)
208 redirect_to :action => 'details', :project_id => @time_entry.project
165 redirect_to :action => 'details', :project_id => @time_entry.project
209 return
166 return
210 end
167 end
211 @activities = Enumeration::get_values('ACTI')
168 @activities = Enumeration::get_values('ACTI')
212 end
169 end
213
170
214 def destroy
171 def destroy
215 render_404 and return unless @time_entry
172 render_404 and return unless @time_entry
216 render_403 and return unless @time_entry.editable_by?(User.current)
173 render_403 and return unless @time_entry.editable_by?(User.current)
217 @time_entry.destroy
174 @time_entry.destroy
218 flash[:notice] = l(:notice_successful_delete)
175 flash[:notice] = l(:notice_successful_delete)
219 redirect_to :back
176 redirect_to :back
220 rescue RedirectBackError
177 rescue RedirectBackError
221 redirect_to :action => 'details', :project_id => @time_entry.project
178 redirect_to :action => 'details', :project_id => @time_entry.project
222 end
179 end
223
180
224 private
181 private
225 def find_project
182 def find_project
226 if params[:id]
183 if params[:id]
227 @time_entry = TimeEntry.find(params[:id])
184 @time_entry = TimeEntry.find(params[:id])
228 @project = @time_entry.project
185 @project = @time_entry.project
229 elsif params[:issue_id]
186 elsif params[:issue_id]
230 @issue = Issue.find(params[:issue_id])
187 @issue = Issue.find(params[:issue_id])
231 @project = @issue.project
188 @project = @issue.project
232 elsif params[:project_id]
189 elsif params[:project_id]
233 @project = Project.find(params[:project_id])
190 @project = Project.find(params[:project_id])
234 else
191 else
235 render_404
192 render_404
236 return false
193 return false
237 end
194 end
238 rescue ActiveRecord::RecordNotFound
195 rescue ActiveRecord::RecordNotFound
239 render_404
196 render_404
240 end
197 end
198
199 # Retreive the date range based on predefined ranges or specific from/to param dates
200 def retrieve_date_range
201 @free_period = false
202 @from, @to = nil, nil
203
204 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
205 case params[:period].to_s
206 when 'today'
207 @from = @to = Date.today
208 when 'yesterday'
209 @from = @to = Date.today - 1
210 when 'current_week'
211 @from = Date.today - (Date.today.cwday - 1)%7
212 @to = @from + 6
213 when 'last_week'
214 @from = Date.today - 7 - (Date.today.cwday - 1)%7
215 @to = @from + 6
216 when '7_days'
217 @from = Date.today - 7
218 @to = Date.today
219 when 'current_month'
220 @from = Date.civil(Date.today.year, Date.today.month, 1)
221 @to = (@from >> 1) - 1
222 when 'last_month'
223 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
224 @to = (@from >> 1) - 1
225 when '30_days'
226 @from = Date.today - 30
227 @to = Date.today
228 when 'current_year'
229 @from = Date.civil(Date.today.year, 1, 1)
230 @to = Date.civil(Date.today.year, 12, 31)
231 end
232 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
233 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
234 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
235 @free_period = true
236 else
237 # default
238 end
239
240 @from, @to = @to, @from if @from && @to && @from > @to
241 end
241 end
242 end
@@ -1,46 +1,30
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}, :class => 'icon icon-report') %>
2 <%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}, :class => 'icon icon-report') %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
4 </div>
4 </div>
5
5
6 <h2><%= l(:label_spent_time) %></h2>
6 <h2><%= l(:label_spent_time) %></h2>
7
7
8 <% if @issue %>
8 <% if @issue %>
9 <h3><%= link_to(@project.name, {:action => 'details', :project_id => @project}) %> / <%= link_to_issue(@issue) %></h3>
9 <h3><%= link_to(@project.name, {:action => 'details', :project_id => @project}) %> / <%= link_to_issue(@issue) %></h3>
10 <% end %>
10 <% end %>
11
11
12 <% form_remote_tag( :url => {}, :method => :get, :update => 'content' ) do %>
12 <% form_remote_tag( :url => {}, :method => :get, :update => 'content' ) do %>
13 <%= hidden_field_tag 'project_id', params[:project_id] %>
13 <%= hidden_field_tag 'project_id', params[:project_id] %>
14 <%= hidden_field_tag 'issue_id', params[:issue_id] if @issue %>
14 <%= hidden_field_tag 'issue_id', params[:issue_id] if @issue %>
15
15 <%= render :partial => 'date_range' %>
16 <fieldset><legend><%= l(:label_date_range) %></legend>
17 <p>
18 <%= radio_button_tag 'period_type', '1', !@free_period %>
19 <%= select_tag 'period', options_for_period_select(params[:period]),
20 :onchange => 'this.form.onsubmit();',
21 :onfocus => '$("period_type_1").checked = true;' %>
22 </p>
23 <p>
24 <%= radio_button_tag 'period_type', '2', @free_period %>
25 <%= l(:label_date_from) %>
26 <%= text_field_tag 'from', @from, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('from') %>
27 <%= l(:label_date_to) %>
28 <%= text_field_tag 'to', @to, :size => 10, :onfocus => '$("period_type_2").checked = true;' %> <%= calendar_for('to') %>
29 <%= submit_tag l(:button_apply), :name => nil, :onclick => '$("period_type_2").checked = true;' %>
30 </p>
31 </fieldset>
32 <% end %>
16 <% end %>
33
17
34 <div class="total-hours">
18 <div class="total-hours">
35 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
19 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
36 </div>
20 </div>
37
21
38 <% unless @entries.empty? %>
22 <% unless @entries.empty? %>
39 <%= render :partial => 'list', :locals => { :entries => @entries }%>
23 <%= render :partial => 'list', :locals => { :entries => @entries }%>
40 <p class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></p>
24 <p class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></p>
41
25
42 <p class="other-formats">
26 <p class="other-formats">
43 <%= l(:label_export_to) %>
27 <%= l(:label_export_to) %>
44 <span><%= link_to 'CSV', params.merge(:format => 'csv'), :class => 'csv' %></span>
28 <span><%= link_to 'CSV', params.merge(:format => 'csv'), :class => 'csv' %></span>
45 </p>
29 </p>
46 <% end %>
30 <% end %>
@@ -1,65 +1,59
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to(l(:label_details), {:controller => 'timelog', :action => 'details', :project_id => @project}, :class => 'icon icon-details') %>
2 <%= link_to(l(:label_details), {:controller => 'timelog', :action => 'details', :project_id => @project}, :class => 'icon icon-details') %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time' %>
4 </div>
4 </div>
5
5
6 <h2><%= l(:label_spent_time) %></h2>
6 <h2><%= l(:label_spent_time) %></h2>
7
7
8 <% form_remote_tag(:url => {:project_id => @project}, :update => 'content') do %>
8 <% form_remote_tag(:url => {}, :update => 'content') do %>
9 <% @criterias.each do |criteria| %>
9 <% @criterias.each do |criteria| %>
10 <%= hidden_field_tag 'criterias[]', criteria %>
10 <%= hidden_field_tag 'criterias[]', criteria %>
11 <% end %>
11 <% end %>
12 <fieldset><legend><%= l(:label_date_range) %></legend>
12 <%= hidden_field_tag 'project_id', params[:project_id] %>
13 <p>
13 <%= render :partial => 'date_range' %>
14 <%= l(:label_date_from) %>
15 <%= text_field_tag 'date_from', @date_from, :size => 10 %><%= calendar_for('date_from') %>
16 <%= l(:label_date_to) %>
17 <%= text_field_tag 'date_to', @date_to, :size => 10 %><%= calendar_for('date_to') %>
18 <%= l(:label_details) %>
19 <%= select_tag 'period', options_for_select([[l(:label_year), 'year'],
20 [l(:label_month), 'month'],
21 [l(:label_week), 'week']], @columns) %>
22 &nbsp;
23 <%= submit_tag l(:button_apply) %>
24 </p>
14 </p>
25 </fieldset>
15 </fieldset>
16 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
17 [l(:label_month), 'month'],
18 [l(:label_week), 'week']], @columns),
19 :onchange => "this.form.onsubmit();" %>
26
20
27 <p><%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
21 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
28 :onchange => "this.form.onsubmit();",
22 :onchange => "this.form.onsubmit();",
29 :style => 'width: 200px',
23 :style => 'width: 200px',
30 :disabled => (@criterias.length >= 3)) %>
24 :disabled => (@criterias.length >= 3)) %>
31 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :date_from => @date_from, :date_to => @date_to, :period => @columns}, :update => 'content'},
25 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :date_from => @date_from, :date_to => @date_to, :period => @columns}, :update => 'content'},
32 :class => 'icon icon-reload' %></p>
26 :class => 'icon icon-reload' %></p>
33
27 <% end %>
28
34 <% unless @criterias.empty? %>
29 <% unless @criterias.empty? %>
35 <div class="total-hours">
30 <div class="total-hours">
36 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
31 <p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
37 </div>
32 </div>
38
33
39 <% unless @hours.empty? %>
34 <% unless @hours.empty? %>
40 <table class="list" id="time-report">
35 <table class="list" id="time-report">
41 <thead>
36 <thead>
42 <tr>
37 <tr>
43 <% @criterias.each do |criteria| %>
38 <% @criterias.each do |criteria| %>
44 <th width="15%"><%= l(@available_criterias[criteria][:label]) %></th>
39 <th width="15%"><%= l(@available_criterias[criteria][:label]) %></th>
45 <% end %>
40 <% end %>
46 <% @periods.each do |period| %>
41 <% @periods.each do |period| %>
47 <th width="<%= ((100 - @criterias.length * 15 - 15 ) / @periods.length).to_i %>%"><%= period %></th>
42 <th width="<%= ((100 - @criterias.length * 15 - 15 ) / @periods.length).to_i %>%"><%= period %></th>
48 <% end %>
43 <% end %>
49 </tr>
44 </tr>
50 </thead>
45 </thead>
51 <tbody>
46 <tbody>
52 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
47 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
53 <tr class="total">
48 <tr class="total">
54 <td><%= l(:label_total) %></td>
49 <td><%= l(:label_total) %></td>
55 <%= '<td></td>' * (@criterias.size - 1) %>
50 <%= '<td></td>' * (@criterias.size - 1) %>
56 <% @periods.each do |period| -%>
51 <% @periods.each do |period| -%>
57 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)) %>
52 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)) %>
58 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
53 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
59 <% end -%>
54 <% end -%>
60 </tr>
55 </tr>
61 </tbody>
56 </tbody>
62 </table>
57 </table>
63 <% end %>
58 <% end %>
64 <% end %>
59 <% end %>
65 <% end %>
@@ -1,163 +1,171
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, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
25 fixtures :projects, :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
83 get :report, :project_id => 1, :criterias => ['project']
84 assert_response :success
85 assert_template 'report'
86 assert_not_nil assigns(:total_hours)
87 assert_equal "162.90", "%.2f" % assigns(:total_hours)
88 end
89
82 def test_report_one_criteria
90 def test_report_one_criteria
83 get :report, :project_id => 1, :period => 'week', :date_from => "2007-04-01", :date_to => "2007-04-30", :criterias => ['project']
91 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
84 assert_response :success
92 assert_response :success
85 assert_template 'report'
93 assert_template 'report'
86 assert_not_nil assigns(:total_hours)
94 assert_not_nil assigns(:total_hours)
87 assert_equal "8.65", "%.2f" % assigns(:total_hours)
95 assert_equal "8.65", "%.2f" % assigns(:total_hours)
88 end
96 end
89
97
90 def test_report_two_criterias
98 def test_report_two_criterias
91 get :report, :project_id => 1, :period => 'month', :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
99 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
92 assert_response :success
100 assert_response :success
93 assert_template 'report'
101 assert_template 'report'
94 assert_not_nil assigns(:total_hours)
102 assert_not_nil assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
103 assert_equal "162.90", "%.2f" % assigns(:total_hours)
96 end
104 end
97
105
98 def test_report_one_criteria_no_result
106 def test_report_one_criteria_no_result
99 get :report, :project_id => 1, :period => 'week', :date_from => "1998-04-01", :date_to => "1998-04-30", :criterias => ['project']
107 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
100 assert_response :success
108 assert_response :success
101 assert_template 'report'
109 assert_template 'report'
102 assert_not_nil assigns(:total_hours)
110 assert_not_nil assigns(:total_hours)
103 assert_equal "0.00", "%.2f" % assigns(:total_hours)
111 assert_equal "0.00", "%.2f" % assigns(:total_hours)
104 end
112 end
105
113
106 def test_details_at_project_level
114 def test_details_at_project_level
107 get :details, :project_id => 1
115 get :details, :project_id => 1
108 assert_response :success
116 assert_response :success
109 assert_template 'details'
117 assert_template 'details'
110 assert_not_nil assigns(:entries)
118 assert_not_nil assigns(:entries)
111 assert_equal 4, assigns(:entries).size
119 assert_equal 4, assigns(:entries).size
112 # project and subproject
120 # project and subproject
113 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
121 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
114 assert_not_nil assigns(:total_hours)
122 assert_not_nil assigns(:total_hours)
115 assert_equal "162.90", "%.2f" % assigns(:total_hours)
123 assert_equal "162.90", "%.2f" % assigns(:total_hours)
116 # display all time by default
124 # display all time by default
117 assert_nil assigns(:from)
125 assert_nil assigns(:from)
118 assert_nil assigns(:to)
126 assert_nil assigns(:to)
119 end
127 end
120
128
121 def test_details_at_project_level_with_date_range
129 def test_details_at_project_level_with_date_range
122 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
130 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
123 assert_response :success
131 assert_response :success
124 assert_template 'details'
132 assert_template 'details'
125 assert_not_nil assigns(:entries)
133 assert_not_nil assigns(:entries)
126 assert_equal 3, assigns(:entries).size
134 assert_equal 3, assigns(:entries).size
127 assert_not_nil assigns(:total_hours)
135 assert_not_nil assigns(:total_hours)
128 assert_equal "12.90", "%.2f" % assigns(:total_hours)
136 assert_equal "12.90", "%.2f" % assigns(:total_hours)
129 assert_equal '2007-03-20'.to_date, assigns(:from)
137 assert_equal '2007-03-20'.to_date, assigns(:from)
130 assert_equal '2007-04-30'.to_date, assigns(:to)
138 assert_equal '2007-04-30'.to_date, assigns(:to)
131 end
139 end
132
140
133 def test_details_at_project_level_with_period
141 def test_details_at_project_level_with_period
134 get :details, :project_id => 1, :period => '7_days'
142 get :details, :project_id => 1, :period => '7_days'
135 assert_response :success
143 assert_response :success
136 assert_template 'details'
144 assert_template 'details'
137 assert_not_nil assigns(:entries)
145 assert_not_nil assigns(:entries)
138 assert_not_nil assigns(:total_hours)
146 assert_not_nil assigns(:total_hours)
139 assert_equal Date.today - 7, assigns(:from)
147 assert_equal Date.today - 7, assigns(:from)
140 assert_equal Date.today, assigns(:to)
148 assert_equal Date.today, assigns(:to)
141 end
149 end
142
150
143 def test_details_at_issue_level
151 def test_details_at_issue_level
144 get :details, :issue_id => 1
152 get :details, :issue_id => 1
145 assert_response :success
153 assert_response :success
146 assert_template 'details'
154 assert_template 'details'
147 assert_not_nil assigns(:entries)
155 assert_not_nil assigns(:entries)
148 assert_equal 2, assigns(:entries).size
156 assert_equal 2, assigns(:entries).size
149 assert_not_nil assigns(:total_hours)
157 assert_not_nil assigns(:total_hours)
150 assert_equal 154.25, assigns(:total_hours)
158 assert_equal 154.25, assigns(:total_hours)
151 # display all time by default
159 # display all time by default
152 assert_nil assigns(:from)
160 assert_nil assigns(:from)
153 assert_nil assigns(:to)
161 assert_nil assigns(:to)
154 end
162 end
155
163
156 def test_details_csv_export
164 def test_details_csv_export
157 get :details, :project_id => 1, :format => 'csv'
165 get :details, :project_id => 1, :format => 'csv'
158 assert_response :success
166 assert_response :success
159 assert_equal 'text/csv', @response.content_type
167 assert_equal 'text/csv', @response.content_type
160 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
168 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
161 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
169 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
162 end
170 end
163 end
171 end
General Comments 0
You need to be logged in to leave comments. Login now