1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/assets/javascripts/controllers/plans.coffee.erb

345 lines
12 KiB
Plaintext
Raw Normal View History

2016-03-23 18:39:41 +01:00
'use strict'
2016-07-08 17:24:51 +02:00
Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScope", "$state", '$uibModal', 'Auth', 'dialogs', 'growl', 'plansPromise', 'groupsPromise', 'Subscription', 'Member', 'subscriptionExplicationsPromise', '_t', 'Wallet', 'helpers'
, ($scope, $rootScope, $state, $uibModal, Auth, dialogs, growl, plansPromise, groupsPromise, Subscription, Member, subscriptionExplicationsPromise, _t, Wallet, helpers) ->
2016-03-23 18:39:41 +01:00
### PUBLIC SCOPE ###
## list of groups
$scope.groups = groupsPromise.filter (g) -> g.slug != 'admins' & !g.disabled
2016-03-23 18:39:41 +01:00
## default : do not show the group changing form
## group ID of the current/selected user
$scope.group =
change: false
id: null
2016-03-23 18:39:41 +01:00
## list of plans, classified by group
$scope.plansClassifiedByGroup = []
2017-08-30 14:57:06 +02:00
for group in $scope.groups
2016-03-23 18:39:41 +01:00
groupObj = { id: group.id, name: group.name, plans: [] }
for plan in plansPromise
groupObj.plans.push(plan) if plan.group_id == group.id
$scope.plansClassifiedByGroup.push(groupObj)
## user to deal with
$scope.ctrl =
member: null
member_id: null
## already subscribed plan of the current user
$scope.paid =
plan: null
2016-03-23 18:39:41 +01:00
## plan to subscribe (shopping cart)
$scope.selectedPlan = null
2016-08-11 18:17:28 +02:00
## Discount coupon to apply to the basket, if any
$scope.coupon =
applied: null
## Storage for the total price (plan price + coupon, if any)
$scope.cart =
total: null
## text that appears in the bottom-right box of the page (subscriptions rules details)
2016-03-23 18:39:41 +01:00
$scope.subscriptionExplicationsAlert = subscriptionExplicationsPromise.setting.value
##
# Callback to deal with the subscription of the user selected in the dropdown list instead of the current user's
# subscription. (admins only)
##
$scope.updateMember = ->
$scope.selectedPlan = null
$scope.paid.plan = null
$scope.group.change = false
Member.get {id: $scope.ctrl.member.id}, (member) ->
$scope.ctrl.member = member
$scope.group.id = $scope.ctrl.member.group_id
2016-03-23 18:39:41 +01:00
##
# Add the provided plan to the shopping basket
# @param plan {Object} The plan to subscribe to
##
$scope.selectPlan = (plan) ->
if $scope.isAuthenticated()
if $scope.selectedPlan != plan
$scope.selectedPlan = plan
2016-08-11 18:17:28 +02:00
updateCartPrice()
2016-03-23 18:39:41 +01:00
else
$scope.selectedPlan = null
else
$scope.login()
##
# Callback to trigger the payment process of the subscription
##
$scope.openSubscribePlanModal = ->
2016-07-08 17:24:51 +02:00
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
amountToPay = helpers.getAmountToPay($scope.cart.total, wallet.amount)
2016-07-08 17:24:51 +02:00
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
payByStripe()
else
if $scope.currentUser.role is 'admin' or amountToPay is 0
payOnSite()
2016-03-23 18:39:41 +01:00
##
# Return the group object, identified by the ID set in $scope.group.id
2016-03-23 18:39:41 +01:00
##
$scope.getUserGroup = ->
for group in $scope.groups
if group.id == $scope.group.id
2016-03-23 18:39:41 +01:00
return group
##
# Change the group of the current/selected user to the one set in $scope.group.id
2016-03-23 18:39:41 +01:00
##
$scope.selectGroup = ->
Member.update {id: $scope.ctrl.member.id}, {user: {group_id: $scope.group.id}}, (user) ->
2016-03-23 18:39:41 +01:00
$scope.ctrl.member = user
$scope.group.change = false
2016-03-23 18:39:41 +01:00
if $scope.currentUser.role isnt 'admin'
$rootScope.currentUser = user
Auth._currentUser.group_id = user.group_id
2016-03-23 18:39:41 +01:00
growl.success(_t('your_group_was_successfully_changed'))
else
growl.success(_t('the_user_s_group_was_successfully_changed'))
, (err) ->
if $scope.currentUser.role isnt 'admin'
growl.error(_t('an_error_prevented_your_group_from_being_changed'))
else
growl.error(_t('an_error_prevented_to_change_the_user_s_group'))
console.error(err)
##
# Return an enumerable meaninful string for the gender of the provider user
# @param user {Object} Database user record
# @return {string} 'male' or 'female'
##
$scope.getGender = (user) ->
if user and user.profile
2016-03-23 18:39:41 +01:00
if user.profile.gender == "true" then 'male' else 'female'
else 'other'
##
# Test if the provided date is in the future
# @param dateTime {Date}
# @return {boolean}
##
$scope.isInFuture = (dateTime)->
if moment().diff(moment(dateTime)) < 0
true
else
false
2017-10-04 18:56:39 +02:00
##
# To use as callback in Array.prototype.filter to get only enabled plans
##
$scope.filterDisabledPlans = (plan) ->
!plan.disabled
2016-03-23 18:39:41 +01:00
### PRIVATE SCOPE ###
##
# Kind of constructor: these actions will be realized first when the controller is loaded
##
initialize = ->
if $scope.currentUser
if $scope.currentUser.role isnt 'admin'
$scope.ctrl.member = $scope.currentUser
$scope.paid.plan = $scope.currentUser.subscribed_plan
$scope.group.id = $scope.currentUser.group_id
2016-03-23 18:39:41 +01:00
$scope.$on 'devise:new-session', (event, user)->
$scope.ctrl.member = user
2016-08-11 18:17:28 +02:00
# 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()
##
# Compute the total amount for the current reservation according to the previously set parameters
# and assign the result in $scope.reserve.amountTotal
##
updateCartPrice = ->
# first we check that a user was selected
if Object.keys($scope.ctrl.member).length > 0
$scope.cart.total = $scope.selectedPlan.amount
# apply the coupon if any
if $scope.coupon.applied
if $scope.coupon.applied.type == 'percent_off'
2016-08-11 18:17:28 +02:00
discount = $scope.cart.total * $scope.coupon.applied.percent_off / 100
else if $scope.coupon.applied.type == 'amount_off'
discount = $scope.coupon.applied.amount_off
$scope.cart.total -= discount
2016-08-11 18:17:28 +02:00
else
$scope.reserve.amountTotal = null
2016-03-23 18:39:41 +01:00
##
# Open a modal window which trigger the stripe payment process
##
payByStripe = ->
$uibModal.open
templateUrl: '<%= asset_path "stripe/payment_modal.html" %>'
size: 'md'
resolve:
selectedPlan: -> $scope.selectedPlan
member: -> $scope.ctrl.member
2016-08-11 18:17:28 +02:00
price: -> $scope.cart.total
2016-07-08 17:24:51 +02:00
wallet: ->
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
2016-08-11 18:17:28 +02:00
coupon: -> $scope.coupon.applied
2016-11-23 14:27:11 +01:00
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'price', 'Subscription', 'CustomAsset', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedPlan, member, price, Subscription, CustomAsset, wallet, helpers, $filter, coupon) ->
# User's wallet amount
2016-07-08 17:24:51 +02:00
$scope.walletAmount = wallet.amount
2016-08-11 18:17:28 +02:00
# Final price to pay by the user
$scope.amount = helpers.getAmountToPay(price, wallet.amount)
2016-07-08 17:24:51 +02:00
2016-08-11 18:17:28 +02:00
# The plan that the user is about to subscribe
2016-03-23 18:39:41 +01:00
$scope.selectedPlan = selectedPlan
2016-07-08 17:24:51 +02:00
2016-08-11 18:17:28 +02:00
# Used in wallet info template to interpolate some translations
2016-07-19 16:24:50 +02:00
$scope.numberFilter = $filter('number')
2016-03-23 18:39:41 +01:00
# retrieve the CGV
CustomAsset.get {name: 'cgv-file'}, (cgv) ->
$scope.cgv = cgv.custom_asset
2016-08-11 18:17:28 +02:00
##
# Callback for click on the 'proceed' button.
# Handle the stripe's card tokenization process response and save the subscription to the API with the
# card token just created.
##
2016-03-23 18:39:41 +01:00
$scope.payment = (status, response) ->
if response.error
growl.error(response.error.message)
else
$scope.attempting = true
Subscription.save
2016-09-20 14:37:35 +02:00
coupon_code: (coupon.code if coupon)
2016-03-23 18:39:41 +01:00
subscription:
plan_id: selectedPlan.id
user_id: member.id
card_token: response.id
, (data, status) -> # success
$uibModalInstance.close(data)
, (data, status) -> # failed
$scope.alerts = []
$scope.alerts.push({msg: _t('an_error_occured_during_the_payment_process_please_try_again_later'), type: 'danger' })
$scope.attempting = false
]
.result['finally'](null).then (subscription)->
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan)
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan)
$scope.paid.plan = angular.copy($scope.selectedPlan)
2016-03-23 18:39:41 +01:00
$scope.selectedPlan = null
$scope.coupon.applied = null
2016-03-23 18:39:41 +01:00
##
# Open a modal window which trigger the local payment process
##
payOnSite = ->
$uibModal.open
templateUrl: '<%= asset_path "plans/payment_modal.html" %>'
size: 'sm'
resolve:
selectedPlan: -> $scope.selectedPlan
member: -> $scope.ctrl.member
2016-08-11 18:17:28 +02:00
price: -> $scope.cart.total
2016-07-08 17:24:51 +02:00
wallet: ->
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
2016-08-11 18:17:28 +02:00
coupon: -> $scope.coupon.applied
2016-11-23 14:27:11 +01:00
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'price', 'Subscription', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedPlan, member, price, Subscription, wallet, helpers, $filter, coupon) ->
2016-07-08 17:24:51 +02:00
# user wallet amount
$scope.walletAmount = wallet.amount
2016-08-11 18:17:28 +02:00
# subcription price, coupon subtracted if any
$scope.price = price
2016-07-08 17:24:51 +02:00
# price to pay
$scope.amount = helpers.getAmountToPay($scope.price, wallet.amount)
2016-08-11 18:17:28 +02:00
# Used in wallet info template to interpolate some translations
2016-07-19 16:24:50 +02:00
$scope.numberFilter = $filter('number')
2016-08-11 18:17:28 +02:00
# The plan that the user is about to subscribe
2016-03-23 18:39:41 +01:00
$scope.plan = selectedPlan
2016-08-11 18:17:28 +02:00
# The member who is subscribing a plan
2016-03-23 18:39:41 +01:00
$scope.member = member
2016-07-08 17:24:51 +02:00
# Button label
if $scope.amount > 0
2016-07-25 12:10:10 +02:00
$scope.validButtonName = _t('confirm_payment_of_html', {ROLE:$scope.currentUser.role, AMOUNT:$filter('currency')($scope.amount)}, "messageformat")
2016-07-08 17:24:51 +02:00
else
if price.price > 0 and $scope.walletAmount == 0
2016-07-25 12:10:10 +02:00
$scope.validButtonName = _t('confirm_payment_of_html', {ROLE:$scope.currentUser.role, AMOUNT:$filter('currency')(price.price)}, "messageformat")
2016-07-08 17:24:51 +02:00
else
$scope.validButtonName = _t('confirm')
2016-08-11 18:17:28 +02:00
##
# Callback for the 'proceed' button.
# Save the subscription to the API
##
2016-03-23 18:39:41 +01:00
$scope.ok = ->
$scope.attempting = true
Subscription.save
2016-09-20 14:37:35 +02:00
coupon_code: (coupon.code if coupon)
2016-03-23 18:39:41 +01:00
subscription:
plan_id: selectedPlan.id
user_id: member.id
, (data, status) -> # success
$uibModalInstance.close(data)
, (data, status) -> # failed
$scope.alerts = []
$scope.alerts.push({msg: _t('an_error_occured_during_the_payment_process_please_try_again_later'), type: 'danger' })
$scope.attempting = false
2016-08-11 18:17:28 +02:00
##
# Callback for the 'cancel' button.
# Close the modal box.
##
2016-03-23 18:39:41 +01:00
$scope.cancel = ->
$uibModalInstance.dismiss('cancel')
]
.result['finally'](null).then (reservation)->
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan)
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan)
$scope.ctrl.member = null
$scope.paid.plan = angular.copy($scope.selectedPlan)
2016-03-23 18:39:41 +01:00
$scope.selectedPlan = null
$scope.coupon.applied = null
2016-03-23 18:39:41 +01:00
## !!! MUST BE CALLED AT THE END of the controller
initialize()
]