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.2KB

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