annotate .svn/pristine/e5/e58c2b42a5313477b70486289636b96261ea464c.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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