product_wastes_controller.rb 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. @product_wastes = ProductWaste.includes(:product, :user, :pointsale, :warehouse).activos
  13. @product_wastes =
  14. if current_user.usertype == 'G' || current_user.usertype == 'C'
  15. @product_wastes.where(pointsale_id: current_user.pointsale_id)
  16. elsif current_user.usertype == 'S'
  17. @product_wastes.where(warehouse_id: current_user.warehouse_id)
  18. else
  19. @product_wastes
  20. end
  21. end
  22. # GET /product_wastes/new
  23. def new; end
  24. # POST /product_wastes
  25. # POST /product_wastes.json
  26. def create
  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, product_id: @product_waste.product_id)
  33. else
  34. @product_waste.pointsale_id = current_user.pointsale_id
  35. @stock = AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
  36. end
  37. respond_to do |format|
  38. if @stock.stock >= @product_waste.quantity
  39. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} creada"
  40. if @product_waste.save
  41. @stock.stock -= @product_waste.quantity
  42. @stock.save
  43. format.js
  44. else
  45. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  46. end
  47. else
  48. @product_waste.errors.add(:base, "Stock insuficiente, sólo se cuenta con #{@stock.stock} unidades del producto #{@product_waste.product.name}")
  49. format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
  50. end
  51. end
  52. end
  53. # DELETE /product_wastes/1
  54. # DELETE /product_wastes/1.json
  55. def destroy
  56. respond_to do |format|
  57. @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} eliminada."
  58. if @product_waste.update_attributes(status: :inactive)
  59. @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)
  60. @stock.stock = @stock.stock + @product_waste.quantity
  61. @stock.save
  62. format.html { redirect_to product_wastes_url, warning: 'Merma de producto eliminada.' }
  63. format.json { head :ok }
  64. else
  65. format.json { render json: @product_waste.errors, status: :unprocessable_entity }
  66. end
  67. end
  68. end
  69. private
  70. # Use callbacks to share common setup or constraints between actions.
  71. def set_product_waste
  72. @product_waste = ProductWaste.find(params[:id])
  73. end
  74. # Never trust parameters from the scary internet, only allow the white list through.
  75. def product_waste_params
  76. params.require(:product_waste).permit(:warehouse_id, :pointsale_id, :product_id, :quantity, :reason, :status)
  77. end
  78. end