class ProductWastesController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  before_action :set_product_waste, only: [:show, :edit, :update, :destroy]

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :product_wastes_path
  add_breadcrumb "Nueva merma de productos", :new_product_waste_path, only: :new

  before_action :set_pos_config, only: [:show, :edit, :update, :destroy]

  # GET /product_wastes
  # GET /product_wastes.json
  def index
    if current_user.usertype == 'A'
      @product_wastes = ProductWaste.includes(:product, :user, :pointsale, :warehouse).activos
    elsif current_user.usertype == 'G' || current_user.usertype == 'C'
      @product_wastes = ProductWaste.includes(:product, :user).where(pointsale_id: current_user.pointsale_id).activos
    elsif current_user.usertype == 'S'
      @product_wastes = ProductWaste.includes(:product, :user).where(warehouse_id: current_user.warehouse_id).activos
    end
  end

  # GET /product_wastes/new
  def new; end

  # POST /product_wastes
  # POST /product_wastes.json
  def create
    respond_to do |format|
      @product_waste = ProductWaste.new(product_waste_params)
      @product_waste.user_id = current_user.id
      @product_waste.status = :active

      if current_user.usertype == 'S'
        @product_waste.warehouse_id = current_user.warehouse_id
        @stock = WarehouseStock.find_by(warehouse_id: @product_waste.warehouse_id, product_id: @product_waste.product_id)
      else
        @product_waste.pointsale_id = current_user.pointsale_id
        @stock = AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
      end
      if @stock.stock >= @product_waste.quantity
        @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name}  creada"
        if @product_waste.save
          @stock.stock -= @product_waste.quantity
          @stock.save
          format.js
        else
          format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
        end
      else
        @product_waste.errors.add(:base, "Stock insuficiente, solo se cuenta con #{@stock.stock} unidades del producto #{@product_waste.product.name}")
        format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /product_wastes/1
  # PATCH/PUT /product_wastes/1.json
  def update
    respond_to do |format|
      if @product_waste.update(product_waste_params)
        format.html { redirect_to @product_waste, notice: 'Product waste was successfully updated.' }
        format.json { render :show, status: :ok, location: @product_waste }
      else
        format.html { render :edit }
        format.json { render json: @product_waste.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /product_wastes/1
  # DELETE /product_wastes/1.json
  def destroy
    respond_to do |format|
      @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name}  eliminada."
      if @product_waste.update_attributes(status: :inactive)
        @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)
        @stock.stock = @stock.stock + @product_waste.quantity
        @stock.save

        format.html { redirect_to product_wastes_url, warning: 'Merma de producto eliminada.' }
        format.json { head :ok }
      else
        format.json { render json: @product_waste.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_product_waste
    @product_waste = ProductWaste.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def product_waste_params
    params.require(:product_waste).permit(:warehouse_id, :pointsale_id, :product_id, :quantity, :reason, :status)
  end
end
