Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views9 pages

BookingController Methods

The BookingController contains public methods for managing bookings, including checkout, storing booking details, processing payments, and updating booking statuses. All methods are accessible via HTTP requests and are protected by middleware to ensure only authenticated users can access backend routes. The controller interacts with various models like Booking, Room, and User to handle booking data effectively.

Uploaded by

youngvicks2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

BookingController Methods

The BookingController contains public methods for managing bookings, including checkout, storing booking details, processing payments, and updating booking statuses. All methods are accessible via HTTP requests and are protected by middleware to ensure only authenticated users can access backend routes. The controller interacts with various models like Booking, Room, and User to handle booking data effectively.

Uploaded by

youngvicks2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BookingController Methods

Here’s a detailed breakdown of the BookingController methods and their visibility:

Checkout()

Purpose: Renders the checkout view using session data.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function Checkout() {
if (Session::has('book_date')) {
$book_data = Session::get('book_date');
$room = Room::find($book_data['room_id']);
$toDate = Carbon::parse($book_data['check_in']);
$fromDate = Carbon::parse($book_data['check_out']);
$nights = $toDate->diffInDays($fromDate);
return view('frontend.checkout.checkout', compact('book_data',
'room', 'nights'));
} else {
$notification = array(
'message' => 'Something went wrong!',
'alert-type' => 'error'
);
return redirect('/')->with($notification);
}
}
BookingStore(Request $request)

Purpose: Validates and stores booking details in the session.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function BookingStore(Request $request) {
$validateData = $request->validate([
'check_in' => 'required',
'check_out' => 'required',
'persion' => 'required',
'number_of_rooms' => 'required',
]);

if ($request->available_room < $request->number_of_rooms) {


$notification = array(
'message' => 'Something went wrong!',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}
Session::forget('book_date');

$data = array();
$data['number_of_rooms'] = $request->number_of_rooms;
$data['available_room'] = $request->available_room;
$data['persion'] = $request->persion;
$data['check_in'] = date('Y-m-d', strtotime($request->check_in));
$data['check_out'] = date('Y-m-d', strtotime($request->check_out));
$data['room_id'] = $request->room_id;

Session::put('book_date', $data);

return redirect()->route('checkout');
}
CheckoutStore(Request $request)

Purpose: Processes payment and stores booking details in the database.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function CheckoutStore(Request $request) {
$user = User::where('role', 'admin')->get();

$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required',
'country' => 'required',
'phone' => 'required',
'address' => 'required',
'state' => 'required',
'zip_code' => 'required',
'payment_method' => 'required',
]);

$book_data = Session::get('book_date');
$toDate = Carbon::parse($book_data['check_in']);
$fromDate = Carbon::parse($book_data['check_out']);
$total_nights = $toDate->diffInDays($fromDate);

$room = Room::find($book_data['room_id']);
$subtotal = $room->price * $total_nights *
$book_data['number_of_rooms'];
$discount = ($room->discount / 100) * $subtotal;
$total_price = $subtotal - $discount;
$code = rand(000000000, 999999999);

if ($request->payment_method == 'Stripe') {
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$s_pay = Stripe\Charge::create([
"amount" => $total_price * 100,
"currency" => "usd",
"source" => $request->stripeToken,
"description" => "Payment For Booking. Booking No " . $code,
]);
if ($s_pay['status'] == 'succeeded') {
$payment_status = 1;
$transation_id = $s_pay->id;
} else {
$notification = array(
'message' => 'Sorry, Payment Failed',
'alert-type' => 'error'
);
return redirect('/')->with($notification);
}
} else {
$payment_status = 0;
$transation_id = '';
}

$data = new Booking();


$data->rooms_id = $room->id;
$data->user_id = Auth::user()->id;
$data->check_in = date('Y-m-d', strtotime($book_data['check_in']));
$data->check_out = date('Y-m-d', strtotime($book_data['check_out']));
$data->persion = $book_data['persion'];
$data->number_of_rooms = $book_data['number_of_rooms'];
$data->total_night = $total_nights;

