##// END OF EJS Templates
Fixed: Details time log report CSV export doesn't honour date format from settings (patch #2466 by Russell Hind)....
Jean-Philippe Lang -
r2300:11346455a120
parent child
Show More
@@ -1,158 +1,160
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 include ApplicationHelper
20
19 21 def render_timelog_breadcrumb
20 22 links = []
21 23 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
22 24 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
23 25 links << link_to_issue(@issue) if @issue
24 26 breadcrumb links
25 27 end
26 28
27 29 def activity_collection_for_select_options
28 30 activities = Enumeration::get_values('ACTI')
29 31 collection = []
30 32 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
31 33 activities.each { |a| collection << [a.name, a.id] }
32 34 collection
33 35 end
34 36
35 37 def select_hours(data, criteria, value)
36 38 data.select {|row| row[criteria] == value}
37 39 end
38 40
39 41 def sum_hours(data)
40 42 sum = 0
41 43 data.each do |row|
42 44 sum += row['hours'].to_f
43 45 end
44 46 sum
45 47 end
46 48
47 49 def options_for_period_select(value)
48 50 options_for_select([[l(:label_all_time), 'all'],
49 51 [l(:label_today), 'today'],
50 52 [l(:label_yesterday), 'yesterday'],
51 53 [l(:label_this_week), 'current_week'],
52 54 [l(:label_last_week), 'last_week'],
53 55 [l(:label_last_n_days, 7), '7_days'],
54 56 [l(:label_this_month), 'current_month'],
55 57 [l(:label_last_month), 'last_month'],
56 58 [l(:label_last_n_days, 30), '30_days'],
57 59 [l(:label_this_year), 'current_year']],
58 60 value)
59 61 end
60 62
61 63 def entries_to_csv(entries)
62 64 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
63 65 decimal_separator = l(:general_csv_decimal_separator)
64 66 custom_fields = TimeEntryCustomField.find(:all)
65 67 export = StringIO.new
66 68 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
67 69 # csv header fields
68 70 headers = [l(:field_spent_on),
69 71 l(:field_user),
70 72 l(:field_activity),
71 73 l(:field_project),
72 74 l(:field_issue),
73 75 l(:field_tracker),
74 76 l(:field_subject),
75 77 l(:field_hours),
76 78 l(:field_comments)
77 79 ]
78 80 # Export custom fields
79 81 headers += custom_fields.collect(&:name)
80 82
81 83 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
82 84 # csv lines
83 85 entries.each do |entry|
84 fields = [l_date(entry.spent_on),
86 fields = [format_date(entry.spent_on),
85 87 entry.user,
86 88 entry.activity,
87 89 entry.project,
88 90 (entry.issue ? entry.issue.id : nil),
89 91 (entry.issue ? entry.issue.tracker : nil),
90 92 (entry.issue ? entry.issue.subject : nil),
91 93 entry.hours.to_s.gsub('.', decimal_separator),
92 94 entry.comments
93 95 ]
94 96 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
95 97
96 98 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
97 99 end
98 100 end
99 101 export.rewind
100 102 export
101 103 end
102 104
103 105 def format_criteria_value(criteria, value)
104 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]))
105 107 end
106 108
107 109 def report_to_csv(criterias, periods, hours)
108 110 export = StringIO.new
109 111 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
110 112 # Column headers
111 113 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
112 114 headers += periods
113 115 headers << l(:label_total)
114 116 csv << headers.collect {|c| to_utf8(c) }
115 117 # Content
116 118 report_criteria_to_csv(csv, criterias, periods, hours)
117 119 # Total row
118 120 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
119 121 total = 0
120 122 periods.each do |period|
121 123 sum = sum_hours(select_hours(hours, @columns, period.to_s))
122 124 total += sum
123 125 row << (sum > 0 ? "%.2f" % sum : '')
124 126 end
125 127 row << "%.2f" %total
126 128 csv << row
127 129 end
128 130 export.rewind
129 131 export
130 132 end
131 133
132 134 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
133 135 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
134 136 hours_for_value = select_hours(hours, criterias[level], value)
135 137 next if hours_for_value.empty?
136 138 row = [''] * level
137 139 row << to_utf8(format_criteria_value(criterias[level], value))
138 140 row += [''] * (criterias.length - level - 1)
139 141 total = 0
140 142 periods.each do |period|
141 143 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
142 144 total += sum
143 145 row << (sum > 0 ? "%.2f" % sum : '')
144 146 end
145 147 row << "%.2f" %total
146 148 csv << row
147 149
148 150 if criterias.length > level + 1
149 151 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
150 152 end
151 153 end
152 154 end
153 155
154 156 def to_utf8(s)
155 157 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
156 158 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
157 159 end
158 160 end
General Comments 0
You need to be logged in to leave comments. Login now