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