Extracted time report logic from the controller.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8026 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-02 18:46:43 +00:00
parent da140238e7
commit caf898d7d1
6 changed files with 239 additions and 203 deletions

View File

@ -1,7 +1,6 @@
class TimeEntryReportsController < ApplicationController class TimeEntryReportsController < ApplicationController
menu_item :issues menu_item :issues
before_filter :find_optional_project before_filter :find_optional_project
before_filter :load_available_criterias
helper :sort helper :sort
include SortHelper include SortHelper
@ -12,78 +11,12 @@ class TimeEntryReportsController < ApplicationController
include CustomFieldsHelper include CustomFieldsHelper
def report def report
@criterias = params[:criterias] || []
@criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
@criterias.uniq!
@criterias = @criterias[0,3]
@columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
retrieve_date_range retrieve_date_range
@report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], @from, @to)
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_condition = ''
if @project.nil?
sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
elsif @issue.nil?
sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
else
sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
end
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
sql << " FROM #{TimeEntry.table_name}"
sql << time_report_joins
sql << " WHERE"
sql << " (%s) AND" % sql_condition
sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
@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']}"
when 'day'
row['day'] = "#{row['spent_on']}"
end
end
@total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
@periods = []
# Date#at_beginning_of_ not supported in Rails 1.2.x
date_from = @from.to_time
# 100 columns max
while date_from <= @to.to_time && @periods.length < 100
case @columns
when 'year'
@periods << "#{date_from.year}"
date_from = (date_from + 1.year).at_beginning_of_year
when 'month'
@periods << "#{date_from.year}-#{date_from.month}"
date_from = (date_from + 1.month).at_beginning_of_month
when 'week'
@periods << "#{date_from.year}-#{date_from.to_date.cweek}"
date_from = (date_from + 7.day).at_beginning_of_week
when 'day'
@periods << "#{date_from.to_date}"
date_from = date_from + 1.day
end
end
end
respond_to do |format| respond_to do |format|
format.html { render :layout => !request.xhr? } format.html { render :layout => !request.xhr? }
format.csv { send_data(report_to_csv(@criterias, @periods, @hours), :type => 'text/csv; header=present', :filename => 'timelog.csv') } format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
end end
end end
@ -146,64 +79,4 @@ class TimeEntryReportsController < ApplicationController
@from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
@to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
end end
def load_available_criterias
@available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
:klass => Project,
:label => :label_project},
'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
:klass => Version,
:label => :label_version},
'category' => {:sql => "#{Issue.table_name}.category_id",
:klass => IssueCategory,
:label => :field_category},
'member' => {:sql => "#{TimeEntry.table_name}.user_id",
:klass => User,
:label => :label_member},
'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
:klass => Tracker,
:label => :label_tracker},
'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
:klass => TimeEntryActivity,
:label => :label_activity},
'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
:klass => Issue,
:label => :label_issue}
}
# Add list and boolean custom fields as available criterias
custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@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)",
:format => cf.field_format,
:label => cf.name}
end if @project
# Add list and boolean time entry custom fields
TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@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)",
:format => cf.field_format,
:label => cf.name}
end
# Add list and boolean time entry activity custom fields
TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@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)",
:format => cf.field_format,
:label => cf.name}
end
call_hook(:controller_timelog_available_criterias, { :available_criterias => @available_criterias, :project => @project })
@available_criterias
end
def time_report_joins
sql = ''
sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
# TODO: rename hook
call_hook(:controller_timelog_time_report_joins, {:sql => sql} )
sql
end
end end

View File

