2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
# Query code for both campaign_gift_options and campaign_gifts
|
|
|
|
|
|
|
|
require 'psql'
|
|
|
|
|
|
|
|
module QueryCampaignGifts
|
2019-07-30 21:29:24 +00:00
|
|
|
# Create a mapping of: {
|
|
|
|
# 'total_donations' => Integer, # total donations for gift options
|
|
|
|
# 'total_one_time' => Integer, # total one-time donations for gift option
|
|
|
|
# 'total_recurring' => Integer,
|
|
|
|
# 'name' => String, # name of the gift level
|
|
|
|
#
|
|
|
|
# Includes the overall sum as well as the donations without gift options
|
|
|
|
def self.report_metrics(campaign_id)
|
|
|
|
data = Psql.execute(%(
|
2018-03-25 17:30:42 +00:00
|
|
|
SELECT campaign_gift_options.name
|
|
|
|
, COUNT(*) AS total_donations
|
|
|
|
, SUM(ds_one_time.amount) AS total_one_time
|
|
|
|
, SUM(ds_recurring.amount) AS total_recurring
|
2018-12-04 22:40:48 +00:00
|
|
|
FROM (#{donations_for_campaign(campaign_id).parse}) AS ds
|
2019-07-30 21:29:24 +00:00
|
|
|
LEFT OUTER JOIN (#{get_corresponding_payments(campaign_id, %(LEFT OUTER JOIN recurring_donations ON recurring_donations.donation_id = donations.id
|
|
|
|
), %(WHERE recurring_donations.id IS NULL))}) ds_one_time
|
2018-12-04 22:40:48 +00:00
|
|
|
ON ds_one_time.id = ds.id
|
2019-07-30 21:29:24 +00:00
|
|
|
LEFT OUTER JOIN (#{get_corresponding_payments(campaign_id, %(INNER JOIN recurring_donations ON recurring_donations.donation_id = donations.id))}) ds_recurring
|
2018-12-04 22:40:48 +00:00
|
|
|
ON ds_recurring.id = ds.id
|
2018-03-25 17:30:42 +00:00
|
|
|
LEFT OUTER JOIN campaign_gifts
|
|
|
|
ON campaign_gifts.donation_id=ds.id
|
|
|
|
LEFT OUTER JOIN campaign_gift_options
|
|
|
|
ON campaign_gifts.campaign_gift_option_id=campaign_gift_options.id
|
|
|
|
GROUP BY campaign_gift_options.id
|
|
|
|
ORDER BY total_donations DESC
|
|
|
|
))
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
Hamster::Hash[data: data]
|
|
|
|
end
|
2018-12-04 22:40:48 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.donations_for_campaign(campaign_id)
|
|
|
|
Qx.select('donations.id, donations.amount').from(:donations).where('campaign_id IN ($ids)', ids: QueryCampaigns.get_campaign_and_children(campaign_id))
|
|
|
|
end
|
2018-12-04 22:40:48 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.get_corresponding_payments(campaign_id, recurring_clauses, where_clauses = '')
|
|
|
|
%(SELECT donations.id, payments.gross_amount AS amount
|
2018-12-04 22:40:48 +00:00
|
|
|
FROM (#{donations_for_campaign(campaign_id).parse}) donations
|
|
|
|
#{recurring_clauses}
|
|
|
|
JOIN LATERAL (
|
|
|
|
SELECT payments.id, payments.gross_amount, payments.donation_id, payments.created_at FROM payments
|
|
|
|
WHERE payments.donation_id = donations.id
|
|
|
|
ORDER BY payments.created_at ASC
|
|
|
|
LIMIT 1
|
|
|
|
) payments ON true
|
|
|
|
#{where_clauses}
|
|
|
|
)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|