class Origami::SalesController < BaseOrigamiController
  def show
    @webview = false
    if check_mobile
      @webview = true  
    end
    
    @tables   = Table.unscoped.all.active.order('status desc')
    @rooms    = Room.unscoped.all.active.order('status desc')
    @complete = Sale.completed_sale("cashier")
    @orders   = Order.includes("sale_orders").where("DATE_FORMAT(date,'%Y-%m-%d') = ? and status != 'billed' and source != 'quick_service'",DateTime.now.strftime('%Y-%m-%d')).order('date desc')
    @customers = Customer.pluck("customer_id, name")
    @sale = Sale.find(params[:sale_id])
    @order = SaleOrder.find_by_sale_id(@sale.sale_id).order_id
    @booking = BookingOrder.find_by_order_id(@order).booking_id
    if Booking.find(@booking).dining_facility_id.to_i>0
      @table_id = Booking.find(@booking).dining_facility_id
      @dining  = DiningFacility.find(@table_id)
    else
      @table_id = nil
      @dining  = nil
    end
  end

  def add_to_existing_invoice
    dining = params[:dining_id]
    sale_id = params[:sale_id]
    tax_type = params[:tax_type]
    sale_data = []
    table = DiningFacility.find(dining)
    existing_booking = Booking.find_by_sale_id(sale_id)
      table.bookings.active.where("DATE_FORMAT(created_at,'%Y-%m-%d') = '#{DateTime.now.strftime('%Y-%m-%d')}' OR DATE_FORMAT(created_at,'%Y-%m-%d') = '#{Date.today.prev_day}' ").each do |booking|
        # if !booking.checkout_at.nil?
        #     existing_booking.update_attributes(checkout_at: checkout_at)
        # end
        if booking.sale_id.nil?
          order_array = []
          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)
                if !orer_item.set_menu_items.nil?
                  saleobj.add_sub_item(orer_item.set_menu_items)
                end
                sale_data.push(orer_item)
             end

             # Re-compute for add
             saleobj.compute(order.source,tax_type)
             saleobj.save
             order.save
             booking.save

             order_array.push(order.order_id)
            
         end

          receipt_no = Sale.find(sale_id).receipt_no
          action_by = current_user.name
          type = "ADD_TO_EXISTING"
            
          remark = "#{action_by} add to existing order #{order_array} to Receipt No=>#{receipt_no} in #{table.name}"
          sale_audit = SaleAudit.record_audit_sale(sale_id,remark,action_by,type )
         
         booking_order = BookingOrder.where('booking_id=?',booking)
         booking_order.each do |bo|
           bo.booking_id = existing_booking.booking_id
           bo.save
         end
      end
    end
    if !sale_data.empty?
    # InventoryJob.perform_now(self.id)
      InventoryDefinition.calculate_product_count(nil,sale_data)
    end
  end

end
