promotions_controller.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class PromotionsController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :promotions_path
  5. add_breadcrumb "Nueva " + I18n.t("breadcrumbs." + controller_name).singularize, :new_promotion_path, only: :new
  6. before_action :set_promotion, only: [:show, :destroy]
  7. # GET /promotions
  8. # GET /promotions.json
  9. def index
  10. @promotions = Promotion.all.order('id desc')
  11. @promotions.map(&:verify_promotion)
  12. end
  13. # GET /promotions/1
  14. # GET /promotions/1.json
  15. def show; end
  16. # GET /promotions/new
  17. def new
  18. @promotion = Promotion.new
  19. @promotion.percent = nil
  20. end
  21. # POST /promotions
  22. # POST /promotions.json
  23. def create
  24. @promotion = Promotion.new(promotion_params)
  25. @promotion.subcategory_id = nil if !@promotion.nil? && @promotion.subcategory_id.zero?
  26. source =
  27. if @promotion.category_id.present?
  28. "a la #{@promotion.subcategory_id.present? ? 'sublínea' : 'línea' } #{@promotion.subcategory_id.present? ? Category.find(@promotion.subcategory_id).category : @promotion.category.category}"
  29. elsif @promotion.product.present?
  30. "al producto #{@promotion.product.name}"
  31. end
  32. respond_to do |format|
  33. if @promotion.save
  34. message = "Promoción de descuento del #{@promotion.percent}% agregado #{source} del #{@promotion.start_date.strftime("%d/%m/%Y")} al #{@promotion.end_date.strftime("%d/%m/%Y")}"
  35. @promotion.audit_comment = message
  36. format.html { redirect_to promotions_path, notice: message }
  37. format.json { render :show, status: :created, location: @promotion }
  38. else
  39. @filter = params[:filter]
  40. format.html { render :new }
  41. format.json { render json: @promotion.errors, status: :unprocessable_entity }
  42. end
  43. end
  44. end
  45. # DELETE /promotions/1
  46. # DELETE /promotions/1.json
  47. def destroy
  48. message = "Descuento eliminado correctamente"
  49. @promotion.audit_comment = message
  50. @promotion.destroy
  51. respond_to do |format|
  52. format.html { redirect_to promotions_url, notice: message }
  53. format.json { head :no_content }
  54. end
  55. end
  56. private
  57. # Use callbacks to share common setup or constraints between actions.
  58. def set_promotion
  59. @promotion = Promotion.find(params[:id])
  60. end
  61. # Never trust parameters from the scary internet, only allow the white list through.
  62. def promotion_params
  63. params.require(:promotion).permit(:start_date, :end_date, :percent, :product_id, :category_id, :subcategory_id, :status)
  64. end
  65. end