public function cancelReservation()
 {
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $f = new formaction();
     //getting user id
     $username = $_SESSION['username'];
     $res = DB::table('user_table')->where('username', "{$username}")->first();
     $user_id = $res->id;
     // getting the reservation id
     $rr = DB::table('reservation_table')->where('user_id', $res->id)->get();
     if ($rr) {
         //check if its reservation is now change his rooms status to empty
         // then delete its records from the 3 tables table_reserved_rooms,reservation_table and user_table
         for ($i = 0; $i < sizeof($rr); $i++) {
             $checked = $f->checkReservednow($rr[$i]);
             if ($checked) {
                 $f->room_changer($rr[$i]->id);
             }
             DB::table('table_reserved_rooms')->where('reservation_id', $rr[$i]->id)->delete();
         }
         DB::table('reservation_table')->where('user_id', $res->id)->delete();
         return 1;
         // if user have a reservation
     } else {
         return 2;
     }
     return 0;
 }
 public function reserveation(Request $re)
 {
     $op = new operationController();
     $re->request->add(['single' => $re->oneval, 'double' => $re->twoval, 'tripple' => $re->threeval, 'dayin' => $re->rday, 'monthin' => $re->rmonth, 'dayout' => $re->cday, 'monthout' => $re->cmonth]);
     $chechAv = $op->checkAvailability($re);
     if (!$chechAv) {
         return 2;
     }
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $username = $_SESSION['username'];
     $rr = DB::table('user_table')->where('username', "{$username}")->first();
     $rr2 = DB::table('reservation_table')->where('user_id', $rr->id)->first();
     if ($rr2) {
         return 3;
     }
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $form = new formaction();
     $checkIN = $form->make_date($re->rday, $re->rmonth);
     $chechOUT = $form->make_date($re->cday, $re->cmonth);
     $_SESSION['checkin'] = $checkIN;
     $_SESSION['checkout'] = $chechOUT;
     $_SESSION['rt1'] = $re->oneval;
     $_SESSION['rt2'] = $re->twoval;
     $_SESSION['rt3'] = $re->threeval;
     $val1 = $re->oneval;
     $val2 = $re->twoval;
     $val3 = $re->threeval;
     $in_day = $re->rday;
     $in_mon = $re->rmonth;
     $out_day = $re->cday;
     $out_mon = $re->cmonth;
     global $in_key;
     global $out_in_key;
     global $inwit;
     global $tot, $tot2, $tot3;
     global $to, $to2, $to3;
     $this->validate($re, ['oneval' => 'numeric', 'twoval' => 'numeric', 'threeval' => 'numeric']);
     if ($val1 == 0 && $val2 == 0 && $val3 == 0) {
         $this->throwValidationException($re, 0);
     } else {
         if (session_status() == PHP_SESSION_NONE) {
             session_start();
         }
         // job 1 : generate how many nights from the check in and out dates
         // job 2 : generate the price and total amount that should be paid
         $chechin = $form->make_date($re->rday, $re->rmonth);
         $checkout = $form->make_date($re->cday, $re->cmonth);
         // job 1
         // -1 comes out when checkout or checkin is not valid date
         $n = DB::select(DB::raw("SELECT DATEDIFF('{$checkout}','{$chechin}') AS day"));
         $nights = 0;
         if ($n[0]->day != 1) {
             $nights = $n[0]->day - 1;
         } else {
             $nights = $n[0]->day;
         }
         $type1price = 0;
         $type2price = 0;
         $type3price = 0;
         $total = 0;
         if ($nights != -1) {
             // when valid date
             // job 2
             if ($val1 > 0) {
                 // partialPrice =  room price * number of rooms
                 // type1price = onedayprice * nights
                 $partialPrice = 50 * $val1;
                 $type1price = $partialPrice * $nights;
                 $total = $total + $type1price;
             }
             if ($val2 > 0) {
                 $partialPrice = 60 * $val2;
                 $type2price = $partialPrice * $nights;
                 $total = $total + $type2price;
             }
             if ($val3 > 0) {
                 $partialPrice = 70 * $val3;
                 $type3price = $partialPrice * $nights;
                 $total = $total + $type3price;
             }
             $total = $total + 78.75;
         } else {
             // error date is not valid
             return 5;
         }
         $_SESSION['reservation'] = 1;
         $_SESSION['amount'] = $total;
         $ReserveInfo = array('total' => $total, 'room1num' => $val1, 'room2num' => $val2, 'room3num' => $val3, 'room1price' => $type1price, 'room2price' => $type2price, 'room3price' => $type3price, 'nights' => $nights);
         $_SESSION['ReserveInfo'] = $ReserveInfo;
         /* $month = array(
                1 => 'January',
                2 => 'February',
                3 => 'March',
                4 => 'April',
                5 => 'May',
                6 => 'June',
                7 => 'July',
                8 => 'Auguest',
                9 => 'September',
                10=> 'October',
                11=> 'November',
                12=> 'December'
            );*/
         /*$in_all=explode( ' ', $in_mon );
           $out_all=explode( ' ', $out_mon );
           $_SESSION['val1']=$val1;
           $_SESSION['val2']=$val2;
           $_SESSION['val3']=$val3;
           $inwit=abs($in_day-$out_day)-1;
           return "$inwit";*/
         /* if($in_all[0] == $out_all[0] && $in_all[1]==$out_all[1] && $inwit < 0)
            {
                $this->throwValidationException($re ,0);
            }*/
         /*else if($in_all[0] == $out_all[0] && $in_all[1]==$out_all[1])
           {    
                  $_SESSION['inwit']=$inwit;
                    $to=$val1 * $inwit * 50;
                    $_SESSION['to']=$to;
                    $to2=$val2 * $inwit * 60;
                    $_SESSION['to2']=$to2;
                    $to3=$val3 * $inwit * 70;
                    $_SESSION['to3']=$to3;
                    
                   $all_to=$to + $to2 +$to3;
                     $_SESSION['all_to']=$all_to;
                     $_SESSION['amount'] = $all_to;
                     $_SESSION['lock']='0';
            }*/
         /* else
             {
                 foreach ($month as $mo)
                 {
                     if($mo == $in_all[0] )
                     {
                         $in_key= key($month)-1;
                     }
                 }
                   foreach ($month as $mo)
                 {
                     if($mo == $out_all[0] )
                     {
                      $out_in_key= key($month)-1;
                        
                     }
                 }
                 
                 
                
                 $mo=  $out_in_key - $in_key;
                 
                 $year=  abs($in_all[1]-$out_all[1]);
                 $day=$in_day-$out_day;
                
                 if($mo == 1 )
                 {
                 $form = new formaction();
                 $in_a=$form->make_date($in_day, $in_mon);
                 $out_a=$form->make_date($out_day, $out_mon);
                 $diff_in = DB::select(DB::raw("SELECT DATEDIFF('$out_a','$in_a') AS day"));
                 $date = $diff_in[0]->day;
                 if($date == NULL)
                 {
                     echo  $all_days=($mo +$date);
                    $_SESSION['all_days']=$all_days;
                 }
                 else {
                     echo   $all_days=($mo +$date)-1;
                     $_SESSION['all_days']=$all_days;
                 }
                 }
                else 
                    {
                    $mo =$mo *12;
                    $all_days=($mo +$day)-1;
                    $_SESSION['all_days']=$all_days;
                    
                    }
                 
                 if($year >= 0 && $year <= 1)
                 {
                        $tot=$val1 * $all_days * 50;
                        $_SESSION['tot']=$tot;
                       
                         $tot2=$val2 * $all_days * 60;
                         $_SESSION['tot2']=$tot2;
                         
                           $tot3=$val3 * $all_days * 70;
                           $_SESSION['tot3']=$tot3;
                           
                      $all_tota=$tot+$tot2+$tot3;
                         $_SESSION['all_tot']=$all_tota;
                         $_SESSION['amount'] = $all_tota;
                         $_SESSION['lock']=1;
                     
                }
            }*/
     }
 }
 public function checkAvailability(Request $request)
 {
     // same operation for each room type
     // get all empty rooms  -- 1
     // get all rooms that will checkout before my chech in date -- 2
     // add 1 and 2 -- 2.5
     // get all check in between the range of my in and out dates -- 3
     // get all who chech in and end after my check in date -- 4 not sure will not do
     // get all who will checkin between today and my checkin
     // 2.5 - (3 + 4)
     $single_room = -1;
     $double_room = -1;
     $triple_room = -1;
     $form = new formaction();
     $checkin = $form->make_date($request->dayin, $request->monthin);
     $checkout = $form->make_date($request->dayout, $request->monthout);
     $today = date("Y-m-d");
     $form->change_room_status();
     $diff_now = DB::select(DB::raw("SELECT DATEDIFF('{$checkin}','{$today}') AS day"));
     if ($diff_now[0]->day < 0) {
         return 0;
     }
     $diff_co = DB::select(DB::raw("SELECT DATEDIFF('{$checkout}','{$checkin}') AS day"));
     if ($diff_co[0]->day < 0) {
         return 0;
     }
     if ($request->single > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 1)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(1, $unsroom);
         // getting checkin between my checkin and checkout
         //$broom = DB::table('reservation_table')->where('checkin','>',"$checkin",'and','checkin','<=',"$checkout")->get();
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(1, $broom);
         //echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(1, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $single_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $single_room;
         //echo $room;
     } else {
         $single_room = -2;
     }
     if ($request->double > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 2)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(2, $unsroom);
         // getting checkin between my checkin and checkout
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(2, $broom);
         // echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(2, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $double_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $double_room;
         //echo $room;
     } else {
         $double_room = -2;
     }
     if ($request->tripple > 0) {
         // getting all empty rooms
         $sroom = DB::table('table_rooms')->where('room_type_id', 3)->where('room_state', 1)->get();
         // getting all who will checkout between today and my checkin
         $unsroom = DB::table('reservation_table')->whereBetween("checkout", array("{$today}", "{$checkin}"))->get();
         // add 1 and 2 which is empty rooms and rooms that will checkout before my checkin
         $totalEmpty = sizeof($sroom) + $form->get_room(3, $unsroom);
         // getting checkin between my checkin and checkout
         $broom = DB::select(DB::raw("select * from reservation_table where checkin > '{$checkin}' and checkin <= '{$checkout}' "));
         $btw_io = $form->get_room(3, $broom);
         // echo $btw_io;
         //getting all who will checkin between today and my checkin
         $btwroom = DB::table('reservation_table')->whereBetween("checkin", array("{$today}", "{$checkin}"))->get();
         $btwroom_s = $form->get_room(3, $btwroom);
         //echo $btwroom_s;
         // calculate total empty rooms
         //$room = $totalEmpty - ($btw_io + $btwroom_s);
         $triple_room = $totalEmpty - ($btw_io + $btwroom_s);
         //echo $room;
     } else {
         $triple_room = -2;
     }
     //var_dump();
     $room_av = array($single_room, $double_room, $triple_room);
     $req_av = array((int) $request->single, (int) $request->double, (int) $request->tripple);
     for ($i = 0; $i < 3; $i++) {
         if ($room_av[$i] != -2) {
             if (!($room_av[$i] >= $req_av[$i])) {
                 return 0;
             }
         }
     }
     return 1;
 }