2024-10-28 22:02:49 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-10-28 22:38:47 +01:00
|
|
|
module Expenses
|
|
|
|
class TotalQuery
|
|
|
|
def call
|
|
|
|
ActiveRecord::Base.connection.execute(query).first
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def query
|
|
|
|
<<~SQL
|
2024-10-28 22:57:03 +01:00
|
|
|
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,
|
2024-10-28 23:01:00 +01:00
|
|
|
guest_count.confirmed as confirmed_guests,
|
|
|
|
guest_count.projected as projected_guests,
|
2024-10-28 22:57:03 +01:00
|
|
|
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
|
2024-10-28 22:38:47 +01:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|