Passed
Push — master ( d069e2...ff7564 )
by Adam
06:54
created

ReservationController::chooseGuest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 2
rs 9.4285
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 11
    public function __construct(ReservationTableService $reservationTableService)
23
    {
24 11
        $this->reservationTableService = $reservationTableService;
25 11
    }
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 1
    public function current()
52
    {
53 1
        $title = trans('navigation.current_reservations');
54
55 1
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
56 1
            ->with('guest:id,first_name,last_name')
57 1
            ->with('room:id,number')
58 1
            ->getCurrentReservations()
59 1
            ->orderBy('date_end')
60 1
            ->paginate($this->getItemsPerPage());
61
62 1
        if ($dataset->isEmpty()) {
63 1
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
64
        }
65
66
        $viewData = [
67 1
            'columns'       => $this->reservationTableService->getColumns(),
68 1
            'dataset'       => $dataset,
69 1
            'routeName'     => $this->reservationTableService->getRouteName(),
70 1
            'title'         => $title,
71
        ];
72
73 1
        return view('list', $viewData);
74
    }
75
76 1
    public function future()
77
    {
78 1
        $title = trans('navigation.future_reservations');
79
80 1
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
81 1
            ->with('guest:id,first_name,last_name')
82 1
            ->with('room:id,number')
83 1
            ->getFutureReservations()
84 1
            ->orderBy('date_end')
85 1
            ->paginate($this->getItemsPerPage());
86
87 1
        if ($dataset->isEmpty()) {
88 1
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
89
        }
90
91
        $viewData = [
92 1
            'columns'       => $this->reservationTableService->getColumns(),
93 1
            'dataset'       => $dataset,
94 1
            'routeName'     => $this->reservationTableService->getRouteName(),
95 1
            'title'         => $title,
96
        ];
97
98 1
        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
            // TODO: logger helper
188
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
189
190
            return $this->returnBack([
191
                'message'     => trans('general.object_not_found'),
192
                'alert-class' => 'alert-danger',
193
            ]);
194
        }
195
196 2
        $dateStart = Session::get('date_start');
197 2
        $dateEnd = Session::get('date_end');
198 2
        $people = Session::get('people');
199
200 2
        $dateStart = Carbon::parse($dateStart);
201 2
        $dateEnd = Carbon::parse($dateEnd);
202
203 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

203
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
204
205 2
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
206 2
            ->freeRoomsForReservation($dateStart, $dateEnd, $people)
207 2
            ->paginate($this->getItemsPerPage());
208
209 2
        if ($dataset->isEmpty()) {
210 1
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
211
        }
212
213
        $viewData = [
214 2
            'columns'                => $roomTableService->getColumns(),
215 2
            'dataset'                => $dataset,
216 2
            'routeName'              => $roomTableService->getRouteName(),
217 2
            'title'                  => $title,
218 2
            'routeChooseName'        => $this->reservationTableService->getRouteName().'.add',
219 2
            'secondRouteChooseParam' => $guest->id,
220
        ];
221
222 2
        return view('list', $viewData);
223
    }
224
225 1
    public function add($roomId, $guestId)
226
    {
227 1
        if (!$this->isReservationDataInSessionCorrect()) {
228
            return $this->returnBack([
229
                'message'     => trans('general.object_not_found'),
230
                'alert-class' => 'alert-danger',
231
            ]);
232
        }
233
234 1
        $dateStart = Session::get('date_start');
235 1
        $dateEnd = Session::get('date_end');
236 1
        $people = Session::get('people');
237
238
        try {
239 1
            $guest = Guest::select('id')->findOrFail($guestId);
240 1
            $room = Room::select('id')->findOrFail($roomId);
241
        } catch (ModelNotFoundException $e) {
242
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
243
244
            return $this->returnBack([
245
                'message'     => trans('general.object_not_found'),
246
                'alert-class' => 'alert-danger',
247
            ]);
248
        }
249
250 1
        $reservation = new Reservation();
251 1
        $reservation->guest_id = $guest->id;
252 1
        $reservation->room_id = $room->id;
253 1
        $reservation->date_start = $dateStart;
254 1
        $reservation->date_end = $dateEnd;
255 1
        $reservation->people = $people;
256
257 1
        $reservation->save();
258
259 1
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
260
261 1
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
262
    }
263
264
    // TODO
265
    public function store(ReservationSearchRequest $request, $objectId = null)
266
    {
267
        if ($objectId === null) {
268
            $object = new Reservation();
269
        } else {
270
            try {
271
                $object = Reservation::findOrFail($objectId);
272
            } catch (ModelNotFoundException $e) {
273
                return $this->returnBack([
274
                    'message'     => trans('general.object_not_found'),
275
                    'alert-class' => 'alert-danger',
276
                ]);
277
            }
278
        }
279
280
        $object->fill($request->all());
281
        $object->save();
282
283
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
284
            ->with([
285
                'message'     => trans('general.saved'),
286
                'alert-class' => 'alert-success',
287
            ]);
288
    }
