@@ -1,192 +1,192 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | module TimelogHelper |
|
18 | module TimelogHelper | |
19 | include ApplicationHelper |
|
19 | include ApplicationHelper | |
20 |
|
20 | |||
21 | def render_timelog_breadcrumb |
|
21 | def render_timelog_breadcrumb | |
22 | links = [] |
|
22 | links = [] | |
23 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) |
|
23 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) | |
24 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project |
|
24 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project | |
25 | if @issue |
|
25 | if @issue | |
26 | if @issue.visible? |
|
26 | if @issue.visible? | |
27 | links << link_to_issue(@issue, :subject => false) |
|
27 | links << link_to_issue(@issue, :subject => false) | |
28 | else |
|
28 | else | |
29 | links << "##{@issue.id}" |
|
29 | links << "##{@issue.id}" | |
30 | end |
|
30 | end | |
31 | end |
|
31 | end | |
32 | breadcrumb links |
|
32 | breadcrumb links | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | # Returns a collection of activities for a select field. time_entry |
|
35 | # Returns a collection of activities for a select field. time_entry | |
36 | # is optional and will be used to check if the selected TimeEntryActivity |
|
36 | # is optional and will be used to check if the selected TimeEntryActivity | |
37 | # is active. |
|
37 | # is active. | |
38 | def activity_collection_for_select_options(time_entry=nil, project=nil) |
|
38 | def activity_collection_for_select_options(time_entry=nil, project=nil) | |
39 | project ||= @project |
|
39 | project ||= @project | |
40 | if project.nil? |
|
40 | if project.nil? | |
41 | activities = TimeEntryActivity.shared.active |
|
41 | activities = TimeEntryActivity.shared.active | |
42 | else |
|
42 | else | |
43 | activities = project.activities |
|
43 | activities = project.activities | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | collection = [] |
|
46 | collection = [] | |
47 | if time_entry && time_entry.activity && !time_entry.activity.active? |
|
47 | if time_entry && time_entry.activity && !time_entry.activity.active? | |
48 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] |
|
48 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] | |
49 | else |
|
49 | else | |
50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) |
|
50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) | |
51 | end |
|
51 | end | |
52 | activities.each { |a| collection << [a.name, a.id] } |
|
52 | activities.each { |a| collection << [a.name, a.id] } | |
53 | collection |
|
53 | collection | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def select_hours(data, criteria, value) |
|
56 | def select_hours(data, criteria, value) | |
57 | if value.to_s.empty? |
|
57 | if value.to_s.empty? | |
58 | data.select {|row| row[criteria].blank? } |
|
58 | data.select {|row| row[criteria].blank? } | |
59 | else |
|
59 | else | |
60 | data.select {|row| row[criteria].to_s == value.to_s} |
|
60 | data.select {|row| row[criteria].to_s == value.to_s} | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def sum_hours(data) |
|
64 | def sum_hours(data) | |
65 | sum = 0 |
|
65 | sum = 0 | |
66 | data.each do |row| |
|
66 | data.each do |row| | |
67 | sum += row['hours'].to_f |
|
67 | sum += row['hours'].to_f | |
68 | end |
|
68 | end | |
69 | sum |
|
69 | sum | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def options_for_period_select(value) |
|
72 | def options_for_period_select(value) | |
73 | options_for_select([[l(:label_all_time), 'all'], |
|
73 | options_for_select([[l(:label_all_time), 'all'], | |
74 | [l(:label_today), 'today'], |
|
74 | [l(:label_today), 'today'], | |
75 | [l(:label_yesterday), 'yesterday'], |
|
75 | [l(:label_yesterday), 'yesterday'], | |
76 | [l(:label_this_week), 'current_week'], |
|
76 | [l(:label_this_week), 'current_week'], | |
77 | [l(:label_last_week), 'last_week'], |
|
77 | [l(:label_last_week), 'last_week'], | |
78 | [l(:label_last_n_days, 7), '7_days'], |
|
78 | [l(:label_last_n_days, 7), '7_days'], | |
79 | [l(:label_this_month), 'current_month'], |
|
79 | [l(:label_this_month), 'current_month'], | |
80 | [l(:label_last_month), 'last_month'], |
|
80 | [l(:label_last_month), 'last_month'], | |
81 | [l(:label_last_n_days, 30), '30_days'], |
|
81 | [l(:label_last_n_days, 30), '30_days'], | |
82 | [l(:label_this_year), 'current_year']], |
|
82 | [l(:label_this_year), 'current_year']], | |
83 | value) |
|
83 | value) | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | def entries_to_csv(entries) |
|
86 | def entries_to_csv(entries) | |
87 | decimal_separator = l(:general_csv_decimal_separator) |
|
87 | decimal_separator = l(:general_csv_decimal_separator) | |
88 | custom_fields = TimeEntryCustomField.find(:all) |
|
88 | custom_fields = TimeEntryCustomField.find(:all) | |
89 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
89 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
90 | # csv header fields |
|
90 | # csv header fields | |
91 | headers = [l(:field_spent_on), |
|
91 | headers = [l(:field_spent_on), | |
92 | l(:field_user), |
|
92 | l(:field_user), | |
93 | l(:field_activity), |
|
93 | l(:field_activity), | |
94 | l(:field_project), |
|
94 | l(:field_project), | |
95 | l(:field_issue), |
|
95 | l(:field_issue), | |
96 | l(:field_tracker), |
|
96 | l(:field_tracker), | |
97 | l(:field_subject), |
|
97 | l(:field_subject), | |
98 | l(:field_hours), |
|
98 | l(:field_hours), | |
99 | l(:field_comments) |
|
99 | l(:field_comments) | |
100 | ] |
|
100 | ] | |
101 | # Export custom fields |
|
101 | # Export custom fields | |
102 | headers += custom_fields.collect(&:name) |
|
102 | headers += custom_fields.collect(&:name) | |
103 |
|
103 | |||
104 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
104 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( | |
105 | c.to_s, |
|
105 | c.to_s, | |
106 | l(:general_csv_encoding) ) } |
|
106 | l(:general_csv_encoding) ) } | |
107 | # csv lines |
|
107 | # csv lines | |
108 | entries.each do |entry| |
|
108 | entries.each do |entry| | |
109 | fields = [format_date(entry.spent_on), |
|
109 | fields = [format_date(entry.spent_on), | |
110 | entry.user, |
|
110 | entry.user, | |
111 | entry.activity, |
|
111 | entry.activity, | |
112 | entry.project, |
|
112 | entry.project, | |
113 | (entry.issue ? entry.issue.id : nil), |
|
113 | (entry.issue ? entry.issue.id : nil), | |
114 | (entry.issue ? entry.issue.tracker : nil), |
|
114 | (entry.issue ? entry.issue.tracker : nil), | |
115 | (entry.issue ? entry.issue.subject : nil), |
|
115 | (entry.issue ? entry.issue.subject : nil), | |
116 | entry.hours.to_s.gsub('.', decimal_separator), |
|
116 | entry.hours.to_s.gsub('.', decimal_separator), | |
117 | entry.comments |
|
117 | entry.comments | |
118 | ] |
|
118 | ] | |
119 | fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) } |
|
119 | fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) } | |
120 |
|
120 | |||
121 | csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
121 | csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8( | |
122 | c.to_s, |
|
122 | c.to_s, | |
123 | l(:general_csv_encoding) ) } |
|
123 | l(:general_csv_encoding) ) } | |
124 | end |
|
124 | end | |
125 | end |
|
125 | end | |
126 | export |
|
126 | export | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def format_criteria_value(criteria, value) |
|
129 | def format_criteria_value(criteria, value) | |
130 | if value.blank? |
|
130 | if value.blank? | |
131 | l(:label_none) |
|
131 | l(:label_none) | |
132 | elsif k = @available_criterias[criteria][:klass] |
|
132 | elsif k = @available_criterias[criteria][:klass] | |
133 | obj = k.find_by_id(value.to_i) |
|
133 | obj = k.find_by_id(value.to_i) | |
134 | if obj.is_a?(Issue) |
|
134 | if obj.is_a?(Issue) | |
135 |
obj.visible? ? |
|
135 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" | |
136 | else |
|
136 | else | |
137 | obj |
|
137 | obj | |
138 | end |
|
138 | end | |
139 | else |
|
139 | else | |
140 | format_value(value, @available_criterias[criteria][:format]) |
|
140 | format_value(value, @available_criterias[criteria][:format]) | |
141 | end |
|
141 | end | |
142 | end |
|
142 | end | |
143 |
|
143 | |||
144 | def report_to_csv(criterias, periods, hours) |
|
144 | def report_to_csv(criterias, periods, hours) | |
145 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
145 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
146 | # Column headers |
|
146 | # Column headers | |
147 | headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) } |
|
147 | headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) } | |
148 | headers += periods |
|
148 | headers += periods | |
149 | headers << l(:label_total) |
|
149 | headers << l(:label_total) | |
150 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
150 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( | |
151 | c.to_s, |
|
151 | c.to_s, | |
152 | l(:general_csv_encoding) ) } |
|
152 | l(:general_csv_encoding) ) } | |
153 | # Content |
|
153 | # Content | |
154 | report_criteria_to_csv(csv, criterias, periods, hours) |
|
154 | report_criteria_to_csv(csv, criterias, periods, hours) | |
155 | # Total row |
|
155 | # Total row | |
156 | row = [ l(:label_total) ] + [''] * (criterias.size - 1) |
|
156 | row = [ l(:label_total) ] + [''] * (criterias.size - 1) | |
157 | total = 0 |
|
157 | total = 0 | |
158 | periods.each do |period| |
|
158 | periods.each do |period| | |
159 | sum = sum_hours(select_hours(hours, @columns, period.to_s)) |
|
159 | sum = sum_hours(select_hours(hours, @columns, period.to_s)) | |
160 | total += sum |
|
160 | total += sum | |
161 | row << (sum > 0 ? "%.2f" % sum : '') |
|
161 | row << (sum > 0 ? "%.2f" % sum : '') | |
162 | end |
|
162 | end | |
163 | row << "%.2f" %total |
|
163 | row << "%.2f" %total | |
164 | csv << row |
|
164 | csv << row | |
165 | end |
|
165 | end | |
166 | export |
|
166 | export | |
167 | end |
|
167 | end | |
168 |
|
168 | |||
169 | def report_criteria_to_csv(csv, criterias, periods, hours, level=0) |
|
169 | def report_criteria_to_csv(csv, criterias, periods, hours, level=0) | |
170 | hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| |
|
170 | hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| | |
171 | hours_for_value = select_hours(hours, criterias[level], value) |
|
171 | hours_for_value = select_hours(hours, criterias[level], value) | |
172 | next if hours_for_value.empty? |
|
172 | next if hours_for_value.empty? | |
173 | row = [''] * level |
|
173 | row = [''] * level | |
174 | row << Redmine::CodesetUtil.from_utf8( |
|
174 | row << Redmine::CodesetUtil.from_utf8( | |
175 | format_criteria_value(criterias[level], value).to_s, |
|
175 | format_criteria_value(criterias[level], value).to_s, | |
176 | l(:general_csv_encoding) ) |
|
176 | l(:general_csv_encoding) ) | |
177 | row += [''] * (criterias.length - level - 1) |
|
177 | row += [''] * (criterias.length - level - 1) | |
178 | total = 0 |
|
178 | total = 0 | |
179 | periods.each do |period| |
|
179 | periods.each do |period| | |
180 | sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) |
|
180 | sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) | |
181 | total += sum |
|
181 | total += sum | |
182 | row << (sum > 0 ? "%.2f" % sum : '') |
|
182 | row << (sum > 0 ? "%.2f" % sum : '') | |
183 | end |
|
183 | end | |
184 | row << "%.2f" %total |
|
184 | row << "%.2f" %total | |
185 | csv << row |
|
185 | csv << row | |
186 |
|
186 | |||
187 | if criterias.length > level + 1 |
|
187 | if criterias.length > level + 1 | |
188 | report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1) |
|
188 | report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1) | |
189 | end |
|
189 | end | |
190 | end |
|
190 | end | |
191 | end |
|
191 | end | |
192 | end |
|
192 | end |
General Comments 0
You need to be logged in to leave comments.
Login now