Patch #1028 ยป cleanup_timelog.diff
| app/controllers/timelog_controller.rb (working copy) | ||
|---|---|---|
|
include CustomFieldsHelper
|
||
|
|
||
|
def report
|
||
|
@available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
|
||
|
@available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
|
||
|
:klass => Project,
|
||
|
:label => :label_project},
|
||
|
'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
|
||
| ... | ... | |
|
:label => :label_issue}
|
||
|
}
|
||
|
|
||
|
# Add list and boolean custom fields as available criterias
|
||
|
# Add list and boolean custom fields as available criteria
|
||
|
@project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
|
||
|
@available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
|
||
|
@available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
|
||
|
:format => cf.field_format,
|
||
|
:label => cf.name}
|
||
|
end
|
||
|
|
||
|
@criterias = params[:criterias] || []
|
||
|
@criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
|
||
|
@criterias.uniq!
|
||
|
@criterias = @criterias[0,3]
|
||
|
@criteria = params[:criteria] || []
|
||
|
@criteria = @criteria.select{|criterion| @available_criteria.has_key? criterion}
|
||
|
@criteria.uniq!
|
||
|
@criteria = @criteria[0,3]
|
||
|
|
||
|
@columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
|
||
|
|
||
|
retrieve_date_range
|
||
|
|
||
|
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(', ')
|
||
|
unless @criteria.empty?
|
||
|
sql_select = @criteria.collect{|criterion| @available_criteria[criterion][:sql] + " AS " + criterion}.join(', ')
|
||
|
sql_group_by = @criteria.collect{|criterion| @available_criteria[criterion][:sql]}.join(', ')
|
||
|
|
||
|
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
|
||
|
sql << " FROM #{TimeEntry.table_name}"
|
||
| ... | ... | |
|
|
||
|
respond_to do |format|
|
||
|
format.html { render :layout => !request.xhr? }
|
||
|
format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
|
||
|
format.csv { send_data(report_to_csv(@criteria, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
|
||
|
end
|
||
|
end
|
||
|
|
||
| app/helpers/timelog_helper.rb (working copy) | ||
|---|---|---|
|
# 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}
|
||
|
def select_hours(data, criterion, value)
|
||
|
data.select {|row| row[criterion] == value}
|
||
|
end
|
||
|
|
||
|
def sum_hours(data)
|
||
| ... | ... | |
|
export
|
||
|
end
|
||
|
|
||
|
def format_criteria_value(criteria, value)
|
||
|
value.blank? ? l(:label_none) : ((k = @available_criterias[criteria][:klass]) ? k.find_by_id(value.to_i) : format_value(value, @available_criterias[criteria][:format]))
|
||
|
def format_criterion_value(criterion, value)
|
||
|
value.blank? ? l(:label_none) : ((k = @available_criteria[criterion][:klass]) ? k.find_by_id(value.to_i) : format_value(value, @available_criteria[criterion][:format]))
|
||
|
end
|
||
|
|
||
|
def report_to_csv(criterias, periods, hours)
|
||
|
def report_to_csv(criteria, periods, hours)
|
||
|
export = StringIO.new
|
||
|
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
|
||
|
# Column headers
|
||
|
headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
|
||
|
headers = criteria.collect {|criterion| l(@available_criteria[criterion][:label]) }
|
||
|
headers += periods
|
||
|
headers << l(:label_total)
|
||
|
csv << headers.collect {|c| to_utf8(c) }
|
||
|
# Content
|
||
|
report_criteria_to_csv(csv, criterias, periods, hours)
|
||
|
report_criteria_to_csv(csv, criteria, periods, hours)
|
||
|
# Total row
|
||
|
row = [ l(:label_total) ] + [''] * (criterias.size - 1)
|
||
|
row = [ l(:label_total) ] + [''] * (criteria.size - 1)
|
||
|
total = 0
|
||
|
periods.each do |period|
|
||
|
sum = sum_hours(select_hours(hours, @columns, period.to_s))
|
||
| ... | ... | |
|
export
|
||
|
end
|
||
|
|
||
|
def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
|
||
|
hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
|
||
|
hours_for_value = select_hours(hours, criterias[level], value)
|
||
|
def report_criteria_to_csv(csv, criteria, periods, hours, level=0)
|
||
|
hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
|
||
|
hours_for_value = select_hours(hours, criteria[level], value)
|
||
|
next if hours_for_value.empty?
|
||
|
row = [''] * level
|
||
|
row << to_utf8(format_criteria_value(criterias[level], value))
|
||
|
row += [''] * (criterias.length - level - 1)
|
||
|
row << to_utf8(format_criterion_value(criteria[level], value))
|
||
|
row += [''] * (criteria.length - level - 1)
|
||
|
total = 0
|
||
|
periods.each do |period|
|
||
|
sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
|
||
| ... | ... | |
|
row << "%.2f" %total
|
||
|
csv << row
|
||
|
|
||
|
if criterias.length > level + 1
|
||
|
report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
|
||
|
if criteria.length > level + 1
|
||
|
report_criteria_to_csv(csv, criteria, periods, hours_for_value, level + 1)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| app/views/timelog/report.rhtml (working copy) | ||
|---|---|---|
|
<h2><%= l(:label_spent_time) %></h2>
|
||
|
<% form_remote_tag(:url => {}, :update => 'content') do %>
|
||
|
<% @criterias.each do |criteria| %>
|
||
|
<%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
|
||
|
<% @criteria.each do |criterion| %>
|
||
|
<%= hidden_field_tag 'criteria[]', criterion, :id => nil %>
|
||
|
<% end %>
|
||
|
<%= hidden_field_tag 'project_id', params[:project_id] %>
|
||
|
<%= render :partial => 'date_range' %>
|
||
| ... | ... | |
|
[l(:label_day_plural).titleize, 'day']], @columns),
|
||
|
:onchange => "this.form.onsubmit();" %>
|
||
|
<%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l(@available_criterias[k][:label]), k]}),
|
||
|
<%= l(:button_add) %>: <%= select_tag('criteria[]', options_for_select([[]] + (@available_criteria.keys - @criteria).collect{|k| [l(@available_criteria[k][:label]), k]}),
|
||
|
:onchange => "this.form.onsubmit();",
|
||
|
:style => 'width: 200px',
|
||
|
:id => nil,
|
||
|
:disabled => (@criterias.length >= 3)) %>
|
||
|
:disabled => (@criteria.length >= 3)) %>
|
||
|
<%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
|
||
|
:update => 'content'
|
||
|
}, :class => 'icon icon-reload' %></p>
|
||
|
<% end %>
|
||
|
<% unless @criterias.empty? %>
|
||
|
<% unless @criteria.empty? %>
|
||
|
<div class="total-hours">
|
||
|
<p><%= l(:label_total) %>: <%= html_hours(lwr(:label_f_hour, @total_hours)) %></p>
|
||
|
</div>
|
||
| ... | ... | |
|
<table class="list" id="time-report">
|
||
|
<thead>
|
||
|
<tr>
|
||
|
<% @criterias.each do |criteria| %>
|
||
|
<th><%= l(@available_criterias[criteria][:label]) %></th>
|
||
|
<% @criteria.each do |criterion| %>
|
||
|
<th><%= l(@available_criteria[criterion][:label]) %></th>
|
||
|
<% end %>
|
||
|
<% columns_width = (40 / (@periods.length+1)).to_i %>
|
||
|
<% @periods.each do |period| %>
|
||
| ... | ... | |
|
</tr>
|
||
|
</thead>
|
||
|
<tbody>
|
||
|
<%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
|
||
|
<%= render :partial => 'report_criteria', :locals => {:criteria => @criteria, :hours => @hours, :level => 0} %>
|
||
|
<tr class="total">
|
||
|
<td><%= l(:label_total) %></td>
|
||
|
<%= '<td></td>' * (@criterias.size - 1) %>
|
||
|
<%= '<td></td>' * (@criteria.size - 1) %>
|
||
|
<% total = 0 -%>
|
||
|
<% @periods.each do |period| -%>
|
||
|
<% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
|
||
| app/views/timelog/_report_criteria.rhtml (working copy) | ||
|---|---|---|
|
<% @hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| %>
|
||
|
<% hours_for_value = select_hours(hours, criterias[level], value) -%>
|
||
|
<% @hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| %>
|
||
|
<% hours_for_value = select_hours(hours, criteria[level], value) -%>
|
||
|
<% 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 criteria.length > level+1 %>">
|
||
|
<%= '<td></td>' * level %>
|
||
|
<td><%= format_criteria_value(criterias[level], value) %></td>
|
||
|
<%= '<td></td>' * (criterias.length - level - 1) -%>
|
||
|
<td><%= format_criterion_value(criteria[level], value) %></td>
|
||
|
<%= '<td></td>' * (criteria.length - level - 1) -%>
|
||
|
<% total = 0 -%>
|
||
|
<% @periods.each do |period| -%>
|
||
|
<% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)); total += sum -%>
|
||
| ... | ... | |
|
<% end -%>
|
||
|
<td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
|
||
|
</tr>
|
||
|
<% if criterias.length > level+1 -%>
|
||
|
<%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
|
||
|
<% if criteria.length > level+1 -%>
|
||
|
<%= render(:partial => 'report_criteria', :locals => {:criteria => criteria, :hours => hours_for_value, :level => (level + 1)}) %>
|
||
|
<% end -%>
|
||
|
<% end %>
|
||
| test/functional/timelog_controller_test.rb (working copy) | ||
|---|---|---|
|
end
|
||
|
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_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
| ... | ... | |
|
end
|
||
|
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_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
| ... | ... | |
|
assert_tag :tag => 'th', :content => '2007-03-12'
|
||
|
end
|
||
|
|
||
|
def test_report_one_criteria
|
||
|
get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
|
||
|
def test_report_one_criterion
|
||
|
get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
|
||
|
assert_response :success
|
||
|
assert_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
|
assert_equal "8.65", "%.2f" % assigns(:total_hours)
|
||
|
end
|
||
|
|
||
|
def test_report_two_criterias
|
||
|
get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
|
||
|
def test_report_two_criteria
|
||
|
get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
|
||
|
assert_response :success
|
||
|
assert_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
|
assert_equal "162.90", "%.2f" % assigns(:total_hours)
|
||
|
end
|
||
|
|
||
|
def test_report_custom_field_criteria
|
||
|
get :report, :project_id => 1, :criterias => ['project', 'cf_1']
|
||
|
def test_report_custom_field_criterion
|
||
|
get :report, :project_id => 1, :criteria => ['project', 'cf_1']
|
||
|
assert_response :success
|
||
|
assert_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
|
assert_not_nil assigns(:criterias)
|
||
|
assert_equal 2, assigns(:criterias).size
|
||
|
assert_not_nil assigns(:criteria)
|
||
|
assert_equal 2, assigns(:criteria).size
|
||
|
assert_equal "162.90", "%.2f" % assigns(:total_hours)
|
||
|
# Custom field column
|
||
|
assert_tag :tag => 'th', :content => 'Database'
|
||
| ... | ... | |
|
:content => '1' }}
|
||
|
end
|
||
|
|
||
|
def test_report_one_criteria_no_result
|
||
|
get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
|
||
|
def test_report_one_criterion_no_result
|
||
|
get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
|
||
|
assert_response :success
|
||
|
assert_template 'report'
|
||
|
assert_not_nil assigns(:total_hours)
|
||
| ... | ... | |
|
end
|
||
|
|
||
|
def test_report_csv_export
|
||
|
get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
|
||
|
get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criteria => ["project", "member", "activity"], :format => "csv"
|
||
|
assert_response :success
|
||
|
assert_equal 'text/csv', @response.content_type
|
||
|
lines = @response.body.chomp.split("\n")
|
||