Extract cost attribute extraction

pull/6827/head
Hagen Schink 10 years ago
parent cd03ee6719
commit 7b368be262
  1. 99
      lib/open_project/costs/attributes_helper.rb
  2. 72
      lib/open_project/costs/hooks/work_packages_show_attributes.rb

@ -0,0 +1,99 @@
#-- copyright
# OpenProject Costs Plugin
#
# Copyright (C) 2009 - 2014 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3.
#
# 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 OpenProject::Costs
class AttributesHelper
def initialize(work_package)
@work_package = work_package
end
def time_entries_sum
@time_entries_sum ||= compute_time_entries_sum
end
def overall_costs
@overall_costs ||= compute_overall_costs
end
def summarized_cost_entries
@summarized_cost_entries ||= compute_summarized_cost_entries
end
private
def compute_time_entries_sum
time_entries.sum(&:hours) if user_allowed_to?(:view_time_entries, :view_own_time_entries)
end
def compute_overall_costs
if material_costs || labor_costs
sum_costs = 0
sum_costs += material_costs if material_costs
sum_costs += labor_costs if labor_costs
else
sum_costs = nil
end
sum_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
end
def time_entries
@work_package.time_entries.visible(User.current, @work_package.project)
end
def material_costs
cost_entries_with_rate = cost_entries.select{|c| c.costs_visible_by?(User.current)}
cost_entries_with_rate.blank? ? nil : cost_entries_with_rate.collect(&:real_costs).sum
end
def labor_costs
time_entries_with_rate = time_entries.select{|c| c.costs_visible_by?(User.current)}
time_entries_with_rate.blank? ? nil : time_entries_with_rate.collect(&:real_costs).sum
end
def cost_entries
@cost_entries ||= @work_package.cost_entries.visible(User.current, @work_package.project)
end
def user_allowed_to?(*privileges)
privileges.inject(false) do |result, privilege|
result || User.current.allowed_to?(privilege, @work_package.project)
end
end
end
end

@ -39,93 +39,45 @@ module OpenProject::Costs::Hooks
private
def cost_entries
@work_package.cost_entries.visible(User.current, @work_package.project)
end
def material_costs
cost_entries_with_rate = cost_entries.select{|c| c.costs_visible_by?(User.current)}
cost_entries_with_rate.blank? ? nil : cost_entries_with_rate.collect(&:real_costs).sum
end
def time_entries
@work_package.time_entries.visible(User.current, @work_package.project)
end
def labor_costs
time_entries_with_rate = time_entries.select{|c| c.costs_visible_by?(User.current)}
time_entries_with_rate.blank? ? nil : time_entries_with_rate.collect(&:real_costs).sum
end
def overall_costs
if material_costs || labor_costs
sum_costs = 0
sum_costs += material_costs if material_costs
sum_costs += labor_costs if labor_costs
else
sum_costs = nil
end
sum_costs
end
def cost_work_package_attributes
attributes = []
attributes_helper = OpenProject::Costs::AttributesHelper.new(@work_package)
attributes << work_package_show_table_row(:cost_object) do
@work_package.cost_object ?
link_to_cost_object(@work_package.cost_object) :
empty_element_tag
end
if User.current.allowed_to?(:view_time_entries, @project) ||
User.current.allowed_to?(:view_own_time_entries, @project)
if attributes_helper.time_entries_sum
attributes << work_package_show_table_row(:spent_hours) do
# TODO: put inside controller or model
summed_hours = time_entries.sum(&:hours)
summed_hours = attributes_helper.time_entries_sum
summed_hours > 0 ?
link_to(l_hours(summed_hours), work_package_time_entries_path(@work_package)) :
empty_element_tag
end
end
attributes << work_package_show_table_row(:overall_costs) do
overall_costs.nil? ?
empty_element_tag :
number_to_currency(overall_costs)
attributes_helper.overall_costs ?
number_to_currency(attributes_helper.overall_costs) :
empty_element_tag
end
if User.current.allowed_to?(:view_cost_entries, @project) ||
User.current.allowed_to?(:view_own_cost_entries, @project)
if attributes_helper.summarized_cost_entries
attributes << work_package_show_table_row(:spent_units) do
summarized_cost_entries(cost_entries, @work_package)
summarized_cost_entry_links(attributes_helper.summarized_cost_entries, @work_package)
end
end
attributes
end
def summarized_cost_entries(cost_entries, work_package, create_link=true)
last_cost_type = ""
return empty_element_tag if cost_entries.blank?
result = cost_entries.sort_by(&:id).inject(Hash.new) do |result, entry|
if entry.cost_type == last_cost_type
result[last_cost_type][:units] += entry.units
else
last_cost_type = entry.cost_type
result[last_cost_type] = {}
result[last_cost_type][:units] = entry.units
result[last_cost_type][:unit] = entry.cost_type.unit
result[last_cost_type][:unit_plural] = entry.cost_type.unit_plural
end
result
end
def summarized_cost_entry_links(cost_entries, work_package, create_link=true)
str_array = []
result.each do |k, v|
cost_entries.each do |k, v|
txt = pluralize(v[:units], v[:unit], v[:unit_plural])
if create_link
# TODO why does this have project_id, work_package_id and cost_type_id params?

Loading…
Cancel
Save