2015-05-05 03:10:25 +02:00
|
|
|
class Event < ActiveRecord::Base
|
|
|
|
include NotifyWith::NotificationAttachedObject
|
2017-08-31 17:32:04 +02:00
|
|
|
include ApplicationHelper
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
has_one :event_image, as: :viewable, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :event_image, allow_destroy: true
|
|
|
|
has_many :event_files, as: :viewable, dependent: :destroy
|
2016-03-31 14:02:16 +02:00
|
|
|
accepts_nested_attributes_for :event_files, allow_destroy: true, reject_if: :all_blank
|
2016-07-25 16:16:25 +02:00
|
|
|
belongs_to :category
|
|
|
|
validates :category, presence: true
|
2016-05-04 18:17:50 +02:00
|
|
|
has_many :reservations, as: :reservable, dependent: :destroy
|
2018-12-12 13:49:14 +01:00
|
|
|
has_and_belongs_to_many :event_themes, join_table: 'events_event_themes', dependent: :destroy
|
2016-06-28 15:56:37 +02:00
|
|
|
|
2017-03-01 17:09:37 +01:00
|
|
|
has_many :event_price_categories, dependent: :destroy
|
2016-08-24 12:30:48 +02:00
|
|
|
has_many :price_categories, through: :event_price_categories
|
2017-03-02 18:34:55 +01:00
|
|
|
accepts_nested_attributes_for :event_price_categories, allow_destroy: true
|
2016-08-24 12:30:48 +02:00
|
|
|
|
2016-06-28 15:56:37 +02:00
|
|
|
belongs_to :age_range
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
belongs_to :availability, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :availability
|
|
|
|
|
|
|
|
attr_accessor :recurrence, :recurrence_end_at
|
|
|
|
|
|
|
|
after_create :event_recurrence
|
2016-04-13 13:37:05 +02:00
|
|
|
before_save :update_nb_free_places
|
2016-06-24 18:26:11 +02:00
|
|
|
# update event updated_at for index cache
|
2018-12-03 15:10:04 +01:00
|
|
|
after_save -> { touch }
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
def name
|
|
|
|
title
|
|
|
|
end
|
|
|
|
|
2016-06-28 15:56:37 +02:00
|
|
|
def themes
|
2018-12-03 15:10:04 +01:00
|
|
|
event_themes
|
2016-06-28 15:56:37 +02:00
|
|
|
end
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
def recurrence_events
|
2018-12-03 15:10:04 +01:00
|
|
|
Event.includes(:availability)
|
|
|
|
.where('events.recurrence_id = ? AND events.id != ? AND availabilities.start_at >= ?', recurrence_id, id, Time.now)
|
|
|
|
.references(:availabilities)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def safe_destroy
|
|
|
|
reservations = Reservation.where(reservable_type: 'Event', reservable_id: id)
|
2018-12-03 15:10:04 +01:00
|
|
|
if reservations.size.zero?
|
2016-03-23 18:39:41 +01:00
|
|
|
destroy
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-31 11:58:37 +02:00
|
|
|
##
|
|
|
|
# @deprecated
|
|
|
|
# <b>DEPRECATED:</b> Please use <tt>event_price_categories</tt> instead.
|
|
|
|
# This method is for backward compatibility only, do not use in new code
|
|
|
|
def reduced_amount
|
|
|
|
if ActiveRecord::Base.connection.column_exists?(:events, :reduced_amount)
|
|
|
|
read_attribute(:reduced_amount)
|
|
|
|
else
|
|
|
|
pc = PriceCategory.find_by(name: I18n.t('price_category.reduced_fare'))
|
|
|
|
reduced_fare = event_price_categories.where(price_category: pc).first
|
|
|
|
if reduced_fare.nil?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
reduced_fare.amount
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-04 18:17:50 +02:00
|
|
|
# def reservations
|
|
|
|
# Reservation.where(reservable: self)
|
|
|
|
# end
|
2016-04-13 13:37:05 +02:00
|
|
|
|
2019-11-20 17:06:42 +01:00
|
|
|
def update_nb_free_places
|
|
|
|
if nb_total_places.nil?
|
|
|
|
self.nb_free_places = nil
|
|
|
|
else
|
|
|
|
reserved_places = reservations.joins(:slots).where('slots.canceled_at': nil).map(&:total_booked_seats).inject(0) { |sum, t| sum + t }
|
|
|
|
self.nb_free_places = (nb_total_places - reserved_places)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
private
|
2018-12-03 15:10:04 +01:00
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
def event_recurrence
|
2018-12-03 15:10:04 +01:00
|
|
|
return unless recurrence.present? && recurrence != 'none'
|
|
|
|
|
|
|
|
on = case recurrence
|
|
|
|
when 'day'
|
|
|
|
nil
|
|
|
|
when 'week'
|
|
|
|
availability.start_at.wday
|
|
|
|
when 'month'
|
|
|
|
availability.start_at.day
|
|
|
|
when 'year'
|
|
|
|
[availability.start_at.month, availability.start_at.day]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
r = Recurrence.new(every: recurrence, on: on, starts: availability.start_at+1.day, until: recurrence_end_at)
|
|
|
|
r.events.each do |date|
|
|
|
|
days_diff = availability.end_at.day - availability.start_at.day
|
|
|
|
start_at = DateTime.new(
|
|
|
|
date.year,
|
|
|
|
date.month,
|
|
|
|
date.day,
|
|
|
|
availability.start_at.hour,
|
|
|
|
availability.start_at.min,
|
|
|
|
availability.start_at.sec,
|
|
|
|
availability.start_at.zone
|
|
|
|
)
|
|
|
|
start_at = dst_correction(availability.start_at,start_at)
|
|
|
|
end_date = date + days_diff.days
|
|
|
|
end_at = DateTime.new(
|
|
|
|
end_date.year,
|
|
|
|
end_date.month,
|
|
|
|
end_date.day,
|
|
|
|
availability.end_at.hour,
|
|
|
|
availability.end_at.min,
|
|
|
|
availability.end_at.sec,
|
|
|
|
availability.end_at.zone
|
|
|
|
)
|
|
|
|
end_at = dst_correction(availability.start_at,end_at)
|
|
|
|
ei = EventImage.new(attachment: event_image.attachment) if event_image
|
|
|
|
efs = event_files.map do |f|
|
|
|
|
EventFile.new(attachment: f.attachment)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2018-12-03 15:10:04 +01:00
|
|
|
event_price_cats = []
|
|
|
|
event_price_categories.each do |epc|
|
|
|
|
event_price_cats.push(EventPriceCategory.new(price_category_id: epc.price_category_id, amount: epc.amount))
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2018-12-03 15:10:04 +01:00
|
|
|
event = Event.new(
|
|
|
|
recurrence: 'none',
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
event_image: ei,
|
|
|
|
event_files: efs,
|
|
|
|
availability: Availability.new(start_at: start_at, end_at: end_at, available_type: 'event'),
|
|
|
|
availability_id: nil,
|
|
|
|
category_id: category_id,
|
|
|
|
age_range_id: age_range_id,
|
|
|
|
event_themes: event_themes,
|
|
|
|
amount: amount,
|
|
|
|
event_price_categories: event_price_cats,
|
|
|
|
nb_total_places: nb_total_places,
|
|
|
|
recurrence_id: id
|
|
|
|
)
|
|
|
|
event.save
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2018-12-03 15:10:04 +01:00
|
|
|
update_columns(recurrence_id: id)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
end
|