2016-08-08 14:42:17 +02:00
|
|
|
### COMMON CODE ###
|
|
|
|
|
|
|
|
# The validity per user defines how many time a user may ba able to use the same coupon
|
|
|
|
# Here are the various options for this parameter
|
|
|
|
userValidities = ['once', 'forever']
|
|
|
|
|
2016-08-04 18:13:19 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
# Controller used in the coupon creation page
|
|
|
|
##
|
2016-11-23 14:27:11 +01:00
|
|
|
Application.Controllers.controller "NewCouponController", ["$scope", "$state", 'Coupon', 'growl', '_t'
|
|
|
|
, ($scope, $state, Coupon, growl, _t) ->
|
2016-08-04 18:13:19 +02:00
|
|
|
|
|
|
|
## Values for the coupon currently created
|
|
|
|
$scope.coupon =
|
|
|
|
active: true
|
2016-11-23 12:43:42 +01:00
|
|
|
type: 'percent_off'
|
|
|
|
|
2016-08-08 14:42:17 +02:00
|
|
|
## Options for the validity per user
|
|
|
|
$scope.validities = userValidities
|
|
|
|
|
2016-08-04 18:13:19 +02:00
|
|
|
## Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection)
|
|
|
|
$scope.datePicker =
|
|
|
|
format: Fablab.uibDateFormat
|
|
|
|
opened: false # default: datePicker is not shown
|
2016-08-08 12:08:09 +02:00
|
|
|
minDate: moment().toDate()
|
2016-08-04 18:13:19 +02:00
|
|
|
options:
|
|
|
|
startingDay: Fablab.weekStartingDay
|
|
|
|
|
2016-08-08 12:08:09 +02:00
|
|
|
|
|
|
|
|
2016-08-04 18:13:19 +02:00
|
|
|
##
|
2016-08-08 12:08:09 +02:00
|
|
|
# Shows/hides the validity limit datepicker
|
2016-08-04 18:13:19 +02:00
|
|
|
# @param $event {Object} jQuery event object
|
|
|
|
##
|
2016-08-08 12:08:09 +02:00
|
|
|
$scope.toggleDatePicker = ($event) ->
|
2016-08-04 18:13:19 +02:00
|
|
|
$event.preventDefault()
|
|
|
|
$event.stopPropagation()
|
2016-08-08 12:08:09 +02:00
|
|
|
$scope.datePicker.opened = !$scope.datePicker.opened
|
|
|
|
|
|
|
|
|
2016-08-04 18:13:19 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to save the new coupon in $scope.coupon and redirect the user to the listing page
|
|
|
|
##
|
|
|
|
$scope.saveCoupon = ->
|
|
|
|
Coupon.save coupon: $scope.coupon, (coupon) ->
|
|
|
|
$state.go('app.admin.pricing')
|
|
|
|
, (err)->
|
2016-08-08 14:42:17 +02:00
|
|
|
growl.error(_t('unable_to_create_the_coupon_check_code_already_used'))
|
2016-08-04 18:13:19 +02:00
|
|
|
console.error(err)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-08-08 12:08:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-04 18:13:19 +02:00
|
|
|
##
|
|
|
|
# Controller used in the coupon edition page
|
|
|
|
##
|
2016-12-13 12:01:34 +01:00
|
|
|
Application.Controllers.controller "EditCouponController", ["$scope", "$state", 'Coupon', 'couponPromise', '_t', 'growl'
|
|
|
|
, ($scope, $state, Coupon, couponPromise, _t, growl) ->
|
2016-08-04 18:13:19 +02:00
|
|
|
|
2016-08-08 12:08:09 +02:00
|
|
|
### PUBLIC SCOPE ###
|
2016-08-04 18:13:19 +02:00
|
|
|
|
|
|
|
|
2016-08-08 15:21:33 +02:00
|
|
|
## Used in the form to freeze unmodifiable fields
|
|
|
|
$scope.mode = 'EDIT'
|
2016-08-08 12:08:09 +02:00
|
|
|
|
|
|
|
## Coupon to edit
|
|
|
|
$scope.coupon = couponPromise
|
|
|
|
|
2016-08-08 14:42:17 +02:00
|
|
|
## Options for the validity per user
|
|
|
|
$scope.validities = userValidities
|
|
|
|
|
2016-08-08 12:08:09 +02:00
|
|
|
## Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection)
|
|
|
|
$scope.datePicker =
|
|
|
|
format: Fablab.uibDateFormat
|
|
|
|
opened: false # default: datePicker is not shown
|
|
|
|
minDate: moment().toDate()
|
|
|
|
options:
|
|
|
|
startingDay: Fablab.weekStartingDay
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Shows/hides the validity limit datepicker
|
|
|
|
# @param $event {Object} jQuery event object
|
|
|
|
##
|
|
|
|
$scope.toggleDatePicker = ($event) ->
|
|
|
|
$event.preventDefault()
|
|
|
|
$event.stopPropagation()
|
|
|
|
$scope.datePicker.opened = !$scope.datePicker.opened
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to save the coupon's changes to the API
|
|
|
|
##
|
|
|
|
$scope.updateCoupon = ->
|
|
|
|
Coupon.update {id: $scope.coupon.id}, coupon: $scope.coupon, (coupon) ->
|
|
|
|
$state.go('app.admin.pricing')
|
|
|
|
, (err)->
|
|
|
|
growl.error(_t('unable_to_update_the_coupon_an_error_occurred'))
|
|
|
|
console.error(err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### PRIVATE SCOPE ###
|
|
|
|
|
|
|
|
##
|
|
|
|
# Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
##
|
|
|
|
initialize = ->
|
|
|
|
# parse the date if any
|
|
|
|
if (couponPromise.valid_until)
|
|
|
|
$scope.coupon.valid_until = moment(couponPromise.valid_until).toDate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## !!! MUST BE CALLED AT THE END of the controller
|
|
|
|
initialize()
|
2016-08-04 18:13:19 +02:00
|
|
|
]
|