289
290 1
    public function delete($objectId)
291
    {
292
        try {
293 1
            $object = Reservation::findOrFail($objectId);
294
        } catch (ModelNotFoundException $e) {
295
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
296
297
            return response()->json($data);
298
        }
299
300 1
        $object->delete();
301
302 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
303
304 1
        return response()->json($data);
305
    }
306
307 1
    public function showAddEditForm($objectId = null)
308
    {
309 1
        if ($objectId === null) {
310
            $dataset = new Reservation();
311
            $title = trans('navigation.add_reservation');
312
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
313
        } else {
314
            try {
315 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
316 1
                ->with('guest:id,first_name,last_name')
317 1
                ->with('room:id,number')
318 1
                ->findOrFail($objectId);
319 1
            } catch (ModelNotFoundException $e) {
320 1
                return $this->returnBack([
321 1
                    'message'     => trans('general.object_not_found'),
322 1
                    'alert-class' => 'alert-danger',
323
                ]);
324
            }
325
326
            $title = trans('navigation.edit_reservation');
327
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
328
        }
329
330
        $fiels = $this->getFields();
331
        array_unshift($fiels, $this->getGuestField(), $this->getRoomField(), $this->getActionButtons());
332
333
        $viewData = [
334
            'dataset'     => $dataset,
335
            'fields'      => $fiels,
336
            'title'       => $title,
337
            'submitRoute' => $submitRoute,
338
            'routeName'   => $this->reservationTableService->getRouteName(),
339
        ];
340
341
        return view('addedit', $viewData);
342
    }
343
344 3
    public function getGuestField()
345
    {
346
        return [
347 3
            'id'    => 'guest',
348 3
            'title' => trans('general.guest'),
349 3
            'value' => function (Reservation $data) {
350 3
                return $data->guest->full_name;
351 3
            },
352
            'optional' => [
353
                'readonly' => 'readonly',
354
            ],
355
        ];
356
    }
357
358
    public function getRoomField()
359
    {
360
        return [
361
            'id'    => 'room',
362
            'title' => trans('general.room'),
363
            'value' => function (Reservation $data) {
364
                return $data->room->number;
365
            },
366
            'optional' => [
367
                'readonly' => 'readonly',
368
            ],
369
        ];
370
    }
371
372
    public function getActionButtons() {
373
        return [
374
            'id'    => 'action_buttons',
375
            'type'       => 'buttons',
376
            'buttons' => [
377
                [
378
                    'value' => function () {
379
                        return 'Zmień gościa';
380
                    },
381
                    'route_name'  => 'reservation.change_guest',
382
                    'route_param' => function (Reservation $data) {
383
                        return $data->id;
384
                    },
385
                    'optional' => [
386
                        'class' => 'btn btn-primary',
387
                    ],
388
                ],
389
                [
390
                    'value' => function () {
391
                        return 'Zmień pokój';
392
                    },
393
                    'route_name' => 'reservation.change_room',
394
                    'route_param' => function (Reservation $data) {
395
                        return $data->id;
396
                    },
397
                    'optional' => [
398
                        'class' => 'btn btn-primary',
399
                    ],
400
                ],
401
            ],
402
        ];
403
    }
404
405 3
    public function getFields()
406
    {
407
        return [
408
            [
409 3
                'id'    => 'date_start',
410 3
                'title' => trans('general.date_start'),
411 3
                'value' => function (Reservation $data) {
412 3
                    return $data->date_start;
413 3
                },
414 3
                'type'     => 'text',
415
                'optional' => [
416
                    'required'    => 'required',
417
                    'class'       => 'datepicker start-date',
418
                    'placeholder' => 'dd.mm.rrrr',
419
                ],
420
            ],
421
            [
422 3
                'id'    => 'date_end',
423 3
                'title' => trans('general.date_end'),
424 3
                'value' => function (Reservation $data) {
425 3
                    return $data->date_end;
426 3
                },
427 3
                'type'     => 'text',
428
                'optional' => [
429
                    'required'    => 'required',
430
                    'class'       => 'datepicker end-date',
431
                    'placeholder' => 'dd.mm.rrrr',
432
                ],
433
            ],
434
            [
435 3
                'id'    => 'people',
436 3
                'title' => trans('general.number_of_people'),
437 3
                'value' => function (Reservation $data) {
438 3
                    return $data->people ?: 1;
439 3
                },
440 3
                'type'     => 'number',
441
                'optional' => [
442
                    'required' => 'required',
443
                    'min'      => '1',
444
                ],
445
            ],
446
        ];
447
    }
448
449 2
    private function isReservationDataInSessionCorrect()
450
    {
451 2
        if (!Session::has(['date_start', 'date_end', 'people'])) {
452
            Log::error('Missing one of Session keys: date_start, date_end, people');
453
454
            return false;
455
        }
456
457 2
        Session::reflash();
458
459 2
        return true;
460
    }
461
}
462