annotate app/helpers/timelog_helper.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@909 3 # Redmine - project management software
Chris@1494 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@909 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@909 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 module TimelogHelper
Chris@0 21 include ApplicationHelper
Chris@909 22
Chris@0 23 def render_timelog_breadcrumb
Chris@0 24 links = []
Chris@0 25 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
Chris@0 26 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
Chris@0 27 if @issue
Chris@0 28 if @issue.visible?
Chris@0 29 links << link_to_issue(@issue, :subject => false)
Chris@0 30 else
Chris@0 31 links << "##{@issue.id}"
Chris@0 32 end
Chris@0 33 end
Chris@0 34 breadcrumb links
Chris@0 35 end
Chris@0 36
Chris@0 37 # Returns a collection of activities for a select field. time_entry
Chris@0 38 # is optional and will be used to check if the selected TimeEntryActivity
Chris@0 39 # is active.
Chris@0 40 def activity_collection_for_select_options(time_entry=nil, project=nil)
Chris@0 41 project ||= @project
Chris@0 42 if project.nil?
Chris@0 43 activities = TimeEntryActivity.shared.active
Chris@0 44 else
Chris@0 45 activities = project.activities
Chris@0 46 end
Chris@0 47
Chris@0 48 collection = []
Chris@0 49 if time_entry && time_entry.activity && !time_entry.activity.active?
Chris@0 50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
Chris@0 51 else
Chris@0 52 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
Chris@0 53 end
Chris@0 54 activities.each { |a| collection << [a.name, a.id] }
Chris@0 55 collection
Chris@0 56 end
Chris@909 57
Chris@0 58 def select_hours(data, criteria, value)
Chris@1464 59 if value.to_s.empty?
Chris@1464 60 data.select {|row| row[criteria].blank? }
Chris@909 61 else
Chris@1464 62 data.select {|row| row[criteria].to_s == value.to_s}
Chris@0 63 end
Chris@0 64 end
Chris@909 65
Chris@0 66 def sum_hours(data)
Chris@0 67 sum = 0
Chris@0 68 data.each do |row|
Chris@0 69 sum += row['hours'].to_f
Chris@0 70 end
Chris@0 71 sum
Chris@0 72 end
Chris@909 73
Chris@0 74 def options_for_period_select(value)
Chris@0 75 options_for_select([[l(:label_all_time), 'all'],
Chris@0 76 [l(:label_today), 'today'],
Chris@0 77 [l(:label_yesterday), 'yesterday'],
Chris@0 78 [l(:label_this_week), 'current_week'],
Chris@0 79 [l(:label_last_week), 'last_week'],
Chris@1115 80 [l(:label_last_n_weeks, 2), 'last_2_weeks'],
Chris@0 81 [l(:label_last_n_days, 7), '7_days'],
Chris@0 82 [l(:label_this_month), 'current_month'],
Chris@0 83 [l(:label_last_month), 'last_month'],
Chris@0 84 [l(:label_last_n_days, 30), '30_days'],
Chris@0 85 [l(:label_this_year), 'current_year']],
Chris@0 86 value)
Chris@0 87 end
Chris@909 88
Chris@1115 89 def format_criteria_value(criteria_options, value)
Chris@0 90 if value.blank?
Chris@1115 91 "[#{l(:label_none)}]"
Chris@1115 92 elsif k = criteria_options[:klass]
Chris@0 93 obj = k.find_by_id(value.to_i)
Chris@0 94 if obj.is_a?(Issue)
Chris@0 95 obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
Chris@0 96 else
Chris@0 97 obj
Chris@0 98 end
Chris@1517 99 elsif cf = criteria_options[:custom_field]
Chris@1517 100 format_value(value, cf)
Chris@0 101 else
Chris@1517 102 value.to_s
Chris@0 103 end
Chris@0 104 end
Chris@909 105
Chris@1115 106 def report_to_csv(report)
Chris@909 107 decimal_separator = l(:general_csv_decimal_separator)
Chris@0 108 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
Chris@0 109 # Column headers
Chris@1115 110 headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
Chris@1115 111 headers += report.periods
Chris@1464 112 headers << l(:label_total_time)
Chris@909 113 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
Chris@909 114 c.to_s,
Chris@909 115 l(:general_csv_encoding) ) }
Chris@0 116 # Content
Chris@1115 117 report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
Chris@0 118 # Total row
Chris@1464 119 str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding))
Chris@1115 120 row = [ str_total ] + [''] * (report.criteria.size - 1)
Chris@0 121 total = 0
Chris@1115 122 report.periods.each do |period|
Chris@1115 123 sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
Chris@0 124 total += sum
Chris@909 125 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
Chris@0 126 end
Chris@909 127 row << ("%.2f" % total).gsub('.',decimal_separator)
Chris@0 128 csv << row
Chris@0 129 end
Chris@0 130 export
Chris@0 131 end
Chris@909 132
Chris@1115 133 def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
Chris@909 134 decimal_separator = l(:general_csv_decimal_separator)
Chris@1115 135 hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
Chris@1115 136 hours_for_value = select_hours(hours, criteria[level], value)
Chris@0 137 next if hours_for_value.empty?
Chris@0 138 row = [''] * level
Chris@909 139 row << Redmine::CodesetUtil.from_utf8(
Chris@1115 140 format_criteria_value(available_criteria[criteria[level]], value).to_s,
Chris@909 141 l(:general_csv_encoding) )
Chris@1115 142 row += [''] * (criteria.length - level - 1)
Chris@0 143 total = 0
Chris@0 144 periods.each do |period|
Chris@1115 145 sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
Chris@0 146 total += sum
Chris@909 147 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
Chris@0 148 end
Chris@909 149 row << ("%.2f" % total).gsub('.',decimal_separator)
Chris@0 150 csv << row
Chris@1115 151 if criteria.length > level + 1
Chris@1115 152 report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
Chris@0 153 end
Chris@0 154 end
Chris@0 155 end
Chris@0 156 end