diff --git a/app/assets/javascripts/controllers/machines.coffee.erb b/app/assets/javascripts/controllers/machines.coffee.erb index 98f2c8009..99158a01b 100644 --- a/app/assets/javascripts/controllers/machines.coffee.erb +++ b/app/assets/javascripts/controllers/machines.coffee.erb @@ -268,8 +268,8 @@ Application.Controllers.controller "ShowMachineController", ['$scope', '$state', # This controller workflow is pretty similar to the trainings reservation controller. ## -Application.Controllers.controller "ReserveMachineController", ["$scope", "$state", '$stateParams', "$uibModal", '_t', "moment", 'Machine', 'Auth', 'dialogs', '$timeout', 'Price', 'Member', 'Availability', 'Slot', 'Setting', 'CustomAsset', 'plansPromise', 'groupsPromise', 'growl', 'settingsPromise', 'Wallet', 'helpers', 'uiCalendarConfig', 'CalendarConfig', -($scope, $state, $stateParams, $uibModal, _t, moment, Machine, Auth, dialogs, $timeout, Price, Member, Availability, Slot, Setting, CustomAsset, plansPromise, groupsPromise, growl, settingsPromise, Wallet, helpers, uiCalendarConfig, CalendarConfig) -> +Application.Controllers.controller "ReserveMachineController", ["$scope", "$state", '$stateParams', "$uibModal", '_t', "moment", 'Machine', 'Auth', 'dialogs', '$timeout', 'Price', 'Member', 'Availability', 'Slot', 'Setting', 'CustomAsset', 'plansPromise', 'groupsPromise', 'growl', 'machinePromise', 'settingsPromise', 'Wallet', 'helpers', 'uiCalendarConfig', 'CalendarConfig', +($scope, $state, $stateParams, $uibModal, _t, moment, Machine, Auth, dialogs, $timeout, Price, Member, Availability, Slot, Setting, CustomAsset, plansPromise, groupsPromise, growl, machinePromise, settingsPromise, Wallet, helpers, uiCalendarConfig, CalendarConfig) -> @@ -309,6 +309,10 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat ## total amount of the bill to pay $scope.amountTotal = 0 + ## Discount coupon to apply to the basket, if any + $scope.coupon = + applied: null + ## is the user allowed to change the date of his booking $scope.enableBookingMove = true @@ -328,7 +332,7 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat member: {} ## current machine to reserve - $scope.machine = {} + $scope.machine = machinePromise ## fullCalendar (v2) configuration $scope.calendarConfig = CalendarConfig @@ -599,11 +603,10 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat if $scope.currentUser.role isnt 'admin' $scope.ctrl.member = $scope.currentUser - $scope.machine = Machine.get {id: $stateParams.id} - , -> - return - , -> - $state.go('app.public.machines_list') + # watch when a coupon is applied to re-compute the total price + $scope.$watch 'coupon.applied', (newValue, oldValue) -> + unless newValue == null and oldValue == null + updateCartPrice() @@ -638,7 +641,10 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat updateCartPrice = -> if Object.keys($scope.ctrl.member).length > 0 r = mkReservation($scope.ctrl.member, $scope.eventsReserved, $scope.selectedPlan) - Price.compute {reservation: r}, (res) -> + params = {reservation: r} + if $scope.coupon.applied + params['coupon_id'] = $scope.coupon.applied.id + Price.compute params, (res) -> $scope.amountTotal = res.price setSlotsDetails(res.details) else diff --git a/app/assets/javascripts/directives/coupon.coffee.erb b/app/assets/javascripts/directives/coupon.coffee.erb index d71d9081a..3caf5d36d 100644 --- a/app/assets/javascripts/directives/coupon.coffee.erb +++ b/app/assets/javascripts/directives/coupon.coffee.erb @@ -3,6 +3,7 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g restrict: 'E' scope: show: '=' + coupon: '=' templateUrl: '<%= asset_path "shared/_coupon.html" %>' link: ($scope, element, attributes) -> @@ -24,12 +25,15 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g $scope.validateCode = -> if $scope.couponCode == '' $scope.status = 'pending' + $scope.coupon = null else Coupon.validate {code: $scope.couponCode}, (res) -> $scope.status = 'valid' + $scope.coupon = res growl.success(_t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off})) , (err) -> $scope.status = 'invalid' + $scope.coupon = null growl.error(_t('unable_to_apply_the_coupon_because_'+err.data.status)) } ] diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index f08acea6c..712718f72 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -351,6 +351,9 @@ angular.module('application.router', ['ui.router']). groupsPromise: ['Group', (Group)-> Group.query().$promise ] + machinePromise: ['Machine', '$stateParams', (Machine, $stateParams)-> + Machine.get(id: $stateParams.id).$promise + ] settingsPromise: ['Setting', (Setting)-> Setting.query(names: "['machine_explications_alert', 'booking_window_start', diff --git a/app/assets/templates/machines/reserve.html.erb b/app/assets/templates/machines/reserve.html.erb index 91407ba76..7c0bc4c6e 100644 --- a/app/assets/templates/machines/reserve.html.erb +++ b/app/assets/templates/machines/reserve.html.erb @@ -78,7 +78,7 @@
-