diff --git a/app/assets/javascripts/controllers/admin/pricing.coffee.erb b/app/assets/javascripts/controllers/admin/pricing.coffee.erb index 5bc1a82ec..b5c8961d4 100644 --- a/app/assets/javascripts/controllers/admin/pricing.coffee.erb +++ b/app/assets/javascripts/controllers/admin/pricing.coffee.erb @@ -350,7 +350,10 @@ Application.Controllers.controller "EditPricingController", ["$scope", "$state", , (error) -> console.error('[EditPricingController::deleteCoupon] Error: '+error.statusText) if error.statusText - growl.error(_t('unable_to_delete_the_specified_coupon_an_error_occurred')) + if error.status == 422 + growl.error(_t('unable_to_delete_the_specified_coupon_already_in_use')) + else + growl.error(_t('unable_to_delete_the_specified_coupon_an_unexpected_error_occurred')) diff --git a/app/controllers/api/coupons_controller.rb b/app/controllers/api/coupons_controller.rb index 728410814..430b7bc7f 100644 --- a/app/controllers/api/coupons_controller.rb +++ b/app/controllers/api/coupons_controller.rb @@ -30,8 +30,11 @@ class API::CouponsController < API::ApiController def destroy authorize Coupon - @coupon.destroy - head :no_content + if @coupon.safe_destroy + head :no_content + else + head :unprocessable_entity + end end private diff --git a/app/models/coupon.rb b/app/models/coupon.rb index cafb5d778..7705a4d26 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -7,4 +7,12 @@ class Coupon < ActiveRecord::Base validates :percent_off, presence: true validates :percent_off, :inclusion => 0..100 + def safe_destroy + if self.invoices.size == 0 + destroy + else + false + end + end + end diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index 079392f89..0fdaafbdd 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -155,7 +155,8 @@ en: active: "Active" do_you_really_want_to_delete_this_coupon: "Do you really want to delete this coupon?" coupon_was_successfully_deleted: "Coupon was successfully deleted." - unable_to_delete_the_specified_coupon_an_error_occurred: "Unable to delete the specified coupon, an error occurred." + unable_to_delete_the_specified_coupon_already_in_use: "Unable to delete the specified coupon: it is already used with some invoices." + unable_to_delete_the_specified_coupon_an_unexpected_error_occurred: "Unable to delete the specified coupon: an unexpected error occurred." coupons_new: # ajouter un code promotionnel diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index b80423eeb..14b5cc7a3 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -155,7 +155,8 @@ fr: active: "Actif" do_you_really_want_to_delete_this_coupon: "Êtes-vous sûr(e) de vouloir supprimer ce code promotionnel ?" 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." + unable_to_delete_the_specified_coupon_already_in_use: "Impossible de supprimer le code promotionnel : il est utilisé dans des factures." + unable_to_delete_the_specified_coupon_an_unexpected_error_occurred: "Impossible de supprimer le code promotionnel : une erreur inattendue s'est produite." coupons_new: # ajouter un code promotionnel