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
|
|
|
module QueryEventMetrics
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.expression(additional_selects = [])
|
2018-03-25 17:30:42 +00:00
|
|
|
selects = [
|
2019-07-30 21:29:24 +00:00
|
|
|
'coalesce(tickets.total, 0) AS total_attendees',
|
|
|
|
'coalesce(tickets.checked_in_count, 0) AS checked_in_count',
|
|
|
|
'coalesce(ticket_payments.total_paid, 0) AS tickets_total_paid',
|
|
|
|
'coalesce(donations.payment_total, 0) AS donations_total_paid',
|
|
|
|
'coalesce(ticket_payments.total_paid, 0) + coalesce(donations.payment_total, 0) AS total_paid'
|
2018-03-25 17:30:42 +00:00
|
|
|
]
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
tickets_sub = Qx.select('event_id', 'SUM(quantity) AS total', 'SUM(tickets.checked_in::int) AS checked_in_count')
|
|
|
|
.from('tickets')
|
|
|
|
.group_by('event_id')
|
|
|
|
.as('tickets')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
ticket_payments_subquery = Qx.select('payment_id', 'MAX(event_id) AS event_id').from('tickets').group_by('payment_id').as('tickets')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
ticket_payments_sub = Qx.select('SUM(payments.gross_amount) AS total_paid', 'tickets.event_id')
|
|
|
|
.from(:payments)
|
|
|
|
.join(ticket_payments_subquery, 'payments.id=tickets.payment_id')
|
|
|
|
.group_by('tickets.event_id')
|
|
|
|
.as('ticket_payments')
|
|
|
|
|
|
|
|
donations_sub = Qx.select('event_id', 'SUM(payments.gross_amount) as payment_total')
|
|
|
|
.from('donations')
|
|
|
|
.group_by('event_id')
|
|
|
|
.left_join('payments', 'donations.id=payments.donation_id')
|
|
|
|
.as('donations')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
selects = selects.concat(additional_selects)
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.select(*selects)
|
|
|
|
.from('events')
|
|
|
|
.left_join(
|
|
|
|
[tickets_sub, 'tickets.event_id = events.id'],
|
|
|
|
[donations_sub, 'donations.event_id = events.id'],
|
|
|
|
[ticket_payments_sub, 'ticket_payments.event_id=events.id']
|
2018-03-25 17:30:42 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_event_ids(event_ids)
|
|
|
|
return [] if event_ids.empty?
|
2019-07-30 21:29:24 +00:00
|
|
|
|
|
|
|
QueryEventMetrics.expression.where('events.id in ($ids)', ids: event_ids).execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.for_listings(id_type, id, params)
|
|
|
|
selects = [
|
|
|
|
'events.id',
|
|
|
|
'events.name',
|
|
|
|
'events.venue_name',
|
|
|
|
'events.address',
|
|
|
|
'events.city',
|
|
|
|
'events.state_code',
|
|
|
|
'events.zip_code',
|
|
|
|
'events.start_datetime',
|
|
|
|
'events.end_datetime',
|
|
|
|
'events.organizer_email'
|
|
|
|
]
|
|
|
|
|
|
|
|
exp = QueryEventMetrics.expression(selects)
|
|
|
|
|
|
|
|
if id_type == 'profile'
|
2019-07-30 21:29:24 +00:00
|
|
|
exp = exp.and_where(['events.profile_id = $id', id: id])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
if id_type == 'nonprofit'
|
2019-07-30 21:29:24 +00:00
|
|
|
exp = exp.and_where(['events.nonprofit_id = $id', id: id])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
if params['active'].present?
|
|
|
|
exp = exp
|
2019-07-30 21:29:24 +00:00
|
|
|
.and_where(['events.end_datetime >= $date', date: Time.now])
|
|
|
|
.and_where(['events.published = TRUE AND coalesce(events.deleted, FALSE) = FALSE'])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
if params['past'].present?
|
|
|
|
exp = exp
|
2019-07-30 21:29:24 +00:00
|
|
|
.and_where(['events.end_datetime < $date', date: Time.now])
|
|
|
|
.and_where(['events.published = TRUE AND coalesce(events.deleted, FALSE) = FALSE'])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
if params['unpublished'].present?
|
2019-07-30 21:29:24 +00:00
|
|
|
exp = exp.and_where(['coalesce(events.published, FALSE) = FALSE AND coalesce(events.deleted, FALSE) = FALSE'])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
exp = exp.and_where(['events.deleted = TRUE']) if params['deleted'].present?
|
|
|
|
exp.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|