annotate .svn/pristine/a7/a7add1443588b3af2ef99c91219368b928dd34d0.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 module Redmine
Chris@1296 19 module Helpers
Chris@1296 20 class TimeReport
Chris@1296 21 attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods
Chris@1296 22
Chris@1296 23 def initialize(project, issue, criteria, columns, from, to)
Chris@1296 24 @project = project
Chris@1296 25 @issue = issue
Chris@1296 26
Chris@1296 27 @criteria = criteria || []
Chris@1296 28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
Chris@1296 29 @criteria.uniq!
Chris@1296 30 @criteria = @criteria[0,3]
Chris@1296 31
Chris@1296 32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
Chris@1296 33 @from = from
Chris@1296 34 @to = to
Chris@1296 35
Chris@1296 36 run
Chris@1296 37 end
Chris@1296 38
Chris@1296 39 def available_criteria
Chris@1296 40 @available_criteria || load_available_criteria
Chris@1296 41 end
Chris@1296 42
Chris@1296 43 private
Chris@1296 44
Chris@1296 45 def run
Chris@1296 46 unless @criteria.empty?
Chris@1296 47 scope = TimeEntry.visible.spent_between(@from, @to)
Chris@1296 48 if @issue
Chris@1296 49 scope = scope.on_issue(@issue)
Chris@1296 50 elsif @project
Chris@1296 51 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
Chris@1296 52 end
Chris@1296 53 time_columns = %w(tyear tmonth tweek spent_on)
Chris@1296 54 @hours = []
Chris@1296 55 scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
Chris@1296 56 h = {'hours' => hours}
Chris@1296 57 (@criteria + time_columns).each_with_index do |name, i|
Chris@1296 58 h[name] = hash[i]
Chris@1296 59 end
Chris@1296 60 @hours << h
Chris@1296 61 end
Chris@1296 62
Chris@1296 63 @hours.each do |row|
Chris@1296 64 case @columns
Chris@1296 65 when 'year'
Chris@1296 66 row['year'] = row['tyear']
Chris@1296 67 when 'month'
Chris@1296 68 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
Chris@1296 69 when 'week'
Chris@1296 70 row['week'] = "#{row['tyear']}-#{row['tweek']}"
Chris@1296 71 when 'day'
Chris@1296 72 row['day'] = "#{row['spent_on']}"
Chris@1296 73 end
Chris@1296 74 end
Chris@1296 75
Chris@1296 76 if @from.nil?
Chris@1296 77 min = @hours.collect {|row| row['spent_on']}.min
Chris@1296 78 @from = min ? min.to_date : Date.today
Chris@1296 79 end
Chris@1296 80
Chris@1296 81 if @to.nil?
Chris@1296 82 max = @hours.collect {|row| row['spent_on']}.max
Chris@1296 83 @to = max ? max.to_date : Date.today
Chris@1296 84 end
Chris@1296 85
Chris@1296 86 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
Chris@1296 87
Chris@1296 88 @periods = []
Chris@1296 89 # Date#at_beginning_of_ not supported in Rails 1.2.x
Chris@1296 90 date_from = @from.to_time
Chris@1296 91 # 100 columns max
Chris@1296 92 while date_from <= @to.to_time && @periods.length < 100
Chris@1296 93 case @columns
Chris@1296 94 when 'year'
Chris@1296 95 @periods << "#{date_from.year}"
Chris@1296 96 date_from = (date_from + 1.year).at_beginning_of_year
Chris@1296 97 when 'month'
Chris@1296 98 @periods << "#{date_from.year}-#{date_from.month}"
Chris@1296 99 date_from = (date_from + 1.month).at_beginning_of_month
Chris@1296 100 when 'week'
Chris@1296 101 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
Chris@1296 102 date_from = (date_from + 7.day).at_beginning_of_week
Chris@1296 103 when 'day'
Chris@1296 104 @periods << "#{date_from.to_date}"
Chris@1296 105 date_from = date_from + 1.day
Chris@1296 106 end
Chris@1296 107 end
Chris@1296 108 end
Chris@1296 109 end
Chris@1296 110
Chris@1296 111 def load_available_criteria
Chris@1296 112 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
Chris@1296 113 :klass => Project,
Chris@1296 114 :label => :label_project},
Chris@1296 115 'status' => {:sql => "#{Issue.table_name}.status_id",
Chris@1296 116 :klass => IssueStatus,
Chris@1296 117 :label => :field_status},
Chris@1296 118 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
Chris@1296 119 :klass => Version,
Chris@1296 120 :label => :label_version},
Chris@1296 121 'category' => {:sql => "#{Issue.table_name}.category_id",
Chris@1296 122 :klass => IssueCategory,
Chris@1296 123 :label => :field_category},
Chris@1296 124 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
Chris@1296 125 :klass => User,
Chris@1296 126 :label => :label_member},
Chris@1296 127 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
Chris@1296 128 :klass => Tracker,
Chris@1296 129 :label => :label_tracker},
Chris@1296 130 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
Chris@1296 131 :klass => TimeEntryActivity,
Chris@1296 132 :label => :label_activity},
Chris@1296 133 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
Chris@1296 134 :klass => Issue,
Chris@1296 135 :label => :label_issue}
Chris@1296 136 }
Chris@1296 137
Chris@1296 138 # Add list and boolean custom fields as available criteria
Chris@1296 139 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
Chris@1296 140 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
Chris@1296 141 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id ORDER BY c.value LIMIT 1)",
Chris@1296 142 :format => cf.field_format,
Chris@1296 143 :label => cf.name}
Chris@1296 144 end if @project
Chris@1296 145
Chris@1296 146 # Add list and boolean time entry custom fields
Chris@1296 147 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
Chris@1296 148 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id ORDER BY c.value LIMIT 1)",
Chris@1296 149 :format => cf.field_format,
Chris@1296 150 :label => cf.name}
Chris@1296 151 end
Chris@1296 152
Chris@1296 153 # Add list and boolean time entry activity custom fields
Chris@1296 154 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
Chris@1296 155 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id ORDER BY c.value LIMIT 1)",
Chris@1296 156 :format => cf.field_format,
Chris@1296 157 :label => cf.name}
Chris@1296 158 end
Chris@1296 159
Chris@1296 160 @available_criteria
Chris@1296 161 end
Chris@1296 162 end
Chris@1296 163 end
Chris@1296 164 end