Passed
Push — master ( 88b2ff...66be1b )
by Adam
06:14
created

ReservationController::getSearchFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 49
ccs 0
cts 24
cp 0
crap 2
rs 9.2258
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Carbon\Carbon;
6
use App\Http\Interfaces\ManageTableInterface;
7
// TODO: Change request
8
use App\Http\Requests\GuestRequest;
9
use App\Http\Requests\ReservationSearchRequest;
10
use App\Models\Guest;
11
use App\Models\Reservation;
12
use App\Models\Room;
13
use App\Services\GuestTableService;
14
use App\Services\ReservationTableService;
15
use App\Services\RoomTableService;
16
use Illuminate\Database\Eloquent\ModelNotFoundException;
17
use Illuminate\Database\Query\Builder;
18
use Illuminate\Support\Facades\Log;
19
use Illuminate\Support\Facades\Session;
20
21
class ReservationController extends Controller implements ManageTableInterface
22
{
23
    protected $reservationTableService;
24
25 3
    public function __construct(ReservationTableService $reservationTableService)
26
    {
27 3
        $this->reservationTableService = $reservationTableService;
28 3
    }
29
30 3
    public function index()
31
    {
32 3
        $title = trans('general.reservations');
33
34 3
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
35 3
            ->with('guest:id,first_name,last_name')
36 3
            ->with('room:id,number')
37 3
            ->paginate($this->getItemsPerPage());
38
39 3
        if ($dataset->isEmpty()) {
40 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
41
        }
42
43
        $viewData = [
44 3
            'columns'       => $this->reservationTableService->getColumns(),
45 3
            'dataset'       => $dataset,
46 3
            'routeName'     => $this->reservationTableService->getRouteName(),
47 3
            'title'         => $title,
48
            // TODO
49 3
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.reservation') can also be of type array; however, parameter $str of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
Loading history...
50
        ];
51
52 3
        return view('list', $viewData);
53
    }
54
55
    public function chooseGuest(GuestTableService $guestTableService)
56
    {
57
        $title = trans('navigation.choose_guest');
58
59
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
60
            ->paginate($this->getItemsPerPage());
61
62
        if ($dataset->isEmpty()) {
63
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
64
        }
65
66
        $viewData = [
67
            'columns'         => $guestTableService->getColumns(),
68
            'dataset'         => $dataset,
69
            'routeName'       => $guestTableService->getRouteName(),
70
            'title'           => $title,
71
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
72
        ];
73
74
        return view('list', $viewData);
75
    }
76
77
    public function searchFreeRooms($guestId)
78
    {
79
        try {
80
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
81
        } catch (ModelNotFoundException $e) {
82
            return $this->returnBack([
83
                'message'     => trans('general.object_not_found'),
84
                'alert-class' => 'alert-danger',
85
            ]);
86
        }
87
88
        $dataset = new Reservation();
89
        $dataset->guest()->associate($guest);
90
        $title = trans('navigation.search_free_rooms');
91
        $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

91
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
92
93
        $viewData = [
94
            'dataset'     => $dataset,
95
            'fields'      => $this->getSearchFields(),
96
            'title'       => $title,
97
            'submitRoute' => $submitRoute,
98
        ];
99
100
        return view('addedit', $viewData);
101
    }
102
103
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
104
    {
105
        try {
106
            $guest = Guest::select('id')->findOrFail($guestId);
107
        } catch (ModelNotFoundException $e) {
108
            return $this->returnBack([
109
                'message'     => trans('general.object_not_found'),
110
                'alert-class' => 'alert-danger',
111
            ]);
112
        }
113
114
        $data = $request->only(['date_start', 'date_end', 'people']);
115
116
        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

116
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
117
            ->with($data);
118
    }
119
120
    /**
121
     * @todo
122
     *
123
     * @param RoomTableService $roomTableService
124
     * @param int              $guestId
125
     *
126
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
127
     */
128
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
129
    {
130
        if (!Session::has(['date_start', 'date_end', 'people'])) {
131
            Log::error('Missing one of Session keys: date_start, date_end, people');
132
133
            return $this->returnBack([
134
                'message'     => trans('general.object_not_found'),
135
                'alert-class' => 'alert-danger',
136
            ]);
137
        }
138
139
        Session::reflash();
140
141
        try {
142
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
143
        } catch (ModelNotFoundException $e) {
144
            // TODO: logger helper
145
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
146
147
            return $this->returnBack([
148
                'message'     => trans('general.object_not_found'),
149
                'alert-class' => 'alert-danger',
150
            ]);
151
        }
152
153
        $dateStart = Session::get('date_start');
154
        $dateEnd = Session::get('date_end');
155
        $people = Session::get('people');
156
157
        $dateStart = Carbon::parse($dateStart);
158
        $dateEnd = Carbon::parse($dateEnd);
159
160
        $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

160
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
161
162
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
163
            ->whereNotIn('id', function (Builder $query) use ($dateStart, $dateEnd) {
164
                $query->select('room_id')->from('reservations')
165
                    ->where('date_start', '<', $dateEnd)
166
                    ->where('date_end', '>', $dateStart);
167
            })
168
            ->where('capacity', '>=', $people)
169
            ->orderBy('capacity')
170
            ->paginate($this->getItemsPerPage());
171
172
        if ($dataset->isEmpty()) {
173
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
174
        }
175
176
        $viewData = [
177
            'columns'                => $roomTableService->getColumns(),
178
            'dataset'                => $dataset,
179
            'routeName'              => $roomTableService->getRouteName(),
180
            'title'                  => $title,
181
            'routeChooseName'        => $this->reservationTableService->getRouteName().'.add',
182
            'secondRouteChooseParam' => $guest->id,
183
        ];
184
185
        return view('list', $viewData);
186
    }
187
188
    public function add($roomId, $guestId)
189
    {
190
        if (!Session::has(['date_start', 'date_end', 'people'])) {
191
            Log::error('Missing one of Session keys: date_start, date_end, people');
192
193
            return $this->returnBack([
194
                'message'     => trans('general.object_not_found'),
195
                'alert-class' => 'alert-danger',
196
            ]);
197
        }
198
199
        Session::reflash();
200
201
        try {
202
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
0 ignored issues
show
Unused Code introduced by
The assignment to $guest is dead and can be removed.
Loading history...
203
        } catch (ModelNotFoundException $e) {
204
            // TODO: logger helper
205
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
206
207
            return $this->returnBack([
208
                'message'     => trans('general.object_not_found'),
209
                'alert-class' => 'alert-danger',
210
            ]);
211
        }
212
213
        $dateStart = Session::get('date_start');
214
        $dateEnd = Session::get('date_end');
215
        $people = Session::get('people');
216
217
        try {
218
            $guest = Guest::select('id')->findOrFail($guestId);
219
            $room = Room::select('id')->findOrFail($roomId);
220
        } catch (ModelNotFoundException $e) {
221
            return $this->returnBack([
222
                'message'     => trans('general.object_not_found'),
223
                'alert-class' => 'alert-danger',
224
            ]);
225
        }
226
227
        $reservation = new Reservation;
228
        $reservation->guest_id = $guest->id;
229
        $reservation->room_id = $room->id;
230
        $reservation->date_start = $dateStart;
231
        $reservation->date_end = $dateEnd;
232
        $reservation->people = $people;
233
234
        $reservation->save();
235
236
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
237
238
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
239
    }
240
241
    // TODO
242
    public function store(GuestRequest $request, $objectId = null)
243
    {
244
        if ($objectId === null) {
245
            $object = new Reservation();
246
        } else {
247
            try {
248
                $object = Reservation::findOrFail($objectId);
249
            } catch (ModelNotFoundException $e) {
250
                return $this->returnBack([
251
                    'message'     => trans('general.object_not_found'),
252
                    'alert-class' => 'alert-danger',
253
                ]);
254
            }
255
        }
256
257
        $object->fill($request->all());
258
        $object->save();
259
260
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
261
            ->with([
262
                'message'     => trans('general.saved'),
263
                'alert-class' => 'alert-success',
264
            ]);
265
    }
266
267
    public function delete($objectId)
268
    {
269
        Reservation::destroy($objectId);
270
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
271
272
        return response()->json($data);
273
    }
274
275
    // TODO
276 1
    public function showAddEditForm($objectId = null)
277
    {
278 1
        if ($objectId === null) {
279
            $dataset = new Reservation();
280
            $title = trans('navigation.add_reservation');
281
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
282
        } else {
283
            try {
284 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
285 1
                ->with('guest:id,first_name,last_name')
286 1
                ->with('room:id,number')
287 1
                ->findOrFail($objectId);
288 1
            } catch (ModelNotFoundException $e) {
289 1
                return $this->returnBack([
290 1
                    'message'     => trans('general.object_not_found'),
291 1
                    'alert-class' => 'alert-danger',
292
                ]);
293
            }
294
295
            $title = trans('navigation.edit_reservation');
296
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
297
        }
298
299
        $viewData = [
300
            'dataset'     => $dataset,
301
            'fields'      => $this->getFields(),
302
            'title'       => $title,
303
            'submitRoute' => $submitRoute,
304
            'routeName'   => $this->reservationTableService->getRouteName(),
305
        ];
306
307
        return view('addedit', $viewData);
308
    }
309
310
    public function getSearchFields()
311
    {
312
        return [
313
            [
314
                'id'    => 'guest',
315
                'title' => trans('general.guest'),
316
                'value' => function (Reservation $data) {
317
                    return $data->guest->full_name;
318
                },
319
                'optional' => [
320
                    'readonly' => 'readonly',
321
                ],
322
            ],
323
            [
324
                'id'    => 'date_start',
325
                'title' => trans('general.date_start'),
326
                'value' => function (Reservation $data) {
327
                    return $data->date_start;
328
                },
329
                'type'     => 'text',
330
                'optional' => [
331
                    'required'    => 'required',
332
                    'class'       => 'datepicker start-date',
333
                    'placeholder' => 'dd.mm.rrrr',
334
                ],
335
            ],
336
            [
337
                'id'    => 'date_end',
338
                'title' => trans('general.date_end'),
339
                'value' => function (Reservation $data) {
340
                    return $data->date_end;
341
                },
342
                'type'     => 'text',
343
                'optional' => [
344
                    'required'    => 'required',
345
                    'class'       => 'datepicker end-date',
346
                    'placeholder' => 'dd.mm.rrrr',
347
                ],
348
            ],
349
            [
350
                'id'    => 'people',
351
                'title' => trans('general.number_of_people'),
352
                'value' => function () {
353
                    return 1;
354
                },
355
                'type'     => 'number',
356
                'optional' => [
357
                    'required' => 'required',
358
                    'min'      => '1',
359
                ],
360
            ],
361
        ];
362
    }
363
364
    // TODO
365
    public function getFields()
366
    {
367
        return [
368
            [
369
                'id'    => 'first_name',
370
                'title' => trans('general.first_name'),
371
                'value' => function (Reservation $data) {
372
                    return $data->first_name;
0 ignored issues
show
Bug introduced by
The property first_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
373
                },
374
                'optional' => [
375
                    'required' => 'required',
376
                ],
377
            ],
378
            [
379
                'id'    => 'last_name',
380
                'title' => trans('general.last_name'),
381
                'value' => function (Reservation $data) {
382
                    return $data->last_name;
0 ignored issues
show
Bug introduced by
The property last_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
383
                },
384
                'optional' => [
385
                    'required' => 'required',
386
                ],
387
            ],
388
            [
389
                'id'    => 'address',
390
                'title' => trans('general.address'),
391
                'value' => function (Reservation $data) {
392
                    return $data->address;
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
393
                },
394
                'optional' => [
395
                    'required' => 'required',
396
                ],
397
            ],
398
            [
399
                'id'    => 'zip_code',
400
                'title' => trans('general.zip_code'),
401
                'value' => function (Reservation $data) {
402
                    return $data->zip_code;
0 ignored issues
show
Bug introduced by
The property zip_code does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
403
                },
404
                'optional' => [
405
                    'required'    => 'required',
406
                    'placeholder' => '00-000',
407
                ],
408
            ],
409
            [
410
                'id'    => 'place',
411
                'title' => trans('general.place'),
412
                'value' => function (Reservation $data) {
413
                    return $data->place;
0 ignored issues
show
Bug introduced by
The property place does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
414
                },
415
                'optional' => [
416
                    'required' => 'required',
417
                ],
418
            ],
419
            [
420
                'id'    => 'PESEL',
421
                'title' => trans('general.PESEL'),
422
                'value' => function (Reservation $data) {
423
                    return $data->PESEL;
0 ignored issues
show
Bug introduced by
The property PESEL does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
424
                },
425
                'optional' => [
426
                    'required'    => 'required',
427
                    'placeholder' => '12345654321',
428
                ],
429
            ],
430
            [
431
                'id'    => 'contact',
432
                'title' => trans('general.contact'),
433
                'value' => function (Reservation $data) {
434
                    return $data->contact;
0 ignored issues
show
Bug introduced by
The property contact does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
435
                },
436
                'type'     => 'textarea',
437
                'optional' => [
438
                    'placeholder' => trans('general.contact_placeholder'),
439
                ],
440
            ],
441
        ];
442
    }
443
}
444