diff --git a/app/PersonTarget.php b/app/PersonTarget.php index f16c984..2630d45 100644 --- a/app/PersonTarget.php +++ b/app/PersonTarget.php @@ -200,15 +200,15 @@ class PersonTarget extends Model $selection = DB::select(' SELECT p_t.*, - p_t.amount * i_o2.factor as target, - sum(i_o1.factor * s_b.amount) as actual - FROM sub_bookings s_b - INNER JOIN account_types a_t ON a_t.id = s_b.account_type_id - INNER JOIN account_classes a_c ON a_c.id = a_t.account_class_id - INNER JOIN i_o_types i_o1 ON i_o1.id = a_c.io_type_id - INNER JOIN person_targets p_t ON p_t.id = s_b.person_target_id + p_t.amount * i_o2.factor AS target, + IFNULL(SUM(i_o1.factor * s_b.amount), 0) AS actual + FROM person_targets p_t INNER JOIN i_o_types i_o2 ON i_o2.id = p_t.io_type_id - GROUP BY s_b.person_target_id + LEFT JOIN sub_bookings s_b ON p_t.id = s_b.person_target_id + LEFT JOIN account_types a_t ON a_t.id = s_b.account_type_id + LEFT JOIN account_classes a_c ON a_c.id = a_t.account_class_id + LEFT JOIN i_o_types i_o1 ON i_o1.id = a_c.io_type_id + GROUP BY p_t.id HAVING ' . $having . ';'); // Remove the unused columns 'target' and 'actual' from the query