diff --git a/lib/open_project/costs/hooks/work_packages_show_attributes.rb b/lib/open_project/costs/hooks/work_packages_show_attributes.rb index 01df724939..f858bef030 100644 --- a/lib/open_project/costs/hooks/work_packages_show_attributes.rb +++ b/lib/open_project/costs/hooks/work_packages_show_attributes.rb @@ -1,19 +1,22 @@ module OpenProject::Costs::Hooks - class WorkPackagesShowHook < Redmine::Hook::Listener - include ActionView::Helpers::TagHelper + class WorkPackagesShowHook < Redmine::Hook::ViewListener + #include ActionView::Helpers::TagHelper + #include ActionView::Helpers::NumberHelper + #include ActionView::Helpers::UrlHelper + #include ActionView::Helpers::TextHelper include ActionView::Context include WorkPackagesHelper def work_packages_show_attributes(context = {}) - work_package = context[:work_package] - project = context[:project] + @work_package = context[:work_package] + @project = context[:project] attributes = context[:attributes] - return unless project.module_enabled? :costs_module + return unless @project.module_enabled? :costs_module attributes.reject!{ |a| a.attribute == :spent_time } - attributes << cost_work_package_attributes(work_package) + attributes << cost_work_package_attributes(@work_package) attributes.flatten! attributes @@ -21,6 +24,40 @@ module OpenProject::Costs::Hooks private + def cost_entries + @cost_entries ||= @work_package.cost_entries.visible(User.current, @work_package.project) + end + + def material_costs + return @material_costs if @material_costs + + cost_entries_with_rate = cost_entries.select{|c| c.costs_visible_by?(User.current)} + @material_costs = cost_entries_with_rate.blank? ? nil : cost_entries_with_rate.collect(&:real_costs).sum + end + + def time_entries + @time_entries ||= @work_package.time_entries.visible(User.current, @work_package.project) + end + + def labor_costs + return @labor_costs if @labor_costs + + time_entries_with_rate = time_entries.select{|c| c.costs_visible_by?(User.current)} + @labor_costs = time_entries_with_rate.blank? ? nil : time_entries_with_rate.collect(&:real_costs).sum + end + + def overall_costs + return @overall_costs if @overall_costs + + unless material_costs.nil? && @labor_costs.nil? + @overall_costs = 0 + @overall_costs += material_costs unless material_costs.nil? + @overall_costs += labor_costs unless labor_costs.nil? + else + @overall_costs = nil + end + end + def cost_work_package_attributes(work_package) attributes = [] @@ -34,7 +71,7 @@ module OpenProject::Costs::Hooks attributes << work_package_show_table_row(:spent_hours) do # TODO: put inside controller or model - summed_hours = @time_entries.sum(&:hours) + summed_hours = time_entries.sum(&:hours) summed_hours > 0 ? link_to(l_hours(summed_hours), work_package_time_entries_path(work_package)) : @@ -43,16 +80,16 @@ module OpenProject::Costs::Hooks end attributes << work_package_show_table_row(:overall_costs) do - @overall_costs.nil? ? + overall_costs.nil? ? empty_element_tag : - number_to_currency(@overall_costs) + number_to_currency(overall_costs) end if User.current.allowed_to?(:view_cost_entries, @project) || User.current.allowed_to?(:view_own_cost_entries, @project) attributes << work_package_show_table_row(:spent_units) do - summarized_cost_entries(@cost_entries, work_package) + summarized_cost_entries(cost_entries, work_package) end end