@ -126,10 +126,10 @@ module TimelogHelper
export export
end end
def format_criteria_value(criteria, value) def format_criteria_value(criteria_options, value)
if value.blank? if value.blank?
l(:label_none) l(:label_none)
elsif k = @available_criterias[criteria][:klass] elsif k = criteria_options[:klass]
obj = k.find_by_id(value.to_i) obj = k.find_by_id(value.to_i)
if obj.is_a?(Issue) if obj.is_a?(Issue)
obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
@ -137,28 +137,28 @@ module TimelogHelper
obj obj
end end
else else
format_value(value, @available_criterias[criteria][:format]) format_value(value, criteria_options[:format])
end end
end end
def report_to_csv(criterias, periods, hours) def report_to_csv(report)
decimal_separator = l(:general_csv_decimal_separator) decimal_separator = l(:general_csv_decimal_separator)
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
# Column headers # Column headers
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) } headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
headers += periods headers += report.periods
headers << l(:label_total) headers << l(:label_total)
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
c.to_s, c.to_s,
l(:general_csv_encoding) ) } l(:general_csv_encoding) ) }
# Content # Content
report_criteria_to_csv(csv, criterias, periods, hours) report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
# Total row # Total row
str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding)) str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding))
row = [ str_total ] + [''] * (criterias.size - 1) row = [ str_total ] + [''] * (report.criteria.size - 1)
total = 0 total = 0
periods.each do |period| report.periods.each do |period|
sum = sum_hours(select_hours(hours, @columns, period.to_s)) sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
total += sum total += sum
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
end end
@ -168,26 +168,26 @@ module TimelogHelper
export export
end end
def report_criteria_to_csv(csv, criterias, periods, hours, level=0) def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
decimal_separator = l(:general_csv_decimal_separator) decimal_separator = l(:general_csv_decimal_separator)
hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
hours_for_value = select_hours(hours, criterias[level], value) hours_for_value = select_hours(hours, criteria[level], value)
next if hours_for_value.empty? next if hours_for_value.empty?
row = [''] * level row = [''] * level
row << Redmine::CodesetUtil.from_utf8( row << Redmine::CodesetUtil.from_utf8(
format_criteria_value(criterias[level], value).to_s, format_criteria_value(available_criteria[criteria[level]], value).to_s,
l(:general_csv_encoding) ) l(:general_csv_encoding) )
row += [''] * (criterias.length - level - 1) row += [''] * (criteria.length - level - 1)
total = 0 total = 0
periods.each do |period| periods.each do |period|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
total += sum total += sum
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
end end
row << ("%.2f" % total).gsub('.',decimal_separator) row << ("%.2f" % total).gsub('.',decimal_separator)
csv << row csv << row
if criterias.length > level + 1 if criteria.length > level + 1
report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1) report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
end end
end end
end end

View File

@ -1,13 +1,13 @@
<% @hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| %> <% @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 = select_hours(hours, criterias[level], value) -%>
<% next if hours_for_value.empty? -%> <% next if hours_for_value.empty? -%>
<tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>"> <tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>">
<%= '<td></td>' * level %> <%= '<td></td>' * level %>
<td><%= h(format_criteria_value(criterias[level], value)) %></td> <td><%= h(format_criteria_value(@report.available_criteria[criterias[level]], value)) %></td>
<%= '<td></td>' * (criterias.length - level - 1) -%> <%= '<td></td>' * (criterias.length - level - 1) -%>
<% total = 0 -%> <% total = 0 -%>
<% @periods.each do |period| -%> <% @report.periods.each do |period| -%>
<% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)); total += sum -%> <% 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"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
<% end -%> <% end -%>
<td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td> <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>

View File

