timelog_helper.rb 4.25 KB
Newer Older
1 2
# encoding: utf-8
#
3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
jplang's avatar
jplang committed
5 6 7 8 9
#
# 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.
10
#
jplang's avatar
jplang committed
11 12 13 14
# 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.
15
#
jplang's avatar
jplang committed
16 17 18 19
# 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.

20
module TimelogHelper
21
  include ApplicationHelper
22

23 24 25
  # 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.
26
  def activity_collection_for_select_options(time_entry=nil, project=nil)
27
    project ||= time_entry.try(:project)
28 29
    project ||= @project
    if project.nil?
30
      activities = TimeEntryActivity.shared.active
31 32 33 34
    else
      activities = project.activities
    end

35
    collection = []
36
    if time_entry && time_entry.activity && !time_entry.activity.active?
37 38 39 40
      collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
    else
      collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
    end
41 42 43
    activities.each { |a| collection << [a.name, a.id] }
    collection
  end
44

45 46 47 48 49
  def user_collection_for_select_options(time_entry)
    collection = time_entry.assignable_users
    principals_options_for_select(collection, time_entry.user_id)
  end

jplang's avatar
jplang committed
50
  def select_hours(data, criteria, value)
51 52
    if value.to_s.empty?
      data.select {|row| row[criteria].blank? }
53
    else
54
      data.select {|row| row[criteria].to_s == value.to_s}
55
    end
jplang's avatar
jplang committed
56
  end
57

jplang's avatar
jplang committed
58 59 60 61 62 63 64
  def sum_hours(data)
    sum = 0
    data.each do |row|
      sum += row['hours'].to_f
    end
    sum
  end
65

66
  def format_criteria_value(criteria_options, value, html=true)
67
    if value.blank?
68
      "[#{l(:label_none)}]"
69
    elsif k = criteria_options[:klass]
70
      obj = k.find_by_id(value.to_i)
71
      format_object(obj, html)
72 73
    elsif cf = criteria_options[:custom_field]
      format_value(value, cf)
74
    else
75
      value.to_s
76
    end
77
  end
78

79
  def report_to_csv(report)
80
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
81
      # Column headers
82 83
      headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
      headers += report.periods
84
      headers << l(:label_total_time)
85
      csv << headers
86
      # Content
87
      report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
88
      # Total row
89
      str_total = l(:label_total_time)
90
      row = [ str_total ] + [''] * (report.criteria.size - 1)
91
      total = 0
92 93
      report.periods.each do |period|
        sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
94
        total += sum
95
        row << (sum > 0 ? sum : '')
96
      end
97
      row << total
98 99 100
      csv << row
    end
  end
101

102 103 104
  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)
105 106
      next if hours_for_value.empty?
      row = [''] * level
107
      row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
108
      row += [''] * (criteria.length - level - 1)
109 110
      total = 0
      periods.each do |period|
111
        sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
112
        total += sum
113
        row << (sum > 0 ? sum : '')
114
      end
115
      row << total
116
      csv << row
117 118
      if criteria.length > level + 1
        report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
119 120 121
      end
    end
  end
122

123
  def cancel_button_tag_for_time_entry(project)
124
    fallback_path = project ? project_time_entries_path(project) : time_entries_path
125
    cancel_button_tag(fallback_path)
126 127
  end

128
end