class Transactions::ManualSalesController < ApplicationController

  def void
   @sale = params[:sale_id]   
   @reason = Lookup.where("lookup_type = 'void_reason'")
   respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @sales }
    end
  end

  def manual_void_sale
     sale_id           = params[:sale_id]
     reason            = params[:reason]
     sale              = Sale.find(sale_id)
     sale.sale_status  = 'void'
     sale.requested_by = current_login_employee.id
     if sale.save  
       @sale = SaleAudit.record_audit_void(sale_id, current_login_employee.id, current_login_employee.id, reason)

          if sale.sale_orders
            
           sale.sale_orders.each do |bo|
               order =Order.find(bo.order_id)
               order.status= 'void'
               order.save
               booking_order = BookingOrder.find_by_order_id(bo.order_id)
        
               if booking_order.booking_id
                  booking                = Booking.find(booking_order.booking_id)
                  dining_facility        = booking.dining_facility
                  dining_facility.status = 'available'
                  dining_facility.save
               end
           end

          end
      end
     respond_to do |format|
       format.html { redirect_to transactions_sales_url, notice: 'Sale was successfully void.' }
     end

  end

  def manual_complete_sale
    sale_id           = params[:sale_id]
    sale              = Sale.find(sale_id)

    sale.sale_status  = 'completed'
    sale.requested_by = current_login_employee.id
    remark            = "Complete for Sale ID #{sale_id} By #{current_login_employee.name}"
    if sale.save
        @sale = SaleAudit.record_audit_complete(sale_id, remark, current_login_employee.id)

        if sale.sale_orders
            
           sale.sale_orders.each do |bo|
               # order =Order.find(bo.order_id)
               # order.status= 'void'
               # order.save
               booking_order = BookingOrder.find_by_order_id(bo.order_id)
        
               if booking_order.booking_id
                  booking                = Booking.find(booking_order.booking_id)
                  dining_facility        = booking.dining_facility
                  dining_facility.status = 'available'
                  dining_facility.save
               end
           end

        end

    end
    redirect_to transactions_sales_path
  end
  
end
