class Transactions::SalesController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_transactions_sale, only: [:show, :edit, :update, :destroy]

  # GET /transactions/sales
  # GET /transactions/sales.json
  def index
    
    search_date = params[:date]
    receipt_no  = params[:receipt_no]
    today       = Date.today
    
    if receipt_no.nil? && search_date.nil?
         @sales = Sale.where("NOT sale_status = 'void' " ).order("sale_id desc").limit(500) 
         @sales = Kaminari.paginate_array(@sales).page(params[:page]).per(50)
    else
        if !search_date.blank? && receipt_no.blank?  
          sale = Sale.where("DATE_FORMAT(receipt_date,'%d-%m-%Y') = ? and NOT sale_status = 'void' ", search_date).order("sale_id desc").limit(500).page(params[:page])
        elsif !search_date.blank? && !receipt_no.blank? 
          sale = Sale.where("receipt_no LIKE ? or DATE_FORMAT(receipt_date,'%d-%m-%Y') = ? and NOT sale_status = 'void' ", "%#{receipt_no}%", search_date).order("sale_id desc").limit(500).page(params[:page])
        else  
          sale = Sale.where("receipt_no LIKE ? and NOT sale_status = 'void' ", receipt_no).order("sale_id desc").limit(500).page(params[:page]) 
        end  
        if sale.count > 0
          @sales = sale 
          @sales = Kaminari.paginate_array(@sales).page(params[:page]).per(50)
        else
          @sales = 0
        end
    end

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

  end

  # GET /transactions/sales/1
  # GET /transactions/sales/1.json
  def show
   
    @sale = Sale.find(params[:id])

    @order_items = []
    @sale.sale_orders.each do |sale_order|
      order = Order.find(sale_order.order_id)
      #if (order.status == "new")
        @order_items = @order_items + order.order_items
      #end
    end

    @sale_receivables = SalePayment.where('sale_id = ?', @sale.id)

    #get customer amount
      @customer       = Customer.find(@sale.customer_id)
       @response       = Customer.get_membership_transactions(@customer)
    #end customer amount

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @sale }
    end
  end

  # GET /transactions/sales/new
  def new
    @transactions_sale = Sale.new
  end

  # GET /transactions/sales/1/edit
  def edit
  end

  # POST /transactions/sales
  # POST /transactions/sales.json
  def create
    @transactions_sale = Sale.new(transactions_sale_params)

    respond_to do |format|
      if @transactions_sale.save
        format.html { redirect_to @transactions_sale, notice: 'Sale was successfully created.' }
        format.json { render :show, status: :created, location: @transactions_sale }
      else
        format.html { render :new }
        format.json { render json: @transactions_sale.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /transactions/sales/1
  # PATCH/PUT /transactions/sales/1.json
  def update
    respond_to do |format|
      if @transactions_sale.update(transactions_sale_params)
        format.html { redirect_to @transactions_sale, notice: 'Sale was successfully updated.' }
        format.json { render :show, status: :ok, location: @transactions_sale }
      else
        format.html { render :edit }
        format.json { render json: @transactions_sale.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /transactions/sales/1
  # DELETE /transactions/sales/1.json
  def destroy
    @transactions_sale.destroy
    respond_to do |format|
      format.html { redirect_to transactions_sales_url, notice: 'Sale was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def transactions_sale_params
      params.require(:transactions_sale).permit(:cashier_id, :cashier_name, :requested_by, :requested_at, :receipt_no, :receipt_date, :customer_id, :payment_status, :sale_status, :total_amount, :total_discount, :total_tax, :tax_type, :grand_total, :rounding_adjustment, :amount_received, :amount_changed)
    end
end
