diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/time_entry_reports_controller.rb | 131 | ||||
-rw-r--r-- | app/helpers/timelog_helper.rb | 36 | ||||
-rw-r--r-- | app/views/time_entry_reports/_report_criteria.html.erb | 8 | ||||
-rw-r--r-- | app/views/time_entry_reports/report.html.erb | 34 |
4 files changed, 41 insertions, 168 deletions
diff --git a/app/controllers/time_entry_reports_controller.rb b/app/controllers/time_entry_reports_controller.rb index 467598d2a..bd6918192 100644 --- a/app/controllers/time_entry_reports_controller.rb +++ b/app/controllers/time_entry_reports_controller.rb @@ -1,7 +1,6 @@ class TimeEntryReportsController < ApplicationController menu_item :issues before_filter :find_optional_project - before_filter :load_available_criterias helper :sort include SortHelper @@ -12,78 +11,12 @@ class TimeEntryReportsController < ApplicationController include CustomFieldsHelper 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 - - 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 + @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], @from, @to) respond_to do |format| 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 @@ -146,64 +79,4 @@ class TimeEntryReportsController < ApplicationController @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) 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 diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index 0467acc21..c892ae1ce 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -126,10 +126,10 @@ module TimelogHelper export end - def format_criteria_value(criteria, value) + def format_criteria_value(criteria_options, value) if value.blank? l(:label_none) - elsif k = @available_criterias[criteria][:klass] + 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}" @@ -137,28 +137,28 @@ module TimelogHelper obj end else - format_value(value, @available_criterias[criteria][:format]) + format_value(value, criteria_options[:format]) end end - def report_to_csv(criterias, periods, hours) + def report_to_csv(report) decimal_separator = l(:general_csv_decimal_separator) export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| # Column headers - headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) } - headers += periods + headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } + headers += report.periods headers << l(:label_total) csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( c.to_s, l(:general_csv_encoding) ) } # 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 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 - periods.each do |period| - sum = sum_hours(select_hours(hours, @columns, period.to_s)) + report.periods.each do |period| + sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) total += sum row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') end @@ -168,26 +168,26 @@ module TimelogHelper export 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) - 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? row = [''] * level 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) ) - row += [''] * (criterias.length - level - 1) + row += [''] * (criteria.length - level - 1) total = 0 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 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') end row << ("%.2f" % total).gsub('.',decimal_separator) 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, available_criteria, columns, criteria, periods, hours_for_value, level + 1) end end end diff --git a/app/views/time_entry_reports/_report_criteria.html.erb b/app/views/time_entry_reports/_report_criteria.html.erb index c9a1cfb45..356a7ab10 100644 --- a/app/views/time_entry_reports/_report_criteria.html.erb +++ b/app/views/time_entry_reports/_report_criteria.html.erb @@ -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) -%> <% next if hours_for_value.empty? -%> <tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>"> <%= '<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) -%> <% total = 0 -%> - <% @periods.each do |period| -%> - <% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)); total += sum -%> + <% @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> <% end -%> <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td> diff --git a/app/views/time_entry_reports/report.html.erb b/app/views/time_entry_reports/report.html.erb index 0af89a1de..bd876aacd 100644 --- a/app/views/time_entry_reports/report.html.erb +++ b/app/views/time_entry_reports/report.html.erb @@ -7,53 +7,53 @@ <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 %> - <% @criterias.each do |criteria| %> - <%= hidden_field_tag 'criterias[]', criteria, :id => nil %> + <% @report.criteria.each do |criterion| %> + <%= hidden_field_tag 'criteria[]', criterion, :id => nil %> <% end %> <%= render :partial => 'timelog/date_range' %> <p><label for='columns'><%= l(:label_details) %></label>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'], [l(:label_month), 'month'], [l(:label_week), 'week'], - [l(:label_day_plural).titleize, 'day']], @columns), + [l(:label_day_plural).titleize, 'day']], @report.columns), :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();", :style => 'width: 200px', :id => nil, - :disabled => (@criterias.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> + :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 => @report.columns}, :class => 'icon icon-reload' %></p> <% end %> -<% unless @criterias.empty? %> +<% unless @report.criteria.empty? %> <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> -<% unless @hours.empty? %> +<% unless @report.hours.empty? %> <div class="autoscroll"> <table class="list" id="time-report"> <thead> <tr> -<% @criterias.each do |criteria| %> - <th><%= l_or_humanize(@available_criterias[criteria][:label]) %></th> +<% @report.criteria.each do |criteria| %> + <th><%= l_or_humanize(@report.available_criteria[criteria][:label]) %></th> <% end %> -<% columns_width = (40 / (@periods.length+1)).to_i %> -<% @periods.each do |period| %> +<% columns_width = (40 / (@report.periods.length+1)).to_i %> +<% @report.periods.each do |period| %> <th class="period" width="<%= columns_width %>%"><%= period %></th> <% end %> <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th> </tr> </thead> <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"> <td><%= l(:label_total) %></td> - <%= '<td></td>' * (@criterias.size - 1) %> + <%= '<td></td>' * (@report.criteria.size - 1) %> <% total = 0 -%> - <% @periods.each do |period| -%> - <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%> + <% @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> |