class CashRegistersController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :cash_registers_path
  add_breadcrumb "Nueva Caja registradora " , :new_cash_register_path, only: :new
  add_breadcrumb "Detalle de la Caja registradora "  , :cash_register_path, only: :show
  add_breadcrumb "Editar Caja registradora "  , :edit_cash_register_path, only: :edit

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

  #before_action :get_pointsale, only: [:index]

  # GET /cash_registers
  # GET /cash_registers.json
  def index
    if current_user.usertype == 'A'
      @cash_registers = CashRegister.vigentes.includes(:pointsale)
    else
      @cash_registers = CashRegister.includes(:pointsale).where(:pointsale_id => current_user.pointsale_id ).vigentes
    end
  end

  # GET /cash_registers/1
  # GET /cash_registers/1.json
  def show
  end

  # GET /cash_registers/new
  def new
    @cash_register = CashRegister.new
  end

  # GET /cash_registers/1/edit
  def edit
  end

  # POST /cash_registers
  # POST /cash_registers.json
  def create
    @cash_register = CashRegister.new(cash_register_params)
    @cash_register.pointsale_id = @current_user.pointsale_id
    respond_to do |format|
      if @cash_register.save
          format.html { redirect_to cash_registers_url, success: "La caja " + @cash_register.name + " fue registrada." }
        format.json { render :show, status: :created, location: @cash_register }
      else
        format.html { render :new }
        format.json { render json: @cash_register.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cash_registers/1
  # PATCH/PUT /cash_registers/1.json
  def update
    respond_to do |format|
      cash_register_opened = OpenCashRegister.where(:cash_register_id => @cash_register.id, :status => 0).any?

      if cash_register_opened == true && params[:cash_register][:status] == 'inactive'
        @cash_register.errors.add(:status, "No se puede desactivar una caja abierta.")
        format.html { render :edit }
        format.json { render json: @cash_register.errors, status: :unprocessable_entity }
      else
        @cash_register.update(cash_register_params)
        format.html { redirect_to cash_registers_url,   success: "La caja " + @cash_register.name + " fue modificada." }
        format.json { render :show, status: :ok, location: @cash_register }
      end
    end
  end

  def update_status
      cash_register = CashRegister.find(params[:cash_register_id])
      puts cash_register.active?
      if cash_register.active?
        cash_register.status = 2
      elsif cash_register.inactive?
        cash_register.status = 1
      end
      respond_to do |format|
        if cash_register.save(:validate => false)
            format.html { redirect_to cash_registers_url, warning: "La caja registradora " + cash_register.name + " fue "+ (cash_register.active? ? "activada" : "desactivada")+"." }
          # format.json { render :show, status: :ok, location: @pointsale }
          format.json { head :no_content }
        else
          format.html { redirect_to cash_registers_url }
          format.json { render json: @cash_register.errors, status: :unprocessable_entity }
        end
      end
  end

  # DELETE /cash_registers/1
  # DELETE /cash_registers/1.json
  def destroy
    #@cash_register.destroy
    respond_to do |format|
      cash_register_opened = OpenCashRegister.where(:cash_register_id => @cash_register.id, :status => 0).any?
      if cash_register_opened == true
        @cash_register.errors.add(:status, "No se puede eliminar una caja abierta.")
        format.html { render :edit }
        format.json { render json: @cash_register.errors, status: :unprocessable_entity }
      else
        if @cash_register.main == 'yes'
          @cash_register.errors.add(:base, "No se puede eliminar la caja principal.")
          format.html { redirect_to cash_registers_url,  warning: "La caja " + @cash_register.name + " no se puede eliminar por ser la principal." }
          format.json { render json: @cash_register.errors, status: :unprocessable_entity }
        else
          @cash_register.update_attributes(:status => 0)
          format.html { redirect_to cash_registers_url,  warning: "La caja " + @cash_register.name + " fue eliminada." }
          format.json { head :no_content }
        end
      end
    end
  end

  def get_cash_fund
    last_cash_out = CashRegister.find(params[:cash_register_id]).cash_outs.last
    render :json => last_cash_out.present? ? last_cash_out.cash_fund : 0
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cash_register
      @cash_register = CashRegister.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cash_register_params
      params.require(:cash_register).permit(:name, :description, :pointsale_id, :status)
    end
end