$data->actual_price = $room->price;
$data->subtotal = $subtotal;
$data->discount = $discount;
$data->total_price = $total_price;
$data->payment_method = $request->payment_method;
$data->transation_id = $transation_id;
$data->payment_status = $payment_status;

$data->name = $request->name;
$data->email = $request->email;
$data->phone = $request->phone;
$data->country = $request->country;
$data->state = $request->state;
$data->zip_code = $request->zip_code;
$data->address = $request->address;

$data->code = $code;
$data->status = 0;
$data->created_at = Carbon::now();
$data->save();

$sdate = date('Y-m-d', strtotime($book_data['check_in']));


$edate = date('Y-m-d', strtotime($book_data['check_out']));
$eldate = Carbon::create($edate)->subDay();
$d_period = CarbonPeriod::create($sdate, $eldate);
foreach ($d_period as $period) {
$booked_dates = new RoomBookedDate();
$booked_dates->booking_id = $data->id;
$booked_dates->room_id = $room->id;
$booked_dates->book_date = date('Y-m-d', strtotime($period));
$booked_dates->save();
}

Session::forget('book_date');

$notification = array(
'message' => 'Booking Added Successfully',
'alert-type' => 'success'
);

Notification::send($user, new BookingComplete($request->name));

return redirect('/')->with($notification);
}
BookingList()

Purpose: Fetches and displays all bookings.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function BookingList() {
$allData = Booking::orderBy('id', 'desc')->get();
return view('backend.booking.booking_list', compact('allData'));
}
EditBooking($id)

Purpose: Fetches booking details for editing.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function EditBooking($id) {
$editData = Booking::with('room')->find($id);
return view('backend.booking.edit_booking', compact('editData'));
}
UpdateBookingStatus(Request $request, $id)

