##// END OF EJS Templates
remove trailing white-spaces from app/controllers/time_entry_reports_controller.rb....
Toshi MARUYAMA -
r6748:011b505f9a92
parent child
Show More
@@ -1,209 +1,209
1 1 class TimeEntryReportsController < ApplicationController
2 2 menu_item :issues
3 3 before_filter :find_optional_project
4 4 before_filter :load_available_criterias
5 5
6 6 helper :sort
7 7 include SortHelper
8 8 helper :issues
9 9 helper :timelog
10 10 include TimelogHelper
11 11 helper :custom_fields
12 12 include CustomFieldsHelper
13 13
14 14 def report
15 15 @criterias = params[:criterias] || []
16 16 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
17 17 @criterias.uniq!
18 18 @criterias = @criterias[0,3]
19
19
20 20 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
21
21
22 22 retrieve_date_range
23
23
24 24 unless @criterias.empty?
25 25 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
26 26 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
27 27 sql_condition = ''
28
28
29 29 if @project.nil?
30 30 sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
31 31 elsif @issue.nil?
32 32 sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
33 33 else
34 34 sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
35 35 end
36 36
37 37 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
38 38 sql << " FROM #{TimeEntry.table_name}"
39 39 sql << time_report_joins
40 40 sql << " WHERE"
41 41 sql << " (%s) AND" % sql_condition
42 42 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
43 43 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
44
44
45 45 @hours = ActiveRecord::Base.connection.select_all(sql)
46
46
47 47 @hours.each do |row|
48 48 case @columns
49 49 when 'year'
50 50 row['year'] = row['tyear']
51 51 when 'month'
52 52 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
53 53 when 'week'
54 54 row['week'] = "#{row['tyear']}-#{row['tweek']}"
55 55 when 'day'
56 56 row['day'] = "#{row['spent_on']}"
57 57 end
58 58 end
59
59
60 60 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
61
61
62 62 @periods = []
63 63 # Date#at_beginning_of_ not supported in Rails 1.2.x
64 64 date_from = @from.to_time
65 65 # 100 columns max
66 66 while date_from <= @to.to_time && @periods.length < 100
67 67 case @columns
68 68 when 'year'
69 69 @periods << "#{date_from.year}"
70 70 date_from = (date_from + 1.year).at_beginning_of_year
71 71 when 'month'
72 72 @periods << "#{date_from.year}-#{date_from.month}"
73 73 date_from = (date_from + 1.month).at_beginning_of_month
74 74 when 'week'
75 75 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
76 76 date_from = (date_from + 7.day).at_beginning_of_week
77 77 when 'day'
78 78 @periods << "#{date_from.to_date}"
79 79 date_from = date_from + 1.day
80 80 end
81 81 end
82 82 end
83
83
84 84 respond_to do |format|
85 85 format.html { render :layout => !request.xhr? }
86 86 format.csv { send_data(report_to_csv(@criterias, @periods, @hours), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
87 87 end
88 88 end
89
89
90 90 private
91 91
92 92 # TODO: duplicated in TimelogController
93 93 def find_optional_project
94 94 if !params[:issue_id].blank?
95 95 @issue = Issue.find(params[:issue_id])
96 96 @project = @issue.project
97 97 elsif !params[:project_id].blank?
98 98 @project = Project.find(params[:project_id])
99 99 end
100 100 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
101 101 end
102 102
103 103 # Retrieves the date range based on predefined ranges or specific from/to param dates
104 104 # TODO: duplicated in TimelogController
105 105 def retrieve_date_range
106 106 @free_period = false
107 107 @from, @to = nil, nil
108 108
109 109 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
110 110 case params[:period].to_s
111 111 when 'today'
112 112 @from = @to = Date.today
113 113 when 'yesterday'
114 114 @from = @to = Date.today - 1
115 115 when 'current_week'
116 116 @from = Date.today - (Date.today.cwday - 1)%7
117 117 @to = @from + 6
118 118 when 'last_week'
119 119 @from = Date.today - 7 - (Date.today.cwday - 1)%7
120 120 @to = @from + 6
121 121 when '7_days'
122 122 @from = Date.today - 7
123 123 @to = Date.today
124 124 when 'current_month'
125 125 @from = Date.civil(Date.today.year, Date.today.month, 1)
126 126 @to = (@from >> 1) - 1
127 127 when 'last_month'
128 128 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
129 129 @to = (@from >> 1) - 1
130 130 when '30_days'
131 131 @from = Date.today - 30
132 132 @to = Date.today
133 133 when 'current_year'
134 134 @from = Date.civil(Date.today.year, 1, 1)
135 135 @to = Date.civil(Date.today.year, 12, 31)
136 136 end
137 137 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
138 138 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
139 139 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
140 140 @free_period = true
141 141 else
142 142 # default
143 143 end
144
144
145 145 @from, @to = @to, @from if @from && @to && @from > @to
146 146 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
147 147 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
148 148 end
149 149
150 150 def load_available_criterias
151 151 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
152 152 :klass => Project,
153 153 :label => :label_project},
154 154 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
155 155 :klass => Version,
156 156 :label => :label_version},
157 157 'category' => {:sql => "#{Issue.table_name}.category_id",
158 158 :klass => IssueCategory,
159 159 :label => :field_category},
160 160 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
161 161 :klass => User,
162 162 :label => :label_member},
163 163 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
164 164 :klass => Tracker,
165 165 :label => :label_tracker},
166 166 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
167 167 :klass => TimeEntryActivity,
168 168 :label => :label_activity},
169 169 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
170 170 :klass => Issue,
171 171 :label => :label_issue}
172 172 }
173
173
174 174 # Add list and boolean custom fields as available criterias
175 175 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
176 176 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
177 177 @available_criterias["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)",
178 178 :format => cf.field_format,
179 179 :label => cf.name}
180 180 end if @project
181
181
182 182 # Add list and boolean time entry custom fields
183 183 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
184 184 @available_criterias["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)",
185 185 :format => cf.field_format,
186 186 :label => cf.name}
187 187 end
188 188
189 189 # Add list and boolean time entry activity custom fields
190 190 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
191 191 @available_criterias["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)",
192 192 :format => cf.field_format,
193 193 :label => cf.name}
194 194 end
195 195
196 196 call_hook(:controller_timelog_available_criterias, { :available_criterias => @available_criterias, :project => @project })
197 197 @available_criterias
198 198 end
199 199
200 200 def time_report_joins
201 201 sql = ''
202 202 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
203 203 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
204 204 # TODO: rename hook
205 205 call_hook(:controller_timelog_time_report_joins, {:sql => sql} )
206 206 sql
207 207 end
208 208
209 209 end
General Comments 0
You need to be logged in to leave comments. Login now