1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Rinvex\Bookings\Models\Booking; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Rinvex\Bookings\Models\BookingRate; |
11
|
|
|
use Rinvex\Bookings\Models\BookingAvailability; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
13
|
|
|
|
14
|
|
|
trait Bookable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Get all bookings. |
18
|
|
|
* |
19
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
20
|
|
|
*/ |
21
|
|
|
public function bookings(): MorphMany |
22
|
|
|
{ |
23
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking'), 'bookable'); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get bookings by the given user. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
30
|
|
|
*/ |
31
|
|
|
public function bookingsBy(Model $user): MorphMany |
32
|
|
|
{ |
33
|
|
|
return $this->bookings()->where('user_id', $user->getKey()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get past bookings. |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
40
|
|
|
*/ |
41
|
|
|
public function pastBookings(): MorphMany |
42
|
|
|
{ |
43
|
|
|
return $this->bookings() |
44
|
|
|
->whereNull('cancelled_at') |
45
|
|
|
->whereNotNull('ends_at') |
46
|
|
|
->where('ends_at', '<', Carbon::now()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get future bookings. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
53
|
|
|
*/ |
54
|
|
|
public function futureBookings(): MorphMany |
55
|
|
|
{ |
56
|
|
|
return $this->bookings() |
57
|
|
|
->whereNull('cancelled_at') |
58
|
|
|
->whereNotNull('starts_at') |
59
|
|
|
->where('starts_at', '>', Carbon::now()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get current bookings. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
66
|
|
|
*/ |
67
|
|
|
public function currentBookings(): MorphMany |
68
|
|
|
{ |
69
|
|
|
return $this->bookings() |
70
|
|
|
->whereNull('cancelled_at') |
71
|
|
|
->whereNotNull('starts_at') |
72
|
|
|
->whereNotNull('ends_at') |
73
|
|
|
->where('starts_at', '<', Carbon::now()) |
74
|
|
|
->where('ends_at', '>', Carbon::now()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get cancelled bookings. |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
81
|
|
|
*/ |
82
|
|
|
public function cancelledBookings(): MorphMany |
83
|
|
|
{ |
84
|
|
|
return $this->bookings() |
85
|
|
|
->whereNotNull('cancelled_at'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get bookings starts before the given date. |
90
|
|
|
* |
91
|
|
|
* @param string $date |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
94
|
|
|
*/ |
95
|
|
|
public function bookingsStartsBefore(string $date): MorphMany |
96
|
|
|
{ |
97
|
|
|
return $this->bookings() |
98
|
|
|
->whereNull('cancelled_at') |
99
|
|
|
->whereNotNull('starts_at') |
100
|
|
|
->where('starts_at', '<', new Carbon($date)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get bookings starts after the given date. |
105
|
|
|
* |
106
|
|
|
* @param string $date |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
109
|
|
|
*/ |
110
|
|
|
public function bookingsStartsAfter(string $date): MorphMany |
111
|
|
|
{ |
112
|
|
|
return $this->bookings() |
113
|
|
|
->whereNull('cancelled_at') |
114
|
|
|
->whereNotNull('starts_at') |
115
|
|
|
->where('starts_at', '>', new Carbon($date)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get bookings starts between the given dates. |
120
|
|
|
* |
121
|
|
|
* @param string $starts |
122
|
|
|
* @param string $ends |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
125
|
|
|
*/ |
126
|
|
|
public function bookingsStartsBetween(string $starts, string $ends): MorphMany |
127
|
|
|
{ |
128
|
|
|
return $this->bookings() |
129
|
|
|
->whereNull('cancelled_at') |
130
|
|
|
->whereNotNull('starts_at') |
131
|
|
|
->where('starts_at', '>', new Carbon($starts)) |
132
|
|
|
->where('starts_at', '<', new Carbon($ends)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get bookings ends before the given date. |
137
|
|
|
* |
138
|
|
|
* @param string $date |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
141
|
|
|
*/ |
142
|
|
|
public function bookingsEndsBefore(string $date): MorphMany |
143
|
|
|
{ |
144
|
|
|
return $this->bookings() |
145
|
|
|
->whereNull('cancelled_at') |
146
|
|
|
->whereNotNull('ends_at') |
147
|
|
|
->where('ends_at', '<', new Carbon($date)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get bookings ends after the given date. |
152
|
|
|
* |
153
|
|
|
* @param string $date |
154
|
|
|
* |
155
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
156
|
|
|
*/ |
157
|
|
|
public function bookingsEndsAfter(string $date): MorphMany |
158
|
|
|
{ |
159
|
|
|
return $this->bookings() |
160
|
|
|
->whereNull('cancelled_at') |
161
|
|
|
->whereNotNull('ends_at') |
162
|
|
|
->where('ends_at', '>', new Carbon($date)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get bookings ends between the given dates. |
167
|
|
|
* |
168
|
|
|
* @param string $starts |
169
|
|
|
* @param string $ends |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
172
|
|
|
*/ |
173
|
|
|
public function bookingsEndsBetween(string $starts, string $ends): MorphMany |
174
|
|
|
{ |
175
|
|
|
return $this->bookings() |
176
|
|
|
->whereNull('cancelled_at') |
177
|
|
|
->whereNotNull('ends_at') |
178
|
|
|
->where('ends_at', '>', new Carbon($starts)) |
179
|
|
|
->where('ends_at', '<', new Carbon($ends)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get bookings cancelled before the given date. |
184
|
|
|
* |
185
|
|
|
* @param string $date |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
188
|
|
|
*/ |
189
|
|
|
public function bookingsCancelledBefore(string $date): MorphMany |
190
|
|
|
{ |
191
|
|
|
return $this->bookings() |
192
|
|
|
->whereNotNull('cancelled_at') |
193
|
|
|
->where('cancelled_at', '<', new Carbon($date)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get bookings cancelled after the given date. |
198
|
|
|
* |
199
|
|
|
* @param string $date |
200
|
|
|
* |
201
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
202
|
|
|
*/ |
203
|
|
|
public function bookingsCancelledAfter(string $date): MorphMany |
204
|
|
|
{ |
205
|
|
|
return $this->bookings() |
206
|
|
|
->whereNotNull('cancelled_at') |
207
|
|
|
->where('cancelled_at', '>', new Carbon($date)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get bookings cancelled between the given dates. |
212
|
|
|
* |
213
|
|
|
* @param string $starts |
214
|
|
|
* @param string $ends |
215
|
|
|
* |
216
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
217
|
|
|
*/ |
218
|
|
|
public function bookingsCancelledBetween(string $starts, string $ends): MorphMany |
219
|
|
|
{ |
220
|
|
|
return $this->bookings() |
221
|
|
|
->whereNotNull('cancelled_at') |
222
|
|
|
->where('cancelled_at', '>', new Carbon($starts)) |
223
|
|
|
->where('cancelled_at', '<', new Carbon($ends)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get all rates. |
228
|
|
|
* |
229
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
230
|
|
|
*/ |
231
|
|
|
public function rates(): MorphMany |
232
|
|
|
{ |
233
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking_rate'), 'bookable'); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get all availabilities. |
238
|
|
|
* |
239
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
240
|
|
|
*/ |
241
|
|
|
public function availabilities(): MorphMany |
242
|
|
|
{ |
243
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking_availability'), 'bookable'); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Book the model for the given user at the given dates with the given price. |
248
|
|
|
* |
249
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
250
|
|
|
* @param string $starts |
251
|
|
|
* @param string $ends |
252
|
|
|
* @param float $price |
253
|
|
|
* |
254
|
|
|
* @return \Rinvex\Bookings\Models\Booking |
255
|
|
|
*/ |
256
|
|
|
public function newBooking(Model $user, string $starts, string $ends, float $price): Booking |
257
|
|
|
{ |
258
|
|
|
return $this->bookings()->create([ |
259
|
|
|
'bookable_id' => static::getKey(), |
260
|
|
|
'bookable_type' => static::class, |
261
|
|
|
'user_id' => $user->getKey(), |
262
|
|
|
'starts_at' => $starts, |
263
|
|
|
'ends_at' => $ends, |
264
|
|
|
'price' => $price, |
265
|
|
|
]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Create a new booking rate. |
270
|
|
|
* |
271
|
|
|
* @param string $percentage |
272
|
|
|
* @param string $operator |
273
|
|
|
* @param int $amount |
274
|
|
|
* |
275
|
|
|
* @return \Rinvex\Bookings\Models\BookingRate |
276
|
|
|
*/ |
277
|
|
|
public function newRate(string $percentage, string $operator, int $amount): BookingRate |
278
|
|
|
{ |
279
|
|
|
return $this->rates()->create([ |
280
|
|
|
'bookable_id' => static::getKey(), |
281
|
|
|
'bookable_type' => static::class, |
282
|
|
|
'percentage' => $percentage, |
283
|
|
|
'operator' => $operator, |
284
|
|
|
'amount' => $amount, |
285
|
|
|
]); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Create a new booking availability. |
290
|
|
|
* |
291
|
|
|
* @param string $day |
292
|
|
|
* @param string $starts |
293
|
|
|
* @param string $ends |
294
|
|
|
* @param float|null $price |
295
|
|
|
* |
296
|
|
|
* @return \Rinvex\Bookings\Models\BookingAvailability |
297
|
|
|
*/ |
298
|
|
|
public function newAvailability(string $day, string $starts, string $ends, float $price = null): BookingAvailability |
299
|
|
|
{ |
300
|
|
|
return $this->rates()->create([ |
301
|
|
|
'bookable_id' => static::getKey(), |
302
|
|
|
'bookable_type' => static::class, |
303
|
|
|
'day' => $day, |
304
|
|
|
'starts_at' => $starts, |
305
|
|
|
'ends_at' => $ends, |
306
|
|
|
'price' => $price, |
307
|
|
|
]); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.