class Origami::SalesController < BaseOrigamiController

  def show
    @tables = Table.all.active.order('status desc')
    @rooms = Room.all.active.order('status desc')
    @complete = Sale.all
    @orders = Order.all.order('date desc')
    @sale = Sale.find(params[:sale_id])
  end

  def add_to_existing_invoice
    dining = params[:dining_id]
    sale_id = params[:sale_id]
    table = DiningFacility.find(dining)
      table.bookings.each do |booking|
       if booking.sale_id.nil?
         booking.booking_orders.each do |booking_order|
             booking.booking_status = 'moved'
             order = Order.find(booking_order.order_id)
             order.status = 'billed'
             order.order_items.each do |item|
               item.order_item_status = 'billed'
             end
             #  create sale item
             saleobj = Sale.find(sale_id)
             order.order_items.each do |orer_item|
               saleobj.add_item (orer_item)
             end

             # Re-compute for add
             saleobj.compute
             saleobj.save
             order.save
             booking.save
         end
         existing_booking = Booking.find_by_sale_id(sale_id)
         booking_order = BookingOrder.where('booking_id=?',booking)
         booking_order.each do |bo|
           bo.booking_id = existing_booking.booking_id
           bo.save
         end
      end
    end
  end


end
