From 3015251cf6a80cd83ab7e86bfb2e21249aaaf92b Mon Sep 17 00:00:00 2001 From: hjust Date: Mon, 8 Feb 2010 17:49:09 +0000 Subject: [PATCH] Added display of units sums for report groups. (refs #14168) Unit sums are displayed for group_by = cost_type_id or if only time entries are displayed. git-svn-id: https://dev.finn.de/svn/cockpit/trunk@503 7926756e-e54e-46e6-9721-ed318f58905e --- app/controllers/cost_reports_controller.rb | 47 +++++++++++---------- app/views/cost_reports/_list_group_by.rhtml | 32 ++++++++++---- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/app/controllers/cost_reports_controller.rb b/app/controllers/cost_reports_controller.rb index ad87b1d634..c50083295a 100644 --- a/app/controllers/cost_reports_controller.rb +++ b/app/controllers/cost_reports_controller.rb @@ -49,23 +49,23 @@ class CostReportsController < ApplicationController else render :layout => !request.xhr? end - rescue Exception => e - logger.error "#{e.class.name}: #{e.message}" if logger - session.delete :cost_query - - # Give it a name, required to be valid - @query = CostQuery.new(:name => "_") - @query.project = @project - - get_entries(limit) - respond_to do |format| - format.html do - @custom_error = l(:error_generic) - render :layout => !request.xhr? - end - format.atom {render_500(l(:error_generic))} - format.csv {render_500(l(:error_generic))} - end + # rescue Exception => e + # logger.error "#{e.class.name}: #{e.message}" if logger + # session.delete :cost_query + # + # # Give it a name, required to be valid + # @query = CostQuery.new(:name => "_") + # @query.project = @project + # + # get_entries(limit) + # respond_to do |format| + # format.html do + # @custom_error = l(:error_generic) + # render :layout => !request.xhr? + # end + # format.atom {render_500(l(:error_generic))} + # format.csv {render_500(l(:error_generic))} + # end end def new @@ -155,6 +155,7 @@ private 0.0000 END ) AS sum, + SUM(#{table == "time_entries" ? "hours" : "units"}) as unit_sum, COUNT(*) AS count FROM #{from} WHERE #{where_statement} @@ -163,15 +164,15 @@ private end.join(" UNION ") if scopes.length == 2 - sql = "SELECT #{fields}, SUM(sum) as sum, SUM(count) AS count FROM (#{subselect}) AS entries GROUP BY #{fields}" + sql = "SELECT #{fields}, SUM(sum) as sum, SUM(unit_sum) as unit_sum, SUM(count) AS count FROM (#{subselect}) AS entries GROUP BY #{fields}" else sql = subselect end @grouped_entries = ActiveRecord::Base.connection.select_all(sql) - @entry_count, @entry_sum = @grouped_entries.inject([0, 0.0]) do |r,i| - r[0] += i["count"].to_i - r[1] += i["sum"].to_f + @entry_sum, @entry_count = @grouped_entries.inject([0, 0.0]) do |r,i| + r[0] += i["sum"].to_f + r[1] += i["count"].to_i r end end @@ -250,7 +251,7 @@ private @entry_pages = Paginator.new self, @entry_count, limit, params['page'] @entries = TimeEntry.find :all, {:order => (sort_clause if time_sort_column), - :include => [:issue, :activity, :user], + :include => [:issue, :activity, :user, :project], :conditions => time_where, :limit => limit, :offset => @entry_pages.current.offset} @@ -261,7 +262,7 @@ private @entry_pages = Paginator.new self, @entry_count, limit, params['page'] @entries = CostEntry.find :all, {:order => (sort_clause if cost_sort_column), - :include => [:issue, :cost_type, :user], + :include => [:issue, :cost_type, :user, :project], :conditions => cost_where, :limit => limit, :offset => @entry_pages.current.offset} diff --git a/app/views/cost_reports/_list_group_by.rhtml b/app/views/cost_reports/_list_group_by.rhtml index 512ebd38d6..519416a7b5 100644 --- a/app/views/cost_reports/_list_group_by.rhtml +++ b/app/views/cost_reports/_list_group_by.rhtml @@ -1,16 +1,20 @@ <% - def get_column + def group_by_column CostQuery.group_by_columns[@query.group_by[:name]] end + def display_costs + CostEntry.column_names.include?(get_column[:db_field].to_s) && @query.display_cost_entries + end + + def display_time + TimeEntry.column_names.include?(get_column[:db_field].to_s) && @query.display_time_entries + end + + def display_js(invert=false) - group_by_column = get_column - return "'' + Form.serialize('filter-options')" unless group_by_column[:scope] == :costs - display_costs = ((CostEntry.column_names.include? group_by_column[:db_field].to_s) && @query.display_cost_entries) - display_time = ((TimeEntry.column_names.include? group_by_column[:db_field].to_s) && @query.display_time_entries) - if invert display_costs = !display_costs display_time = !display_time @@ -27,8 +31,6 @@ def filter_js(filter_hash) - group_by_column = get_column - if group_by_column[:scope] == :costs && filter_hash[:values].nil? return "" end @@ -41,6 +43,7 @@ Group By Count + <% if (@query.group_by["name"] == "cost_type_id") || (!display_costs && display_time) %><%= l(:caption_cost_unit_plural) %><% end %> Sum Drill Down @@ -58,7 +61,7 @@ filter_js = filter_js(filter) group_by = {:group_by=>{:name=>"", :granularity=>"year"}} - fields = entry.keys - %w[count sum] + fields = entry.keys - %w[count sum unit_sum] if fields.include? "tmonth" name = "#{entry[:tyear]}, #{month_name(entry["tmonth"].to_i)}" elsif fields.include? "tweek" @@ -74,6 +77,17 @@ <%= name %> <%=l :x_entries, :count => entry["count"].to_i %> + + <% if (@query.group_by["name"] == "cost_type_id") || (!display_costs && display_time) %> + <% cost_type = CostType.find_by_id(entry["cost_type_id"]) %> + + <% if cost_type %> + <%= pluralize(entry["unit_sum"], cost_type.unit, cost_type.unit_plural) %> + <%- else -%> + <%= entry["unit_sum"] + "h" %> + <%- end %> + + <%- end %> <%= number_to_currency(entry["sum"]) %> <%= link_to_remote "Drill Down", {