@ -7,53 +7,53 @@
<h2><%= l(:label_spent_time) %></h2> <h2><%= l(:label_spent_time) %></h2>
<% form_tag({:controller => 'time_entry_reports', :action => 'report', :project_id => @project, :issue_id => @issue}, :method => :get, :id => 'query_form') do %> <% form_tag({:controller => 'time_entry_reports', :action => 'report', :project_id => @project, :issue_id => @issue}, :method => :get, :id => 'query_form') do %>
<% @criterias.each do |criteria| %> <% @report.criteria.each do |criterion| %>
<%= hidden_field_tag 'criterias[]', criteria, :id => nil %> <%= hidden_field_tag 'criteria[]', criterion, :id => nil %>
<% end %> <% end %>
<%= render :partial => 'timelog/date_range' %> <%= render :partial => 'timelog/date_range' %>
<p><label for='columns'><%= l(:label_details) %></label>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'], <p><label for='columns'><%= l(:label_details) %></label>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
[l(:label_month), 'month'], [l(:label_month), 'month'],
[l(:label_week), 'week'], [l(:label_week), 'week'],
[l(:label_day_plural).titleize, 'day']], @columns), [l(:label_day_plural).titleize, 'day']], @report.columns),
:onchange => "this.form.onsubmit();" %> :onchange => "this.form.onsubmit();" %>
<label for='criterias'><%= l(:button_add) %></label>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l_or_humanize(@available_criterias[k][:label]), k]}), <label for='criterias'><%= l(:button_add) %></label>: <%= select_tag('criteria[]', options_for_select([[]] + (@report.available_criteria.keys - @report.criteria).collect{|k| [l_or_humanize(@report.available_criteria[k][:label]), k]}),
:onchange => "this.form.submit();", :onchange => "this.form.submit();",
:style => 'width: 200px', :style => 'width: 200px',
:id => nil, :id => nil,
:disabled => (@criterias.length >= 3), :id => "criterias") %> :disabled => (@report.criteria.length >= 3), :id => "criterias") %>
<%= link_to l(:button_clear), {:project_id => @project, :issue_id => @issue, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns}, :class => 'icon icon-reload' %></p> <%= link_to l(:button_clear), {:project_id => @project, :issue_id => @issue, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @report.columns}, :class => 'icon icon-reload' %></p>
<% end %> <% end %>
<% unless @criterias.empty? %> <% unless @report.criteria.empty? %>
<div class="total-hours"> <div class="total-hours">
<p><%= l(:label_total) %>: <%= html_hours(l_hours(@total_hours)) %></p> <p><%= l(:label_total) %>: <%= html_hours(l_hours(@report.total_hours)) %></p>
</div> </div>
<% unless @hours.empty? %> <% unless @report.hours.empty? %>
<div class="autoscroll"> <div class="autoscroll">
<table class="list" id="time-report"> <table class="list" id="time-report">
<thead> <thead>
<tr> <tr>
<% @criterias.each do |criteria| %> <% @report.criteria.each do |criteria| %>
<th><%= l_or_humanize(@available_criterias[criteria][:label]) %></th> <th><%= l_or_humanize(@report.available_criteria[criteria][:label]) %></th>
<% end %> <% end %>
<% columns_width = (40 / (@periods.length+1)).to_i %> <% columns_width = (40 / (@report.periods.length+1)).to_i %>
<% @periods.each do |period| %> <% @report.periods.each do |period| %>
<th class="period" width="<%= columns_width %>%"><%= period %></th> <th class="period" width="<%= columns_width %>%"><%= period %></th>
<% end %> <% end %>
<th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th> <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %> <%= render :partial => 'report_criteria', :locals => {:criterias => @report.criteria, :hours => @report.hours, :level => 0} %>
<tr class="total"> <tr class="total">
<td><%= l(:label_total) %></td> <td><%= l(:label_total) %></td>
<%= '<td></td>' * (@criterias.size - 1) %> <%= '<td></td>' * (@report.criteria.size - 1) %>
<% total = 0 -%> <% total = 0 -%>
<% @periods.each do |period| -%> <% @report.periods.each do |period| -%>
<% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%> <% 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> <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
<% end -%> <% end -%>
<td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td> <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>

View File

