From a9be2c7e7a6030f0a475477e4bd38d1713ae9dd5 Mon Sep 17 00:00:00 2001
From: Egidijus Zideckas <e.zideckas@gmail.com>
Date: Wed, 14 Nov 2012 16:56:29 +0200
Subject: Add links in timelog report to hours, users, projects, issues

---
 app/controllers/timelog_controller.rb       |    8 ++++++++
 app/helpers/timelog_helper.rb               |   21 ++++++++++++++++++++-
 app/models/time_entry.rb                    |   19 +++++++++++++++++++
 app/views/timelog/_report_criteria.html.erb |   16 +++++++++-------
 app/views/timelog/report.html.erb           |   13 ++-----------
 5 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb
index 1756758..f25d4e6 100644
--- a/app/controllers/timelog_controller.rb
+++ b/app/controllers/timelog_controller.rb
@@ -49,6 +49,14 @@ class TimelogController < ApplicationController
     retrieve_date_range
 
     scope = TimeEntry.visible.spent_between(@from, @to)
+    scope = scope.on_user(params[:member]) unless params[:member].nil?
+    scope = scope.on_activity(params[:activity]) unless params[:activity].nil?
+    scope = scope.on_tracker(params[:tracker]) unless params[:tracker].nil?
+    scope = scope.on_version(params[:version]) unless params[:version].nil?
+    scope = scope.on_category(params[:category]) unless params[:category].nil?
+    scope = scope.on_project(Project.find(params[:project].to_i), false) unless params[:project].nil?
+    scope = scope.on_project(Project.find(params[:toplevel_project].to_i), true) unless params[:toplevel_project].nil?	
+    @issue = Issue.find(params[:issue]) unless params[:issue].nil?
     if @issue
       scope = scope.on_issue(@issue)
     elsif @project
diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb
index 11a4476..a692b19 100644
--- a/app/helpers/timelog_helper.rb
+++ b/app/helpers/timelog_helper.rb
@@ -135,7 +135,9 @@ module TimelogHelper
     elsif k = criteria_options[:klass]
       obj = k.find_by_id(value.to_i)
       if obj.is_a?(Issue)
-        obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
+        obj.visible? ? link_to_issue(obj, :project => true) : "##{obj.id}"
+      elsif obj.is_a?(User) || obj.is_a?(Project)
+        link_to(h(obj.to_s), obj)
       else
         obj
       end
@@ -143,6 +145,23 @@ module TimelogHelper
       format_value(value, criteria_options[:format])
     end
   end
+  
+  def criteria_date_range(period, value)
+    from, to = nil, nil 
+    case period 
+    when 'day' 
+      from = to = value 
+    when 'week'
+      from = Date.commercial(value.split('-')[0].to_f, value.split('-')[1].to_f, 1); to = from + 6 
+    when 'month' 
+      from = Date.civil(value.split('-')[0].to_f, value.split('-')[1].to_f, 1); to = (from >> 1) - 1 
+    when 'year' 
+      from = Date.civil(value.split('-')[0].to_f, 1, 1); to = Date.civil(value.split('-')[0].to_f, 12, 31) 
+    end 
+    from = params[:from].to_s.to_date if !params[:from].blank? && params[:from].to_s.to_date > from.to_date 
+    to = params[:to].to_s.to_date if !params[:to].blank? && params[:to].to_s.to_date < to.to_date 
+    return {:from => from, :to => to}
+  end
 
   def report_to_csv(report)
     decimal_separator = l(:general_csv_decimal_separator)
diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb
index ef085d8..cb9a5f3 100644
--- a/app/models/time_entry.rb
+++ b/app/models/time_entry.rb
@@ -66,6 +66,25 @@ class TimeEntry < ActiveRecord::Base
     end
   }
 
+  scope :on_user, lambda {|user| {
+    :conditions => ["#{TimeEntry.table_name}.user_id = ?", user]
+  }}
+  scope :on_activity, lambda {|activity| {
+    :conditions => ["#{TimeEntry.table_name}.activity_id = ?", activity]
+  }}
+  scope :on_tracker, lambda {|tracker| {
+    :include => :issue,
+    :conditions => ["#{Issue.table_name}.tracker_id = ?", tracker]
+  }}
+  scope :on_version, lambda {|version| {
+    :include => :issue,
+    :conditions => ["#{Issue.table_name}.fixed_version_id = ?", version]
+  }}
+  scope :on_category, lambda {|category| {
+    :include => :issue,
+    :conditions => ["#{Issue.table_name}.category_id = ?", category]
+  }}
+    
   safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
 
   def initialize(attributes=nil, *args)
