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