1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

check cash coupons does not exceed cart amount for trainings & machines

This commit is contained in:
Sylvain 2016-11-24 13:58:41 +01:00
parent 3dc5f1a0e4
commit 04e10c8bb6
11 changed files with 28 additions and 9 deletions

View File

@ -315,6 +315,9 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
## total amount of the bill to pay
$scope.amountTotal = 0
## total amount of the elements in the cart, without considering any coupon
$scope.totalNoCoupon = 0
## Discount coupon to apply to the basket, if any
$scope.coupon =
applied: null
@ -661,6 +664,7 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
r = mkReservation($scope.ctrl.member, $scope.eventsReserved, $scope.selectedPlan)
Price.compute mkRequestParams(r, $scope.coupon.applied), (res) ->
$scope.amountTotal = res.price
$scope.totalNoCoupon = res.price_without_coupon
setSlotsDetails(res.details)
else
# otherwise we alert, this error musn't occur when the current user is not admin

View File

@ -137,6 +137,12 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
$scope.coupon =
applied: null
## Total price of the cart, that the user will pay
$scope.amountTotal = 0
## Total amount of the elements in the cart, without considering any coupon
$scope.totalNoCoupon = 0
## fullCalendar (v2) configuration
$scope.calendarConfig = CalendarConfig
minTime: moment.duration(moment(settingsPromise.booking_window_start).format('HH:mm:ss'))
@ -353,6 +359,7 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
r = mkReservation($scope.ctrl.member, $scope.selectedTraining, $scope.selectedPlan)
Price.compute mkRequestParams(r, $scope.coupon.applied), (res) ->
$scope.amountTotal = res.price
$scope.totalNoCoupon = res.price_without_coupon
else
$scope.amountTotal = null

View File

@ -4,6 +4,7 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g
scope:
show: '='
coupon: '='
total: '@'
userId: '@'
templateUrl: '<%= asset_path "shared/_coupon.html" %>'
link: ($scope, element, attributes) ->
@ -15,11 +16,9 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g
# Available status are: 'pending', 'valid', 'invalid'
$scope.status = 'pending'
# Binding for the code inputed
# Binding for the code inputed (see the attached template)
$scope.couponCode = null
##
# Callback to validate the code
##
@ -28,7 +27,7 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g
$scope.status = 'pending'
$scope.coupon = null
else
Coupon.validate {code: $scope.couponCode, user_id: $scope.userId}, (res) ->
Coupon.validate {code: $scope.couponCode, user_id: $scope.userId, amount: $scope.total}, (res) ->
$scope.status = 'valid'
$scope.coupon = res
growl.success(_t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off}))

View File

@ -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.applied" user-id="{{ctrl.member.id}}"></coupon>
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)" coupon="coupon.applied" total="{{totalNoCoupon}}" user-id="{{ctrl.member.id}}"></coupon>
<span ng-hide="fablabWithoutPlans">
<div ng-if="machineSlotsValid() && !ctrl.member.subscribed_plan" ng-show="!plansAreShown">

View File

@ -82,7 +82,7 @@
<a class="pull-right m-t-xs text-u-l" href="#" ng-click="removeTraining($event)" ng-if="trainingIsValid" translate>{{ 'remove_this_slot' }}</a>
</div>
<coupon show="trainingIsValid && (!plansIsShow || selectedPlan)" coupon="coupon.applied" user-id="{{ctrl.member.id}}"></coupon>
<coupon show="trainingIsValid && (!plansIsShow || selectedPlan)" coupon="coupon.applied" user-id="{{ctrl.member.id}}" total="{{totalNoCoupon}}"></coupon>
<span ng-hide="fablabWithoutPlans">
<div ng-if="trainingIsValid && !ctrl.member.subscribed_plan" ng-show="!plansIsShow">

View File

@ -30,7 +30,7 @@ class API::CouponsController < API::ApiController
_user_id = params[:user_id]
end
status = @coupon.status(_user_id)
status = @coupon.status(_user_id, params[:amount])
if status != 'active'
render json: {status: status}, status: :unprocessable_entity
else

View File

@ -26,12 +26,15 @@ class Coupon < ActiveRecord::Base
# - may has expired because the validity date has been reached,
# - may have been used the maximum number of times it was allowed
# - may have already been used by the provided user, if the coupon is configured to allow only one use per user,
# - may exceed the current cart's total amount, if the coupon is configured to discount an amount (and not a percentage)
# - may be available for use
# @param [user_id] {Number} if provided and if the current coupon's validity_per_user == 'once', check that the coupon
# was already used by the provided user
# @param [amount] {Number} if provided and if the current coupon's type == 'amont_off' check that the coupon
# does not exceed the cart total price
# @return {String} status identifier
##
def status(user_id = nil)
def status(user_id = nil, amount = nil)
if not active?
'disabled'
elsif (!valid_until.nil?) and valid_until.at_end_of_day < DateTime.now
@ -40,6 +43,8 @@ class Coupon < ActiveRecord::Base
'sold_out'
elsif (!user_id.nil?) and validity_per_user == 'once' and users_ids.include?(user_id.to_i)
'already_used'
elsif (!amount.nil?) and type == 'amount_off' and amount_off > amount.to_f
'amount_exceeded'
else
'active'
end

View File

@ -111,10 +111,11 @@ class Price < ActiveRecord::Base
end
# === apply Coupon if any ===
_amount_no_coupon = _amount
_amount = CouponApplyService.new.(_amount, coupon_code)
# return result
{elements: _elements, total: _amount}
{elements: _elements, total: _amount, before_coupon: _amount_no_coupon}
end
private

View File

@ -1,4 +1,5 @@
json.price @amount[:total] / 100.00
json.price_without_coupon @amount[:before_coupon] / 100.00
json.details do
json.slots @amount[:elements][:slots] do |slot|
json.start_at slot[:start_at]

View File

@ -372,4 +372,5 @@ en:
unable_to_apply_the_coupon_because_expired: "Unable to apply the coupon: this code has expired."
unable_to_apply_the_coupon_because_sold_out: "Unable to apply the coupon: this code reached its quota."
unable_to_apply_the_coupon_because_already_used: "Unable to apply the coupon: you have already used this code once before."
unable_to_apply_the_coupon_because_amount_exceeded: "Unable to apply the coupon: the discount exceed the total amount of this purchase."
unable_to_apply_the_coupon_because_rejected: "This code does not exists."

View File

@ -372,4 +372,5 @@ fr:
unable_to_apply_the_coupon_because_expired: "Impossible d'appliquer la réduction : ce code promo a expiré."
unable_to_apply_the_coupon_because_sold_out: "Impossible d'appliquer la réduction : ce code promo a atteint son quota."
unable_to_apply_the_coupon_because_already_used: "Impossible d'appliquer la réduction : vous avez déjà utilisé ce code promo par le passé."
unable_to_apply_the_coupon_because_amount_exceeded: "Impossible d'appliquer la réduction : la réduction dépasse le total de cet achat."
unable_to_apply_the_coupon_because_rejected: "Ce code promo n'existe pas."