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