diff --git a/app/views/timelog/_report_criteria.html.erb b/app/views/timelog/_report_criteria.html.erb
index 9a5d142..b3267bc 100644
--- a/app/views/timelog/_report_criteria.html.erb
+++ b/app/views/timelog/_report_criteria.html.erb
@@ -1,19 +1,21 @@
 <% @report.hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| %>
-<% hours_for_value = select_hours(hours, criterias[level], value) -%>
+<% hours_for_value = grand_total ? hours: select_hours(hours, criterias[level], value) -%>
 <% next if hours_for_value.empty? -%>
-<tr class="<%= cycle('odd', 'even') %> <%= criterias.length > level+1 ? 'subtotal' : 'last-level' %>">
+<tr class="<%= cycle('odd', 'even') %> <%= grand_total ? 'total' : criterias.length > level+1 ? 'subtotal' : 'last-level' %>  <%= 'current-users-row' if criterias[level] == 'member' && User.current.try(:id) == value.to_i %>">
 <%= ("<td></td>" * level).html_safe %>
-<td><%= h(format_criteria_value(@report.available_criteria[criterias[level]], value)) %></td>
+<td><%= grand_total ? l(:label_total) : h(format_criteria_value(@report.available_criteria[criterias[level]], value)) %></td>
 <%= ("<td></td>" * (criterias.length - level - 1)).html_safe -%>
-  <% total = 0 -%>
+  <% total = 0; criteria_values = {} if level == 0 -%>
+  <% criteria_level_values = criteria_values.merge({criterias[level].to_sym => value}) -%>
   <% @report.periods.each do |period| -%>
     <% sum = sum_hours(select_hours(hours_for_value, @report.columns, period.to_s)); total += sum -%>
-    <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
+    <td class="hours"><%= link_to html_hours("%.2f" % sum), {:controller => 'timelog', :action => 'index'}.merge(criteria_level_values).merge(criteria_date_range(@report.columns.to_s, period)) if sum > 0 %></td>
   <% end -%>
-  <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
+  <td class="hours"><%= (link_to html_hours("%.2f" % total), {:controller => 'timelog', :action => 'index', :period => params[:period], :from => @from, :to => @to}.merge(criteria_level_values)) if total > 0 %></td>
 </tr>
+<% break if grand_total  %>
 <% if criterias.length > level+1 -%>
-  <%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
+  <%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :criteria_values => criteria_level_values.merge({criterias[level].to_sym => value}), :hours => hours_for_value, :level => (level + 1), :grand_total => false}) %>
 <% end -%>
 
 <% end %>
diff --git a/app/views/timelog/report.html.erb b/app/views/timelog/report.html.erb
index 4b9f0be..266ce65 100644
--- a/app/views/timelog/report.html.erb
+++ b/app/views/timelog/report.html.erb
@@ -49,17 +49,8 @@
 </tr>
 </thead>
 <tbody>
-<%= render :partial => 'report_criteria', :locals => {:criterias => @report.criteria, :hours => @report.hours, :level => 0} %>
-  <tr class="total">
-  <td><%= l(:label_total) %></td>
-  <%= ('<td></td>' * (@report.criteria.size - 1)).html_safe %>
-  <% total = 0 -%>
-  <% @report.periods.each do |period| -%>
-    <% sum = sum_hours(select_hours(@report.hours, @report.columns, period.to_s)); total += sum -%>
-    <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
-  <% end -%>
-  <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
-  </tr>
+<%= render :partial => 'report_criteria', :locals => {:criterias => @report.criteria, :hours => @report.hours, :level => 0, :grand_total => false} %>
+<%= render :partial => 'report_criteria', :locals => {:criterias => @report.criteria, :hours => @report.hours, :level => 0, :grand_total => true} %>
 </tbody>
 </table>
 </div>
-- 
1.7.10.4

