product_wastes_controller.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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'
  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
  22. end
  23. # POST /product_wastes
  24. # POST /product_wastes.json
  25. def create
  26. respond_to do |format|
  27. @product_waste = ProductWaste.new(product_waste_params)
  28. @product_waste.user_id = current_user.id
  29. @product_waste.status = :active
  30. if current_user.usertype == 'S'
  31. @product_waste.warehouse_id = current_user.warehouse_id
  32. @stock = WarehouseStock.find_by(:warehouse_id => @product_waste.warehouse_id,
  33. :product_id => @product_waste.product_id)
  34. else
  35. @product_waste.pointsale_id = current_user.pointsale_id
  36. @stock = AvailableProduct.find_by(:pointsale_id => @product_waste.pointsale_id,
  37. :product_id => @product_waste.product_id)
  38. end
  39. if @stock.stock >= @product_waste.quantity
  40. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} creada"
  41. if @product_waste.save
  42. @stock.stock -= @product_waste.quantity
  43. @stock.save
  44. format.js
  45. else
  46. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  47. end
  48. else
  49. @product_waste.errors.add(:base, "Stock insuficiente, solo se cuenta con #{@stock.stock} unidades del producto #{@product_waste.product.name}")
  50. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  51. end
  52. end
  53. end
  54. # PATCH/PUT /product_wastes/1
  55. # PATCH/PUT /product_wastes/1.json
  56. def update
  57. respond_to do |format|
  58. if @product_waste.update(product_waste_params)
  59. format.html { redirect_to @product_waste, notice: 'Product waste was successfully updated.' }
  60. format.json { render :show, status: :ok, location: @product_waste }
  61. else
  62. format.html { render :edit }
  63. format.json { render json: @product_waste.errors, status: :unprocessable_entity }
  64. end
  65. end
  66. end
  67. # DELETE /product_wastes/1
  68. # DELETE /product_wastes/1.json
  69. def destroy
  70. respond_to do |format|
  71. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} eliminada."
  72. if @product_waste.update_attributes(:status => :inactive)
  73. if current_user.usertype == "S"
  74. @stock = WarehouseStock.find_by(:warehouse_id => @product_waste.warehouse_id,
  75. :product_id => @product_waste.product_id)
  76. else
  77. @stock = AvailableProduct.find_by(:pointsale_id => @product_waste.pointsale_id,
  78. :product_id => @product_waste.product_id)
  79. end
  80. @stock.stock = @stock.stock + @product_waste.quantity
  81. @stock.save
  82. format.html { redirect_to product_wastes_url, warning: 'Merma de producto eliminada.' }
  83. format.json { head :ok }
  84. else
  85. format.json { render json: @product_waste.errors, status: :unprocessable_entity }
  86. end
  87. end
  88. end
  89. private
  90. # Use callbacks to share common setup or constraints between actions.
  91. def set_product_waste
  92. @product_waste = ProductWaste.find(params[:id])
  93. end
  94. # Never trust parameters from the scary internet, only allow the white list through.
  95. def product_waste_params
  96. params.require(:product_waste).permit(:warehouse_id, :pointsale_id, :product_id, :quantity, :reason, :status)
  97. end
  98. end