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