Completed
Push — master ( 326962...ed7fcd )
by Adam
05:52
created

ReservationController::getSearchFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 43
ccs 0
cts 24
cp 0
crap 2
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
// TODO
7
use App\Http\Requests\GuestRequest;
8
use App\Http\Requests\ReservationSearchRequest;
9
use App\Models\Guest;
10
use App\Models\Reservation;
11
use App\Models\Room;
12
use App\Services\GuestTableService;
13
use App\Services\ReservationTableService;
14
use App\Services\RoomTableService;
15
use Illuminate\Database\Eloquent\ModelNotFoundException;
16
use Illuminate\Support\Facades\Session;
17
18
class ReservationController extends Controller implements ManageTableInterface
19
{
20
    protected $reservationTableService;
21
22 2
    public function __construct(ReservationTableService $reservationTableService)
23
    {
24 2
        $this->reservationTableService = $reservationTableService;
25 2
    }
26
27 2
    public function index()
28
    {
29 2
        $title = trans('general.reservations');
30
31 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
32 2
            ->with('guest:id,first_name,last_name')
33 2
            ->with('room:id,number')
34 2
            ->paginate($this->getItemsPerPage());
35
36 2
        if ($dataset->isEmpty()) {
37 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
38
        }
39
40
        $viewData = [
41 2
            'columns'       => $this->reservationTableService->getColumns(),
42 2
            'dataset'       => $dataset,
43 2
            'routeName'     => $this->reservationTableService->getRouteName(),
44 2
            'title'         => $title,
45
            // TODO
46 2
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
47
        ];
48
49 2
        return view('list', $viewData);
50
    }
51
52
    public function chooseGuest(GuestTableService $guestTableService)
53
    {
54
        $title = trans('navigation.choose_guest');
55
56
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
57
            ->paginate($this->getItemsPerPage());
58
59
        if ($dataset->isEmpty()) {
60
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
61
        }
62
63
        $viewData = [
64
            'columns'         => $guestTableService->getColumns(),
65
            'dataset'         => $dataset,
66
            'routeName'       => $guestTableService->getRouteName(),
67
            'title'           => $title,
68
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
69
        ];
70
71
        return view('list', $viewData);
72
    }
73
74
    public function searchFreeRooms($guestId)
75
    {
76
        try {
77
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
78
        } catch (ModelNotFoundException $e) {
79
            return $this->returnBack([
80
                'message'     => trans('general.object_not_found'),
81
                'alert-class' => 'alert-danger',
82
            ]);
83
        }
84
85
        $dataset = new Reservation();
86
        $dataset->guest()->associate($guest);
87
        $title = trans('navigation.search_free_rooms');
88
        $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

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

113
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
114
            ->with($data);
115
    }
116
117
    // TODO
118
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
119
    {
120
        try {
121
            $guest = Guest::select('id')->findOrFail($guestId);
0 ignored issues
show
Unused Code introduced by
The assignment to $guest is dead and can be removed.
Loading history...
122
        } catch (ModelNotFoundException $e) {
123
            return $this->returnBack([
124
                'message'     => trans('general.object_not_found'),
125
                'alert-class' => 'alert-danger',
126
            ]);
127
        }
128
129
        if (!Session::has(['date_start', 'date_end', 'people'])) {
130
            dd('blaaaa');
131
        }
132
133
        // TODO: walidacja
134
        $dateStart = Session::get('date_start');
135
        $dateEnd = Session::get('date_end');
136
        $people = Session::get('people');
137
138
        Session::reflash();
139
140
        $title = trans('navigation.choose_room');
141
142
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
143
            ->whereNotIn('id', function($query) use ($dateStart, $dateEnd) {
144
                $query->select('room_id')->from('reservations')
145
                    ->where('date_start', '<', $dateEnd)
146
                    ->where('date_end', '>', $dateStart);
147
            })
148
            ->where('capacity', '>=', $people)
149
            ->orderBy('capacity')
150
            ->paginate($this->getItemsPerPage());
151
152
        if ($dataset->isEmpty()) {
153
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
154
        }
155
156
        $viewData = [
157
            'columns'         => $roomTableService->getColumns(),
158
            'dataset'         => $dataset,
159
            'routeName'       => $roomTableService->getRouteName(),
160
            'title'           => $title,
161
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
162
        ];
163
164
        return view('list', $viewData);
165
    }
166
167
    // TODO
168
    public function store(GuestRequest $request, $objectId = null)
169
    {
170
        if ($objectId === null) {
171
            $object = new Reservation();
172
        } else {
173
            try {
174
                $object = Reservation::findOrFail($objectId);
175
            } catch (ModelNotFoundException $e) {
176
                return $this->returnBack([
177
                    'message'     => trans('general.object_not_found'),
178
                    'alert-class' => 'alert-danger',
179
                ]);
180
            }
181
        }
182
183
        $object->fill($request->all());
184
        $object->save();
185
186
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
187
            ->with([
188
                'message'     => trans('general.saved'),
189
                'alert-class' => 'alert-success',
190
            ]);
191
    }
192
193
    public function delete($objectId)
194
    {
195
        Reservation::destroy($objectId);
196
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
197
198
        return response()->json($data);
199
    }
200
201
    // TODO
202 1
    public function showAddEditForm($objectId = null)
203
    {
204 1
        if ($objectId === null) {
205
            $dataset = new Reservation();
206
            $title = trans('navigation.add_reservation');
207
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
208
        } else {
209
            try {
210 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
211 1
                ->with('guest:id,first_name,last_name')
212 1
                ->with('room:id,number')
213 1
                ->findOrFail($objectId);
214 1
            } catch (ModelNotFoundException $e) {
215 1
                return $this->returnBack([
216 1
                    'message'     => trans('general.object_not_found'),
217 1
                    'alert-class' => 'alert-danger',
218
                ]);
219
            }
220
221
            $title = trans('navigation.edit_reservation');
222
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
223
        }
224
225
        $viewData = [
226
            'dataset'     => $dataset,
227
            'fields'      => $this->getFields(),
228
            'title'       => $title,
229
            'submitRoute' => $submitRoute,
230
            'routeName'   => $this->reservationTableService->getRouteName(),
231
        ];
232
233
        return view('addedit', $viewData);
234
    }
235
236
    // TODO
237
    public function getSearchFields()
238
    {
239
        return [
240
            [
241
                'id'    => 'guest',
242
                'title' => trans('general.guest'),
243
                'value' => function (Reservation $data) {
244
                    return $data->guest->full_name;
245
                },
246
                'optional' => [
247
                    'readonly' => 'readonly',
248
                ],
249
            ],
250
            [
251
                'id'    => 'date_start',
252
                'title' => trans('general.date_start'),
253
                'value' => function (Reservation $data) {
254
                    return $data->date_start;
255
                },
256
                'type'     => 'date',
257
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
258
                    'required' => 'required',
259
                ],*/
260
            ],
261
            [
262
                'id'    => 'date_end',
263
                'title' => trans('general.date_end'),
264
                'value' => function (Reservation $data) {
265
                    return $data->date_end;
266
                },
267
                'type'     => 'date',
268
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
269
                    'required' => 'required',
270
                ],*/
271
            ],
272
            [
273
                'id'    => 'people',
274
                'title' => trans('general.number_of_people'),
275
                'value' => function () {
276
                    // TODO
277
                    return 1;
278
                },
279
                'type'     => 'number',
280
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
281
                    'required' => 'required',
282
                ],*/
283
            ],
