annotate .svn/pristine/a7/a7add1443588b3af2ef99c91219368b928dd34d0.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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