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 @@
{{ 'remove_this_slot' }}
- +
diff --git a/app/controllers/api/prices_controller.rb b/app/controllers/api/prices_controller.rb index ac7d2765f..66351af97 100644 --- a/app/controllers/api/prices_controller.rb +++ b/app/controllers/api/prices_controller.rb @@ -44,7 +44,7 @@ class API::PricesController < API::ApiController @amount = {elements: nil, total: 0} else _reservable = _price_params[:reservable_type].constantize.find(_price_params[:reservable_id]) - @amount = Price.compute(current_user.is_admin?, _user, _reservable, _price_params[:slots_attributes], _price_params[:plan_id], _price_params[:nb_reserve_places], _price_params[:nb_reserve_reduced_places]) + @amount = Price.compute(current_user.is_admin?, _user, _reservable, _price_params[:slots_attributes], _price_params[:plan_id], _price_params[:nb_reserve_places], _price_params[:nb_reserve_reduced_places], coupon_params[:coupon_id]) end @@ -64,4 +64,8 @@ class API::PricesController < API::ApiController params.require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places, :nb_reserve_reduced_places, slots_attributes: [:id, :start_at, :end_at, :availability_id, :offered]) end + + def coupon_params + params.permit(:coupon_id) + end end diff --git a/app/models/price.rb b/app/models/price.rb index f2b7c379c..1e74b5005 100644 --- a/app/models/price.rb +++ b/app/models/price.rb @@ -14,9 +14,10 @@ class Price < ActiveRecord::Base # @param [plan_id] {Number} if the user is subscribing to a plan at the same time of his reservation, specify the plan's ID here # @param [nb_places] {Number} for _reservable_ of type Event, pass here the number of booked places # @param [nb_reduced_places] {Number} for _reservable_ of type Event, pass here the number of booked places at reduced price + # @param [coupon_id] {Number} ID of the coupon to apply to the total price # @return {Hash} total and price detail ## - def self.compute(admin, user, reservable, slots, plan_id = nil, nb_places = nil, nb_reduced_places = nil) + def self.compute(admin, user, reservable, slots, plan_id = nil, nb_places = nil, nb_reduced_places = nil, coupon_id = nil) _amount = 0 _elements = Hash.new _elements[:slots] = Array.new @@ -110,6 +111,12 @@ class Price < ActiveRecord::Base _amount += plan.amount end + # === apply Coupon if any === + unless coupon_id.nil? + _coupon = Coupon.find(coupon_id) + _amount = _amount - (_amount * _coupon.percent_off / 100) + end + # return result {elements: _elements, total: _amount} end