comparison lib/redmine/helpers/time_report.rb @ 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 e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 module Redmine 18 module Redmine
19 module Helpers 19 module Helpers
20 class TimeReport 20 class TimeReport
21 attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods 21 attr_reader :criteria, :columns, :hours, :total_hours, :periods
22 22
23 def initialize(project, issue, criteria, columns, from, to) 23 def initialize(project, issue, criteria, columns, time_entry_scope)
24 @project = project 24 @project = project
25 @issue = issue 25 @issue = issue
26 26
27 @criteria = criteria || [] 27 @criteria = criteria || []
28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria} 28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
29 @criteria.uniq! 29 @criteria.uniq!
30 @criteria = @criteria[0,3] 30 @criteria = @criteria[0,3]
31 31
32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month' 32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
33 @from = from 33 @scope = time_entry_scope
34 @to = to
35 34
36 run 35 run
37 end 36 end
38 37
39 def available_criteria 38 def available_criteria
42 41
43 private 42 private
44 43
45 def run 44 def run
46 unless @criteria.empty? 45 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) 46 time_columns = %w(tyear tmonth tweek spent_on)
54 @hours = [] 47 @hours = []
55 scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours| 48 @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|
56 h = {'hours' => hours} 52 h = {'hours' => hours}
57 (@criteria + time_columns).each_with_index do |name, i| 53 (@criteria + time_columns).each_with_index do |name, i|
58 h[name] = hash[i] 54 h[name] = hash[i]
59 end 55 end
60 @hours << h 56 @hours << h
65 when 'year' 61 when 'year'
66 row['year'] = row['tyear'] 62 row['year'] = row['tyear']
67 when 'month' 63 when 'month'
68 row['month'] = "#{row['tyear']}-#{row['tmonth']}" 64 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
69 when 'week' 65 when 'week'
70 row['week'] = "#{row['tyear']}-#{row['tweek']}" 66 row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}"
71 when 'day' 67 when 'day'
72 row['day'] = "#{row['spent_on']}" 68 row['day'] = "#{row['spent_on']}"
73 end 69 end
74 end 70 end
75 71
76 if @from.nil? 72 min = @hours.collect {|row| row['spent_on']}.min
77 min = @hours.collect {|row| row['spent_on']}.min 73 @from = min ? min.to_date : Date.today
78 @from = min ? min.to_date : Date.today
79 end
80 74
81 if @to.nil? 75 max = @hours.collect {|row| row['spent_on']}.max
82 max = @hours.collect {|row| row['spent_on']}.max 76 @to = max ? max.to_date : Date.today
83 @to = max ? max.to_date : Date.today
84 end
85 77
86 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} 78 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
87 79
88 @periods = [] 80 @periods = []
89 # Date#at_beginning_of_ not supported in Rails 1.2.x 81 # Date#at_beginning_of_ not supported in Rails 1.2.x
96 date_from = (date_from + 1.year).at_beginning_of_year 88 date_from = (date_from + 1.year).at_beginning_of_year
97 when 'month' 89 when 'month'
98 @periods << "#{date_from.year}-#{date_from.month}" 90 @periods << "#{date_from.year}-#{date_from.month}"
99 date_from = (date_from + 1.month).at_beginning_of_month 91 date_from = (date_from + 1.month).at_beginning_of_month
100 when 'week' 92 when 'week'
101 @periods << "#{date_from.year}-#{date_from.to_date.cweek}" 93 @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}"
102 date_from = (date_from + 7.day).at_beginning_of_week 94 date_from = (date_from + 7.day).at_beginning_of_week
103 when 'day' 95 when 'day'
104 @periods << "#{date_from.to_date}" 96 @periods << "#{date_from.to_date}"
105 date_from = date_from + 1.day 97 date_from = date_from + 1.day
106 end 98 end
119 :klass => Version, 111 :klass => Version,
120 :label => :label_version}, 112 :label => :label_version},
121 'category' => {:sql => "#{Issue.table_name}.category_id", 113 'category' => {:sql => "#{Issue.table_name}.category_id",
122 :klass => IssueCategory, 114 :klass => IssueCategory,
123 :label => :field_category}, 115 :label => :field_category},
124 'member' => {:sql => "#{TimeEntry.table_name}.user_id", 116 'user' => {:sql => "#{TimeEntry.table_name}.user_id",
125 :klass => User, 117 :klass => User,
126 :label => :label_member}, 118 :label => :label_user},
127 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", 119 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
128 :klass => Tracker, 120 :klass => Tracker,
129 :label => :label_tracker}, 121 :label => :label_tracker},
130 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", 122 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
131 :klass => TimeEntryActivity, 123 :klass => TimeEntryActivity,
133 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", 125 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
134 :klass => Issue, 126 :klass => Issue,
135 :label => :label_issue} 127 :label => :label_issue}
136 } 128 }
137 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
138 # Add list and boolean custom fields as available criteria 139 # 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| 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 ORDER BY c.value LIMIT 1)", 141 @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
142 :format => cf.field_format, 142 :joins => cf.join_for_order_statement,
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 ORDER BY c.value LIMIT 1)",
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 ORDER BY c.value LIMIT 1)",
156 :format => cf.field_format, 143 :format => cf.field_format,
157 :label => cf.name} 144 :label => cf.name}
158 end 145 end
159 146
160 @available_criteria 147 @available_criteria