1

Am developing a system for booking a venuesfor meeting conducting, where some departments can book after other finished for the same day.

I tried creating a function to to return false when the start time requested is less or equal to booked time and the same for requested end time,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Venue;
use App\VenueRequest;
use Carbon\Carbon;

class CheckAvailability extends Controller
{
    public function checkAvailability(Request $request){

        if(!is_numeric($request->id)){
            echo "Choose among of the listed venues";
        }
        else{
        $requested_venue = VenueRequest::where('venue_id',$request->id)->get();

        // first time request
        if($requested_venue->isEmpty()){
            //allowed to book
            $available = "Available!";
            return $available;
        }
        elseif(count($requested_venue) > 0 ){
            // start check

            foreach ($requested_venue as $venue) {

                $rstart = strtotime($request->meeting_start_time);
                $rend = strtotime($request->meeting_end_time);
                $rdate = strtotime($request->meeting_date);

                $venue_ = $venue->venue->name;
                $depart = $venue->department->alias;
                $start = strtotime($venue->stime);
                $end = strtotime($venue->etime);
                $date = strtotime($venue->book_date);

                if($rdate == $date){
                   $is_start_allowed = $this->timeAvailabilty($rstart, $start, $end);
                   if($is_start_allowed){
                       $is_end_allowed = $this->timeAvailabilty($rend, $start, $end);
                        if($is_end_allowed){
                            echo "Available!";
                        }
                        else{
                            echo "Venue occupied by ".$depart;

                        }
                   }
                   else{
                            echo "Venue occupied by ".$depart;

                        }

                }
                else{
                    echo "Available!";
                }


            }

        }
        else{
            echo "not known";
        }
        }



    }

   public function timeAvailabilty($request_time, $booked_start_time, $booked_end_time)
   {

        return $request_time >= $booked_start_time && $request_time <= $booked_end_time ? false : true ;
   }
}

Expected to restrict the time collision but the system allows some one to book if still thereis a meeting conducted, such as if booked start at 08.00 to 10.00 the other ca book at 07.00 to 11.00 which is not required

Rashed Hasan
  • 3,721
  • 11
  • 40
  • 82
Makaveli
  • 11
  • 6
  • 1
    I don't understand your loop. You have a new appointment. You check every existing appointment for that venue? Your question is very broad - have your tried writing pseudo code, basically comments, might help materialize the logic you need. The answer is to probably leverage your database and fire off one query using a `BETWEEN`. – ficuscr Aug 29 '19 at 17:40
  • 1
    If it was me, I'd remove all the non-sql stuff from the question and instead see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query – Strawberry Aug 29 '19 at 21:38
  • Thanks for the replies, I have th table called venue request which has venue id booking_date, starttime, endtime and others(not thet necessary), whenever i need to add new request i have to check the existance of the venue in venue request table, and check if i can book at thet day on different time, (hope you'll understand me, thenks) @ficuscr – Makaveli Aug 30 '19 at 04:57
  • Thanks @Strawberry but honestly i did'nt understand what you meant, i hope the clarification i made above can help you guys understand what i need to achieve. – Makaveli Aug 30 '19 at 05:02
  • For example: https://stackoverflow.com/questions/49012431/sql-to-check-if-the-room-is-available-between-the-dates or https://stackoverflow.com/questions/17407481/check-if-a-time-is-between-two-times-time-datatype or https://stackoverflow.com/questions/28073011/return-available-rooms-by-time/28075696#28075696 You can probably find other examples. Use SQL. – ficuscr Aug 30 '19 at 05:19
  • Possible duplicate of [SQL To check if the room is available between the dates](https://stackoverflow.com/questions/49012431/sql-to-check-if-the-room-is-available-between-the-dates) – ficuscr Sep 01 '19 at 03:21

0 Answers0