+# 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
+# redMine - project management software
+# Copyright (C) 2006 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.
+
module TimelogHelper
+ def select_hours(data, criteria, value)
+ data.select {|row| row[criteria] == value.to_s}
+ end
+
+ def sum_hours(data)
+ sum = 0
+ data.each do |row|
+ sum += row['hours'].to_f
+ end
+ sum
+ end
end
+# 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 TimeEntry < ActiveRecord::Base
# could have used polymorphic association
# project association here allows easy loading of time entries at project level with one database trip
-<h2><%=l(:label_report_plural)%></h2>
-
<% if @total_hours %>
-<h3 class="textright"><%= l(:label_spent_time) %>:
-<%= link_to(lwr(:label_f_hour, @total_hours), {:controller => 'timelog', :action => 'details', :project_id => @project}, :class => 'icon icon-time') %>
-</h3>
+<div style="float:right;text-align:right;">
+<strong><%= l(:label_spent_time) %></strong>: <span class="icon icon-time"><%= lwr(:label_f_hour, @total_hours) %></span><br />
+<%= link_to(l(:label_details), {:controller => 'timelog', :action => 'details', :project_id => @project}) %> |
+<%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}) %>
+</div>
<% end %>
+<h2><%=l(:label_report_plural)%></h2>
+
<div class="splitcontentleft">
<h3><%=l(:field_tracker)%> <%= link_to image_tag('zoom_in.png'), :detail => 'tracker' %></h3>
<%= render :partial => 'simple', :locals => { :data => @issues_by_tracker, :field_name => "tracker_id", :rows => @trackers } %>
--- /dev/null
+<% @available_criterias[criterias[level]][:values].each do |value| %>
+<tr class="<%= cycle('odd', 'even') if criterias.length < level + 2 %>">
+<%= '<td></td>' * level %>
+<td><%= value.name %></td>
+<%= '<td></td>' * (criterias.length - level - 1) %>
+<% hours_for_value = select_hours(hours, criterias[level], value.id) %>
+ <% @periods.each do |period| %>
+ <% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) %>
+ <td align="center"><%= sum > 0 ? sum : "-" %></td>
+ <% end %>
+</tr>
+<% if criterias.length > level+1 %>
+ <%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
+<% end %>
+
+<% end %>
+<% reset_cycle %>
--- /dev/null
+<h2><%= l(:label_spent_time) %></h2>
+
+<% form_remote_tag(:url => {:project_id => @project}, :update => 'content') do %>
+ <% @criterias.each do |criteria| %>
+ <%= hidden_field_tag 'criterias[]', criteria %>
+ <% end %>
+ <p>
+ <%= l(:label_date_from) %>: <%= text_field_tag 'date_from', @date_from, :size => 10 %><%= calendar_for('date_from') %>
+
+ <%= l(:label_date_to) %>: <%= text_field_tag 'date_to', @date_to, :size => 10 %><%= calendar_for('date_to') %>
+
+ <%= l(:label_details) %>:
+ <%= select_tag 'period', options_for_select([[l(:label_year), 'year'],
+ [l(:label_month), 'month'],
+ [l(:label_week), 'week']], @columns) %>
+
+ <%= submit_tag l(:button_apply) %>
+ <%= link_to_remote l(:button_clear), {:url => {:project_id => @project}, :update => 'content'}, :class => 'icon icon-reload' %>
+ </p>
+
+ <% if @criterias.length < 3 %>
+ <p><%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}), :onchange => "this.form.onsubmit();") %></p>
+ <% end %>
+
+<br />
+
+<% unless @criterias.empty? %>
+<table class="list">
+<thead>
+<tr>
+<% @criterias.each do |criteria| %>
+ <th width="15%"><%= l(@available_criterias[criteria][:label]) %></th>
+<% end %>
+<% @periods.each do |period| %>
+ <th width="<%= ((100 - @criterias.length * 15 - 15 ) / @periods.length).to_i %>%"><%= period %></th>
+<% end %>
+</tr>
+</thead>
+
+<tbody>
+<%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
+</tbody>
+</table>
+<% end %>
+<% end %>
+
+<% content_for :header_tags do %>
+<%= javascript_include_tag 'calendar/calendar' %>
+<%= javascript_include_tag "calendar/lang/calendar-#{current_language}.js" %>
+<%= javascript_include_tag 'calendar/calendar-setup' %>
+<%= stylesheet_link_tag 'calendar' %>
+<% end %>
label_next: Следващ
label_previous: Предишен
label_used_by: Използва се от
-label_details: Детайли...
+label_details: Детайли
label_add_note: Добавяне на бележка
label_per_page: На страница
label_calendar: Календар
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Вход
button_submit: Изпращане
label_next: Weiter
label_previous: Zurück
label_used_by: Benutzt von
-label_details: Details...
+label_details: Details
label_add_note: Kommentar hinzufügen
label_per_page: Pro Seite
label_calendar: Kalender
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Einloggen
button_submit: OK
label_next: Next
label_previous: Previous
label_used_by: Used by
-label_details: Details...
+label_details: Details
label_add_note: Add a note
label_per_page: Per page
label_calendar: Calendar
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Login
button_submit: Submit
label_next: Próximo
label_previous: Precedente
label_used_by: Utilizado por
-label_details: Detalles...
+label_details: Detalles
label_add_note: Agregar una nota
label_per_page: Por la página
label_calendar: Calendario
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Conexión
button_submit: Someter
label_next: Suivant
label_previous: Précédent
label_used_by: Utilisé par
-label_details: Détails...
+label_details: Détails
label_add_note: Ajouter une note
label_per_page: Par page
label_calendar: Calendrier
label_message_new: Nouveau message
label_reply_plural: Réponses
label_send_information: Envoyer les informations à l'utilisateur
+label_year: Année
+label_month: Mois
+label_week: Semaine
+label_date_from: Du
+label_date_to: Au
button_login: Connexion
button_submit: Soumettre
label_next: Successivo
label_previous: Precedente
label_used_by: Usato da
-label_details: Dettagli...
+label_details: Dettagli
label_add_note: Aggiungi una nota
label_per_page: Per pagina
label_calendar: Calendario
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Login
button_submit: Invia
label_next: 次
label_previous: 前
label_used_by: 使用中
-label_details: 詳細...
+label_details: 詳細
label_add_note: 注記を追加
label_per_page: ページ毎
label_calendar: カレンダー
label_message_new: 新しいメッセージ
label_reply_plural: 返答
label_send_information: アカウント情報をユーザに送信
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: ログイン
button_submit: 変更
label_next: Volgende
label_previous: Vorige
label_used_by: Gebruikt door
-label_details: Details...
+label_details: Details
label_add_note: Voeg een notitie toe
label_per_page: Per pagina
label_calendar: Kalender
label_message_new: Nieuw bericht
label_reply_plural: Antwoorden
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Inloggen
button_submit: Toevoegen
label_next: Proximo\r
label_previous: Anterior\r
label_used_by: Usado por\r
-label_details: Detalhes...\r
+label_details: Detalhes\r
label_add_note: Adicionar nota\r
label_per_page: Por pagina\r
label_calendar: Calendario\r
label_message_new: New message\r
label_reply_plural: Replies\r
label_send_information: Send account information to the user\r
+label_year: Year\r
+label_month: Month\r
+label_week: Week\r
+label_date_from: From\r
+label_date_to: To\r
\r
button_login: Login\r
button_submit: Enviar\r
label_next: Próximo
label_previous: Anterior
label_used_by: Usado por
-label_details: Detalhes...
+label_details: Detalhes
label_add_note: Adicionar nota
label_per_page: Por página
label_calendar: Calendário
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Login
button_submit: Enviar
label_next: Nästa
label_previous: Föregående
label_used_by: Använd av
-label_details: Detaljer...
+label_details: Detaljer
label_add_note: Lägg till anteckning
label_per_page: Per sida
label_calendar: Kalender
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: Logga in
button_submit: Skicka
label_next: 下一个
label_previous: 上一个
label_used_by: 使用中
-label_details: 详情...
+label_details: 详情
label_add_note: 添加说明
label_per_page: 每面
label_calendar: 日历
label_message_new: New message
label_reply_plural: Replies
label_send_information: Send account information to the user
+label_year: Year
+label_month: Month
+label_week: Week
+label_date_from: From
+label_date_to: To
button_login: 登录
button_submit: 提交
--- /dev/null
+--- \r
+time_entries_001: \r
+ created_on: 2007-03-23 12:54:18 +01:00\r
+ tweek: 12\r
+ tmonth: 3\r
+ project_id: 1\r
+ comments: My hours\r
+ updated_on: 2007-03-23 12:54:18 +01:00\r
+ activity_id: 8\r
+ spent_on: 2007-03-23\r
+ issue_id: 1\r
+ id: 1\r
+ hours: 4.25\r
+ user_id: 2\r
+ tyear: 2007\r
+time_entries_002: \r
+ created_on: 2007-03-23 14:11:04 +01:00\r
+ tweek: 12\r
+ tmonth: 3\r
+ project_id: 1\r
+ comments: ""\r
+ updated_on: 2007-03-23 14:11:04 +01:00\r
+ activity_id: 8\r
+ spent_on: 2007-03-23\r
+ issue_id: 1\r
+ id: 2\r
+ hours: 150.0\r
+ user_id: 1\r
+ tyear: 2007\r
+time_entries_003: \r
+ created_on: 2007-04-21 12:20:48 +02:00\r
+ tweek: 16\r
+ tmonth: 4\r
+ project_id: 1\r
+ comments: ""\r
+ updated_on: 2007-04-21 12:20:48 +02:00\r
+ activity_id: 8\r
+ spent_on: 2007-04-21\r
+ issue_id: 2\r
+ id: 3\r
+ hours: 1.0\r
+ user_id: 1\r
+ tyear: 2007\r
--- /dev/null
+# 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.
+
+require File.dirname(__FILE__) + '/../test_helper'
+require 'timelog_controller'
+
+# Re-raise errors caught by the controller.
+class TimelogController; def rescue_action(e) raise e end; end
+
+class TimelogControllerTest < Test::Unit::TestCase
+ fixtures :time_entries, :issues
+
+ def setup
+ @controller = TimelogController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_report_no_criteria
+ get :report, :project_id => 1
+ assert_response :success
+ assert_template 'report'
+ end
+
+ def test_report_one_criteria
+ get :report, :project_id => 1, :period => "month", :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member"]
+ assert_response :success
+ assert_template 'report'
+ assert_not_nil assigns(:hours)
+ end
+
+ def test_report_two_criterias
+ get :report, :project_id => 1, :period => "week", :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
+ assert_response :success
+ assert_template 'report'
+ assert_not_nil assigns(:hours)
+ end
+end