diff --git a/app/models/work_package/abstract_costs.rb b/app/models/work_package/abstract_costs.rb index 9817684acf..6c0b36661b 100644 --- a/app/models/work_package/abstract_costs.rb +++ b/app/models/work_package/abstract_costs.rb @@ -46,6 +46,10 @@ class WorkPackage raise NotImplementedError, "subclass responsiblity" end + def costs_sum_alias + raise NotImplementedError, "subclass responsiblity" + end + private def work_package_ids(work_packages) @@ -65,22 +69,9 @@ class WorkPackage end def add_costs_to(scope) - query = costs.to_sql - scope - .joins( - "LEFT JOIN (#{query}) as #{table_alias} ON #{table_alias}.wp_id = #{wp_table.name}.id") - .select("#{table_alias}.#{costs_table_name}_sum") - end - - def costs - filter_authorized all_costs - end - - def all_costs - WorkPackage .joins(sum_arel.join_sources) - .select("work_packages.id as wp_id, #{costs_sum} as #{costs_table_name}_sum") + .select("#{costs_sum} AS #{costs_sum_alias}") .group(wp_table[:id]) end @@ -103,9 +94,7 @@ class WorkPackage def sum_arel wp_table - .from(wp_table) - .join(ce_table, Arel::Nodes::OuterJoin).on(ce_table[:work_package_id].eq(wp_table[:id])) - .join(projects_table).on(projects_table[:id].eq(wp_table[:project_id])) + .outer_join(ce_table).on(ce_table_join_condition) .group(wp_table[:id]) end @@ -117,6 +106,13 @@ class WorkPackage costs_model.arel_table end + def ce_table_join_condition + authorization_scope = filter_authorized costs_model.all + authorization_where = authorization_scope.ast.cores.last.wheres.last + + ce_table[:work_package_id].eq(wp_table[:id]).and(authorization_where) + end + def projects_table Project.arel_table end diff --git a/app/models/work_package/labor_costs.rb b/app/models/work_package/labor_costs.rb index 82d91d95bf..06b18f2266 100644 --- a/app/models/work_package/labor_costs.rb +++ b/app/models/work_package/labor_costs.rb @@ -7,5 +7,13 @@ class WorkPackage def filter_authorized(scope) TimeEntry.with_visible_costs_on scope end + + def ce_table + super.alias 'time_entry_labor' + end + + def costs_sum_alias + 'time_entries_sum' + end end end diff --git a/app/models/work_package/material_costs.rb b/app/models/work_package/material_costs.rb index 3cadbb9a43..0ca21bbb14 100644 --- a/app/models/work_package/material_costs.rb +++ b/app/models/work_package/material_costs.rb @@ -7,5 +7,9 @@ class WorkPackage def filter_authorized(scope) CostEntry.with_visible_costs_on scope end + + def costs_sum_alias + 'cost_entries_sum' + end end end