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