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 @ 1298:4f746d8966dd

History | View | Annotate | Download (6.43 KB)

1 1115:433d4f72a19b Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 1115:433d4f72a19b Chris
#
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 1295:622f24f53b42 Chris
      attr_reader :criteria, :columns, :hours, :total_hours, :periods
22 1115:433d4f72a19b Chris
23 1295:622f24f53b42 Chris
      def initialize(project, issue, criteria, columns, time_entry_scope)
24 1115:433d4f72a19b Chris
        @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 1295:622f24f53b42 Chris
        @scope = time_entry_scope
34 1115:433d4f72a19b Chris
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 1295:622f24f53b42 Chris
          @scope.sum(:hours,
49
              :include => [:issue, :activity],
50
              :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns,
51
              :joins => @criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).each do |hash, hours|
52 1115:433d4f72a19b Chris
            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 1295:622f24f53b42 Chris
              row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}"
67 1115:433d4f72a19b Chris
            when 'day'
68
              row['day'] = "#{row['spent_on']}"
69
            end
70
          end
71
72 1295:622f24f53b42 Chris
          min = @hours.collect {|row| row['spent_on']}.min
73
          @from = min ? min.to_date : Date.today
74 1115:433d4f72a19b Chris
75 1295:622f24f53b42 Chris
          max = @hours.collect {|row| row['spent_on']}.max
76
          @to = max ? max.to_date : Date.today
77 1115:433d4f72a19b Chris
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 1295:622f24f53b42 Chris
              @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}"
94 1115:433d4f72a19b Chris
              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 1295:622f24f53b42 Chris
                                 'user' => {:sql => "#{TimeEntry.table_name}.user_id",
117 1115:433d4f72a19b Chris
                                             :klass => User,
118 1295:622f24f53b42 Chris
                                             :label => :label_user},
119 1115:433d4f72a19b Chris
                                 '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 1295:622f24f53b42 Chris
        # 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 1115:433d4f72a19b Chris
        # Add list and boolean custom fields as available criteria
140
        custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
141 1295:622f24f53b42 Chris
          @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
142
                                                 :joins => cf.join_for_order_statement,
143 1115:433d4f72a19b Chris
                                                 :format => cf.field_format,
144
                                                 :label => cf.name}
145
        end
146
147
        @available_criteria
148
      end
149
    end
150
  end
151
end