Completed
Push — master ( bdb8ac...55db1a )
by Adam
07:17
created

ReservationController::editChooseGuest()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 2
dl 0
loc 31
ccs 0
cts 19
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Interfaces\ManageTableInterface;
6
use App\Http\Requests\ReservationSearchRequest;
7
use App\Models\Guest;
8
use App\Models\Reservation;
9
use App\Models\Room;
10
use App\Services\GuestTableService;
11
use App\Services\ReservationTableService;
12
use App\Services\RoomTableService;
13
use Carbon\Carbon;
14
use Illuminate\Database\Eloquent\ModelNotFoundException;
15
use Illuminate\Support\Facades\Log;
16
use Illuminate\Support\Facades\Session;
17
18
class ReservationController extends Controller implements ManageTableInterface
19
{
20
    protected $reservationTableService;
21
22 14
    public function __construct(ReservationTableService $reservationTableService)
23
    {
24 14
        $this->reservationTableService = $reservationTableService;
25 14
    }
26
27 4
    public function index()
28
    {
29 4
        $title = trans('navigation.all_reservations');
30
31 4
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
32 4
            ->with('guest:id,first_name,last_name')
33 4
            ->with('room:id,number')
34 4
            ->orderBy('date_end', 'DESC')
35 4
            ->paginate($this->getItemsPerPage());
36
37 4
        if ($dataset->isEmpty()) {
38 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
39
        }
40
41
        $viewData = [
42 4
            'columns'       => $this->reservationTableService->getColumns(),
43 4
            'dataset'       => $dataset,
44 4
            'routeName'     => $this->reservationTableService->getRouteName(),
45 4
            'title'         => $title,
46
        ];
47
48 4
        return view('list', $viewData);
49
    }
50
51 2
    public function current()
52
    {
53 2
        $title = trans('navigation.current_reservations');
54
55 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
56 2
            ->with('guest:id,first_name,last_name')
57 2
            ->with('room:id,number')
58 2
            ->getCurrentReservations()
59 2
            ->orderBy('date_end')
60 2
            ->paginate($this->getItemsPerPage());
61
62 2
        if ($dataset->isEmpty()) {
63 1
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
64
        }
65
66
        $viewData = [
67 2
            'columns'       => $this->reservationTableService->getColumns(),
68 2
            'dataset'       => $dataset,
69 2
            'routeName'     => $this->reservationTableService->getRouteName(),
70 2
            'title'         => $title,
71
        ];
72
73 2
        return view('list', $viewData);
74
    }
75
76 2
    public function future()
77
    {
78 2
        $title = trans('navigation.future_reservations');
79
80 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
81 2
            ->with('guest:id,first_name,last_name')
82 2
            ->with('room:id,number')
83 2
            ->getFutureReservations()
84 2
            ->orderBy('date_end')
85 2
            ->paginate($this->getItemsPerPage());
86
87 2
        if ($dataset->isEmpty()) {
88 1
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
89
        }
90
91
        $viewData = [
92 2
            'columns'       => $this->reservationTableService->getColumns(),
93 2
            'dataset'       => $dataset,
94 2
            'routeName'     => $this->reservationTableService->getRouteName(),
95 2
            'title'         => $title,
96
        ];
97
98 2
        return view('list', $viewData);
99
    }
100
101 3
    public function chooseGuest(GuestTableService $guestTableService)
102
    {
103 3
        $title = trans('navigation.choose_guest');
104
105 3
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
106 3
            ->paginate($this->getItemsPerPage());
107
108 3
        if ($dataset->isEmpty()) {
109 1
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
110
        }
111
112
        $viewData = [
113 3
            'columns'         => $guestTableService->getColumns(),
114 3
            'dataset'         => $dataset,
115 3
            'routeName'       => $guestTableService->getRouteName(),
116 3
            'title'           => $title,
117 3
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
118
        ];
119
120 3
        return view('list', $viewData);
121
    }
122
123 3
    public function searchFreeRooms($guestId)
124
    {
125
        try {
126 3
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
127
        } catch (ModelNotFoundException $e) {
128
            return $this->returnBack([
129
                'message'     => trans('general.object_not_found'),
130
                'alert-class' => 'alert-danger',
131
            ]);
132
        }
133
134 3
        $dataset = new Reservation();
135 3
        $dataset->guest()->associate($guest);
136 3
        $title = trans('navigation.search_free_rooms');
137 3
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', $dataset->guest->id);
0 ignored issues
show
Bug introduced by
$dataset->guest->id of type integer is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
138
139 3
        $fiels = $this->getFields();
140 3
        array_unshift($fiels, $this->getGuestField());
141
142
        $viewData = [
143 3
            'dataset'     => $dataset,
144 3
            'fields'      => $fiels,
145 3
            'title'       => $title,
146 3
            'submitRoute' => $submitRoute,
147
        ];
148
149 3
        return view('addedit', $viewData);
150
    }
151
152 2
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
153
    {
154
        try {
155 2
            $guest = Guest::select('id')->findOrFail($guestId);
156
        } catch (ModelNotFoundException $e) {
157
            return $this->returnBack([
158
                'message'     => trans('general.object_not_found'),
159
                'alert-class' => 'alert-danger',
160
            ]);
161
        }
162
163 2
        $data = $request->only(['date_start', 'date_end', 'people']);
164
165 2
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', $guest->id)
0 ignored issues
show
Bug introduced by
$guest->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
166 2
            ->with($data);
167
    }
168
169
    /**
170
     * @param RoomTableService $roomTableService
171
     * @param int              $guestId
172
     *
173
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
174
     */
175 2
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
176
    {
177 2
        if (!$this->isReservationDataInSessionCorrect()) {
178
            return $this->returnBack([
179
                'message'     => trans('general.object_not_found'),
180
                'alert-class' => 'alert-danger',
181
            ]);
182
        }
183
184
        try {
185 2
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
186
        } catch (ModelNotFoundException $e) {
187
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
188
189
            return $this->returnBack([
190
                'message'     => trans('general.object_not_found'),
191
                'alert-class' => 'alert-danger',
192
            ]);
193
        }
194
195 2
        $dateStart = Session::get('date_start');
196 2
        $dateEnd = Session::get('date_end');
197 2
        $people = Session::get('people');
198
199 2
        $dateStart = Carbon::parse($dateStart);
200 2
        $dateEnd = Carbon::parse($dateEnd);
201
202 2
        $title = trans('navigation.choose_room_for').' '.$guest->fullName;
0 ignored issues
show
Bug introduced by
Are you sure trans('navigation.choose_room_for') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
203
204 2
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
205 2
            ->freeRoomsForReservation($dateStart, $dateEnd, $people)
206 2
            ->paginate($this->getItemsPerPage());
207
208 2
        if ($dataset->isEmpty()) {
209 1
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
210
        }
211
212
        $viewData = [
213 2
            'columns'               => $roomTableService->getColumns(),
214 2
            'dataset'               => $dataset,
215 2
            'routeName'             => $roomTableService->getRouteName(),
216 2
            'title'                 => $title,
217 2
            'routeChooseName'       => $this->reservationTableService->getRouteName().'.add',
218 2
            'additionalRouteParams' => $guest->id,
219
        ];
220
221 2
        return view('list', $viewData);
222
    }
223
224 1
    public function add($guestId, $roomId)
225
    {
226 1
        if (!$this->isReservationDataInSessionCorrect()) {
227
            return $this->returnBack([
228
                'message'     => trans('general.object_not_found'),
229
                'alert-class' => 'alert-danger',
230
            ]);
231
        }
232
233 1
        $dateStart = Session::get('date_start');
234 1
        $dateEnd = Session::get('date_end');
235 1
        $people = Session::get('people');
236
237
        try {
238 1
            $guest = Guest::select('id')->findOrFail($guestId);
239 1
            $room = Room::select('id')->findOrFail($roomId);
240
        } catch (ModelNotFoundException $e) {
241
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
242
243
            return $this->returnBack([
244
                'message'     => trans('general.object_not_found'),
245
                'alert-class' => 'alert-danger',
246
            ]);
247
        }
248
249 1
        $reservation = new Reservation();
250 1
        $reservation->guest_id = $guest->id;
251 1
        $reservation->room_id = $room->id;
252 1
        $reservation->date_start = $dateStart;
253 1
        $reservation->date_end = $dateEnd;
254 1
        $reservation->people = $people;
255
256 1
        $reservation->save();
257
258 1
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
259
260 1
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
261
    }
262
263
    // TODO
264
    public function store(ReservationSearchRequest $request, $objectId = null)
265
    {
266
        if ($objectId === null) {
267
            $object = new Reservation();
268
        } else {
269
            try {
270
                $object = Reservation::findOrFail($objectId);
271
            } catch (ModelNotFoundException $e) {
272
                return $this->returnBack([
273
                    'message'     => trans('general.object_not_found'),
274
                    'alert-class' => 'alert-danger',
275
                ]);
276
            }
277
        }
278
279
        $object->fill($request->all());
280
        $object->save();
281
282
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
283
            ->with([
284
                'message'     => trans('general.saved'),
285
                'alert-class' => 'alert-success',
286
            ]);
287
    }
288
289 1
    public function delete($objectId)
290
    {
291
        try {
292 1
            $object = Reservation::findOrFail($objectId);
293
        } catch (ModelNotFoundException $e) {
294
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
295
296
            return response()->json($data);
297
        }
298
299 1
        $object->delete();
300
301 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
302
303 1
        return response()->json($data);
304
    }
305
306 2
    public function showEditForm($objectId)
307
    {
308
        try {
309 2
            $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
310 2
            ->with('guest:id,first_name,last_name')
311 2
            ->with('room:id,number')
312 2
            ->findOrFail($objectId);
313 1
        } catch (ModelNotFoundException $e) {
314 1
            return $this->returnBack([
315 1
                'message'     => trans('general.object_not_found'),
316 1
                'alert-class' => 'alert-danger',
317
            ]);
318
        }
319
320 1
        $title = trans('navigation.edit_reservation');
321 1
        $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
322
323 1
        $fiels = $this->getFields();
324 1
        array_unshift($fiels, $this->getGuestField(), $this->getRoomField(), $this->getActionButtons());
325
326
        $viewData = [
327 1
            'dataset'     => $dataset,
328 1
            'fields'      => $fiels,
329 1
            'title'       => $title,
330 1
            'submitRoute' => $submitRoute,
331 1
            'routeName'   => $this->reservationTableService->getRouteName(),
332
        ];
333
334 1
        return view('addedit', $viewData);
335
    }
336
337
    public function editChooseGuest(GuestTableService $guestTableService, $reservationId)
338
    {
339
        try {
340
            $reservation = Reservation::select('id', 'guest_id')->findOrFail($reservationId);
341
        } catch (ModelNotFoundException $e) {
342
            return $this->returnBack([
343
                'message'     => trans('general.object_not_found'),
344
                'alert-class' => 'alert-danger',
345
            ]);
346
        }
347
348
        $title = trans('navigation.change_guest_for_reservation');
349
350
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
351
            ->whereNotIn('id', [$reservation->guest_id])
352
            ->paginate($this->getItemsPerPage());
353
354
        if ($dataset->isEmpty()) {
355
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
356
        }
357
358
        $viewData = [
359
            'columns'               => $guestTableService->getColumns(),
360
            'dataset'               => $dataset,
361
            'routeName'             => $guestTableService->getRouteName(),
362
            'title'                 => $title,
363
            'routeChooseName'       => $this->reservationTableService->getRouteName().'.edit_change_guest',
364
            'additionalRouteParams' => $reservation->id,
365
        ];
366
367
        return view('list', $viewData);
368
    }
369
370
    public function editChangeGuest($reservationId, $guestId)
371
    {
372
        try {
373
            $reservation = Reservation::select('id')->findOrFail($reservationId);
374
            $guest = Guest::select('id')->findOrFail($guestId);
375
        } catch (ModelNotFoundException $e) {
376
            return $this->returnBack([
377
                'message'     => trans('general.object_not_found'),
378
                'alert-class' => 'alert-danger',
379
            ]);
380
        }
381
382
        $reservation->guest_id = $guest->id;
383
        $reservation->save();
384
385
        return redirect()->route($this->reservationTableService->getRouteName().'.editform', $reservation->id)
0 ignored issues
show
Bug introduced by
$reservation->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

385
        return redirect()->route($this->reservationTableService->getRouteName().'.editform', /** @scrutinizer ignore-type */ $reservation->id)
Loading history...
386
            ->with([
387
                'message'     => trans('general.saved'),
388
                'alert-class' => 'alert-success',
389
            ]);
390
    }
391
392
    public function editChooseRoom(RoomTableService $roomTableService, $reservationId)
393
    {
394
        try {
395
            $reservation = Reservation::select('id', 'guest_id', 'date_start', 'date_end', 'people')
396
                ->findOrFail($reservationId);
397
        } catch (ModelNotFoundException $e) {
398
            return $this->returnBack([
399
                'message'     => trans('general.object_not_found'),
400
                'alert-class' => 'alert-danger',
401
            ]);
402
        }
403
404
        $dateStart = Carbon::parse($reservation->date_start);
405
        $dateEnd = Carbon::parse($reservation->date_end);
406
407
        $title = trans('navigation.change_room_for_reservation');
408
409
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
410
            ->freeRoomsForReservation($dateStart, $dateEnd, $reservation->people)
411
            ->paginate($this->getItemsPerPage());
412
413
        if ($dataset->isEmpty()) {
414
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
415
        }
416
417
        $viewData = [
418
            'columns'               => $roomTableService->getColumns(),
419
            'dataset'               => $dataset,
420
            'routeName'             => $roomTableService->getRouteName(),
421
            'title'                 => $title,
422
            'routeChooseName'       => $this->reservationTableService->getRouteName().'.edit_change_room',
423
            'additionalRouteParams' => $reservation->id,
424
        ];
425
426
        return view('list', $viewData);
427
    }
428
429
    public function editChangeRoom($reservationId, $roomId)
430
    {
431
        try {
432
            $reservation = Reservation::select('id')->findOrFail($reservationId);
433
            $room = Room::select('id')->findOrFail($roomId);
434
        } catch (ModelNotFoundException $e) {
435
            return $this->returnBack([
436
                'message'     => trans('general.object_not_found'),
437
                'alert-class' => 'alert-danger',
438
            ]);
439
        }
440
441
        $reservation->room_id = $room->id;
442
        $reservation->save();
443
444
        return redirect()->route($this->reservationTableService->getRouteName().'.editform', $reservation->id)
0 ignored issues
show
Bug introduced by
$reservation->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

444
        return redirect()->route($this->reservationTableService->getRouteName().'.editform', /** @scrutinizer ignore-type */ $reservation->id)
Loading history...
445
            ->with([
446
                'message'     => trans('general.saved'),
447
                'alert-class' => 'alert-success',
448
            ]);
449
    }
450
451 4
    public function getGuestField()
452
    {
453
        return [
454 4
            'id'    => 'guest',
455 4
            'title' => trans('general.guest'),
456 4
            'value' => function (Reservation $data) {
457 4
                return $data->guest->full_name;
458 4
            },
459
            'optional' => [
460
                'readonly' => 'readonly',
461
            ],
462
        ];
463
    }
464
465 1
    public function getRoomField()
466
    {
467
        return [
468 1
            'id'    => 'room',
469 1
            'title' => trans('general.room'),
470 1
            'value' => function (Reservation $data) {
471 1
                return $data->room->number;
472 1
            },
473
            'optional' => [
474
                'readonly' => 'readonly',
475
            ],
476
        ];
477
    }
478
479 1
    public function getActionButtons()
480
    {
481
        return [
482 1
            'id'         => 'action_buttons',
483 1
            'type'       => 'buttons',
484
            'buttons'    => [
485
                [
486 1
                    'value' => function () {
487 1
                        return 'Zmień gościa';
488 1
                    },
489 1
                    'route_name'  => 'reservation.edit_choose_guest',
490 1
                    'route_param' => function (Reservation $data) {
491 1
                        return $data->id;
492 1
                    },
493
                    'optional' => [
494
                        'class' => 'btn btn-primary',
495
                    ],
496
                ],
497
                [
498 1
                    'value' => function () {
499 1
                        return 'Zmień pokój';
500 1
                    },
501 1
                    'route_name'  => 'reservation.edit_choose_room',
502 1
                    'route_param' => function (Reservation $data) {
503 1
                        return $data->id;
504 1
                    },
505
                    'optional' => [
506
                        'class' => 'btn btn-primary',
507
                    ],
508
                ],
509
            ],
510
        ];
511
    }
512
513 4
    public function getFields()
514
    {
515
        return [
516
            [
517 4
                'id'    => 'date_start',
518 4
                'title' => trans('general.date_start'),
519 4
                'value' => function (Reservation $data) {
520 4
                    return $data->date_start;
521 4
                },
522 4
                'type'     => 'text',
523
                'optional' => [
524
                    'required'    => 'required',
525
                    'class'       => 'datepicker start-date',
526
                    'placeholder' => 'dd.mm.rrrr',
527
                ],
528
            ],
529
            [
530 4
                'id'    => 'date_end',
531 4
                'title' => trans('general.date_end'),
532 4
                'value' => function (Reservation $data) {
533 4
                    return $data->date_end;
534 4
                },
535 4
                'type'     => 'text',
536
                'optional' => [
537
                    'required'    => 'required',
538
                    'class'       => 'datepicker end-date',
539
                    'placeholder' => 'dd.mm.rrrr',
540
                ],
541
            ],
542
            [
543 4
                'id'    => 'people',
544 4
                'title' => trans('general.number_of_people'),
545 4
                'value' => function (Reservation $data) {
546 4
                    return $data->people ?: 1;
547 4
                },
548 4
                'type'     => 'number',
549
                'optional' => [
550
                    'required' => 'required',
551
                    'min'      => '1',
552
                ],
553
            ],
554
        ];
555
    }
556
557 2
    private function isReservationDataInSessionCorrect()
558
    {
559 2
        if (!Session::has(['date_start', 'date_end', 'people'])) {
560
            Log::error('Missing one of Session keys: date_start, date_end, people');
561
562
            return false;
563
        }
564
565 2
        Session::reflash();
566
567 2
        return true;
568
    }
569
}
570