class Origami::PendingOrderController < BaseOrigamiController
  def index
    # @dining= DiningFacility.where("status = 'occupied'")
    # @order = Order.where("DATE_FORMAT(date,'%Y-%m-%d') = ? and status = 'billed' and source = 'quick_service'",DateTime.now.strftime('%Y-%m-%d'))
    @cashier_type = params[:type]
    @sale = Sale.pending_sale(@cashier_type)
    @order = Sale.pending_order(@cashier_type)
    @completed = Sale.completed_sale(@cashier_type)
    @customers = Customer.pluck("customer_id, name")
    
    @occupied_table = @sale.count.length + @order.count.length
    
  end

  def show
    @cashier_type = params[:type]
    @sales  = Sale.pending_sale(@cashier_type)
    @orders = Sale.pending_order(@cashier_type)
    @completed = Sale.completed_sale(@cashier_type)
    @customers = Customer.pluck("customer_id, name")

    @occupied_table = @sales.count.length + @orders.count.length

    @id     = params[:sale_id]
    if(@id[0,3] == "SAL")     
      @sale = Sale.find(@id)
      @order = SaleOrder.find_by_sale_id(@sale.sale_id).order_id
      @booking = BookingOrder.find_by_order_id(@order).booking_id
      @bookings = Booking.find(@booking)
      @status = "sale"
    else
      # @booking = BookingOrder.find_by_order_id(@order).booking_id
      @bookings = Booking.find(@id)
      @status = "order"

      @order_items = Array.new
      @bookings.booking_orders.each do |booking_order|
        @order = Order.find(booking_order.order_id)
        if (@order.status == "new")
          @order.order_items.each do |item|
            @order_items.push(item)
          end
        end
      end
    end
    # puts @status
    if @bookings.dining_facility_id.to_i > 0
      @table_id = Booking.find(@bookings.booking_id).dining_facility_id
      @dining  = DiningFacility.find(@table_id)
    else
      @table_id = nil
      @dining  = nil
    end
    @customers = Customer.pluck("customer_id, name")
  end

  def completed_sale
    @cashier_type = params[:type]
    @sales     = Sale.pending_sale(@cashier_type)
    @orders    = Sale.pending_order(@cashier_type)
    @completed = Sale.completed_sale(@cashier_type)
    @customers = Customer.pluck("customer_id, name")

    @occupied_table = @sales.count.length + @orders.count.length

    @id        = params[:sale_id]
     
    @sale      = Sale.find(@id)
    @order     = SaleOrder.find_by_sale_id(@sale.sale_id).order_id
    @booking   = BookingOrder.find_by_order_id(@order).booking_id
    @bookings  = Booking.find(@booking)
    @status    = "sale"
    
    if @bookings.dining_facility_id.to_i > 0
      @table_id = Booking.find(@bookings.booking_id).dining_facility_id
      @dining  = DiningFacility.find(@table_id)
    else
      @table_id = nil
      @dining  = nil
    end
  end

  def cash_ins
    @cashier_type = params[:type]
    render "origami/cash_ins/new"
  end
  def cash_outs
    @cashier_type = params[:type]
    render "origami/cash_outs/new"
  end

  def credit_sale
    @cashier_type = params[:type]
    @sales     = Sale.pending_sale(@cashier_type)
    @orders    = Sale.pending_order(@cashier_type)
    @completed = Sale.completed_sale(@cashier_type)

    @occupied_table = @sales.count.length + @orders.count.length
    
    @id        = params[:sale_id]
    @customers = Customer.pluck("customer_id, name")

    @sale = Sale.find_by_sale_id(params[:sale_id])
    @order     = SaleOrder.find_by_sale_id(@sale.sale_id).order_id
    @booking   = BookingOrder.find_by_order_id(@order).booking_id
    @bookings  = Booking.find(@booking)
    @status    = "sale"
    
    if @bookings.dining_facility_id.to_i > 0
      @table_id = Booking.find(@bookings.booking_id).dining_facility_id
      @dining  = DiningFacility.find(@table_id)
    else
      @table_id = nil
      @dining  = nil
    end

    @sale_payment = SalePayment.select("SUM(payment_amount) as payment_amount")
                  .where("sale_id = ? and payment_method=?", @sale.sale_id, "creditnote")
    
    @sale_taxes = []
    sale_taxes = SaleTax.where("sale_id = ?", @sale.sale_id)
    if !sale_taxes.empty?
      sale_taxes.each do |sale_tax|
        @sale_taxes.push(sale_tax)
      end
    end
  end

end
