From 7c434db09adf6823b3d5db86ee1de05811a04a8a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 4 Aug 2016 18:13:19 +0200 Subject: [PATCH] interface to create new coupons --- .../controllers/admin/coupons.coffee | 54 ++++++++++++++ .../controllers/admin/pricing.coffee.erb | 4 +- app/assets/javascripts/router.coffee.erb | 20 +++--- .../templates/admin/coupons/_coupon.html.erb | 0 .../templates/admin/coupons/_form.html.erb | 72 +++++++++++++++++++ .../templates/admin/coupons/edit.html.erb | 41 ++++++++++- .../templates/admin/coupons/new.html.erb | 34 ++++++++- .../templates/admin/pricing/coupons.html.erb | 10 +-- app/models/coupon.rb | 3 +- app/views/api/coupons/_coupon.json.jbuilder | 2 +- config/locales/app.admin.fr.yml | 5 ++ config/locales/app.shared.fr.yml | 12 ++++ 12 files changed, 236 insertions(+), 21 deletions(-) create mode 100644 app/assets/javascripts/controllers/admin/coupons.coffee delete mode 100644 app/assets/templates/admin/coupons/_coupon.html.erb create mode 100644 app/assets/templates/admin/coupons/_form.html.erb diff --git a/app/assets/javascripts/controllers/admin/coupons.coffee b/app/assets/javascripts/controllers/admin/coupons.coffee new file mode 100644 index 000000000..ff3f5c0af --- /dev/null +++ b/app/assets/javascripts/controllers/admin/coupons.coffee @@ -0,0 +1,54 @@ + +## +# Controller used in the coupon creation page +## +Application.Controllers.controller "NewCouponController", ["$scope", "$state",'Coupon', 'growl', '_t' +, ($scope, $state, Coupon, growl, _t) -> + + + + ### PUBLIC SCOPE ### + + ## Values for the coupon currently created + $scope.coupon = + active: true + + ## Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection) + $scope.datePicker = + format: Fablab.uibDateFormat + opened: false # default: datePicker is not shown + options: + startingDay: Fablab.weekStartingDay + + ## + # Shows the validity limit datepicker + # @param $event {Object} jQuery event object + ## + $scope.openDatePicker = ($event) -> + $event.preventDefault() + $event.stopPropagation() + $scope.datePicker.opened = true + + ## + # 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)-> + growl.error(_t('unable_to_create_the_coupon_an_error_occurred')) + console.error(err) +] + + +## +# Controller used in the coupon edition page +## +Application.Controllers.controller "EditCouponController", ["$scope", 'Coupon', '_t' +, ($scope, Coupon, _t) -> + + + + ### PUBLIC SCOPE ### + $scope.test = 'edit' +] \ No newline at end of file diff --git a/app/assets/javascripts/controllers/admin/pricing.coffee.erb b/app/assets/javascripts/controllers/admin/pricing.coffee.erb index 151ca90c1..503b7e896 100644 --- a/app/assets/javascripts/controllers/admin/pricing.coffee.erb +++ b/app/assets/javascripts/controllers/admin/pricing.coffee.erb @@ -319,9 +319,9 @@ Application.Controllers.controller "EditPricingController", ["$scope", "$state", $scope.getCouponStatus = (coupon) -> unless coupon.active return _t('disabled') - unless moment(coupon.valid).isSameOrBefore() + if coupon.valid_until and moment(coupon.valid_until).isAfter() return _t('expired') - unless coupon.usages <= coupon.max_usages + if coupon.max_usages and coupon.usages <= coupon.max_usages return _t('sold_out') return _t('active') diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 247b3899a..82fff8d5b 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -790,26 +790,26 @@ angular.module('application.router', ['ui.router']). ] # coupons - .state 'app.admin.coupons.new', + .state 'app.admin.coupons_new', url: '/admin/coupons/new' views: 'main@': templateUrl: '<%= asset_path "admin/coupons/new.html" %>' controller: 'NewCouponController' - resolve: - translations: [ 'Translations', (Translations) -> - Translations.query(['app.admin.coupons.new', 'app.shared.coupon']).$promise - ] - .state 'app.admin.coupons.edit', + resolve: + translations: [ 'Translations', (Translations) -> + Translations.query(['app.admin.coupons_new', 'app.shared.coupon']).$promise + ] + .state 'app.admin.coupons_edit', url: '/admin/coupons/:id/edit' views: 'main@': templateUrl: '<%= asset_path "admin/coupons/edit.html" %>' controller: 'EditCouponController' - resolve: - translations: [ 'Translations', (Translations) -> - Translations.query(['app.admin.coupons.edit', 'app.shared.coupon']).$promise - ] + resolve: + translations: [ 'Translations', (Translations) -> + Translations.query(['app.admin.coupons_edit', 'app.shared.coupon']).$promise + ] diff --git a/app/assets/templates/admin/coupons/_coupon.html.erb b/app/assets/templates/admin/coupons/_coupon.html.erb deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/assets/templates/admin/coupons/_form.html.erb b/app/assets/templates/admin/coupons/_form.html.erb new file mode 100644 index 000000000..156efd241 --- /dev/null +++ b/app/assets/templates/admin/coupons/_form.html.erb @@ -0,0 +1,72 @@ +
+ + + {{ 'name_is_required' }} +
+ +
+ + + {{ 'code_is_required' }} + {{ 'code_must_be_composed_of_capital_letters_and_or_digits' }} +
+ +
+ +
+ + +
+ {{ 'percent_off_is_required' }} + {{ 'percentage_must_be_between_0_and_100' }} +
+ +
+ + +
+ +
+ + + {{ 'max_usages_must_be_equal_or_greater_than_0' }} +
+ +
+ + + +
diff --git a/app/assets/templates/admin/coupons/edit.html.erb b/app/assets/templates/admin/coupons/edit.html.erb index 324e1dfe7..498e01dab 100644 --- a/app/assets/templates/admin/coupons/edit.html.erb +++ b/app/assets/templates/admin/coupons/edit.html.erb @@ -1 +1,40 @@ - \ No newline at end of file +
+
+
+
+ +
+
+
+
+

