Passed
Push — master ( 61e8f9...ccc374 )
by Adam
06:58
created

ReservationController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 5
cts 8
cp 0.625
crap 2.2109
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 showEditForm($objectId)
308
    {
309
        try {
310 1
            $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
311 1
            ->with('guest:id,first_name,last_name')
312 1
            ->with('room:id,number')
313 1
            ->findOrFail($objectId);
314 1
        } catch (ModelNotFoundException $e) {
315 1
            return $this->returnBack([
316 1
                'message'     => trans('general.object_not_found'),
317 1
                'alert-class' => 'alert-danger',
318
            ]);
319
        }
320
321
        $title = trans('navigation.edit_reservation');
322
        $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
323
324
        $fiels = $this->getFields();
325
        array_unshift($fiels, $this->getGuestField(), $this->getRoomField(), $this->getActionButtons());
326
327
        $viewData = [
328
            'dataset'     => $dataset,
329
            'fields'      => $fiels,
330
            'title'       => $title,
331
            'submitRoute' => $submitRoute,
332
            'routeName'   => $this->reservationTableService->getRouteName(),
333
        ];
334
335
        return view('addedit', $viewData);
336
    }
337
338 3
    public function getGuestField()
339
    {
340
        return [
341 3
            'id'    => 'guest',
342 3
            'title' => trans('general.guest'),
343 3
            'value' => function (Reservation $data) {
344 3
                return $data->guest->full_name;
345 3
            },
346
            'optional' => [
347
                'readonly' => 'readonly',
348
            ],
349
        ];
350
    }
351
352
    public function getRoomField()
353
    {
354
        return [
355
            'id'    => 'room',
356
            'title' => trans('general.room'),
357
            'value' => function (Reservation $data) {
358
                return $data->room->number;
359
            },
360
            'optional' => [
361
                'readonly' => 'readonly',
362
            ],
363
        ];
364
    }
365
366
    public function getActionButtons()
367
    {
368
        return [
369
            'id'         => 'action_buttons',
370
            'type'       => 'buttons',
371
            'buttons'    => [
372
                [
373
                    'value' => function () {
374
                        return 'Zmień gościa';
375
                    },
376
                    'route_name'  => 'reservation.change_guest',
377
                    'route_param' => function (Reservation $data) {
378
                        return $data->id;
379
                    },
380
                    'optional' => [
381
                        'class' => 'btn btn-primary',
382
                    ],
383
                ],
384
                [
385
                    'value' => function () {
386
                        return 'Zmień pokój';
387
                    },
388
                    'route_name'  => 'reservation.change_room',
389
                    'route_param' => function (Reservation $data) {
390
                        return $data->id;
391
                    },
392
                    'optional' => [
393
                        'class' => 'btn btn-primary',
394
                    ],
395
                ],
396
            ],
397
        ];
398
    }
399
400 3
    public function getFields()
401
    {
402
        return [
403
            [
404 3
                'id'    => 'date_start',
405 3
                'title' => trans('general.date_start'),
406 3
                'value' => function (Reservation $data) {
407 3
                    return $data->date_start;
408 3
                },
409 3
                'type'     => 'text',
410
                'optional' => [
411
                    'required'    => 'required',
412
                    'class'       => 'datepicker start-date',
413
                    'placeholder' => 'dd.mm.rrrr',
414
                ],
415
            ],
416
            [
417 3
                'id'    => 'date_end',
418 3
                'title' => trans('general.date_end'),
419 3
                'value' => function (Reservation $data) {
420 3
                    return $data->date_end;
421 3
                },
422 3
                'type'     => 'text',
423
                'optional' => [
424
                    'required'    => 'required',
425
                    'class'       => 'datepicker end-date',
426
                    'placeholder' => 'dd.mm.rrrr',
427
                ],
428
            ],
429
            [
430 3
                'id'    => 'people',
431 3
                'title' => trans('general.number_of_people'),
432 3
                'value' => function (Reservation $data) {
433 3
                    return $data->people ?: 1;
434 3
                },
435 3
                'type'     => 'number',
436
                'optional' => [
437
                    'required' => 'required',
438
                    'min'      => '1',
439
                ],
440
            ],
441
        ];
442
    }
443
444 2
    private function isReservationDataInSessionCorrect()
445
    {
446 2
        if (!Session::has(['date_start', 'date_end', 'people'])) {
447
            Log::error('Missing one of Session keys: date_start, date_end, people');
448
449
            return false;
450
        }
451
452 2
        Session::reflash();
453
454 2
        return true;
455
    }
456
}
457