class Origami::VoidController < BaseOrigamiController
   authorize_resource :class => false
  def overall_void

    sale_id = params[:sale_id]
    if Sale.exists?(sale_id)
      sale = Sale.find_by_sale_id(sale_id)
      sale.payment_status = 'void'
      sale.sale_status = 'void'
      sale.save

      bookings = sale.bookings
      bookings.each do |booking|
        orders = booking.orders
        orders.each do |order|
          # order.status = 'void'
        end
      end

      table_avaliable = true
      table = sale.bookings[0].dining_facility
      table.bookings.each do |booking|
        if booking.sale.sale_status == 'new'
          table_avaliable = false
        end
      end

      if table_avaliable == true
        table.status = 'available'
        table.save
      end

      # update complete order items in oqs
      SaleOrder.where("sale_id = '#{ sale_id }'").find_each do |sodr|        
        AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").find_each do |aoi|
          aoi.delivery_status = 1
          aoi.save
        end
      end
    end
  end

end