|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\DMIEsys; |
| 4 | + |
| 5 | +use App\Models\BookingLab; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use App\Http\Controllers\Controller; |
| 8 | +use App\Mail\FacilityBookingApproved; |
| 9 | +use App\Mail\FacilityBookingAssigned; |
| 10 | +use App\Mail\FacilityBookingPlaced; |
| 11 | +use App\Mail\FacilityBookingReceived; |
| 12 | +use App\Mail\FacilityBookingRejected; |
| 13 | +use App\Models\Lab; |
| 14 | +use App\Models\User; |
| 15 | +use Illuminate\Support\Facades\Mail; |
| 16 | + |
| 17 | +class BookingLabController extends Controller |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Display a listing of the resource. |
| 21 | + */ |
| 22 | + public function index() |
| 23 | + { |
| 24 | + if ($this->can('approve booking')) { |
| 25 | + $approve_booking_auth = true; |
| 26 | + } else { |
| 27 | + abort(404); |
| 28 | + } |
| 29 | + |
| 30 | + $bookings = BookingLab::where('rejected', 0)->where('date', '>=', now()->format('Y-m-d')) |
| 31 | + ->where('approved', 0)->where('rejected', 0) |
| 32 | + ->orderBy('date', 'desc')->orderBy('start_time', 'desc')->with('getLab')->get(); |
| 33 | + |
| 34 | + // dd($bookings); |
| 35 | + |
| 36 | + return view('dmiesys.laboratories.book-facility', compact('approve_booking_auth', 'bookings')); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Show the form for creating a new resource. |
| 41 | + */ |
| 42 | + public function create() |
| 43 | + { |
| 44 | + $book_lab_auth = true; |
| 45 | + |
| 46 | + $starting_times = config('settings.starting_times'); |
| 47 | + $ending_times = config('settings.ending_times'); |
| 48 | + |
| 49 | + $min_days = config('settings.booking.min_days'); |
| 50 | + $max_days = config('settings.booking.max_days'); |
| 51 | + |
| 52 | + return view('dmiesys.laboratories.book-facility', compact('book_lab_auth', 'starting_times', 'ending_times', 'min_days', 'max_days')); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Store a newly created resource in storage. |
| 57 | + */ |
| 58 | + public function store(Request $request) |
| 59 | + { |
| 60 | + $request->validate([ |
| 61 | + 'lab' => 'required|exists:labs,id', |
| 62 | + 'date' => ['required', 'date', 'after:' . now()->addDays(config('settings.booking.min_days'))->format('Y-m-d'), 'before:' . now()->addDays(config('settings.booking.max_days'))->format('Y-m-d')], |
| 63 | + 'phone' => ['bail', 'nullable', 'alpha_num:ascii', 'min:10', 'max:10', 'regex:/^0[0-9]{9}$/'], |
| 64 | + 'start_time' => 'required|date_format:H:i|before:end_time|after:07:59|before:17:01', |
| 65 | + 'end_time' => 'required|date_format:H:i|after:start_time|after:07:59|before:17:01', |
| 66 | + 'note' => 'nullable|string', |
| 67 | + ]); |
| 68 | + |
| 69 | + //validate if the lab is available for booking on the selected date and time |
| 70 | + |
| 71 | + $bookings = BookingLab::where('date', date('Y-m-d', strtotime($request->date))) |
| 72 | + ->where('lab_id', $request->lab) |
| 73 | + ->where('rejected', 0) |
| 74 | + ->where(function ($query) use ($request) { |
| 75 | + $query->whereBetween('start_time', [$request->start_time, $request->end_time]) |
| 76 | + ->orWhereBetween('end_time', [$request->start_time, $request->end_time]); |
| 77 | + }) |
| 78 | + ->get(); |
| 79 | + |
| 80 | + |
| 81 | + if ($bookings->count() > 0) { |
| 82 | + return back()->withErrors('The lab is not available for booking on the selected date and time. Please select another date and time.')->withInput(); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + try { |
| 87 | + $lab_booking = new BookingLab(); |
| 88 | + $lab_booking->lab_id = $request->lab; |
| 89 | + $lab_booking->date = date('Y-m-d', strtotime($request->date)); |
| 90 | + $lab_booking->start_time = $request->start_time; |
| 91 | + $lab_booking->end_time = $request->end_time; |
| 92 | + $lab_booking->from = 'internal'; |
| 93 | + $lab_booking->name = auth()->user()->name; |
| 94 | + $lab_booking->email = auth()->user()->email; |
| 95 | + $lab_booking->phone = $request->phone; |
| 96 | + $lab_booking->department = 'Department of Manufacturing and Industrial Engineering'; |
| 97 | + $lab_booking->notes = $request->note; |
| 98 | + $lab_booking->save(); |
| 99 | + } catch (\Throwable $th) { |
| 100 | + $this->failed($th); |
| 101 | + return back()->withErrors("There was a problem, please check the logs to see more about this!")->withInput(); |
| 102 | + } |
| 103 | + |
| 104 | + $this->passed('Lab booked by internal members ' . auth()->user()->name . ' ' . ' on ' . $request->date . ' from ' . $request->start_time . ' to ' . $request->end_time); |
| 105 | + |
| 106 | + //send email LabBookingPlaced to the person who placed the booking |
| 107 | + $body = [ |
| 108 | + 'name' => auth()->user()->name, |
| 109 | + 'email' => auth()->user()->email, |
| 110 | + 'phone' => $request->phone, |
| 111 | + 'request_type' => 'internal', |
| 112 | + 'department' => 'Manufacturing and Industrial Engineering', |
| 113 | + 'facility' => Lab::find($request->lab)->name, |
| 114 | + 'date' => date('Y-m-d', strtotime($request->date)), |
| 115 | + 'start_time' => date('h:i a', strtotime($request->start_time)), |
| 116 | + 'end_time' => date('h:i a', strtotime($request->end_time)), |
| 117 | + 'notes' => $request->note, |
| 118 | + 'url' => route('book-labs.index'), |
| 119 | + ]; |
| 120 | + |
| 121 | + Mail::to(auth()->user()->email)->send(new FacilityBookingPlaced($body)); |
| 122 | + Mail::to(config('settings.emails.office'))->send(new FacilityBookingReceived($body)); |
| 123 | + |
| 124 | + return redirect()->route('book-labs.create')->with('toast_success', 'Your booking request has been sent successfully. Please wait for the approval.'); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Display the specified resource. |
| 129 | + */ |
| 130 | + public function show($booking_id) |
| 131 | + { |
| 132 | + if ($this->can('approve booking')) { |
| 133 | + $approve_booking_single_auth = true; |
| 134 | + } else { |
| 135 | + abort(404); |
| 136 | + } |
| 137 | + |
| 138 | + $booking = BookingLab::where('id', $booking_id)->where('rejected', 0)->where('approved', 0)->where('date', '>=', now()->format('Y-m-d'))->with('getLab')->firstOrFail(); |
| 139 | + |
| 140 | + $technical_officers = User::active()->role(['Technical Officer', 'Instrument Mechanic', 'Machine Operator'])->get(['id', 'name']); |
| 141 | + |
| 142 | + |
| 143 | + return view('dmiesys.laboratories.book-facility', compact('approve_booking_single_auth', 'booking', 'technical_officers')); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Update the specified resource in storage. |
| 148 | + */ |
| 149 | + public function update(Request $request, $booking_id) |
| 150 | + { |
| 151 | + abort_unless(($this->can('approve booking')), 404); |
| 152 | + |
| 153 | + $booking = BookingLab::where('id', $booking_id)->where('rejected', 0)->where('approved', 0)->where('date', '>=', now()->format('Y-m-d'))->firstOrFail(); |
| 154 | + |
| 155 | + $request->validate([ |
| 156 | + 'status' => 'required|in:approved,rejected', |
| 157 | + 'technical_officer' => 'required_if:status,approved', |
| 158 | + 'reason' => 'required_if:status,rejected', |
| 159 | + ]); |
| 160 | + |
| 161 | + if ($request->status == 'approved') { |
| 162 | + $user = User::role(['Technical Officer', 'Instrument Mechanic', 'Machine Operator']) |
| 163 | + ->where('id', $request->technical_officer)->firstOrFail(); |
| 164 | + |
| 165 | + $body = [ |
| 166 | + 'name' => $booking->name, |
| 167 | + 'email' => $booking->email, |
| 168 | + 'phone' => $booking->phone, |
| 169 | + 'to_name' => $user->name, |
| 170 | + 'to_email' => $user->email, |
| 171 | + 'to_phone' => $user->phone, |
| 172 | + 'department' => 'Manufacturing and Industrial Engineering', |
| 173 | + 'facility' => Lab::find($booking->lab_id)->name, |
| 174 | + 'date' => date('Y-m-d', strtotime($booking->date)), |
| 175 | + 'start_time' => date('h:i a', strtotime($booking->start_time)), |
| 176 | + 'end_time' => date('h:i a', strtotime($booking->end_time)), |
| 177 | + 'notes' => $booking->notes, |
| 178 | + ]; |
| 179 | + |
| 180 | + $booking->approved = 1; |
| 181 | + $booking->technical_staff = $request->technical_officer; |
| 182 | + $booking->save(); |
| 183 | + |
| 184 | + Mail::to($booking->email)->send(new FacilityBookingApproved($body)); |
| 185 | + Mail::to($user->email)->send(new FacilityBookingAssigned($body)); |
| 186 | + |
| 187 | + $this->passed('Lab booking approved for ' . $booking->name . ' ' . ' on ' . $booking->date . ' from ' . $booking->start_time . ' to ' . $booking->end_time); |
| 188 | + |
| 189 | + return redirect()->route('book-labs.index')->with('toast_success', 'Booking approved successfully!'); |
| 190 | + } else { |
| 191 | + $booking->rejected = 1; |
| 192 | + $booking->save(); |
| 193 | + |
| 194 | + $body = [ |
| 195 | + 'name' => $booking->name, |
| 196 | + 'email' => $booking->email, |
| 197 | + 'phone' => $booking->phone, |
| 198 | + 'request_type' => 'internal', |
| 199 | + 'department' => 'Manufacturing and Industrial Engineering', |
| 200 | + 'facility' => Lab::find($booking->lab_id)->name, |
| 201 | + 'date' => date('Y-m-d', strtotime($booking->date)), |
| 202 | + 'start_time' => date('h:i a', strtotime($booking->start_time)), |
| 203 | + 'end_time' => date('h:i a', strtotime($booking->end_time)), |
| 204 | + 'reason' => $request->reason, |
| 205 | + ]; |
| 206 | + |
| 207 | + Mail::to($booking->email)->send(new FacilityBookingRejected($body)); |
| 208 | + |
| 209 | + $this->passed('Lab booking rejected for ' . $booking->name . ' ' . ' on ' . $booking->date . ' from ' . $booking->start_time . ' to ' . $booking->end_time); |
| 210 | + |
| 211 | + return redirect()->route('book-labs.index')->with('toast_success', 'Booking rejected successfully!'); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments