1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 include ApplicationHelper
21 def render_timelog_breadcrumb
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
27 links << link_to_issue(@issue, :subject => false)
29 links << "##{@issue.id}"
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
38 def activity_collection_for_select_options(time_entry=nil, project=nil)
41 activities = TimeEntryActivity.shared.active
43 activities = project.activities
47 if time_entry && time_entry.activity && !time_entry.activity.active?
48 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
52 activities.each { |a| collection << [a.name, a.id] }
56 def select_hours(data, criteria, value)
58 data.select {|row| row[criteria].blank? }
60 data.select {|row| row[criteria].to_s == value.to_s}
67 sum += row['hours'].to_f
72 def options_for_period_select(value)
73 options_for_select([[l(:label_all_time), 'all'],
74 [l(:label_today), 'today'],
75 [l(:label_yesterday), 'yesterday'],
76 [l(:label_this_week), 'current_week'],
77 [l(:label_last_week), 'last_week'],
78 [l(:label_last_n_days, 7), '7_days'],
79 [l(:label_this_month), 'current_month'],
80 [l(:label_last_month), 'last_month'],
81 [l(:label_last_n_days, 30), '30_days'],
82 [l(:label_this_year), 'current_year']],
86 def entries_to_csv(entries)
87 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
88 decimal_separator = l(:general_csv_decimal_separator)
89 custom_fields = TimeEntryCustomField.find(:all)
90 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
92 headers = [l(:field_spent_on),
102 # Export custom fields
103 headers += custom_fields.collect(&:name)
105 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
107 entries.each do |entry|
108 fields = [format_date(entry.spent_on),
112 (entry.issue ? entry.issue.id : nil),
113 (entry.issue ? entry.issue.tracker : nil),
114 (entry.issue ? entry.issue.subject : nil),
115 entry.hours.to_s.gsub('.', decimal_separator),
118 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
120 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
126 def format_criteria_value(criteria, value)
129 elsif k = @available_criterias[criteria][:klass]
130 obj = k.find_by_id(value.to_i)
132 obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
137 format_value(value, @available_criterias[criteria][:format])
141 def report_to_csv(criterias, periods, hours)
142 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
144 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
146 headers << l(:label_total)
147 csv << headers.collect {|c| to_utf8(c) }
149 report_criteria_to_csv(csv, criterias, periods, hours)
151 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
153 periods.each do |period|
154 sum = sum_hours(select_hours(hours, @columns, period.to_s))
156 row << (sum > 0 ? "%.2f" % sum : '')
164 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
165 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
166 hours_for_value = select_hours(hours, criterias[level], value)
167 next if hours_for_value.empty?
169 row << to_utf8(format_criteria_value(criterias[level], value))
170 row += [''] * (criterias.length - level - 1)
172 periods.each do |period|
173 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
175 row << (sum > 0 ? "%.2f" % sum : '')
180 if criterias.length > level + 1
181 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
187 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
188 begin; @ic.iconv(s.to_s); rescue; s.to_s; end