Manuel Bustillo 253d6b0591
All checks were successful
Add copyright notice / copyright_notice (pull_request) Successful in 1m52s
Run unit tests / unit_tests (pull_request) Successful in 3m32s
Build Nginx-based docker image / build-static-assets (pull_request) Successful in 43m38s
Add copyright notice
2024-10-28 22:02:49 +00:00

48 lines
1.8 KiB
Ruby

# 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