class Origami::PendingOrderController < BaseOrigamiController
  def index
    # @dining= DiningFacility.where("status = 'occupied'")
    # @order = Order.where("DATE_FORMAT(CONVERT_TZ(date, '+00:00', '<%= Time.zone.formatted_offset %>'), '%Y-%m-%d') = ? and status = 'billed' and source = 'quick_service'",Time.current.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
    id = params[:sale_id]
    if id.include? "SAL"
      @sale = Sale.find(id)
      if @sale.sale_status == "new"
        @bookings = @sale.bookings.first
        @status = "sale"
      else
        redirect_to "/origami/#{params[:type]}" and return
      end
    elsif id.include? "BKI"
      @bookings = Booking.find(id)
      @order = @bookings.orders.where(status: "new").first
      @order_items = @bookings.order_items
      @status = "order"
    end

    if @dining = @bookings.dining_facility
      @table_id = @dining.id
    end

    @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
  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
