annotate .svn/pristine/e8/e8aa327e67355ce663b09a685e02ede1d8c6edd6.svn-base @ 1488:423d68919ebb redmine-2.4-integration

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