promotions_controller.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. source =
  26. if @promotion.category_id.present? || @promotion.subcategory_id.to_i != 0
  27. "a la #{@promotion.subcategory_id.to_i != 0 ? 'sublínea' : 'línea' } #{@promotion.subcategory_id.to_i != 0 ? Category.find(@promotion.subcategory_id).category : @promotion.category.category}"
  28. elsif @promotion.product.present?
  29. "al producto #{@promotion.product.name}"
  30. end
  31. respond_to do |format|
  32. if @promotion.save
  33. 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")}"
  34. @promotion.audit_comment = message
  35. format.html { redirect_to promotions_path, notice: message }
  36. format.json { render :show, status: :created, location: @promotion }
  37. else
  38. @filter = params[:filter]
  39. format.html { render :new }
  40. format.json { render json: @promotion.errors, status: :unprocessable_entity }
  41. end
  42. end
  43. end
  44. # DELETE /promotions/1
  45. # DELETE /promotions/1.json
  46. def destroy
  47. message = "Descuento eliminado correctamente"
  48. @promotion.audit_comment = message
  49. @promotion.destroy
  50. respond_to do |format|
  51. format.html { redirect_to promotions_url, notice: message }
  52. format.json { head :no_content }
  53. end
  54. end
  55. private
  56. # Use callbacks to share common setup or constraints between actions.
  57. def set_promotion
  58. @promotion = Promotion.find(params[:id])
  59. end
  60. # Never trust parameters from the scary internet, only allow the white list through.
  61. def promotion_params
  62. params.require(:promotion).permit(:start_date, :end_date, :percent, :product_id, :category_id, :subcategory_id, :status)
  63. end
  64. end