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