You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_helper.rb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module TimelogHelper
  19. include ApplicationHelper
  20. # Returns a collection of activities for a select field. time_entry
  21. # is optional and will be used to check if the selected TimeEntryActivity
  22. # is active.
  23. def activity_collection_for_select_options(time_entry=nil, project=nil)
  24. project ||= time_entry.try(:project)
  25. project ||= @project
  26. if project.nil?
  27. activities = TimeEntryActivity.shared.active
  28. else
  29. activities = project.activities
  30. end
  31. collection = []
  32. if time_entry && time_entry.activity && !time_entry.activity.active?
  33. collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
  34. else
  35. collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
  36. end
  37. activities.each { |a| collection << [a.name, a.id] }
  38. collection
  39. end
  40. def user_collection_for_select_options(time_entry)
  41. collection = time_entry.assignable_users
  42. principals_options_for_select(collection, time_entry.user_id)
  43. end
  44. def select_hours(data, criteria, value)
  45. if value.to_s.empty?
  46. data.select {|row| row[criteria].blank? }
  47. else
  48. data.select {|row| row[criteria].to_s == value.to_s}
  49. end
  50. end
  51. def sum_hours(data)
  52. sum = 0
  53. data.each do |row|
  54. sum += row['hours'].to_f
  55. end
  56. sum
  57. end
  58. def format_criteria_value(criteria_options, value, html=true)
  59. if value.blank?
  60. "[#{l(:label_none)}]"
  61. elsif k = criteria_options[:klass]
  62. obj = k.find_by_id(value.to_i)
  63. format_object(obj, html)
  64. elsif cf = criteria_options[:custom_field]
  65. format_value(value, cf)
  66. else
  67. value.to_s
  68. end
  69. end
  70. def report_to_csv(report)
  71. Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
  72. # Column headers
  73. headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
  74. headers += report.periods
  75. headers << l(:label_total_time)
  76. csv << headers
  77. # Content
  78. report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
  79. # Total row
  80. str_total = l(:label_total_time)
  81. row = [ str_total ] + [''] * (report.criteria.size - 1)
  82. total = 0
  83. report.periods.each do |period|
  84. sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
  85. total += sum
  86. row << (sum > 0 ? sum : '')
  87. end
  88. row << total
  89. csv << row
  90. end
  91. end
  92. def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
  93. hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
  94. hours_for_value = select_hours(hours, criteria[level], value)
  95. next if hours_for_value.empty?
  96. row = [''] * level
  97. row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
  98. row += [''] * (criteria.length - level - 1)
  99. total = 0
  100. periods.each do |period|
  101. sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
  102. total += sum
  103. row << (sum > 0 ? sum : '')
  104. end
  105. row << total
  106. csv << row
  107. if criteria.length > level + 1
  108. report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
  109. end
  110. end
  111. end
  112. def cancel_button_tag_for_time_entry(project)
  113. fallback_path = project ? project_time_entries_path(project) : time_entries_path
  114. cancel_button_tag(fallback_path)
  115. end
  116. end