@ -0,0 +1,164 @@
# Redmine - project management software
# Copyright (C) 2006-2011 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 Redmine
module Helpers
class TimeReport
attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods
def initialize(project, issue, criteria, columns, from, to)
@project = project
@issue = issue
@criteria = criteria || []
@criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
@criteria.uniq!
@criteria = @criteria[0,3]
@columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
@from = from
@to = to
run
end
def available_criteria
@available_criteria || load_available_criteria
end
private
def run
unless @criteria.empty?
sql_select = @criteria.collect{|criteria| @available_criteria[criteria][:sql] + " AS " + criteria}.join(', ')
sql_group_by = @criteria.collect{|criteria| @available_criteria[criteria][:sql]}.join(', ')
sql_condition = ''
if @project.nil?
sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
elsif @issue.nil?
sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
else
sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
end
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
sql << " FROM #{TimeEntry.table_name}"
sql << time_report_joins
sql << " WHERE"
sql << " (%s) AND" % sql_condition
sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
@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']}"
when 'day'
row['day'] = "#{row['spent_on']}"
end
end
@total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
@periods = []
# Date#at_beginning_of_ not supported in Rails 1.2.x
date_from = @from.to_time
# 100 columns max
while date_from <= @to.to_time && @periods.length < 100
case @columns
when 'year'
@periods << "#{date_from.year}"
date_from = (date_from + 1.year).at_beginning_of_year
when 'month'
@periods << "#{date_from.year}-#{date_from.month}"
date_from = (date_from + 1.month).at_beginning_of_month
when 'week'
@periods << "#{date_from.year}-#{date_from.to_date.cweek}"
date_from = (date_from + 7.day).at_beginning_of_week
when 'day'
@periods << "#{date_from.to_date}"
date_from = date_from + 1.day
end
end
end
end
def load_available_criteria
@available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
:klass => Project,
:label => :label_project},
'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
:klass => Version,
:label => :label_version},
'category' => {:sql => "#{Issue.table_name}.category_id",
:klass => IssueCategory,
:label => :field_category},
'member' => {:sql => "#{TimeEntry.table_name}.user_id",
:klass => User,
:label => :label_member},
'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
:klass => Tracker,
:label => :label_tracker},
'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
:klass => TimeEntryActivity,
:label => :label_activity},
'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
:klass => Issue,
:label => :label_issue}
}
# Add list and boolean custom fields as available criteria
custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@available_criteria["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)",
:format => cf.field_format,
:label => cf.name}
end if @project
# Add list and boolean time entry custom fields
TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@available_criteria["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)",
:format => cf.field_format,
:label => cf.name}
end
# Add list and boolean time entry activity custom fields
TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@available_criteria["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)",
:format => cf.field_format,
:label => cf.name}
end
@available_criteria
end
def time_report_joins
sql = ''
sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
sql
end
end
end
end

View File

