2015-05-05 03:10:25 +02:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
Application.Controllers.controller "EventsController", ["$scope", "$state", 'Event', 'categoriesPromise', 'themesPromise', 'ageRangesPromise'
|
|
|
|
, ($scope, $state, Event, categoriesPromise, themesPromise, ageRangesPromise) ->
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
### PUBLIC SCOPE ###
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
## The events displayed on the page
|
|
|
|
$scope.events = []
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
## The currently displayed page number
|
|
|
|
$scope.page = 1
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
## List of categories for the events
|
|
|
|
$scope.categories = categoriesPromise
|
|
|
|
|
|
|
|
## List of events themes
|
|
|
|
$scope.themes = themesPromise
|
|
|
|
|
|
|
|
## List of age ranges
|
|
|
|
$scope.ageRanges = ageRangesPromise
|
|
|
|
|
2016-06-30 15:26:34 +02:00
|
|
|
## Hide or show the 'load more' button
|
|
|
|
$scope.noMoreResults = false
|
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
## Active filters for the events list
|
|
|
|
$scope.filters =
|
|
|
|
category_id: null
|
|
|
|
theme_id: null
|
|
|
|
age_range_id: null
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
2016-06-29 16:09:27 +02:00
|
|
|
# Adds a resultset of events to the bottom of the page, grouped by month
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
$scope.loadMoreEvents = ->
|
2016-06-29 16:09:27 +02:00
|
|
|
Event.query Object.assign({page: $scope.page}, $scope.filters), (data) ->
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.events = $scope.events.concat data
|
2016-06-30 12:41:47 +02:00
|
|
|
groupEvents($scope.events)
|
2016-06-29 16:09:27 +02:00
|
|
|
$scope.page += 1
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
if (!data[0] || data[0].nb_total_events <= $scope.events.length)
|
|
|
|
$scope.noMoreResults = true
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
# Callback to redirect the user to the specified event page
|
|
|
|
# @param event {{id:number}}
|
|
|
|
##
|
|
|
|
$scope.showEvent = (event) ->
|
|
|
|
$state.go('app.public.events_show', {id: event.id})
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
##
|
|
|
|
# Callback to refresh the events list according to the filters set
|
|
|
|
##
|
|
|
|
$scope.filterEvents = ->
|
|
|
|
# reinitialize results datasets
|
|
|
|
$scope.page = 1
|
|
|
|
$scope.eventsGroupByMonth = {}
|
|
|
|
$scope.events = []
|
|
|
|
$scope.monthOrder = []
|
2016-06-30 15:26:34 +02:00
|
|
|
$scope.noMoreResults = false
|
2016-06-29 16:09:27 +02:00
|
|
|
|
|
|
|
# run a search query
|
|
|
|
Event.query Object.assign({page: $scope.page}, $scope.filters), (data) ->
|
|
|
|
$scope.events = data
|
|
|
|
groupEvents(data)
|
|
|
|
$scope.page += 1
|
|
|
|
|
|
|
|
if (!data[0] || data[0].nb_total_events <= $scope.events.length)
|
|
|
|
$scope.noMoreResults = true
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-30 16:56:13 +02:00
|
|
|
##
|
|
|
|
# Test if the provided event occurs on a single day or on many days
|
|
|
|
# @param event {{start_date:Date, end_date:Date}} Event object as retreived from the API
|
|
|
|
# @return {boolean} false if the event occurs on many days
|
|
|
|
##
|
|
|
|
$scope.onSingleDay = (event) ->
|
|
|
|
moment(event.start_date).isSame(event.end_date, 'day')
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
### PRIVATE SCOPE ###
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
# Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
##
|
|
|
|
initialize = ->
|
2016-06-29 16:09:27 +02:00
|
|
|
$scope.filterEvents()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Group the provided events by month/year and concat them with existing results
|
|
|
|
# Then compute the ordered list of months for the complete resultset.
|
|
|
|
# Affect the resulting events groups in $scope.eventsGroupByMonth and the ordered month keys in $scope.monthOrder.
|
|
|
|
# @param {Array} Events retrived from the API
|
|
|
|
##
|
|
|
|
groupEvents = (events) ->
|
|
|
|
if events.length > 0
|
|
|
|
eventsGroupedByMonth = _.groupBy(events, (obj) ->
|
|
|
|
_.map ['month', 'year'], (key, value) -> obj[key]
|
|
|
|
)
|
|
|
|
$scope.eventsGroupByMonth = Object.assign($scope.eventsGroupByMonth, eventsGroupedByMonth)
|
|
|
|
|
|
|
|
monthsOrder = _.sortBy _.keys($scope.eventsGroupByMonth), (k)->
|
|
|
|
monthYearArray = k.split(',')
|
|
|
|
date = new Date()
|
|
|
|
date.setMonth(monthYearArray[0])
|
|
|
|
date.setYear(monthYearArray[1])
|
|
|
|
return -date.getTime()
|
|
|
|
|
|
|
|
$scope.monthOrder = monthsOrder
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
## !!! MUST BE CALLED AT THE END of the controller
|
|
|
|
initialize()
|
2015-05-05 03:10:25 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-25 15:36:52 +02:00
|
|
|
Application.Controllers.controller "ShowEventController", ["$scope", "$state", "$stateParams", "Event", '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'eventPromise', 'reducedAmountAlert', 'growl', '_t', 'Wallet', 'helpers', 'priceCategoriesPromise',
|
|
|
|
($scope, $state, $stateParams, Event, $uibModal, Member, Reservation, Price, CustomAsset, eventPromise, reducedAmountAlert, growl, _t, Wallet, helpers, priceCategoriesPromise) ->
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
### PUBLIC SCOPE ###
|
|
|
|
$scope.reducedAmountAlert = reducedAmountAlert.setting.value
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
## reservations for the currently shown event
|
|
|
|
$scope.reservations = []
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
## user to deal with
|
|
|
|
$scope.ctrl =
|
|
|
|
member: {}
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
## parameters for a new reservation
|
|
|
|
$scope.reserve =
|
2016-08-29 11:09:36 +02:00
|
|
|
nbPlaces:
|
|
|
|
normal: []
|
2016-03-23 18:39:41 +01:00
|
|
|
nbReservePlaces: 0
|
2016-08-29 11:09:36 +02:00
|
|
|
tickets: {}
|
2016-03-23 18:39:41 +01:00
|
|
|
toReserve: false
|
|
|
|
amountTotal : 0
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-08-11 12:34:55 +02:00
|
|
|
## Discount coupon to apply to the basket, if any
|
|
|
|
$scope.coupon =
|
|
|
|
applied: null
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-08-25 15:36:52 +02:00
|
|
|
## Get the details for the current event (event's id is recovered from the current URL)
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.event = eventPromise
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-08-25 15:36:52 +02:00
|
|
|
## List of price categories for the events
|
|
|
|
$scope.priceCategories = priceCategoriesPromise
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
# Callback to delete the provided event (admins only)
|
|
|
|
# @param event {$resource} angular's Event $resource
|
|
|
|
##
|
|
|
|
$scope.deleteEvent = (event) ->
|
|
|
|
event.$delete ->
|
|
|
|
$state.go('app.public.events_list')
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
2016-08-29 11:09:36 +02:00
|
|
|
# Callback to call when the number of tickets to book changes in the current booking
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
$scope.changeNbPlaces = ->
|
2016-08-29 11:09:36 +02:00
|
|
|
# compute the total remaing places
|
|
|
|
remain = $scope.event.nb_free_places - $scope.reserve.nbReservePlaces
|
|
|
|
for ticket of $scope.reserve.tickets
|
|
|
|
remain -= $scope.reserve.tickets[ticket]
|
|
|
|
|
|
|
|
# update the availables seats for full price tickets
|
|
|
|
fullPriceRemains = $scope.reserve.nbReservePlaces + remain
|
|
|
|
$scope.reserve.nbPlaces.normal = [0..fullPriceRemains]
|
|
|
|
|
|
|
|
# update the available seats for other prices tickets
|
|
|
|
for key of $scope.reserve.nbPlaces
|
|
|
|
if key != 'normal'
|
|
|
|
priceRemain = $scope.reserve.tickets[key] + remain
|
|
|
|
$scope.reserve.nbPlaces[key] = [0..priceRemain]
|
|
|
|
|
|
|
|
# recompute the total price
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.computeEventAmount()
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to reset the current reservation parameters
|
|
|
|
# @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
|
|
|
|
##
|
|
|
|
$scope.cancelReserve = (e)->
|
|
|
|
e.preventDefault()
|
|
|
|
resetEventReserve()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to allow the user to set the details for his reservation
|
|
|
|
##
|
|
|
|
$scope.reserveEvent = ->
|
|
|
|
if $scope.event.nb_total_places > 0
|
|
|
|
$scope.reserveSuccess = false
|
|
|
|
if !$scope.isAuthenticated()
|
|
|
|
$scope.login null, (user)->
|
|
|
|
$scope.reserve.toReserve = !$scope.reserve.toReserve
|
|
|
|
if user.role isnt 'admin'
|
|
|
|
$scope.ctrl.member = user
|
|
|
|
else
|
|
|
|
$scope.reserve.toReserve = !$scope.reserve.toReserve
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to deal with the reservations of the user selected in the dropdown list instead of the current user's
|
|
|
|
# reservations. (admins only)
|
|
|
|
##
|
|
|
|
$scope.updateMember = ->
|
|
|
|
resetEventReserve()
|
|
|
|
$scope.reserveSuccess = false
|
|
|
|
if $scope.ctrl.member
|
2016-06-14 09:57:39 +02:00
|
|
|
Member.get {id: $scope.ctrl.member.id}, (member) ->
|
|
|
|
$scope.ctrl.member = member
|
|
|
|
getReservations($scope.event.id, 'Event', $scope.ctrl.member.id)
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to trigger the payment process of the current reservation
|
|
|
|
##
|
|
|
|
$scope.payEvent = ->
|
|
|
|
|
|
|
|
# first, we check that a user was selected
|
|
|
|
if Object.keys($scope.ctrl.member).length > 0
|
|
|
|
reservation = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event)
|
|
|
|
|
2016-07-07 15:57:06 +02:00
|
|
|
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
|
|
|
|
amountToPay = helpers.getAmountToPay($scope.reserve.amountTotal, wallet.amount)
|
|
|
|
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
|
|
|
|
payByStripe(reservation)
|
|
|
|
else
|
|
|
|
if $scope.currentUser.role is 'admin' or amountToPay is 0
|
|
|
|
payOnSite(reservation)
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
|
|
|
# otherwise we alert, this error musn't occur when the current user is not admin
|
|
|
|
growl.error(_t('please_select_a_member_first'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to validate the booking of a free event
|
|
|
|
##
|
|
|
|
$scope.validReserveEvent = ->
|
|
|
|
reservation =
|
|
|
|
user_id: $scope.ctrl.member.id
|
|
|
|
reservable_id: $scope.event.id
|
|
|
|
reservable_type: 'Event'
|
|
|
|
slots_attributes: []
|
|
|
|
nb_reserve_places: $scope.reserve.nbReservePlaces
|
2016-08-25 18:41:33 +02:00
|
|
|
nb_reserve_reduced_places: $scope.reserve.nbReserveReducedPlaces #FIXME
|
2016-03-23 18:39:41 +01:00
|
|
|
reservation.slots_attributes.push
|
|
|
|
start_at: $scope.event.start_date
|
|
|
|
end_at: $scope.event.end_date
|
|
|
|
availability_id: $scope.event.availability.id
|
|
|
|
$scope.attempting = true
|
|
|
|
Reservation.save reservation: reservation, (reservation) ->
|
|
|
|
afterPayment(reservation)
|
|
|
|
$scope.attempting = false
|
|
|
|
, (response)->
|
|
|
|
$scope.alerts = []
|
|
|
|
$scope.alerts.push
|
|
|
|
msg: response.data.card[0]
|
|
|
|
type: 'danger'
|
|
|
|
$scope.attempting = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to alter an already booked reservation date. A modal window will be opened to allow the user to choose
|
|
|
|
# a new date for his reservation (if any available)
|
|
|
|
# @param reservation {{id:number, reservable_id:number, nb_reserve_places:number, nb_reserve_reduced_places:number}}
|
|
|
|
# @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
|
|
|
|
##
|
|
|
|
$scope.modifyReservation = (reservation, e)->
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
|
|
|
|
index = $scope.reservations.indexOf(reservation)
|
|
|
|
$uibModal.open
|
|
|
|
templateUrl: '<%= asset_path "events/modify_event_reservation_modal.html" %>'
|
|
|
|
resolve:
|
|
|
|
event: -> $scope.event
|
|
|
|
reservation: -> reservation
|
|
|
|
controller: ['$scope', '$uibModalInstance', 'event', 'reservation', 'Reservation', ($scope, $uibModalInstance, event, reservation, Reservation) ->
|
|
|
|
# we copy the controller's resolved parameters into the scope
|
|
|
|
$scope.event = event
|
|
|
|
$scope.reservation = angular.copy reservation
|
|
|
|
|
|
|
|
# set the reservable_id to the first available event
|
|
|
|
for e in event.recurrence_events
|
|
|
|
if e.nb_free_places > (reservation.nb_reserve_places + reservation.nb_reserve_reduced_places)
|
|
|
|
$scope.reservation.reservable_id = e.id
|
|
|
|
break
|
|
|
|
|
|
|
|
# Callback to validate the new reservation's date
|
|
|
|
$scope.ok = ->
|
|
|
|
eventToPlace = null
|
|
|
|
angular.forEach event.recurrence_events, (e)->
|
|
|
|
if e.id is parseInt($scope.reservation.reservable_id, 10)
|
|
|
|
eventToPlace = e
|
|
|
|
$scope.reservation.slots[0].start_at = eventToPlace.start_date
|
|
|
|
$scope.reservation.slots[0].end_at = eventToPlace.end_date
|
|
|
|
$scope.reservation.slots[0].availability_id = eventToPlace.availability_id
|
|
|
|
$scope.reservation.slots_attributes = $scope.reservation.slots
|
|
|
|
$scope.attempting = true
|
|
|
|
Reservation.update {id: reservation.id}, {reservation: $scope.reservation}, (reservation) ->
|
|
|
|
$uibModalInstance.close(reservation)
|
|
|
|
$scope.attempting = true
|
|
|
|
, (response)->
|
|
|
|
$scope.alerts = []
|
|
|
|
angular.forEach response, (v, k)->
|
|
|
|
angular.forEach v, (err)->
|
|
|
|
$scope.alerts.push({msg: k+': '+err, type: 'danger'})
|
|
|
|
$scope.attempting = false
|
|
|
|
|
|
|
|
# Callback to cancel the modification
|
|
|
|
$scope.cancel = ->
|
|
|
|
$uibModalInstance.dismiss('cancel')
|
|
|
|
]
|
|
|
|
.result['finally'](null).then (reservation)->
|
|
|
|
$scope.reservations.splice(index, 1)
|
|
|
|
$scope.event.nb_free_places = $scope.event.nb_free_places + reservation.nb_reserve_places + reservation.nb_reserve_reduced_places
|
|
|
|
angular.forEach $scope.event.recurrence_events, (e)->
|
|
|
|
if e.id is parseInt(reservation.reservable_id, 10)
|
|
|
|
e.nb_free_places = e.nb_free_places - reservation.nb_reserve_places - reservation.nb_reserve_reduced_places
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Checks if the provided reservation is able to be modified
|
|
|
|
# @param reservation {{nb_reserve_places:number, nb_reserve_reduced_places:number}}
|
|
|
|
##
|
|
|
|
$scope.reservationCanModify = (reservation)->
|
|
|
|
isAble = false
|
|
|
|
angular.forEach $scope.event.recurrence_events, (e)->
|
|
|
|
isAble = true if e.nb_free_places > (reservation.nb_reserve_places + reservation.nb_reserve_reduced_places)
|
|
|
|
isAble
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Compute the total amount for the current reservation according to the previously set parameters
|
|
|
|
# and assign the result in $scope.reserve.amountTotal
|
|
|
|
##
|
|
|
|
$scope.computeEventAmount = ->
|
|
|
|
# first we check that a user was selected
|
|
|
|
if Object.keys($scope.ctrl.member).length > 0
|
|
|
|
r = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event)
|
2016-08-11 12:34:55 +02:00
|
|
|
Price.compute mkRequestParams(r, $scope.coupon.applied), (res) ->
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.reserve.amountTotal = res.price
|
|
|
|
else
|
|
|
|
$scope.reserve.amountTotal = null
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-01 16:19:50 +02:00
|
|
|
##
|
|
|
|
# Return the URL allowing to share the current project on the Facebook social network
|
|
|
|
##
|
|
|
|
$scope.shareOnFacebook = ->
|
|
|
|
'https://www.facebook.com/share.php?u='+$state.href('app.public.events_show', {id: $scope.event.id}, {absolute: true}).replace('#', '%23')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Return the URL allowing to share the current project on the Twitter social network
|
|
|
|
##
|
|
|
|
$scope.shareOnTwitter = ->
|
|
|
|
'https://twitter.com/intent/tweet?url='+encodeURIComponent($state.href('app.public.events_show', {id: $scope.event.id}, {absolute: true}));
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-25 15:36:52 +02:00
|
|
|
##
|
|
|
|
# Return the textual description of the conditions applyable to the given price's category
|
|
|
|
# @param category_id {number} ID of the price's category
|
|
|
|
##
|
|
|
|
$scope.getPriceCategoryConditions = (category_id) ->
|
|
|
|
for cat in $scope.priceCategories
|
|
|
|
if cat.id == category_id
|
|
|
|
return cat.conditions
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = ->
|
2016-08-29 11:09:36 +02:00
|
|
|
# set the controlled user as the current user if the current user is not an admin
|
2016-03-23 18:39:41 +01:00
|
|
|
if $scope.currentUser
|
|
|
|
if $scope.currentUser.role isnt 'admin'
|
|
|
|
$scope.ctrl.member = $scope.currentUser
|
|
|
|
|
|
|
|
# initialize the "reserve" object with the event's data
|
2016-08-29 11:09:36 +02:00
|
|
|
$scope.reserve.nbPlaces.normal = [0..$scope.event.nb_free_places]
|
|
|
|
for price in $scope.event.prices
|
|
|
|
$scope.reserve.nbPlaces[price.id] = [0..$scope.event.nb_free_places]
|
|
|
|
$scope.reserve.tickets[price.id] = 0
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# if non-admin, get the current user's reservations into $scope.reservations
|
|
|
|
if $scope.currentUser
|
|
|
|
getReservations($scope.event.id, 'Event', $scope.currentUser.id)
|
|
|
|
|
2016-08-11 12:34:55 +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
|
|
|
|
$scope.computeEventAmount()
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Retrieve the reservations for the couple event / user
|
|
|
|
# @param reservable_id {number} the current event id
|
|
|
|
# @param reservable_type {string} 'Event'
|
|
|
|
# @param user_id {number} the user's id (current or managed)
|
|
|
|
##
|
|
|
|
getReservations = (reservable_id, reservable_type, user_id)->
|
|
|
|
Reservation.query(reservable_id: reservable_id, reservable_type: reservable_type, user_id: user_id).$promise.then (reservations)->
|
|
|
|
$scope.reservations = reservations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Create an hash map implementing the Reservation specs
|
|
|
|
# @param member {Object} User as retreived from the API: current user / selected user if current is admin
|
|
|
|
# @param reserve {Object} Reservation parameters (places...)
|
2016-06-28 11:42:20 +02:00
|
|
|
# @param event {Object} Current event
|
2016-03-23 18:39:41 +01:00
|
|
|
# @return {{user_id:Number, reservable_id:Number, reservable_type:String, slots_attributes:Array<Object>, nb_reserve_places:Number, nb_reserve_reduced_places:Number}}
|
|
|
|
##
|
|
|
|
mkReservation = (member, reserve, event) ->
|
|
|
|
reservation =
|
|
|
|
user_id: member.id
|
|
|
|
reservable_id: event.id
|
|
|
|
reservable_type: 'Event'
|
|
|
|
slots_attributes: []
|
|
|
|
nb_reserve_places: reserve.nbReservePlaces
|
2016-08-25 18:41:33 +02:00
|
|
|
tickets_attributes: []
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
reservation.slots_attributes.push
|
|
|
|
start_at: event.start_date
|
|
|
|
end_at: event.end_date
|
|
|
|
availability_id: event.availability.id
|
|
|
|
offered: event.offered || false
|
|
|
|
|
2016-08-25 18:41:33 +02:00
|
|
|
for price in event.prices
|
|
|
|
reservation.tickets_attributes.push
|
|
|
|
event_price_category_id: price.id
|
2016-08-29 11:09:36 +02:00
|
|
|
booked: reserve.tickets[price.id]
|
2016-08-25 18:41:33 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
reservation
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-11 12:34:55 +02:00
|
|
|
##
|
|
|
|
# Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object
|
|
|
|
# @param reservation {Object} as returned by mkReservation()
|
|
|
|
# @param coupon {Object} Coupon as returned from the API
|
|
|
|
# @return {{reservation:Object, coupon_code:string}}
|
|
|
|
##
|
|
|
|
mkRequestParams = (reservation, coupon) ->
|
|
|
|
params =
|
|
|
|
reservation: reservation
|
|
|
|
coupon_code: (coupon.code if coupon)
|
|
|
|
|
|
|
|
params
|
|
|
|
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
# Set the current reservation to the default values. This implies to reservation form to be hidden.
|
|
|
|
##
|
|
|
|
resetEventReserve = ->
|
|
|
|
if $scope.event
|
|
|
|
$scope.reserve =
|
|
|
|
nbPlaces: [0..$scope.event.nb_free_places]
|
2016-08-25 18:41:33 +02:00
|
|
|
nbReducedPlaces: [0..$scope.event.nb_free_places] #FIXME
|
2016-03-23 18:39:41 +01:00
|
|
|
nbReservePlaces: 0
|
|
|
|
nbReserveReducedPlaces: 0
|
|
|
|
toReserve: false
|
|
|
|
amountTotal : 0
|
|
|
|
$scope.event.offered = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Open a modal window which trigger the stripe payment process
|
|
|
|
# @param reservation {Object} to book
|
|
|
|
##
|
|
|
|
payByStripe = (reservation) ->
|
|
|
|
$uibModal.open
|
|
|
|
templateUrl: '<%= asset_path "stripe/payment_modal.html" %>'
|
|
|
|
size: 'md'
|
|
|
|
resolve:
|
|
|
|
reservation: ->
|
|
|
|
reservation
|
|
|
|
price: ->
|
2016-08-11 12:34:55 +02:00
|
|
|
Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise
|
2016-07-07 15:57:06 +02:00
|
|
|
wallet: ->
|
|
|
|
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
|
2016-03-23 18:39:41 +01:00
|
|
|
cgv: ->
|
|
|
|
CustomAsset.get({name: 'cgv-file'}).$promise
|
|
|
|
objectToPay: ->
|
|
|
|
eventToReserve: $scope.event
|
|
|
|
reserve: $scope.reserve
|
|
|
|
member: $scope.ctrl.member
|
2016-08-11 12:34:55 +02:00
|
|
|
coupon: ->
|
|
|
|
$scope.coupon.applied
|
|
|
|
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', 'wallet', 'helpers', '$locale', '$filter', 'coupon',
|
|
|
|
($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl, wallet, helpers, $locale, $filter, coupon) ->
|
2016-07-07 15:57:06 +02:00
|
|
|
# user wallet amount
|
|
|
|
$scope.walletAmount = wallet.amount
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
# Price
|
2016-07-07 15:57:06 +02:00
|
|
|
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# CGV
|
|
|
|
$scope.cgv = cgv.custom_asset
|
|
|
|
|
|
|
|
# Reservation
|
|
|
|
$scope.reservation = reservation
|
|
|
|
|
2016-07-07 15:57:06 +02:00
|
|
|
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
|
|
|
|
|
2016-07-19 16:24:50 +02:00
|
|
|
$scope.numberFilter = $filter('number')
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
# Callback for the stripe payment authorization
|
|
|
|
$scope.payment = (status, response) ->
|
|
|
|
if response.error
|
|
|
|
growl.error(response.error.message)
|
|
|
|
else
|
|
|
|
$scope.attempting = true
|
|
|
|
$scope.reservation.card_token = response.id
|
2016-08-11 12:34:55 +02:00
|
|
|
Reservation.save mkRequestParams($scope.reservation, coupon), (reservation) ->
|
2016-03-23 18:39:41 +01:00
|
|
|
$uibModalInstance.close(reservation)
|
|
|
|
, (response)->
|
|
|
|
$scope.alerts = []
|
|
|
|
$scope.alerts.push
|
|
|
|
msg: response.data.card[0]
|
|
|
|
type: 'danger'
|
|
|
|
$scope.attempting = false
|
|
|
|
]
|
|
|
|
.result['finally'](null).then (reservation)->
|
|
|
|
afterPayment(reservation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Open a modal window which trigger the local payment process
|
|
|
|
# @param reservation {Object} to book
|
|
|
|
##
|
|
|
|
payOnSite = (reservation) ->
|
|
|
|
$uibModal.open
|
|
|
|
templateUrl: '<%= asset_path "shared/valid_reservation_modal.html" %>'
|
|
|
|
size: 'sm'
|
|
|
|
resolve:
|
|
|
|
reservation: ->
|
|
|
|
reservation
|
|
|
|
price: ->
|
2016-08-11 12:34:55 +02:00
|
|
|
Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise
|
2016-07-07 15:57:06 +02:00
|
|
|
wallet: ->
|
|
|
|
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
|
2016-08-11 12:34:55 +02:00
|
|
|
coupon: ->
|
|
|
|
$scope.coupon.applied
|
|
|
|
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'wallet', '$locale', 'helpers', '$filter', 'coupon',
|
|
|
|
($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, wallet, $locale, helpers, $filter, coupon) ->
|
2016-07-07 15:57:06 +02:00
|
|
|
# user wallet amount
|
|
|
|
$scope.walletAmount = wallet.amount
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
# Price
|
2016-07-07 15:57:06 +02:00
|
|
|
$scope.price = price.price
|
|
|
|
|
|
|
|
# price to pay
|
|
|
|
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# Reservation
|
|
|
|
$scope.reservation = reservation
|
|
|
|
|
2016-07-07 15:57:06 +02:00
|
|
|
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
|
|
|
|
|
2016-07-19 16:24:50 +02:00
|
|
|
$scope.numberFilter = $filter('number')
|
|
|
|
|
2016-03-23 18:39:41 +01: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-03-23 18:39:41 +01:00
|
|
|
else
|
2016-07-21 15:32:49 +02:00
|
|
|
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-21 15:32:49 +02:00
|
|
|
else
|
|
|
|
$scope.validButtonName = _t('confirm')
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# Callback to validate the payment
|
|
|
|
$scope.ok = ->
|
|
|
|
$scope.attempting = true
|
2016-08-11 12:34:55 +02:00
|
|
|
Reservation.save mkRequestParams($scope.reservation, coupon), (reservation) ->
|
2016-03-23 18:39:41 +01:00
|
|
|
$uibModalInstance.close(reservation)
|
|
|
|
$scope.attempting = true
|
|
|
|
, (response)->
|
|
|
|
$scope.alerts = []
|
|
|
|
angular.forEach response, (v, k)->
|
|
|
|
angular.forEach v, (err)->
|
|
|
|
$scope.alerts.push
|
|
|
|
msg: k+': '+err
|
|
|
|
type: 'danger'
|
|
|
|
$scope.attempting = false
|
|
|
|
|
|
|
|
# Callback to cancel the payment
|
|
|
|
$scope.cancel = ->
|
|
|
|
$uibModalInstance.dismiss('cancel')
|
|
|
|
]
|
|
|
|
.result['finally'](null).then (reservation)->
|
|
|
|
afterPayment(reservation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# What to do after the payment was successful
|
|
|
|
# @param resveration {Object} booked reservation
|
|
|
|
##
|
|
|
|
afterPayment = (reservation)->
|
|
|
|
$scope.event.nb_free_places = $scope.event.nb_free_places - reservation.nb_reserve_places - reservation.nb_reserve_reduced_places
|
|
|
|
resetEventReserve()
|
|
|
|
$scope.reserveSuccess = true
|
|
|
|
$scope.reservations.push reservation
|
|
|
|
if $scope.currentUser.role == 'admin'
|
|
|
|
$scope.ctrl.member = null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## !!! MUST BE CALLED AT THE END of the controller
|
|
|
|
initialize()
|
|
|
|
]
|