diff --git a/app/assets/javascripts/controllers/machines.coffee.erb b/app/assets/javascripts/controllers/machines.coffee.erb index 76ce33b73..8b5a389a2 100644 --- a/app/assets/javascripts/controllers/machines.coffee.erb +++ b/app/assets/javascripts/controllers/machines.coffee.erb @@ -636,7 +636,7 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat # Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object # @param reservation {Object} as returned by mkReservation() # @param coupon {Object} Coupon as returned from the API - # @return {{reservation:Object, coupon_id:number}} + # @return {{reservation:Object, coupon_code:string}} ## mkRequestParams = (reservation, coupon) -> params = diff --git a/app/assets/javascripts/controllers/trainings.coffee.erb b/app/assets/javascripts/controllers/trainings.coffee.erb index e5376ea91..4f73efe9a 100644 --- a/app/assets/javascripts/controllers/trainings.coffee.erb +++ b/app/assets/javascripts/controllers/trainings.coffee.erb @@ -107,6 +107,10 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta ## Selected training unless 'all' trainings are displayed $scope.training = trainingPromise + ## Discount coupon to apply to the basket, if any + $scope.coupon = + applied: null + ## fullCalendar (v2) configuration $scope.calendarConfig = CalendarConfig minTime: moment.duration(moment(settingsPromise.booking_window_start).format('HH:mm:ss')) @@ -321,7 +325,7 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta $scope.updatePrices = -> if Object.keys($scope.ctrl.member).length > 0 r = mkReservation($scope.ctrl.member, $scope.selectedTraining, $scope.selectedPlan) - Price.compute {reservation: r}, (res) -> + Price.compute mkRequestParams(r, $scope.coupon.applied), (res) -> $scope.amountTotal = res.price else $scope.amountTotal = null @@ -338,6 +342,11 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta Member.get id: $scope.currentUser.id, (member) -> $scope.ctrl.member = member + # watch when a coupon is applied to re-compute the total price + $scope.$watch 'coupon.applied', (newValue, oldValue) -> + unless newValue == null and oldValue == null + $scope.updatePrices() + ## @@ -365,6 +374,21 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta + ## + # Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object + # @param reservation {Object} as returned by mkReservation() + # @param coupon {Object} Coupon as returned from the API + # @return {{reservation:Object, coupon_code:string}} + ## + mkRequestParams = (reservation, coupon) -> + params = + reservation: reservation + coupon_code: (coupon.code if coupon) + + params + + + ## # Triggered when the user clicks on a reservation slot in the agenda. # Defines the behavior to adopt depending on the slot status (already booked, free, ready to be reserved ...), @@ -475,12 +499,15 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta reservation: -> reservation price: -> - Price.compute({reservation: reservation}).$promise + Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise wallet: -> Wallet.getWalletByUser({user_id: reservation.user_id}).$promise cgv: -> CustomAsset.get({name: 'cgv-file'}).$promise - controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'wallet', 'cgv', 'Auth', 'Reservation', '$locale', 'helpers', '$filter', ($scope, $uibModalInstance, $state, reservation, price, wallet, cgv, Auth, Reservation, $locale, helpers, $filter) -> + coupon: -> + $scope.coupon.applied + controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'wallet', 'cgv', 'Auth', 'Reservation', '$locale', 'helpers', '$filter', 'coupon' + ($scope, $uibModalInstance, $state, reservation, price, wallet, cgv, Auth, Reservation, $locale, helpers, $filter, coupon) -> # user wallet amount $scope.walletAmount = wallet.amount @@ -506,7 +533,7 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta else $scope.attempting = true $scope.reservation.card_token = response.id - Reservation.save reservation: $scope.reservation, (reservation) -> + Reservation.save mkRequestParams($scope.reservation, coupon), (reservation) -> $uibModalInstance.close(reservation) , (response)-> $scope.alerts = [] @@ -535,10 +562,13 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta reservation: -> reservation price: -> - Price.compute({reservation: reservation}).$promise + Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise wallet: -> Wallet.getWalletByUser({user_id: reservation.user_id}).$promise - controller: ['$scope', '$uibModalInstance', '$state', '$filter', 'reservation', 'price', 'wallet', 'Auth', 'Reservation', '$locale', 'helpers', ($scope, $uibModalInstance, $state, $filter, reservation, price, wallet, Auth, Reservation, $locale, helpers) -> + coupon: -> + $scope.coupon.applied + controller: ['$scope', '$uibModalInstance', '$state', '$filter', 'reservation', 'price', 'wallet', 'Auth', 'Reservation', '$locale', 'helpers', 'coupon' + ($scope, $uibModalInstance, $state, $filter, reservation, price, wallet, Auth, Reservation, $locale, helpers, coupon) -> # user wallet amount $scope.walletAmount = wallet.amount @@ -569,7 +599,7 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta ## $scope.ok = -> $scope.attempting = true - Reservation.save reservation: $scope.reservation, (reservation) -> + Reservation.save mkRequestParams($scope.reservation, coupon), (reservation) -> $uibModalInstance.close(reservation) $scope.attempting = true , (response)-> @@ -592,7 +622,7 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta # first we check that a user was selected if Object.keys($scope.ctrl.member).length > 0 r = mkReservation($scope.ctrl.member, training) # reservation without any Plan -> we get the training price - Price.compute {reservation: r}, (res) -> + Price.compute mkRequestParams(r, $scope.coupon.applied), (res) -> $scope.selectedTrainingAmount = res.price else $scope.selectedTrainingAmount = null diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 712718f72..10c6c8c86 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -444,7 +444,8 @@ angular.module('application.router', ['ui.router']). ] translations: [ 'Translations', (Translations) -> Translations.query(['app.logged.trainings_reserve', 'app.shared.plan_subscribe', 'app.shared.member_select', - 'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', 'app.shared.wallet']).$promise + 'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', + 'app.shared.wallet', 'app.shared.coupon_input']).$promise ] # notifications .state 'app.logged.notifications', diff --git a/app/assets/templates/trainings/reserve.html.erb b/app/assets/templates/trainings/reserve.html.erb index f104a2068..5f03078f4 100644 --- a/app/assets/templates/trainings/reserve.html.erb +++ b/app/assets/templates/trainings/reserve.html.erb @@ -82,6 +82,8 @@ {{ 'remove_this_slot' }} + +

{{ 'to_benefit_from_attractive_prices_and_a_free_training' }}