use ActiveRecord to summarize cost entries

pull/6827/head
Jan Sandbrink 10 years ago
parent 1b1c33b7ce
commit 7b5eeddec1
  1. 17
      lib/open_project/costs/attributes_helper.rb
  2. 10
      lib/open_project/costs/hooks/work_packages_show_attributes.rb

@ -46,22 +46,7 @@ module OpenProject::Costs
end
def compute_summarized_cost_entries
return {} if cost_entries.blank? || !user_allowed_to?(:view_cost_entries, :view_own_cost_entries)
last_cost_type = ""
cost_entries.sort_by(&:id).each_with_object({}) do |entry, hash|
if entry.cost_type == last_cost_type
hash[last_cost_type][:units] += entry.units
else
last_cost_type = entry.cost_type
hash[last_cost_type] = {}
hash[last_cost_type][:units] = entry.units
hash[last_cost_type][:unit] = entry.cost_type.unit
hash[last_cost_type][:unit_plural] = entry.cost_type.unit_plural
end
end
cost_entries.calculate(:sum, :units, group: :cost_type)
end
def time_entries

@ -65,18 +65,18 @@ module OpenProject::Costs::Hooks
def summarized_cost_entry_links(cost_entries, work_package, create_link=true)
str_array = []
cost_entries.each do |k, v|
txt = pluralize(v[:units], v[:unit], v[:unit_plural])
cost_entries.each do |cost_type, units|
txt = pluralize(units, cost_type.unit, cost_type.unit_plural)
if create_link
# TODO why does this have project_id, work_package_id and cost_type_id params?
str_array << link_to(txt, { :controller => '/costlog',
:action => 'index',
:project_id => work_package.project,
:work_package_id => work_package,
:cost_type_id => k },
{ :title => k.name })
:cost_type_id => cost_type },
{ :title => cost_type.name })
else
str_array << "<span title=\"#{h(k.name)}\">#{txt}</span>"
str_array << "<span title=\"#{h(cost_type.name)}\">#{txt}</span>"
end
end
str_array.join(", ").html_safe

Loading…
Cancel
Save