@ -38,72 +38,71 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
end end
def test_report_all_projects_one_criteria def test_report_all_projects_one_criteria
get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project'] get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "8.65", "%.2f" % assigns(:total_hours) assert_equal "8.65", "%.2f" % assigns(:report).total_hours
end end
def test_report_all_time def test_report_all_time
get :report, :project_id => 1, :criterias => ['project', 'issue'] get :report, :project_id => 1, :criteria => ['project', 'issue']
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "162.90", "%.2f" % assigns(:total_hours) assert_equal "162.90", "%.2f" % assigns(:report).total_hours
end end
def test_report_all_time_by_day def test_report_all_time_by_day
get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day' get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "162.90", "%.2f" % assigns(:total_hours) assert_equal "162.90", "%.2f" % assigns(:report).total_hours
assert_tag :tag => 'th', :content => '2007-03-12' assert_tag :tag => 'th', :content => '2007-03-12'
end end
def test_report_one_criteria def test_report_one_criteria
get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project'] get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "8.65", "%.2f" % assigns(:total_hours) assert_equal "8.65", "%.2f" % assigns(:report).total_hours
end end
def test_report_two_criterias def test_report_two_criteria
get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"] get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "162.90", "%.2f" % assigns(:total_hours) assert_equal "162.90", "%.2f" % assigns(:report).total_hours
end end
def test_report_one_day def test_report_one_day
get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criterias => ["member", "activity"] get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"]
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "4.25", "%.2f" % assigns(:total_hours) assert_equal "4.25", "%.2f" % assigns(:report).total_hours
end end
def test_report_at_issue_level def test_report_at_issue_level
get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"] get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "154.25", "%.2f" % assigns(:total_hours) assert_equal "154.25", "%.2f" % assigns(:report).total_hours
assert_tag :form, assert_tag :form,
:attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'} :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
end end
def test_report_custom_field_criteria def test_report_custom_field_criteria
get :report, :project_id => 1, :criterias => ['project', 'cf_1', 'cf_7'] get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7']
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_not_nil assigns(:criterias) assert_equal 3, assigns(:report).criteria.size
assert_equal 3, assigns(:criterias).size assert_equal "162.90", "%.2f" % assigns(:report).total_hours
assert_equal "162.90", "%.2f" % assigns(:total_hours)
# Custom field column # Custom field column
assert_tag :tag => 'th', :content => 'Database' assert_tag :tag => 'th', :content => 'Database'
# Custom field row # Custom field row
@ -116,16 +115,16 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
end end
def test_report_one_criteria_no_result def test_report_one_criteria_no_result
get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project'] get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
assert_response :success assert_response :success
assert_template 'report' assert_template 'report'
assert_not_nil assigns(:total_hours) assert_not_nil assigns(:report)
assert_equal "0.00", "%.2f" % assigns(:total_hours) assert_equal "0.00", "%.2f" % assigns(:report).total_hours
end end
def test_report_all_projects_csv_export def test_report_all_projects_csv_export
get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
:criterias => ["project", "member", "activity"], :format => "csv" :criteria => ["project", "member", "activity"], :format => "csv"
assert_response :success assert_response :success
assert_equal 'text/csv', @response.content_type assert_equal 'text/csv', @response.content_type
lines = @response.body.chomp.split("\n") lines = @response.body.chomp.split("\n")
@ -139,7 +138,7 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
def test_report_csv_export def test_report_csv_export
get :report, :project_id => 1, :columns => 'month', get :report, :project_id => 1, :columns => 'month',
:from => "2007-01-01", :to => "2007-06-30", :from => "2007-01-01", :to => "2007-06-30",
:criterias => ["project", "member", "activity"], :format => "csv" :criteria => ["project", "member", "activity"], :format => "csv"
assert_response :success assert_response :success
assert_equal 'text/csv', @response.content_type assert_equal 'text/csv', @response.content_type
lines = @response.body.chomp.split("\n") lines = @response.body.chomp.split("\n")
@ -177,7 +176,7 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
get :report, :project_id => 1, :columns => 'day', get :report, :project_id => 1, :columns => 'day',
:from => "2011-11-11", :to => "2011-11-11", :from => "2011-11-11", :to => "2011-11-11",
:criterias => ["member"], :format => "csv" :criteria => ["member"], :format => "csv"
assert_response :success assert_response :success
assert_equal 'text/csv', @response.content_type assert_equal 'text/csv', @response.content_type
lines = @response.body.chomp.split("\n") lines = @response.body.chomp.split("\n")
@ -228,7 +227,7 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
get :report, :project_id => 1, :columns => 'day', get :report, :project_id => 1, :columns => 'day',
:from => "2011-11-11", :to => "2011-11-11", :from => "2011-11-11", :to => "2011-11-11",
:criterias => ["member"], :format => "csv" :criteria => ["member"], :format => "csv"
assert_response :success assert_response :success
assert_equal 'text/csv', @response.content_type assert_equal 'text/csv', @response.content_type
lines = @response.body.chomp.split("\n") lines = @response.body.chomp.split("\n")
@ -269,7 +268,7 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
get :report, :project_id => 1, :columns => 'day', get :report, :project_id => 1, :columns => 'day',
:from => "2011-11-11", :to => "2011-11-11", :from => "2011-11-11", :to => "2011-11-11",
:criterias => ["member"], :format => "csv" :criteria => ["member"], :format => "csv"
assert_response :success assert_response :success
assert_equal 'text/csv', @response.content_type assert_equal 'text/csv', @response.content_type
lines = @response.body.chomp.split("\n") lines = @response.body.chomp.split("\n")