product_wastes_controller.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. class ProductWastesController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. before_action :set_product_waste, only: [:show, :edit, :update, :destroy]
  5. ##--- Breadcrum_rails
  6. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :product_wastes_path
  7. add_breadcrumb "Nueva merma de productos", :new_product_waste_path, only: :new
  8. before_action :set_pos_config, only: [:show, :edit, :update, :destroy]
  9. # GET /product_wastes
  10. # GET /product_wastes.json
  11. def index
  12. if current_user.usertype == 'A'
  13. @product_wastes = ProductWaste.includes(:product, :user, :pointsale, :warehouse).activos
  14. elsif current_user.usertype == 'G' || current_user.usertype == 'C'
  15. @product_wastes = ProductWaste.includes(:product, :user).where(pointsale_id: current_user.pointsale_id).activos
  16. elsif current_user.usertype == 'S'
  17. @product_wastes = ProductWaste.includes(:product, :user).where(warehouse_id: current_user.warehouse_id).activos
  18. end
  19. end
  20. # GET /product_wastes/new
  21. def new; end
  22. # POST /product_wastes
  23. # POST /product_wastes.json
  24. def create
  25. respond_to do |format|
  26. @product_waste = ProductWaste.new(product_waste_params)
  27. @product_waste.user_id = current_user.id
  28. @product_waste.status = :active
  29. if current_user.usertype == 'S'
  30. @product_waste.warehouse_id = current_user.warehouse_id
  31. @stock = WarehouseStock.find_by(warehouse_id: @product_waste.warehouse_id, product_id: @product_waste.product_id)
  32. else
  33. @product_waste.pointsale_id = current_user.pointsale_id
  34. @stock = AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
  35. end
  36. if @stock.stock >= @product_waste.quantity
  37. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} creada"
  38. if @product_waste.save
  39. @stock.stock -= @product_waste.quantity
  40. @stock.save
  41. format.js
  42. else
  43. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  44. end
  45. else
  46. @product_waste.errors.add(:base, "Stock insuficiente, solo se cuenta con #{@stock.stock} unidades del producto #{@product_waste.product.name}")
  47. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  48. end
  49. end
  50. end
  51. # PATCH/PUT /product_wastes/1
  52. # PATCH/PUT /product_wastes/1.json
  53. def update
  54. respond_to do |format|
  55. if @product_waste.update(product_waste_params)
  56. format.html { redirect_to @product_waste, notice: 'Product waste was successfully updated.' }
  57. format.json { render :show, status: :ok, location: @product_waste }
  58. else
  59. format.html { render :edit }
  60. format.json { render json: @product_waste.errors, status: :unprocessable_entity }
  61. end
  62. end
  63. end
  64. # DELETE /product_wastes/1
  65. # DELETE /product_wastes/1.json
  66. def destroy
  67. respond_to do |format|
  68. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} eliminada."
  69. if @product_waste.update_attributes(status: :inactive)
  70. @stock = current_user.usertype == "S" ? WarehouseStock.find_by(warehouse_id: @product_waste.warehouse_id, product_id: @product_waste.product_id) : AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
  71. @stock.stock = @stock.stock + @product_waste.quantity
  72. @stock.save
  73. format.html { redirect_to product_wastes_url, warning: 'Merma de producto eliminada.' }
  74. format.json { head :ok }
  75. else
  76. format.json { render json: @product_waste.errors, status: :unprocessable_entity }
  77. end
  78. end
  79. end
  80. private
  81. # Use callbacks to share common setup or constraints between actions.
  82. def set_product_waste
  83. @product_waste = ProductWaste.find(params[:id])
  84. end
  85. # Never trust parameters from the scary internet, only allow the white list through.
  86. def product_waste_params
  87. params.require(:product_waste).permit(:warehouse_id, :pointsale_id, :product_id, :quantity, :reason, :status)
  88. end
  89. end