class Transactions::CreditNotesController < ApplicationController
  
  before_action :set_transactions_sale, only: [:show, :edit, :update, :destroy]
  before_action :check_user

  # GET /transactions/sales
  # GET /transactions/sales.json
  def index
    
    @customers = Customer.all

      filter   = params[:filter]
      customer = params[:customer]
      from     = params[:from]
      to       = params[:to]

      if filter.nil? && from.nil? && to.nil? && customer.nil?
        @credit_notes = Sale.where('payment_status = ?', Sale::SALE_STATUS_OUTSTANDING)
        @credit_notes = Kaminari.paginate_array(@credit_notes).page(params[:page]).per(20)
      else
          sale = Sale.search_credit_sales(customer,filter,from,to) 
          if sale.count > 0
             @credit_notes = sale 
             @credit_notes = Kaminari.paginate_array(@credit_notes).page(params[:page]).per(20)
           else
             @credit_notes = 0
           end  
      end    

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @credit_notes }
    end

  end

  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end
  
end