Completed
Push — master ( 55db1a...3a439b )
by Adam
07:24
created

ReservationController::editChangeRoom()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 2
dl 0
loc 26
ccs 0
cts 17
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', 'capacity')->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
        if ($room->capacity < $people) {
250
            return $this->returnBack([
251
                'message'     => trans('general.people_exceeds_room_capacity'),
252
                'alert-class' => 'alert-danger',
253
            ]);
254
        }
255
256 1
        $reservation = new Reservation();
257 1
        $reservation->guest_id = $guest->id;
258 1
        $reservation->room_id = $room->id;
259 1
        $reservation->date_start = $dateStart;
260 1
        $reservation->date_end = $dateEnd;
261 1
        $reservation->people = $people;
262
263 1
        $reservation->save();
264
265 1
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
266
267 1
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
268
    }
269
270
    // TODO
271
    public function store(ReservationSearchRequest $request, $objectId = null)
272
    {
273
        if ($objectId === null) {
274
            $object = new Reservation();
275
        } else {
276
            try {
277
                $object = Reservation::findOrFail($objectId);
278
            } catch (ModelNotFoundException $e) {
279
                return $this->returnBack([
280
                    'message'     => trans('general.object_not_found'),
281
                    'alert-class' => 'alert-danger',
282
                ]);
283
            }
284
        }
285
286
        // TODO
287
        if ($object->room->capacity < $request->input('people')) {
288
            return $this->returnBack([
289
                'message'     => trans('general.people_exceeds_room_capacity'),
290
                'alert-class' => 'alert-danger',
291
            ]);
292
        }
293
294
        $object->fill($request->all());
295
        $object->save();
296
297
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
298
            ->with([
299
                'message'     => trans('general.saved'),
300
                'alert-class' => 'alert-success',
301
            ]);
302
    }
303
304 1
    public function delete($objectId)
305
    {
306
        try {
307 1
            $object = Reservation::findOrFail($objectId);
308
        } catch (ModelNotFoundException $e) {
309
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
310
311
            return response()->json($data);
312
        }
313
314 1
        $object->delete();
315
316 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
317
318 1
        return response()->json($data);
319
    }
320
321 2
    public function showEditForm($objectId)
322
    {
323
        try {
324 2
            $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
325 2
            ->with('guest:id,first_name,last_name')
326 2
            ->with('room:id,number')
327 2
            ->findOrFail($objectId);
328 1
        } catch (ModelNotFoundException $e) {
329 1
            return $this->returnBack([
330 1
                'message'     => trans('general.object_not_found'),
331 1
                'alert-class' => 'alert-danger',
332
            ]);
333
        }
334
335 1
        $title = trans('navigation.edit_reservation');
336 1
        $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
337
338 1
        $fiels = $this->getFields();
339 1
        array_unshift($fiels, $this->getGuestField(), $this->getRoomField(), $this->getActionButtons());
340
341
        $viewData = [
342 1
            'dataset'     => $dataset,
343 1
            'fields'      => $fiels,
344 1
            'title'       => $title,
345 1
            'submitRoute' => $submitRoute,
346 1
            'routeName'   => $this->reservationTableService->getRouteName(),
347
        ];
348
349 1
        return view('addedit', $viewData);
350
    }
351
352
    public function editChooseGuest(GuestTableService $guestTableService, $reservationId)
353
    {
354
        try {
355
            $reservation = Reservation::select('id', 'guest_id')->findOrFail($reservationId);
356
        } catch (ModelNotFoundException $e) {
357
            return $this->returnBack([
358
                'message'     => trans('general.object_not_found'),
359
                'alert-class' => 'alert-danger',
360
            ]);
361
        }
362
363
        $title = trans('navigation.change_guest_for_reservation');
364
365
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
366
            ->whereNotIn('id', [$reservation->guest_id])
367
            ->paginate($this->getItemsPerPage());
368
369
        if ($dataset->isEmpty()) {
370
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
371
        }
372
373
        $viewData = [
374
            'columns'               => $guestTableService->getColumns(),
375
            'dataset'               => $dataset,
376
            'routeName'             => $guestTableService->getRouteName(),
377
            'title'                 => $title,
378
            'routeChooseName'       => $this->reservationTableService->getRouteName().'.edit_change_guest',
379
            'additionalRouteParams' => $reservation->id,
380
        ];
381
382
        return view('list', $viewData);
383
    }
384
385
    public function editChangeGuest($reservationId, $guestId)
386
    {
387
        try {
388
            $reservation = Reservation::select('id')->findOrFail($reservationId);
389
            $guest = Guest::select('id')->findOrFail($guestId);
390
        } catch (ModelNotFoundException $e) {
391
            return $this->returnBack([
392
                'message'     => trans('general.object_not_found'),
393
                'alert-class' => 'alert-danger',
394
            ]);
395
        }
396
397
        $reservation->guest_id = $guest->id;
398
        $reservation->save();
399
400
        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

400
        return redirect()->route($this->reservationTableService->getRouteName().'.editform', /** @scrutinizer ignore-type */ $reservation->id)
Loading history...
401
            ->with([
402
                'message'     => trans('general.saved'),
403
                'alert-class' => 'alert-success',
404
            ]);
405
    }
406
407
    public function editChooseRoom(RoomTableService $roomTableService, $reservationId)
408
    {
409
        try {
410
            $reservation = Reservation::select('id', 'guest_id', 'date_start', 'date_end', 'people')
411
                ->findOrFail($reservationId);
412
        } catch (ModelNotFoundException $e) {
413
            return $this->returnBack([
414
                'message'     => trans('general.object_not_found'),
415
                'alert-class' => 'alert-danger',
416
            ]);
417
        }
418
419
        $dateStart = Carbon::parse($reservation->date_start);
420
        $dateEnd = Carbon::parse($reservation->date_end);
421
422
        $title = trans('navigation.change_room_for_reservation');
423
424
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
425
            ->freeRoomsForReservation($dateStart, $dateEnd, $reservation->people)
426
            ->paginate($this->getItemsPerPage());
427
428
        if ($dataset->isEmpty()) {
429
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
430
        }
431
432
        $viewData = [
433
            'columns'               => $roomTableService->getColumns(),
434
            'dataset'               => $dataset,
435
            'routeName'             => $roomTableService->getRouteName(),
436
            'title'                 => $title,
437
            'routeChooseName'       => $this->reservationTableService->getRouteName().'.edit_change_room',
438
            'additionalRouteParams' => $reservation->id,
439
        ];
440
441
        return view('list', $viewData);
442
    }
443
444
    public function editChangeRoom($reservationId, $roomId)
445
    {
446
        try {
447
            $reservation = Reservation::select('id', 'people')->findOrFail($reservationId);
448
            $room = Room::select('id', 'capacity')->findOrFail($roomId);
449
        } catch (ModelNotFoundException $e) {
450
            return $this->returnBack([
451
                'message'     => trans('general.object_not_found'),
452
                'alert-class' => 'alert-danger',
453
            ]);
454
        }
455
456
        if ($room->capacity < $reservation->people) {
457
            return $this->returnBack([
458
                'message'     => trans('general.people_exceeds_room_capacity'),
459
                'alert-class' => 'alert-danger',
460
            ]);
461
        }
462
463
        $reservation->room_id = $room->id;
464
        $reservation->save();
465
466
        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

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