class OpenCashRegistersController < ApplicationController
  before_action :set_open_cash_register, only: [:show, :edit, :update, :destroy]

  # GET /open_cash_registers
  # GET /open_cash_registers.json
  def index
    @open_cash_registers = OpenCashRegister.all
  end

  # GET /open_cash_registers/1
  # GET /open_cash_registers/1.json
  def show; end

  # GET /open_cash_registers/new
  def new; end

  # GET /open_cash_registers/1/edit
  def edit; end

  # POST /open_cash_registers
  # POST /open_cash_registers.json
  def create
    @open_cash_register = OpenCashRegister.new(open_cash_register_params)
    @open_cash_register.status = "open"
    @open_cash_register.user_id = current_user.id
    respond_to do |format|
      @open_cash_register.audit_comment = "Caja #{@open_cash_register.cash_register.name} abierta con #{@open_cash_register.initial_cash}"
      if @open_cash_register.save
        session[:open_cash_register_id] = @open_cash_register.id
        format.json { head :no_content }
        format.js
      else
        format.html { render :new }
        format.json { render json: @open_cash_register.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /open_cash_registers/1
  # PATCH/PUT /open_cash_registers/1.json
  def update
    respond_to do |format|
      if @open_cash_register.update(open_cash_register_params)
        format.html { redirect_to @open_cash_register, notice: 'Open cash register was successfully updated.' }
        format.json { render :show, status: :ok, location: @open_cash_register }
      else
        format.html { render :edit }
        format.json { render json: @open_cash_register.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /open_cash_registers/1
  # DELETE /open_cash_registers/1.json
  def destroy
    @open_cash_register.destroy
    respond_to do |format|
      format.html { redirect_to open_cash_registers_url, notice: 'Open cash register was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def open_cash_register_params
    params.require(:open_cash_register).permit(:cash_register_id, :initial_cash, :final_cash, :status)
  end
end
