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