summaryrefslogtreecommitdiffstats
path: root/app/controllers/timelog_controller.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-06-24 16:07:06 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-06-24 16:07:06 +0000
commitfaa3d984ab0a44da354b901f4962d3441b800f99 (patch)
treef750d4fad72e14e5f18b32f0dd0bdef6ab0695d1 /app/controllers/timelog_controller.rb
parent6d7a855ca2f497a0deeb728d4d7da5cf4afa1d51 (diff)
downloadredmine-faa3d984ab0a44da354b901f4962d3441b800f99.tar.gz
redmine-faa3d984ab0a44da354b901f4962d3441b800f99.zip
Added time report.
Report can be generated by member/activity/tracker/version and year/month/week for the selected period. git-svn-id: http://redmine.rubyforge.org/svn/trunk@572 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/controllers/timelog_controller.rb')
-rw-r--r--app/controllers/timelog_controller.rb95
1 files changed, 94 insertions, 1 deletions
diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb
index da323fbf1..bed6b1c07 100644
--- a/app/controllers/timelog_controller.rb
+++ b/app/controllers/timelog_controller.rb
@@ -1,13 +1,106 @@
+# redMine - project management software
+# Copyright (C) 2006-2007 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
class TimelogController < ApplicationController
layout 'base'
before_filter :find_project
before_filter :authorize, :only => :edit
- before_filter :check_project_privacy, :only => :details
+ before_filter :check_project_privacy, :except => :edit
helper :sort
include SortHelper
+ def report
+ @available_criterias = { 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
+ :values => @project.versions,
+ :label => :label_version},
+ 'category' => {:sql => "#{Issue.table_name}.category_id",
+ :values => @project.issue_categories,
+ :label => :field_category},
+ 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
+ :values => @project.users,
+ :label => :label_member},
+ 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
+ :values => Tracker.find(:all),
+ :label => :label_tracker},
+ 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
+ :values => Enumeration::get_values('ACTI'),
+ :label => :label_activity}
+ }
+
+ @criterias = params[:criterias] || []
+ @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
+ @criterias.uniq!
+
+ @columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
+
+ if params[:date_from]
+ begin; @date_from = params[:date_from].to_date; rescue; end
+ end
+ if params[:date_to]
+ begin; @date_to = params[:date_to].to_date; rescue; end
+ end
+ @date_from ||= Date.civil(Date.today.year, 1, 1)
+ @date_to ||= Date.civil(Date.today.year, Date.today.month+1, 1) - 1
+
+ unless @criterias.empty?
+ sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
+ sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
+
+ sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
+ sql << " FROM #{TimeEntry.table_name} LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
+ sql << " WHERE spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
+ sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
+
+ @hours = ActiveRecord::Base.connection.select_all(sql)
+
+ @hours.each do |row|
+ case @columns
+ when 'year'
+ row['year'] = row['tyear']
+ when 'month'
+ row['month'] = "#{row['tyear']}-#{row['tmonth']}"
+ when 'week'
+ row['week'] = "#{row['tyear']}-#{row['tweek']}"
+ end
+ end
+ end
+
+ @periods = []
+ date_from = @date_from
+ # 100 columns max
+ while date_from < @date_to && @periods.length < 100
+ case @columns
+ when 'year'
+ @periods << "#{date_from.year}"
+ date_from = date_from >> 12
+ when 'month'
+ @periods << "#{date_from.year}-#{date_from.month}"
+ date_from = date_from >> 1
+ when 'week'
+ @periods << "#{date_from.year}-#{date_from.cweek}"
+ date_from = date_from + 7
+ end
+ end
+
+ render :layout => false if request.xhr?
+ end
+
def details
sort_init 'spent_on', 'desc'
sort_update