To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / helpers / time_report.rb @ 1568:bc47b68a9487

History | View | Annotate | Download (6.5 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2014  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, :hours, :total_hours, :periods
22

    
23
      def initialize(project, issue, criteria, columns, time_entry_scope)
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
        @scope = time_entry_scope
34

    
35
        run
36
      end
37

    
38
      def available_criteria
39
        @available_criteria || load_available_criteria
40
      end
41

    
42
      private
43

    
44
      def run
45
        unless @criteria.empty?
46
          time_columns = %w(tyear tmonth tweek spent_on)
47
          @hours = []
48
          @scope.includes(:issue, :activity).
49
              group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).
50
              joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).
51
              sum(:hours).each do |hash, hours|
52
            h = {'hours' => hours}
53
            (@criteria + time_columns).each_with_index do |name, i|
54
              h[name] = hash[i]
55
            end
56
            @hours << h
57
          end
58
          
59
          @hours.each do |row|
60
            case @columns
61
            when 'year'
62
              row['year'] = row['tyear']
63
            when 'month'
64
              row['month'] = "#{row['tyear']}-#{row['tmonth']}"
65
            when 'week'
66
              row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}"
67
            when 'day'
68
              row['day'] = "#{row['spent_on']}"
69
            end
70
          end
71
          
72
          min = @hours.collect {|row| row['spent_on']}.min
73
          @from = min ? min.to_date : Date.today
74

    
75
          max = @hours.collect {|row| row['spent_on']}.max
76
          @to = max ? max.to_date : Date.today
77
          
78
          @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
79

    
80
          @periods = []
81
          # Date#at_beginning_of_ not supported in Rails 1.2.x
82
          date_from = @from.to_time
83
          # 100 columns max
84
          while date_from <= @to.to_time && @periods.length < 100
85
            case @columns
86
            when 'year'
87
              @periods << "#{date_from.year}"
88
              date_from = (date_from + 1.year).at_beginning_of_year
89
            when 'month'
90
              @periods << "#{date_from.year}-#{date_from.month}"
91
              date_from = (date_from + 1.month).at_beginning_of_month
92
            when 'week'
93
              @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}"
94
              date_from = (date_from + 7.day).at_beginning_of_week
95
            when 'day'
96
              @periods << "#{date_from.to_date}"
97
              date_from = date_from + 1.day
98
            end
99
          end
100
        end
101
      end
102

    
103
      def load_available_criteria
104
        @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
105
                                              :klass => Project,
106
                                              :label => :label_project},
107
                                 'status' => {:sql => "#{Issue.table_name}.status_id",
108
                                              :klass => IssueStatus,
109
                                              :label => :field_status},
110
                                 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
111
                                              :klass => Version,
112
                                              :label => :label_version},
113
                                 'category' => {:sql => "#{Issue.table_name}.category_id",
114
                                                :klass => IssueCategory,
115
                                                :label => :field_category},
116
                                 'user' => {:sql => "#{TimeEntry.table_name}.user_id",
117
                                             :klass => User,
118
                                             :label => :label_user},
119
                                 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
120
                                              :klass => Tracker,
121
                                              :label => :label_tracker},
122
                                 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
123
                                               :klass => TimeEntryActivity,
124
                                               :label => :label_activity},
125
                                 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
126
                                             :klass => Issue,
127
                                             :label => :label_issue}
128
                               }
129

    
130
        # Add time entry custom fields
131
        custom_fields = TimeEntryCustomField.all
132
        # Add project custom fields
133
        custom_fields += ProjectCustomField.all
134
        # Add issue custom fields
135
        custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
136
        # Add time entry activity custom fields
137
        custom_fields += TimeEntryActivityCustomField.all
138

    
139
        # Add list and boolean custom fields as available criteria
140
        custom_fields.select {|cf| %w(list bool).include?(cf.field_format) && !cf.multiple?}.each do |cf|
141
          @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement,
142
                                                 :joins => cf.join_for_order_statement,
143
                                                 :format => cf.field_format,
144
                                                 :custom_field => cf,
145
                                                 :label => cf.name}
146
        end
147

    
148
        @available_criteria
149
      end
150
    end
151
  end
152
end