mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-17 11:54:22 +01:00
update free places after cancel event reservation
This commit is contained in:
parent
b725a5a071
commit
cabe2e5604
@ -72,6 +72,15 @@ class Event < ActiveRecord::Base
|
||||
# Reservation.where(reservable: self)
|
||||
# end
|
||||
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def event_recurrence
|
||||
@ -141,13 +150,4 @@ class Event < ActiveRecord::Base
|
||||
end
|
||||
update_columns(recurrence_id: id)
|
||||
end
|
||||
|
||||
def update_nb_free_places
|
||||
if nb_total_places.nil?
|
||||
self.nb_free_places = nil
|
||||
else
|
||||
reserved_places = reservations.map(&:total_booked_seats).inject(0){ |sum, t| sum + t }
|
||||
self.nb_free_places = (nb_total_places - reserved_places)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -255,6 +255,13 @@ class Reservation < ActiveRecord::Base
|
||||
statistic_profile.user
|
||||
end
|
||||
|
||||
def update_event_nb_free_places
|
||||
return unless reservable_type == 'Event'
|
||||
|
||||
reservable.update_nb_free_places
|
||||
reservable.save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def machine_not_already_reserved
|
||||
@ -280,8 +287,6 @@ class Reservation < ActiveRecord::Base
|
||||
errors.add(:training, 'already fully reserved') if Availability.find(slot.availability_id).completed?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notify_member_create_reservation
|
||||
NotificationCenter.call type: 'notify_member_create_reservation',
|
||||
receiver: user,
|
||||
@ -294,22 +299,6 @@ class Reservation < ActiveRecord::Base
|
||||
attached_object: self
|
||||
end
|
||||
|
||||
def update_event_nb_free_places
|
||||
if reservable_id_was.blank?
|
||||
# simple reservation creation, we subtract the number of booked seats from the previous number
|
||||
nb_free_places = reservable.nb_free_places - total_booked_seats
|
||||
else
|
||||
# reservation moved from another date (for recurring events)
|
||||
seats = total_booked_seats
|
||||
|
||||
reservable_was = Event.find(reservable_id_was)
|
||||
nb_free_places = reservable_was.nb_free_places + seats
|
||||
reservable_was.update_columns(nb_free_places: nb_free_places)
|
||||
nb_free_places = reservable.nb_free_places - seats
|
||||
end
|
||||
reservable.update_columns(nb_free_places: nb_free_places)
|
||||
end
|
||||
|
||||
def cart_total
|
||||
total = (invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0)
|
||||
if plan_id.present?
|
||||
|
@ -15,6 +15,7 @@ class Slot < ActiveRecord::Base
|
||||
after_update :notify_member_and_admin_slot_is_modified, if: :dates_were_modified?
|
||||
|
||||
after_update :notify_member_and_admin_slot_is_canceled, if: :canceled?
|
||||
after_update :update_event_nb_free_places, if: :canceled?
|
||||
|
||||
# for backward compatibility
|
||||
def reservation
|
||||
@ -67,4 +68,11 @@ class Slot < ActiveRecord::Base
|
||||
def set_ex_start_end_dates_attrs
|
||||
update_columns(ex_start_at: start_at_was, ex_end_at: end_at_was)
|
||||
end
|
||||
|
||||
def update_event_nb_free_places
|
||||
return unless reservation.reservable_type == 'Event'
|
||||
raise NotImplementedError if reservations.count > 1
|
||||
|
||||
reservation.update_event_nb_free_places
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user