|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Query\Builder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* App\Models\Room. |
|
11
|
|
|
* |
|
12
|
|
|
* @property int $id |
|
13
|
|
|
* @property string $number |
|
14
|
|
|
* @property int $floor |
|
15
|
|
|
* @property int $capacity |
|
16
|
|
|
* @property float $price |
|
17
|
|
|
* @property string|null $comment |
|
18
|
|
|
* @property \Illuminate\Support\Carbon $created_at |
|
19
|
|
|
* @property \Illuminate\Support\Carbon $updated_at |
|
20
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Guest[] $guests |
|
21
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Reservation[] $reservations |
|
22
|
|
|
* |
|
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room currentlyFreeRooms() |
|
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room currentlyOccupiedRooms() |
|
25
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room freeRoomsForReservation($dateStart, $dateEnd, $people) |
|
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereCapacity($value) |
|
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereComment($value) |
|
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereCreatedAt($value) |
|
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereFloor($value) |
|
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereId($value) |
|
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereNumber($value) |
|
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room wherePrice($value) |
|
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Room whereUpdatedAt($value) |
|
34
|
|
|
* @mixin \Eloquent |
|
35
|
|
|
*/ |
|
36
|
|
|
class Room extends Model |
|
37
|
|
|
{ |
|
38
|
|
|
protected $fillable = [ |
|
39
|
|
|
'number', 'floor', 'capacity', 'price', 'comment', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
35 |
|
public function setPriceAttribute($value) |
|
43
|
|
|
{ |
|
44
|
35 |
|
$value = str_replace(',', '.', $value); |
|
45
|
35 |
|
$this->attributes['price'] = round($value, 2); |
|
46
|
35 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
50
|
|
|
* @param $dateStart |
|
51
|
|
|
* @param $dateEnd |
|
52
|
|
|
* @param $people |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
55
|
|
|
*/ |
|
56
|
|
|
public function scopeFreeRoomsForReservation($query, $dateStart, $dateEnd, $people) |
|
57
|
|
|
{ |
|
58
|
5 |
|
return $query->whereNotIn('id', function (Builder $query) use ($dateStart, $dateEnd) { |
|
59
|
5 |
|
$query->select('room_id')->from('reservations') |
|
60
|
5 |
|
->where('date_start', '<', $dateEnd) |
|
61
|
5 |
|
->where('date_end', '>', $dateStart); |
|
62
|
5 |
|
}) |
|
63
|
5 |
|
->where('capacity', '>=', $people) |
|
64
|
5 |
|
->orderBy('capacity'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function scopeCurrentlyFreeRooms($query) |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $query->whereNotIn('id', function (Builder $query) { |
|
70
|
2 |
|
$query->select('room_id')->from('reservations') |
|
71
|
2 |
|
->where('date_start', '<=', Carbon::today()) |
|
72
|
2 |
|
->where('date_end', '>', Carbon::today()); |
|
73
|
2 |
|
}); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function scopeCurrentlyOccupiedRooms($query) |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $query->whereIn('id', function (Builder $query) { |
|
79
|
2 |
|
$query->select('room_id')->from('reservations') |
|
80
|
2 |
|
->where('date_start', '<=', Carbon::today()) |
|
81
|
2 |
|
->where('date_end', '>', Carbon::today()); |
|
82
|
2 |
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
public function isFree($dateStart, $dateEnd) |
|
86
|
|
|
{ |
|
87
|
5 |
|
return !$this->reservations() |
|
88
|
5 |
|
->where('date_start', '<', $dateEnd) |
|
89
|
5 |
|
->where('date_end', '>', $dateStart) |
|
90
|
5 |
|
->exists(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
7 |
|
public function reservations() |
|
94
|
|
|
{ |
|
95
|
7 |
|
return $this->hasMany('App\Models\Reservation'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function guests() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->belongsToMany('App\Models\Guest', 'reservations'); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|