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
|
|
|
require 'query/query_supporters'
|
|
|
|
|
|
|
|
module QueryDonations
|
|
|
|
# Export all donation data for a given campaign
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.campaign_export(campaign_id)
|
2018-03-25 17:30:42 +00:00
|
|
|
Psql.execute_vectors(
|
|
|
|
Qexpr.new.select([
|
|
|
|
'donations.created_at',
|
2019-07-30 21:29:24 +00:00
|
|
|
'(payments.gross_amount/100.00)::money::text AS amount',
|
|
|
|
'COUNT(recurring_donations.id) > 0 AS recurring',
|
|
|
|
"STRING_AGG(campaign_gift_options.name, ',') AS campaign_gift_names"
|
2018-03-25 17:30:42 +00:00
|
|
|
].concat(QuerySupporters.supporter_export_selections)
|
|
|
|
.concat([
|
2019-07-30 21:29:24 +00:00
|
|
|
'supporters.id AS "Supporter ID"'
|
|
|
|
]).concat([
|
|
|
|
"coalesce(donations.designation, 'None') AS designation",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('type')}::text AS \"Dedication Type\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('name')}::text AS \"Dedicated To: Name\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('supporter_id')}::text AS \"Dedicated To: Supporter ID\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('contact', 'email')}::text AS \"Dedicated To: Email\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('contact', 'phone')}::text AS \"Dedicated To: Phone\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('contact', 'address')}::text AS \"Dedicated To: Address\"",
|
|
|
|
"#{QueryPayments.get_dedication_or_empty('note')}::text AS \"Dedicated To: Note\"",
|
|
|
|
'donations.campaign_id AS "Campaign Id"',
|
|
|
|
'users.email AS "Campaign Creator Email"'
|
|
|
|
])).from(:donations)
|
|
|
|
.join(:supporters, 'supporters.id=donations.supporter_id')
|
|
|
|
.left_outer_join(:campaign_gifts, 'campaign_gifts.donation_id=donations.id')
|
|
|
|
.left_outer_join(:campaign_gift_options, 'campaign_gift_options.id=campaign_gifts.campaign_gift_option_id')
|
|
|
|
.left_outer_join(:recurring_donations, 'recurring_donations.donation_id = donations.id')
|
2018-12-04 22:40:48 +00:00
|
|
|
.join_lateral(:payments,
|
|
|
|
get_first_payment_for_donation.parse, true)
|
2019-01-22 21:41:13 +00:00
|
|
|
.join(Qx.select('id, profile_id').from('campaigns')
|
|
|
|
.where("id IN (#{QueryCampaigns
|
|
|
|
.get_campaign_and_children(campaign_id)
|
|
|
|
.parse})").as('campaigns').parse,
|
2019-07-30 21:29:24 +00:00
|
|
|
'donations.campaign_id=campaigns.id')
|
2019-01-22 21:41:13 +00:00
|
|
|
.join(Qx.select('users.id, profiles.id AS profiles_id, users.email')
|
|
|
|
.from('users')
|
|
|
|
.add_join('profiles', 'profiles.user_id = users.id')
|
2019-07-30 21:29:24 +00:00
|
|
|
.as('users').parse, 'users.profiles_id=campaigns.profile_id')
|
|
|
|
.group_by('donations.id', 'supporters.id', 'payments.id', 'payments.gross_amount', 'users.email')
|
|
|
|
.order_by('donations.date')
|
2018-03-25 17:30:42 +00:00
|
|
|
)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def self.for_campaign_activities(id)
|
|
|
|
QueryDonations.activities_expression(['donations.recurring'])
|
2019-07-30 21:29:24 +00:00
|
|
|
.where("donations.campaign_id IN (#{QueryCampaigns
|
2018-11-20 22:17:57 +00:00
|
|
|
.get_campaign_and_children(id)
|
|
|
|
.parse})")
|
2019-07-30 21:29:24 +00:00
|
|
|
.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.activities_expression(additional_selects)
|
|
|
|
selects = ["
|
2019-07-30 21:29:24 +00:00
|
|
|
CASE
|
2018-03-25 17:30:42 +00:00
|
|
|
WHEN donations.anonymous='t'
|
2019-07-30 21:29:24 +00:00
|
|
|
OR supporters.anonymous='t'
|
|
|
|
OR supporters.name=''
|
|
|
|
OR supporters.name IS NULL
|
|
|
|
THEN 'A supporter'
|
|
|
|
ELSE supporters.name
|
|
|
|
END AS supporter_name",
|
|
|
|
'(donations.amount / 100.0)::money::text as amount',
|
|
|
|
'donations.date'] + (additional_selects || [])
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.select(selects.join(','))
|
2018-03-25 17:30:42 +00:00
|
|
|
.from(:donations)
|
|
|
|
.left_join(:supporters, 'donations.supporter_id=supporters.id')
|
2019-07-30 21:29:24 +00:00
|
|
|
.order_by('donations.date desc')
|
2018-03-25 17:30:42 +00:00
|
|
|
.limit(15)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return an array of groups of offsite donation payment_ids that exactly match on nonprofit_id, supporter_id, amount, and date
|
|
|
|
# !!! Note this returns the PAYMENT_IDS for each offsite donation
|
|
|
|
def self.dupe_offsite_donations(np_id)
|
|
|
|
payment_ids = Psql.execute_vectors(
|
2019-07-30 21:29:24 +00:00
|
|
|
Qexpr.new.select('ARRAY_AGG(payments.id) AS ids')
|
|
|
|
.from('donations')
|
|
|
|
.join(:offsite_payments, 'offsite_payments.donation_id=donations.id')
|
|
|
|
.join(:payments, 'payments.donation_id=donations.id')
|
|
|
|
.where('donations.nonprofit_id=$id', id: np_id)
|
|
|
|
.group_by('donations.supporter_id', 'donations.amount', 'donations.date')
|
|
|
|
.having('COUNT(donations.id) > 1')
|
2018-03-25 17:30:42 +00:00
|
|
|
)[1..-1].map(&:flatten)
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.get_first_payment_for_donation(table_selector = 'donations')
|
2018-12-04 22:40:48 +00:00
|
|
|
Qx.select('payments.id, payments.gross_amount').from(:payments)
|
2019-07-30 21:29:24 +00:00
|
|
|
.where("payments.donation_id = #{table_selector}.id")
|
|
|
|
.order_by('payments.created_at ASC')
|
|
|
|
.limit(1)
|
2018-12-04 22:40:48 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|