{{ 'coupon' | translate }} {{ coupon.name }}

+
+
+ +
+
+ {{ 'cancel' }} +
+ +
+ + +
+
+ +
+
+ +
+
+ + + + +
+
+ +
+
diff --git a/app/assets/templates/admin/coupons/new.html.erb b/app/assets/templates/admin/coupons/new.html.erb index 324e1dfe7..16bf475cc 100644 --- a/app/assets/templates/admin/coupons/new.html.erb +++ b/app/assets/templates/admin/coupons/new.html.erb @@ -1 +1,33 @@ - \ No newline at end of file +
+
+
+
+ +
+
+
+
+

{{ 'add_a_coupon' }}

+
+
+ +
+
+ +
+
+ +
+
+ + + + + +
+
+ +
+
diff --git a/app/assets/templates/admin/pricing/coupons.html.erb b/app/assets/templates/admin/pricing/coupons.html.erb index 3c177b303..c81773d78 100644 --- a/app/assets/templates/admin/pricing/coupons.html.erb +++ b/app/assets/templates/admin/pricing/coupons.html.erb @@ -1,12 +1,12 @@

{{ 'list_of_the_coupons' }}

- + - - - + + + @@ -16,7 +16,7 @@ diff --git a/app/models/coupon.rb b/app/models/coupon.rb index 0e3dfdd70..cafb5d778 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -1,8 +1,9 @@ class Coupon < ActiveRecord::Base has_many :invoices + validates :name, presence: true validates :code, presence: true - validates :code, format: { with: /[A-Z0-9]+/ ,message: 'only caps letters and numbers'} + validates :code, format: { with: /\A[A-Z0-9]+\z/ ,message: 'only caps letters and numbers'} validates :percent_off, presence: true validates :percent_off, :inclusion => 0..100 diff --git a/app/views/api/coupons/_coupon.json.jbuilder b/app/views/api/coupons/_coupon.json.jbuilder index b07ac132b..2289953b5 100644 --- a/app/views/api/coupons/_coupon.json.jbuilder +++ b/app/views/api/coupons/_coupon.json.jbuilder @@ -1,2 +1,2 @@ json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :max_usages, :active, :created_at -json.usages @coupon.invoices.count \ No newline at end of file +json.usages coupon.invoices.count \ No newline at end of file diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index 12849c404..ff1465542 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -157,6 +157,11 @@ fr: coupon_was_successfully_deleted: "Le code promotionnel a bien été supprimé." unable_to_delete_the_specified_coupon_an_error_occurred: "Impossible de supprimer le code promotionnel, une erreur s'est produite." + coupons_new: + # ajouter un code promotionnel + add_a_coupon: "Ajouter un code promotionnel" + unable_to_create_the_coupon_an_error_occurred: "Impossible de créer le code promotionnel : une erreur est survenue." + plans: new: # ajouter une formule d'abonnement sur la plate-forme diff --git a/config/locales/app.shared.fr.yml b/config/locales/app.shared.fr.yml index 1e1958aa6..6c0ccc02a 100644 --- a/config/locales/app.shared.fr.yml +++ b/config/locales/app.shared.fr.yml @@ -331,3 +331,15 @@ fr: debit_reservation_training: "Payer un reservation de formation" debit_reservation_machine: "Payer un reservation de machine" debit_reservation_event: "Payer un reservation d'évenement" + + coupon: + code: "Code" + code_is_required: "Le code est requis." + code_must_be_composed_of_capital_letters_and_or_digits: "Le code doit être composé de lettres majuscules et/ou de chiffres." + percent_off: "Pourcentage de réduction" + percent_off_is_required: "Le pourcentage de réduction est requis." + percentage_must_be_between_0_and_100: "Le pourcentage doit être compris entre 0 et 100." + valid_until: "Valable jusqu'au (inclus)" + max_usages: "Nombre maximum d'utilisations autorisées" + max_usages_must_be_equal_or_greater_than_0: "Le nombre d'utilisations maximum doit être supérieur ou égal à 0." + enabled: "Activé" \ No newline at end of file
{{ 'name' | translate }}{{ 'percentage_off' | translate }}{{ 'status' | translate }}{{ 'name' }}{{ 'percentage_off' }}{{ 'status' }}
{{coupon.percent_off}} % {{getCouponStatus(coupon)}} - +