Purpose: Updates the status of a booking and sends a confirmation email.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function UpdateBookingStatus(Request $request, $id) {
$booking = Booking::find($id);
$booking->payment_status = $request->payment_status;
$booking->status = $request->status;
$booking->save();

// Send email
$sendmail = Booking::find($id);
$data = [
'check_in' => $sendmail->check_in,
'check_out' => $sendmail->check_out,
'name' => $sendmail->name,
'email' => $sendmail->email,
'phone' => $sendmail->phone,
];

Mail::to($sendmail->email)->send(new BookConfirm($data));

$notification = array(
'message' => 'Information Updated Successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
UpdateBooking(Request $request, $id)

Purpose: Updates the details of a booking.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function UpdateBooking(Request $request, $id) {
if ($request->available_room < $request->number_of_rooms) {
$notification = array(
'message' => 'Something Went Wrong!',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}

$data = Booking::find($id);
$data->number_of_rooms = $request->number_of_rooms;
$data->check_in = date('Y-m-d', strtotime($request->check_in));
$data->check_out = date('Y-m-d', strtotime($request->check_out));
$data->save();

BookingRoomList::where('booking_id', $id)->delete();

$notification = array(
'message' => 'Booking Updated Successfully',
'alert-type' => 'success'
);
return redirect()->route('booking.list')->with($notification);
}
AssignRoom($booking_id)

Purpose: Fetches rooms for assignment based on booking details.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function AssignRoom($booking_id) {
$book = Booking::with('room')->find($booking_id);
$roomtype = RoomType::orderBy('id', 'desc')->get();
$roomno = Room::orderBy('id', 'desc')->get();

return view('backend.booking.assign_room', compact('book', 'roomtype',


'roomno'));
}
AssignRoomStore($booking_id, $room_number_id)

Purpose: Stores room assignments for a booking.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function AssignRoomStore($booking_id, $room_number_id) {
$roomnumber = Room::find($room_number_id);
$assigned_rooms = new BookingRoomList();
$assigned_rooms->booking_id = $booking_id;
$assigned_rooms->room_number = $roomnumber->room_number;
$assigned_rooms->save();

$notification = array(
'message' => 'Room Assigned Successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
AssignRoomDelete($id)

Purpose: Deletes room assignments for a booking.

Access Level: Requires backend access.

Visibility: Public.

php
Copy code
public function AssignRoomDelete($id) {
BookingRoomList::where('id', $id)->delete();

$notification = array(
'message' => 'Room Deleted Successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
DownloadInvoice($id)

Purpose: Generates and downloads a booking invoice.

Access Level: Requires backend access.


Visibility: Public.

php
Copy code
public function DownloadInvoice($id) {
$invoice = Booking::with('room', 'user')->where('id', $id)->first();
$pdf = PDF::loadView('backend.invoice.download_invoice',
compact('invoice'));

return $pdf->download('invoice.pdf');
}
UserBooking()

Purpose: Fetches all bookings for the logged-in user.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function UserBooking() {
$id = Auth::user()->id;
$bookings = Booking::where('user_id', $id)->orderBy('id', 'desc')-
>get();
return view('frontend.booking.booking_list', compact('bookings'));
}
UserInvoice($id)

Purpose: Generates and downloads an invoice for a specific booking.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function UserInvoice($id) {
$invoice = Booking::with('room', 'user')->where('id', $id)->first();
$pdf = PDF::loadView('frontend.invoice.user_invoice',
compact('invoice'));

return $pdf->download('invoice.pdf');
}
MarkAsRead(Request $request, $notificationId)

Purpose: Marks notifications as read.

Access Level: Requires frontend access.

Visibility: Public.

php
Copy code
public function MarkAsRead(Request $request, $notificationId) {
auth()->user()->unreadNotifications->where('id', $notificationId)-
>markAsRead();

return response()->noContent();
}

Explanation of Access Levels

Public Visibility: All methods in the BookingController are public because they need to be
accessible via HTTP requests routed through Laravel’s routing system. This means they are
callable by URLs defined in your route files (usually web.php or api.php).

Access Control: Typically, access control to ensure methods are only accessible by
authenticated users (and possibly users with specific roles or permissions) is handled by
middleware, not method visibility modifiers. In this case, BookingController methods
should be protected by middleware that ensures only authorized users can access backend
routes.

Interaction with the Booking Model

 Checkout(): Interacts with the Room model to fetch room details for the checkout view.
 BookingStore(Request $request): Stores booking details in the session but does not interact
directly with the Booking model.
 CheckoutStore(Request $request): Interacts with the Booking, Room, User, and
RoomBookedDate models to process and store booking details.
 BookingList(): Fetches all booking data using the Booking model.
 EditBooking($id): Fetches specific booking details using the Booking model.
 UpdateBookingStatus(Request $request, $id): Updates the payment status and interacts
with the Booking model.
 UpdateBooking(Request $request, $id): Updates existing booking details using the
Booking model.
 AssignRoom($booking_id): Fetches booking details and room types using the Booking and
RoomType models.
 AssignRoomStore($booking_id, $room_number_id): Stores room assignments using the
BookingRoomList model.
 AssignRoomDelete($id): Deletes room assignments using the BookingRoomList model.
 DownloadInvoice($id): Generates and downloads invoices using the Booking model.
 UserBooking(): Fetches bookings for the logged-in user using the Booking model.
 UserInvoice($id): Generates and downloads invoices for the logged-in user using the
Booking model.
 MarkAsRead(Request $request, $notificationId): Marks notifications as read without
directly interacting with the Booking model.

Laravel Middleware

To ensure that only authenticated users can access the methods in BookingController, you
would typically use middleware. For example:

php
Copy code
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Booking;

class BookingController extends Controller {


public function __construct() {
$this->middleware('auth'); // Ensures the user is authenticated
// Add additional middleware as necessary, e.g., role checks
}

// Methods go here...
}

Summary

 Public Methods: All controller methods are public to be accessible via routing.
 Middleware Protection: Ensure methods are protected by middleware for authentication
and authorization.
 Model Interaction: Methods interact with the Booking model to fetch and manipulate
booking data.

By setting up middleware in the constructor of the controller, you can ensure that all methods
in BookingController are protected, thus maintaining secure access control without
compromising the accessibility required for routing in a Laravel application.

You might also like