From 73e4439036160f868d42d6b721c2d82ae5358d5a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 9 Nov 2016 17:07:48 +0100 Subject: [PATCH] front-end modifications to cancel a booked event --- .../javascripts/controllers/events.coffee.erb | 58 ++++++++++++++++--- app/assets/javascripts/router.coffee.erb | 2 +- app/assets/templates/events/show.html.erb | 3 + app/models/reservation.rb | 8 ++- config/locales/app.logged.en.yml | 2 +- config/locales/app.public.en.yml | 3 + config/locales/app.public.fr.yml | 3 + 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/controllers/events.coffee.erb b/app/assets/javascripts/controllers/events.coffee.erb index 39083272f..88408a28b 100644 --- a/app/assets/javascripts/controllers/events.coffee.erb +++ b/app/assets/javascripts/controllers/events.coffee.erb @@ -132,8 +132,8 @@ Application.Controllers.controller "EventsController", ["$scope", "$state", 'Eve -Application.Controllers.controller "ShowEventController", ["$scope", "$state", "$stateParams", "Event", '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'eventPromise', 'growl', '_t', 'Wallet', 'helpers', 'priceCategoriesPromise', 'settingsPromise', -($scope, $state, $stateParams, Event, $uibModal, Member, Reservation, Price, CustomAsset, eventPromise, growl, _t, Wallet, helpers, priceCategoriesPromise, settingsPromise) -> +Application.Controllers.controller "ShowEventController", ["$scope", "$state", "$stateParams", "Event", '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'Slot', 'eventPromise', 'growl', '_t', 'Wallet', 'helpers', 'priceCategoriesPromise', 'settingsPromise', 'dialogs', +($scope, $state, $stateParams, Event, $uibModal, Member, Reservation, Price, CustomAsset, Slot, eventPromise, growl, _t, Wallet, helpers, priceCategoriesPromise, settingsPromise, dialogs) -> @@ -169,9 +169,15 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " ## Global config: is the user authorized to change his bookings slots? $scope.enableBookingMove = (settingsPromise.booking_move_enable == "true") - ## Global config: delay in hours before a booking while changing the booking slot is forbidden + ## Global config: delay in hours from now while changing the booking slot is forbidden $scope.moveBookingDelay = parseInt(settingsPromise.booking_move_delay) + ## Global config: is the user authorized to cancel his booking slots? + $scope.enableBookingCancel = (settingsPromise.booking_cancel_enable == "true") + + ## Global config: delay in hours from now when rectrictions occurs about cancelling reservations + $scope.cancelBookingDelay = parseInt(settingsPromise.booking_cancel_delay) + ## @@ -236,7 +242,7 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " - ## + ##reservationCanCan # Callback to deal with the reservations of the user selected in the dropdown list instead of the current user's # reservations. (admins only) ## @@ -311,6 +317,29 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " + ## + # + # @param reservation {{id:number, reservable_id:number, nb_reserve_places:number}} + # @param e {Object} see https://docs.angularjs.org/guide/expression#-event- + ## + $scope.cancelReservation = (reservation, e)-> + e.preventDefault() + e.stopPropagation() + + dialogs.confirm + resolve: + object: -> + title: _t('cancel_the_reservation') + msg: _t('do_you_really_want_to_cancel_this_reservation_this_apply_to_all_booked_tickets') + , -> # cancel confirmed + Slot.cancel {id: reservation.slots[0].id}, -> # successfully canceled + growl.success _t('reservation_was_successfully_cancelled') + index = $scope.reservations.indexOf(reservation) + $scope.event.nb_free_places = $scope.event.nb_free_places + reservation.total_booked_seats + $scope.reservations.splice(index, 1) + , (error)-> + growl.warning(_t('cancellation_failed')) + ## # Callback to alter an already booked reservation date. A modal window will be opened to allow the user to choose # a new date for his reservation (if any available) @@ -333,9 +362,9 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " $scope.reservation = angular.copy reservation # set the reservable_id to the first available event - for e in event.recurrence_events - if e.nb_free_places > reservation.total_booked_seats - $scope.reservation.reservable_id = e.id + for evt in event.recurrence_events + if evt.nb_free_places > reservation.total_booked_seats + $scope.reservation.reservable_id = evt.id break # Callback to validate the new reservation's date @@ -377,7 +406,7 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " ## # Checks if the provided reservation is able to be moved (date change) - # @param reservation {{total_booked_seats:number}} + # @param reservation {{slots:[], total_booked_seats:number}} ## $scope.reservationCanModify = (reservation)-> slotStart = moment(reservation.slots[0].start_at) @@ -390,6 +419,17 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " + ## + # Checks if the provided reservation is able to be cancelled + # @param reservation {{slots:[]}} + ## + $scope.reservationCanCancel = (reservation)-> + slotStart = moment(reservation.slots[0].start_at) + now = moment() + + return ($scope.enableBookingCancel and slotStart.diff(now, "hours") >= $scope.cancelBookingDelay) + + ## # Compute the total amount for the current reservation according to the previously set parameters # and assign the result in $scope.reserve.amountTotal @@ -516,7 +556,7 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", " ## - # Set the current reservation to the default values. This implies to reservation form to be hidden. + # Set the current reservation to the default values. This implies the reservation form to be hidden. ## resetEventReserve = -> if $scope.event diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 5d7f16cc0..85bc1d820 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -522,7 +522,7 @@ angular.module('application.router', ['ui.router']). PriceCategory.query().$promise ] settingsPromise: ['Setting', (Setting)-> - Setting.query(names: "['booking_move_enable', 'booking_move_delay']").$promise + Setting.query(names: "['booking_move_enable', 'booking_move_delay', 'booking_cancel_enable', 'booking_cancel_delay']").$promise ] translations: [ 'Translations', (Translations) -> Translations.query(['app.public.events_show', 'app.shared.member_select', 'app.shared.stripe', diff --git a/app/assets/templates/events/show.html.erb b/app/assets/templates/events/show.html.erb index 6ccf877e7..858c46182 100644 --- a/app/assets/templates/events/show.html.erb +++ b/app/assets/templates/events/show.html.erb @@ -159,6 +159,9 @@
{{ 'change' }}
+
+ {{ 'cancel' }} +
diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 61f0713b6..c2d480430 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -327,7 +327,11 @@ class Reservation < ActiveRecord::Base end def total_booked_seats - total = nb_reserve_places + total = 0 + unless slots.first.canceled_at + total = nb_reserve_places + end + if tickets.count > 0 total += tickets.map(&:booked).map(&:to_i).reduce(:+) end @@ -356,7 +360,7 @@ class Reservation < ActiveRecord::Base def training_not_fully_reserved slot = self.slots.first - errors.add(:training, "already fully reserved") if Availability.find(slot.availability_id).is_completed + errors.add(:training, 'already fully reserved') if Availability.find(slot.availability_id).is_completed end private diff --git a/config/locales/app.logged.en.yml b/config/locales/app.logged.en.yml index b0195a92f..bfa3c7f2d 100644 --- a/config/locales/app.logged.en.yml +++ b/config/locales/app.logged.en.yml @@ -119,7 +119,7 @@ en: i_reserve: "I reserve" i_shift: "I shift" i_change: "I change" - do_you_really_want_to_cancel_this_reservation: "So you really want to cancel this reservation?" + do_you_really_want_to_cancel_this_reservation: "Do you really want to cancel this reservation?" reservation_was_cancelled_successfully: "Reservation was cancelled successfully." cancellation_failed: "Cancellation failed." a_problem_occured_during_the_payment_process_please_try_again_later: "A problem occurred during the payment process. Please try again later." diff --git a/config/locales/app.public.en.yml b/config/locales/app.public.en.yml index e85c74001..00ae95794 100644 --- a/config/locales/app.public.en.yml +++ b/config/locales/app.public.en.yml @@ -241,6 +241,9 @@ en: book: "Book" change_the_reservation: "Change the reservation" you_can_shift_this_reservation_on_the_following_slots: "You can shift this reservation on the following slots:" + cancel_the_reservation: "Cancel the reservation" + do_you_really_want_to_cancel_this_reservation_this_apply_to_all_booked_tickets: "Do you really want to cancel this reservation? This apply to ALL booked tickets." + cancellation_failed: "Cancellation failed." calendar: calendar: "Calendar" diff --git a/config/locales/app.public.fr.yml b/config/locales/app.public.fr.yml index 0a3d8b083..3c1a479c1 100644 --- a/config/locales/app.public.fr.yml +++ b/config/locales/app.public.fr.yml @@ -242,6 +242,9 @@ fr: book: "Réserver" change_the_reservation: "Modifier la réservation" you_can_shift_this_reservation_on_the_following_slots: "Vous pouvez déplacer cette réservation sur les créneaux suivants :" + cancel_the_reservation: "Annuler la réservation" + do_you_really_want_to_cancel_this_reservation_this_apply_to_all_booked_tickets: "Êtes vous sur de vouloir annuler cette réservation? Ceci s'applique à TOUTES les places réservées." + cancellation_failed: "L'annulation a échoué." calendar: calendar: "Calendrier"