# Copyright (C) 2024 Manuel Bustillo module Expenses class TotalQuery def call ActiveRecord::Base.connection.execute(query).first end private def query <<~SQL WITH guest_count AS (#{guest_count_per_status}), expense_summary AS (#{expense_summary}) SELECT expense_summary.fixed, expense_summary.fixed_count, expense_summary.variable, expense_summary.variable_count, expense_summary.total_count, guest_count.confirmed as confirmed_guests, guest_count.projected as projected_guests, expense_summary.fixed + expense_summary.variable * guest_count.confirmed as total, expense_summary.fixed + expense_summary.variable * guest_count.projected as max_projected, (expense_summary.fixed + expense_summary.variable * guest_count.confirmed) / guest_count.confirmed as per_person FROM guest_count, expense_summary; SQL end def expense_summary <<~SQL SELECT coalesce(sum(amount) filter (where pricing_type = 'fixed'), 0) as fixed, coalesce(count(amount) filter (where pricing_type = 'fixed'), 0) as fixed_count, coalesce(sum(amount) filter (where pricing_type = 'per_person'), 0) as variable, coalesce(count(amount) filter (where pricing_type = 'per_person'), 0) as variable_count, count(*) as total_count FROM expenses SQL end def guest_count_per_status <<~SQL SELECT COALESCE(count(*) filter(where status = #{Guest.statuses["confirmed"]}), 0) as confirmed, COALESCE(count(*) filter(where status IN (#{Guest.statuses.values_at("confirmed", "invited", "tentative").join(",")})), 0) as projected FROM guests SQL end end end