1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Rinvex\Bookings\Models\BookableBooking; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
11
|
|
|
|
12
|
|
|
trait Bookable |
13
|
|
|
{ |
14
|
|
|
use BookingScopes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Register a saved model event with the dispatcher. |
18
|
|
|
* |
19
|
|
|
* @param \Closure|string $callback |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
abstract public static function saved($callback); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Register a deleted model event with the dispatcher. |
27
|
|
|
* |
28
|
|
|
* @param \Closure|string $callback |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
abstract public static function deleted($callback); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Define a polymorphic one-to-many relationship. |
36
|
|
|
* |
37
|
|
|
* @param string $related |
38
|
|
|
* @param string $name |
39
|
|
|
* @param string $type |
|
|
|
|
40
|
|
|
* @param string $id |
|
|
|
|
41
|
|
|
* @param string $localKey |
|
|
|
|
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
44
|
|
|
*/ |
45
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the booking model name. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
abstract public static function getBookingModel(): string; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the rate model name. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
abstract public static function getRateModel(): string; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the availability model name. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
abstract public static function getAvailabilityModel(): string; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Boot the Bookable trait for the model. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public static function bootBookable() |
74
|
|
|
{ |
75
|
|
|
static::deleted(function (self $model) { |
76
|
|
|
$model->bookings()->delete(); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Attach the given bookings to the model. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
84
|
|
|
* @param mixed $bookings |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function setBookingsAttribute($bookings): void |
89
|
|
|
{ |
90
|
|
|
static::saved(function (self $model) use ($bookings) { |
|
|
|
|
91
|
|
|
$this->bookings()->sync($bookings); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Attach the given rates to the model. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
99
|
|
|
* @param mixed $rates |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function setRatesAttribute($rates): void |
104
|
|
|
{ |
105
|
|
|
static::saved(function (self $model) use ($rates) { |
|
|
|
|
106
|
|
|
$this->rates()->sync($rates); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Attach the given availabilities to the model. |
112
|
|
|
* |
113
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
114
|
|
|
* @param mixed $availabilities |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function setAvailabilitiesAttribute($availabilities): void |
119
|
|
|
{ |
120
|
|
|
static::saved(function (self $model) use ($availabilities) { |
|
|
|
|
121
|
|
|
$this->availabilities()->sync($availabilities); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* The resource may have many bookings. |
127
|
|
|
* |
128
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
129
|
|
|
*/ |
130
|
|
|
public function bookings(): MorphMany |
131
|
|
|
{ |
132
|
|
|
return $this->morphMany(static::getBookingModel(), 'bookable', 'bookable_type', 'bookable_id'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get bookings by the given customer. |
137
|
|
|
* |
138
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
141
|
|
|
*/ |
142
|
|
|
public function bookingsBy(Model $customer): MorphMany |
143
|
|
|
{ |
144
|
|
|
return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The resource may have many availabilities. |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
151
|
|
|
*/ |
152
|
|
|
public function availabilities(): MorphMany |
153
|
|
|
{ |
154
|
|
|
return $this->morphMany(static::getAvailabilityModel(), 'bookable', 'bookable_type', 'bookable_id'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* The resource may have many rates. |
159
|
|
|
* |
160
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
161
|
|
|
*/ |
162
|
|
|
public function rates(): MorphMany |
163
|
|
|
{ |
164
|
|
|
return $this->morphMany(static::getRateModel(), 'bookable', 'bookable_type', 'bookable_id'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Book the model for the given customer at the given dates with the given price. |
169
|
|
|
* |
170
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
171
|
|
|
* @param string $startsAt |
172
|
|
|
* @param string $endsAt |
173
|
|
|
* |
174
|
|
|
* @return \Rinvex\Bookings\Models\BookableBooking |
175
|
|
|
*/ |
176
|
|
|
public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking |
177
|
|
|
{ |
178
|
|
|
return $this->bookings()->create([ |
179
|
|
|
'bookable_id' => static::getKey(), |
180
|
|
|
'bookable_type' => static::getMorphClass(), |
181
|
|
|
'customer_id' => $customer->getKey(), |
182
|
|
|
'customer_type' => $customer->getMorphClass(), |
183
|
|
|
'starts_at' => (new Carbon($startsAt))->toDateTimeString(), |
184
|
|
|
'ends_at' => (new Carbon($endsAt))->toDateTimeString(), |
185
|
|
|
]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.