File: lib/redmine/helpers/time_report.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Redmine#18
  module: Helpers#19
  class: TimeReport#20
inherits from
  Object ( Builtin-Module )
has properties
attribute: criteria [R] #21
attribute: columns [R] #21
attribute: from [R] #21
attribute: to [R] #21
attribute: hours [R] #21
attribute: total_hours [R] #21
attribute: periods [R] #21
method: initialize / 6 #23
method: available_criteria #39
method: run #45
method: load_available_criteria #111

Class Hierarchy

Code

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