mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-06 01:08:21 +01:00
fix price computation in front-end
This commit is contained in:
parent
953224f1f7
commit
dbdedadf8c
@ -612,8 +612,15 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
*/
|
*/
|
||||||
const updateCartPrice = function () {
|
const updateCartPrice = function () {
|
||||||
if (Object.keys($scope.user).length > 0) {
|
if (Object.keys($scope.user).length > 0) {
|
||||||
const r = mkReservation($scope.events.reserved);
|
const items = [];
|
||||||
return Price.compute(mkCartItems([r], ''), function (res) {
|
if ($scope.selectedPlan) {
|
||||||
|
items.push(mkSubscription($scope.selectedPlan.id));
|
||||||
|
}
|
||||||
|
if ($scope.events.reserved && $scope.events.reserved.length > 0) {
|
||||||
|
items.push(mkReservation($scope.events.reserved));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Price.compute(mkCartItems(items), function (res) {
|
||||||
$scope.amountTotal = res.price;
|
$scope.amountTotal = res.price;
|
||||||
$scope.schedule.payment_schedule = res.schedule;
|
$scope.schedule.payment_schedule = res.schedule;
|
||||||
$scope.totalNoCoupon = res.price_without_coupon;
|
$scope.totalNoCoupon = res.price_without_coupon;
|
||||||
@ -679,7 +686,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
* @param paymentMethod {string}
|
* @param paymentMethod {string}
|
||||||
* @return {CartItems}
|
* @return {CartItems}
|
||||||
*/
|
*/
|
||||||
const mkCartItems = function (items, paymentMethod) {
|
const mkCartItems = function (items, paymentMethod = '') {
|
||||||
const cartItems = {
|
const cartItems = {
|
||||||
customer_id: $scope.user.id,
|
customer_id: $scope.user.id,
|
||||||
payment_schedule: $scope.schedule.requested_schedule,
|
payment_schedule: $scope.schedule.requested_schedule,
|
||||||
@ -709,7 +716,6 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
* Open a modal window that allows the user to process a local payment for his current shopping cart (admin only).
|
* Open a modal window that allows the user to process a local payment for his current shopping cart (admin only).
|
||||||
*/
|
*/
|
||||||
const payOnSite = function (items) {
|
const payOnSite = function (items) {
|
||||||
// TODO, refactor to use cartItems: we had to remove the reservation object.
|
|
||||||
$uibModal.open({
|
$uibModal.open({
|
||||||
templateUrl: '/shared/valid_reservation_modal.html',
|
templateUrl: '/shared/valid_reservation_modal.html',
|
||||||
size: $scope.schedule.payment_schedule ? 'lg' : 'sm',
|
size: $scope.schedule.payment_schedule ? 'lg' : 'sm',
|
||||||
@ -718,8 +724,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
return Price.compute(mkCartItems(items, '')).$promise;
|
return Price.compute(mkCartItems(items, '')).$promise;
|
||||||
},
|
},
|
||||||
cartItems () {
|
cartItems () {
|
||||||
// these items are used in case of payment schedule. By default, the schedule is meant to be paid by card
|
return mkCartItems(items, '');
|
||||||
return mkCartItems(items, 'card');
|
|
||||||
},
|
},
|
||||||
wallet () {
|
wallet () {
|
||||||
return Wallet.getWalletByUser({ user_id: $scope.user.id }).$promise;
|
return Wallet.getWalletByUser({ user_id: $scope.user.id }).$promise;
|
||||||
@ -792,7 +797,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
}
|
}
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
// save subscription (if there's only a subscription selected)
|
// save subscription (if there's only a subscription selected)
|
||||||
if ($scope.reservation.slots_attributes.length === 0 && selectedPlan) {
|
if ((!$scope.cartItems.reservation || $scope.cartItems.reservation.slots_attributes.length === 0) && selectedPlan) {
|
||||||
const sub = mkSubscription(selectedPlan.id);
|
const sub = mkSubscription(selectedPlan.id);
|
||||||
|
|
||||||
return Subscription.save(mkCartItems([sub], $scope.method.payment_method),
|
return Subscription.save(mkCartItems([sub], $scope.method.payment_method),
|
||||||
@ -806,8 +811,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// otherwise, save the reservation (may include a subscription)
|
// otherwise, save the reservation (may include a subscription)
|
||||||
const sub = selectedPlan ? mkSubscription(selectedPlan.id) : undefined;
|
Reservation.save(cartItems, function (reservation) {
|
||||||
Reservation.save(mkCartItems([$scope.reservation, sub], coupon), function (reservation) {
|
|
||||||
$uibModalInstance.close(reservation);
|
$uibModalInstance.close(reservation);
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
}, function (response) {
|
}, function (response) {
|
||||||
@ -848,7 +852,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
const initialize = function () {
|
const initialize = function () {
|
||||||
$scope.$watch('method.payment_method', function (newValue) {
|
$scope.$watch('method.payment_method', function (newValue) {
|
||||||
$scope.validButtonName = computeValidButtonName();
|
$scope.validButtonName = computeValidButtonName();
|
||||||
$scope.cartItems = mkCartItems([$scope.reservation], newValue);
|
$scope.cartItems.payment_method = newValue;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -858,7 +862,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
const computeValidButtonName = function () {
|
const computeValidButtonName = function () {
|
||||||
let method = '';
|
let method = '';
|
||||||
if ($scope.schedule) {
|
if ($scope.schedule) {
|
||||||
if (AuthService.isAuthorized(['admin', 'manager']) && $rootScope.currentUser.id !== reservation.user_id) {
|
if (AuthService.isAuthorized(['admin', 'manager']) && $rootScope.currentUser.id !== cartItems.customer_id) {
|
||||||
method = $scope.method.payment_method;
|
method = $scope.method.payment_method;
|
||||||
} else {
|
} else {
|
||||||
method = settings.payment_gateway;
|
method = settings.payment_gateway;
|
||||||
@ -916,7 +920,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
if ($scope.selectedPlan) {
|
if ($scope.selectedPlan) {
|
||||||
items.push(mkSubscription($scope.selectedPlan.id));
|
items.push(mkSubscription($scope.selectedPlan.id));
|
||||||
}
|
}
|
||||||
if ($scope.reservation.slots_attributes.length > 0) {
|
if ($scope.events.reserved && $scope.events.reserved.length > 0) {
|
||||||
items.push(mkReservation($scope.events.reserved));
|
items.push(mkReservation($scope.events.reserved));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,9 +55,7 @@ class CartService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def reservable_from_hash(cart_item, plan_info)
|
def reservable_from_hash(cart_item, plan_info)
|
||||||
return nil if cart_item[:reservable_id].blank?
|
reservable = cart_item[:reservable_type]&.constantize&.find(cart_item[:reservable_id])
|
||||||
|
|
||||||
reservable = cart_item[:reservable_type].constantize.find(cart_item[:reservable_id])
|
|
||||||
case reservable
|
case reservable
|
||||||
when Machine
|
when Machine
|
||||||
CartItem::MachineReservation.new(@customer,
|
CartItem::MachineReservation.new(@customer,
|
||||||
@ -88,6 +86,7 @@ class CartService
|
|||||||
plan: plan_info[:plan],
|
plan: plan_info[:plan],
|
||||||
new_subscription: plan_info[:new_subscription])
|
new_subscription: plan_info[:new_subscription])
|
||||||
else
|
else
|
||||||
|
STDERR.puts "WARNING: the reservable #{reservable} is not implemented"
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user