284
        ];
285
    }
286
287
    // TODO
288
    public function getFields()
289
    {
290
        return [
291
            [
292
                'id'    => 'first_name',
293
                'title' => trans('general.first_name'),
294
                'value' => function (Reservation $data) {
295
                    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...
296
                },
297
                'optional' => [
298
                    'required' => 'required',
299
                ],
300
            ],
301
            [
302
                'id'    => 'last_name',
303
                'title' => trans('general.last_name'),
304
                'value' => function (Reservation $data) {
305
                    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...
306
                },
307
                'optional' => [
308
                    'required' => 'required',
309
                ],
310
            ],
311
            [
312
                'id'    => 'address',
313
                'title' => trans('general.address'),
314
                'value' => function (Reservation $data) {
315
                    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...
316
                },
317
                'optional' => [
318
                    'required' => 'required',
319
                ],
320
            ],
321
            [
322
                'id'    => 'zip_code',
323
                'title' => trans('general.zip_code'),
324
                'value' => function (Reservation $data) {
325
                    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...
326
                },
327
                'optional' => [
328
                    'required'    => 'required',
329
                    'placeholder' => '00-000',
330
                ],
331
            ],
332
            [
333
                'id'    => 'place',
334
                'title' => trans('general.place'),
335
                'value' => function (Reservation $data) {
336
                    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...
337
                },
338
                'optional' => [
339
                    'required' => 'required',
340
                ],
341
            ],
342
            [
343
                'id'    => 'PESEL',
344
                'title' => trans('general.PESEL'),
345
                'value' => function (Reservation $data) {
346
                    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...
347
                },
348
                'optional' => [
349
                    'required'    => 'required',
350
                    'placeholder' => '12345654321',
351
                ],
352
            ],
353
            [
354
                'id'    => 'contact',
355
                'title' => trans('general.contact'),
356
                'value' => function (Reservation $data) {
357
                    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...
358
                },
359
                'type'     => 'textarea',
360
                'optional' => [
361
                    'placeholder' => trans('general.contact_placeholder'),
362
                ],
363
            ],
364
        ];
365
    }
366
}
367