mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-29 10:24:20 +01:00
compute price including coupon discount
This commit is contained in:
parent
2f2099b723
commit
a679bc1638
@ -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
|
||||
|
@ -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))
|
||||
}
|
||||
]
|
||||
|
@ -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',
|
||||
|
@ -78,7 +78,7 @@
|
||||
<div class="clear"><a class="pull-right m-b-sm text-u-l ng-scope m-r-sm" href="#" ng-click="removeMachineSlot(machineSlot, $event)" ng-if="machineSlot.isValid" translate>{{ 'remove_this_slot' }}</a></div>
|
||||
</div>
|
||||
|
||||
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)"></coupon>
|
||||
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)" coupon="coupon.applied" callback="setCouponCb()"></coupon>
|
||||
|
||||
<span ng-hide="fablabWithoutPlans">
|
||||
<div ng-if="machineSlotsValid() && !ctrl.member.subscribed_plan" ng-show="!plansAreShown">
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user