timelog_helper.rb
121 lines
| 4.0 KiB
| text/x-ruby
|
RubyLexer
|
r8090 | # encoding: utf-8 | ||
# | ||||
|
r6791 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r569 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6791 | # | ||
|
r569 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6791 | # | ||
|
r569 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r365 | module TimelogHelper | ||
|
r2300 | include ApplicationHelper | ||
|
r6791 | |||
|
r2832 | # Returns a collection of activities for a select field. time_entry | ||
# is optional and will be used to check if the selected TimeEntryActivity | ||||
# is active. | ||||
|
r2834 | def activity_collection_for_select_options(time_entry=nil, project=nil) | ||
|
r13905 | project ||= time_entry.try(:project) | ||
|
r2834 | project ||= @project | ||
if project.nil? | ||||
|
r2969 | activities = TimeEntryActivity.shared.active | ||
|
r2834 | else | ||
activities = project.activities | ||||
end | ||||
|
r1588 | collection = [] | ||
|
r2834 | if time_entry && time_entry.activity && !time_entry.activity.active? | ||
|
r2832 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] | ||
else | ||||
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) | ||||
end | ||||
|
r1588 | activities.each { |a| collection << [a.name, a.id] } | ||
collection | ||||
end | ||||
|
r6791 | |||
|
r569 | def select_hours(data, criteria, value) | ||
|
r11176 | if value.to_s.empty? | ||
data.select {|row| row[criteria].blank? } | ||||
|
r6791 | else | ||
|
r11176 | data.select {|row| row[criteria].to_s == value.to_s} | ||
|
r2554 | end | ||
|
r569 | end | ||
|
r6791 | |||
|
r569 | def sum_hours(data) | ||
sum = 0 | ||||
data.each do |row| | ||||
sum += row['hours'].to_f | ||||
end | ||||
sum | ||||
end | ||||
|
r6791 | |||
|
r7906 | def format_criteria_value(criteria_options, value) | ||
|
r2929 | if value.blank? | ||
|
r8524 | "[#{l(:label_none)}]" | ||
|
r7906 | elsif k = criteria_options[:klass] | ||
|
r2929 | obj = k.find_by_id(value.to_i) | ||
if obj.is_a?(Issue) | ||||
|
r7821 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" | ||
|
r2929 | else | ||
obj | ||||
end | ||||
|
r12125 | elsif cf = criteria_options[:custom_field] | ||
format_value(value, cf) | ||||
|
r2929 | else | ||
|
r12125 | value.to_s | ||
|
r2929 | end | ||
|
r1325 | end | ||
|
r6791 | |||
|
r7906 | def report_to_csv(report) | ||
|
r13920 | Redmine::Export::CSV.generate do |csv| | ||
|
r1323 | # Column headers | ||
|
r7906 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } | ||
headers += report.periods | ||||
|
r11352 | headers << l(:label_total_time) | ||
|
r13920 | csv << headers | ||
|
r1323 | # Content | ||
|
r7906 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) | ||
|
r1323 | # Total row | ||
|
r13920 | str_total = l(:label_total_time) | ||
|
r7906 | row = [ str_total ] + [''] * (report.criteria.size - 1) | ||
|
r1323 | total = 0 | ||
|
r7906 | report.periods.each do |period| | ||
sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) | ||||
|
r1323 | total += sum | ||
|
r13920 | row << (sum > 0 ? sum : '') | ||
|
r1323 | end | ||
|
r13920 | row << total | ||
|
r1323 | csv << row | ||
end | ||||
end | ||||
|
r6791 | |||
|
r7906 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) | ||
hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| | ||||
hours_for_value = select_hours(hours, criteria[level], value) | ||||
|
r1323 | next if hours_for_value.empty? | ||
row = [''] * level | ||||
|
r13920 | row << format_criteria_value(available_criteria[criteria[level]], value).to_s | ||
|
r7906 | row += [''] * (criteria.length - level - 1) | ||
|
r1323 | total = 0 | ||
periods.each do |period| | ||||
|
r7906 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) | ||
|
r1323 | total += sum | ||
|
r13920 | row << (sum > 0 ? sum : '') | ||
|
r1323 | end | ||
|
r13920 | row << total | ||
|
r1323 | csv << row | ||
|
r7906 | if criteria.length > level + 1 | ||
report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) | ||||
|
r1323 | end | ||
end | ||||
end | ||||
|
r365 | end | ||