public function register(Request $request) { $user = $request->session()->get('user'); if (is_null($user)) { return false; } $error = false; $status = array('success' => 0, 'msg' => 'Error occured'); $user = $request->session()->get('user'); $building = buildingModel::where("id", '=', $request['buildingId'])->first(); $buildingCat = buildingCategory::where('id', '=', $building->buildingCatID)->get(); if (isset($request['date10']) && !empty($request['date10'])) { $date10Obj = $request['date10']; } else { $error = true; } if (isset($request['date11']) && !empty($request['date11'])) { $date11Obj = $request['date11']; } else { $error = true; } if (isset($request['packages']) && !empty($request['packages'])) { //$packageObj = $request['package']; //$package = packageModel::find ($request['package']); if ($buildingCat[0]->buildingCatName == "Hotel" || $buildingCat[0]->buildingCatName == "Appartment") { $bookings = bookingModel::where('tenantID', "=", $user[0]->id)->where('checkin', '>=', $date10Obj)->where('checkout', '<=', $date11Obj)->get(); if (sizeof($bookings) > 0) { $status['msg'] = "Booking conflict has been detected for the period " . $date10Obj . " - " . $date11Obj; return $status; } else { //Check if all rooms available in this package $buildingId = $request['buildingId']; $error = false; $roomPackage = array(); foreach ($request['packages'] as $packaged) { if (isset($packaged['package'])) { $idpackage = $packaged['package']; $child = $packaged['child']; $adult = $packaged['adult']; $roomsNum = $packaged['room']; $package = packageModel::find($idpackage); //var_dump($packaged->id); $rooms = DB::table('tblroom')->select(DB::raw('*'))->where('buildingid', '=', $buildingId)->where('roomCatID', '=', $package->roomCategoryID)->get(); $roomsub = DB::table('tblroombooking')->join('tblroom', 'tblroombooking.roomId', '=', 'tblroom.id')->join('tblbooking', 'tblroombooking.bookingId', '=', 'tblbooking.id')->where('tblroom.roomCatID', '=', $package->roomCategoryID)->where('tblroom.buildingID', '=', $buildingId)->where('tblbooking.checkin', '>=', $date10Obj)->where('tblbooking.checkOut', '<=', $date11Obj)->select('tblroom.*')->get(); if (!empty($rooms) && !empty($roomsub)) { $roomsAvailable = array_diff($rooms, $roomsub); } else { if (!empty($rooms)) { $roomsAvailable = $rooms; } else { if (!empty($roomsub)) { $roomsAvailable = array(); } else { $roomsAvailable = array(); } } } if (isset($roomsNum) && $roomsNum > 0 && $roomsNum <= count($roomsAvailable)) { $roomPackage[$idpackage] = $roomsAvailable; //success } else { if ($roomsNum == 0 && $roomsNum > count($roomsAvailable)) { $error = true; $status['msg'] = "No selected room "; } else { if ($roomsNum > 0 && $roomsNum > count($roomsAvailable)) { $error = true; $status['msg'] = "Not enough rooms for the period " . $date10Obj . " - " . $date11Obj . " for package" . $package->packageName; } else { $error = true; $status['msg'] = "Error "; } } } } } //iF ALL ROOMS AVAILABLE AND NO ERROR INSERT INTO BOOKING //INSERT INTO ROOMBOOKING if (!$error) { $booking = new bookingModel(); $booking->checkin = $date10Obj; $booking->checkOut = $date11Obj; $booking->tenantID = $user[0]->id; $booking->buildingID = $request['buildingId']; $booking->save(); $price = 0; foreach ($request['packages'] as $packaged) { if (isset($packaged['package'])) { $idpackage = $packaged['package']; $child = $packaged['child']; $adult = $packaged['adult']; $roomsNum = $packaged['room']; $roomPackage[$idpackage]; $package = packageModel::find($idpackage); //insert into booking package $bookingpackage = new bookingPackageModel(); $bookingpackage->booking_id = $booking->id; $bookingpackage->package_id = $idpackage; $bookingpackage->save(); //insert into roombooking $availablerooms = $roomPackage[$idpackage]; for ($i = 0; $i < $roomsNum; $i++) { $roomBookingModel = new roomBookingModel(); $roomBookingModel->roomId = $availablerooms[$i]->id; $roomBookingModel->bookingId = $booking->id; $roomBookingModel->save(); } $priceArr = $booking->getPrice($package, $booking); $price += (double) $priceArr['days'] * ($priceArr['child'] * $child + $adult * $priceArr['adult']); } } $booking->price = $price; $booking->save(); $status['success'] = 1; $status['msg'] = "Booking successfully registered"; if (count($request['block-vehicle']) > 0) { foreach ($request['block-vehicle'] as $blockVehicle) { if (isset($blockVehicle)) { $vehicle = $blockVehicle['vehicle']; $travelModel = new travelModel(); $travelModel->bookingID = $booking->id; $travelModel->vehicleID = $vehicle; $travelModel->dispach = $blockVehicle['dispatch']; $travelModel->pickUpTime1 = $booking->checkin; $travelModel->pickUpLocation1 = $user[0]->Address; $travelModel->pickUpDestination1 = $booking->building->buildingLocation; if ($travelModel->dispach == 'true') { $travelModel->pickUpTime2 = $booking->checkOut; $travelModel->pickUpLocation2 = $booking->building->buildingLocation; $travelModel->pickUpDestination2 = $user[0]->Address; } $travelModel->save(); } } } } } } else { $bookings = bookingModel::where('tenantID', '=', $user[0]->id)->where('checkin', '>=', $date10Obj)->where('checkout', '<=', $date11Obj)->get(); if (!$error) { if (sizeof($bookings) > 0) { $status['msg'] = "Booking conflict has been detected for the period " . $date10Obj . " - " . $date11Obj; } else { $booking = new bookingModel(); $booking->checkin = $date10Obj; $booking->checkOut = $date11Obj; $booking->tenantID = $user[0]->id; $booking->buildingID = $request['buildingId']; $booking->save(); $price = 0; foreach ($request['packages'] as $packaged) { if (isset($packaged['package'])) { //Access array packaged key pacakges, ie:number of selected packages $idpackage = $packaged['package']; $child = $packaged['child']; $adult = $packaged['adult']; $package = packageModel::find($idpackage); $price += $booking->getPrice($package, $booking); //Add to booking $bookingpackage = new bookingPackageModel(); $bookingpackage->booking_id = $booking->id; $bookingpackage->package_id = $idpackage; $bookingpackage->save(); } } //update price in booking $booking->price = $price; $booking->save(); $status['success'] = 1; $status['msg'] = "Booking successfully registered"; if (!empty($request['block-vehicle'])) { foreach ($request['block-vehicle'] as $blockVehicle) { if (isset($blockVehicle)) { $vehicle = $blockVehicle['vehicle']; $travelModel = new travelModel(); $travelModel->bookingID = $booking->id; $travelModel->vehicleID = $vehicle; $travelModel->dispach = $blockVehicle['dispatch']; $travelModel->pickUpTime1 = $booking->checkin; $travelModel->pickUpLocation1 = $user[0]->Address; $travelModel->pickUpDestination1 = $booking->building->buildingLocation; if ($travelModel->dispach == 'true') { $travelModel->pickUpTime2 = $booking->checkOut; $travelModel->pickUpLocation2 = $booking->building->buildingLocation; $travelModel->pickUpDestination2 = $user[0]->Address; } $travelModel->save(); } } } } } } } return $status; }