1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Models; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
12
|
|
|
use Rinvex\Bookings\Contracts\BookingContract; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Rinvex\Bookings\Models\Booking. |
17
|
|
|
* |
18
|
|
|
* @property int $id |
19
|
|
|
* @property int $bookable_id |
20
|
|
|
* @property string $bookable_type |
21
|
|
|
* @property int $customer_id |
22
|
|
|
* @property string $customer_type |
23
|
|
|
* @property \Carbon\Carbon $starts_at |
24
|
|
|
* @property \Carbon\Carbon $ends_at |
25
|
|
|
* @property float $price |
26
|
|
|
* @property array $price_equation |
27
|
|
|
* @property \Carbon\Carbon $cancelled_at |
28
|
|
|
* @property string $notes |
29
|
|
|
* @property \Carbon\Carbon|null $created_at |
30
|
|
|
* @property \Carbon\Carbon|null $updated_at |
31
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $bookable |
32
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $customer |
33
|
|
|
* |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking cancelled() |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking cancelledAfter($date) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking cancelledBefore($date) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking cancelledBetween($starts, $ends) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking current() |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking endsAfter($date) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking endsBefore($date) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking endsBetween($starts, $ends) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking future() |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking ofBookable(\Illuminate\Database\Eloquent\Model $bookable) |
|
|
|
|
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking ofCustomer(\Illuminate\Database\Eloquent\Model $customer) |
|
|
|
|
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking past() |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking startsAfter($date) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking startsBefore($date) |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking startsBetween($starts, $ends) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereBookableId($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereBookableType($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereCancelledAt($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereCreatedAt($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereCustomerId($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereCustomerType($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereEndsAt($value) |
56
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereId($value) |
57
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereNotes($value) |
58
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking wherePrice($value) |
59
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking wherePriceEquation($value) |
60
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereStartsAt($value) |
61
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Booking whereUpdatedAt($value) |
62
|
|
|
* @mixin \Eloquent |
63
|
|
|
*/ |
64
|
|
|
class Booking extends Model implements BookingContract |
65
|
|
|
{ |
66
|
|
|
use ValidatingTrait; |
67
|
|
|
use CacheableEloquent; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected $fillable = [ |
73
|
|
|
'bookable_id', |
74
|
|
|
'bookable_type', |
75
|
|
|
'customer_id', |
76
|
|
|
'customer_type', |
77
|
|
|
'starts_at', |
78
|
|
|
'ends_at', |
79
|
|
|
'price', |
80
|
|
|
'price_equation', |
81
|
|
|
'cancelled_at', |
82
|
|
|
'notes', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
protected $casts = [ |
89
|
|
|
'bookable_id' => 'integer', |
90
|
|
|
'bookable_type' => 'string', |
91
|
|
|
'customer_id' => 'integer', |
92
|
|
|
'customer_type' => 'string', |
93
|
|
|
'starts_at' => 'datetime', |
94
|
|
|
'ends_at' => 'datetime', |
95
|
|
|
'price' => 'float', |
96
|
|
|
'price_equation' => 'json', |
97
|
|
|
'cancelled_at' => 'datetime', |
98
|
|
|
'notes' => 'string', |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
protected $observables = [ |
105
|
|
|
'validating', |
106
|
|
|
'validated', |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The default rules that the model will validate against. |
111
|
|
|
* |
112
|
|
|
* @var array |
113
|
|
|
*/ |
114
|
|
|
protected $rules = []; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Whether the model should throw a |
118
|
|
|
* ValidationException if it fails validation. |
119
|
|
|
* |
120
|
|
|
* @var bool |
121
|
|
|
*/ |
122
|
|
|
protected $throwValidationExceptions = true; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create a new Eloquent model instance. |
126
|
|
|
* |
127
|
|
|
* @param array $attributes |
128
|
|
|
*/ |
129
|
|
|
public function __construct(array $attributes = []) |
130
|
|
|
{ |
131
|
|
|
parent::__construct($attributes); |
132
|
|
|
|
133
|
|
|
$this->setTable(config('rinvex.bookings.tables.bookings')); |
134
|
|
|
$this->setRules([ |
135
|
|
|
'bookable_id' => 'required|integer', |
136
|
|
|
'bookable_type' => 'required|string', |
137
|
|
|
'customer_id' => 'required|integer', |
138
|
|
|
'customer_type' => 'required|string', |
139
|
|
|
'starts_at' => 'nullable|date', |
140
|
|
|
'ends_at' => 'nullable|date', |
141
|
|
|
'price' => 'required|numeric', |
142
|
|
|
'price_equation' => 'json', |
143
|
|
|
'cancelled_at' => 'nullable|date', |
144
|
|
|
'notes' => 'nullable|string|max:10000', |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the owning bookable. |
150
|
|
|
* |
151
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
152
|
|
|
*/ |
153
|
|
|
public function bookable(): MorphTo |
154
|
|
|
{ |
155
|
|
|
return $this->morphTo(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the owning customer. |
160
|
|
|
* |
161
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
162
|
|
|
*/ |
163
|
|
|
public function customer(): MorphTo |
164
|
|
|
{ |
165
|
|
|
return $this->morphTo(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get bookings of the given bookable. |
170
|
|
|
* |
171
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
172
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
173
|
|
|
* |
174
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
175
|
|
|
*/ |
176
|
|
|
public function scopeOfBookable(Builder $builder, Model $bookable): Builder |
177
|
|
|
{ |
178
|
|
|
return $builder->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get bookings of the given customer. |
183
|
|
|
* |
184
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
185
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
188
|
|
|
*/ |
189
|
|
|
public function scopeOfCustomer(Builder $builder, Model $customer): Builder |
190
|
|
|
{ |
191
|
|
|
return $builder->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the past bookings. |
196
|
|
|
* |
197
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
198
|
|
|
* |
199
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
200
|
|
|
*/ |
201
|
|
|
public function scopePast(Builder $builder): Builder |
202
|
|
|
{ |
203
|
|
|
return $builder->whereNull('cancelled_at') |
204
|
|
|
->whereNotNull('ends_at') |
205
|
|
|
->where('ends_at', '<', Carbon::now()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the future bookings. |
210
|
|
|
* |
211
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
212
|
|
|
* |
213
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
214
|
|
|
*/ |
215
|
|
|
public function scopeFuture(Builder $builder): Builder |
216
|
|
|
{ |
217
|
|
|
return $builder->whereNull('cancelled_at') |
218
|
|
|
->whereNotNull('starts_at') |
219
|
|
|
->where('starts_at', '>', Carbon::now()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the current bookings. |
224
|
|
|
* |
225
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
226
|
|
|
* |
227
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
228
|
|
|
*/ |
229
|
|
|
public function scopeCurrent(Builder $builder): Builder |
230
|
|
|
{ |
231
|
|
|
return $builder->whereNull('cancelled_at') |
232
|
|
|
->whereNotNull('starts_at') |
233
|
|
|
->whereNotNull('ends_at') |
234
|
|
|
->where('starts_at', '<', Carbon::now()) |
235
|
|
|
->where('ends_at', '>', Carbon::now()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the cancelled bookings. |
240
|
|
|
* |
241
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
242
|
|
|
* |
243
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
244
|
|
|
*/ |
245
|
|
|
public function scopeCancelled(Builder $builder): Builder |
246
|
|
|
{ |
247
|
|
|
return $builder->whereNotNull('cancelled_at'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get bookings starts before the given date. |
252
|
|
|
* |
253
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
254
|
|
|
* @param string $date |
255
|
|
|
* |
256
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
257
|
|
|
*/ |
258
|
|
|
public function scopeStartsBefore(Builder $builder, string $date): Builder |
259
|
|
|
{ |
260
|
|
|
return $builder->whereNull('cancelled_at') |
261
|
|
|
->whereNotNull('starts_at') |
262
|
|
|
->where('starts_at', '<', new Carbon($date)); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get bookings starts after the given date. |
267
|
|
|
* |
268
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
269
|
|
|
* @param string $date |
270
|
|
|
* |
271
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
272
|
|
|
*/ |
273
|
|
|
public function scopeStartsAfter(Builder $builder, string $date): Builder |
274
|
|
|
{ |
275
|
|
|
return $builder->whereNull('cancelled_at') |
276
|
|
|
->whereNotNull('starts_at') |
277
|
|
|
->where('starts_at', '>', new Carbon($date)); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get bookings starts between the given dates. |
282
|
|
|
* |
283
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
284
|
|
|
* @param string $starts |
285
|
|
|
* @param string $ends |
286
|
|
|
* |
287
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
288
|
|
|
*/ |
289
|
|
|
public function scopeStartsBetween(Builder $builder, string $starts, string $ends): Builder |
290
|
|
|
{ |
291
|
|
|
return $builder->whereNull('cancelled_at') |
292
|
|
|
->whereNotNull('starts_at') |
293
|
|
|
->where('starts_at', '>', new Carbon($starts)) |
294
|
|
|
->where('starts_at', '<', new Carbon($ends)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Get bookings ends before the given date. |
299
|
|
|
* |
300
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
301
|
|
|
* @param string $date |
302
|
|
|
* |
303
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
304
|
|
|
*/ |
305
|
|
|
public function scopeEndsBefore(Builder $builder, string $date): Builder |
306
|
|
|
{ |
307
|
|
|
return $builder->whereNull('cancelled_at') |
308
|
|
|
->whereNotNull('ends_at') |
309
|
|
|
->where('ends_at', '<', new Carbon($date)); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get bookings ends after the given date. |
314
|
|
|
* |
315
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
316
|
|
|
* @param string $date |
317
|
|
|
* |
318
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
319
|
|
|
*/ |
320
|
|
|
public function scopeEndsAfter(Builder $builder, string $date): Builder |
321
|
|
|
{ |
322
|
|
|
return $builder->whereNull('cancelled_at') |
323
|
|
|
->whereNotNull('ends_at') |
324
|
|
|
->where('ends_at', '>', new Carbon($date)); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Get bookings ends between the given date. |
329
|
|
|
* |
330
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
331
|
|
|
* @param string $starts |
332
|
|
|
* @param string $ends |
333
|
|
|
* |
334
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
335
|
|
|
*/ |
336
|
|
|
public function scopeEndsBetween(Builder $builder, string $starts, string $ends): Builder |
337
|
|
|
{ |
338
|
|
|
return $builder->whereNull('cancelled_at') |
339
|
|
|
->whereNotNull('ends_at') |
340
|
|
|
->where('ends_at', '>', new Carbon($starts)) |
341
|
|
|
->where('ends_at', '<', new Carbon($ends)); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Get bookings cancelled before the given date. |
346
|
|
|
* |
347
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
348
|
|
|
* @param string $date |
349
|
|
|
* |
350
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
351
|
|
|
*/ |
352
|
|
|
public function scopeCancelledBefore(Builder $builder, string $date): Builder |
353
|
|
|
{ |
354
|
|
|
return $builder->whereNotNull('cancelled_at') |
355
|
|
|
->where('cancelled_at', '<', new Carbon($date)); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Get bookings cancelled after the given date. |
360
|
|
|
* |
361
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
362
|
|
|
* @param string $date |
363
|
|
|
* |
364
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
365
|
|
|
*/ |
366
|
|
|
public function scopeCancelledAfter(Builder $builder, string $date): Builder |
367
|
|
|
{ |
368
|
|
|
return $builder->whereNotNull('cancelled_at') |
369
|
|
|
->where('cancelled_at', '>', new Carbon($date)); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Get bookings cancelled between the given dates. |
374
|
|
|
* |
375
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
376
|
|
|
* @param string $starts |
377
|
|
|
* @param string $ends |
378
|
|
|
* |
379
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
380
|
|
|
*/ |
381
|
|
|
public function scopeCancelledBetween(Builder $builder, string $starts, string $ends): Builder |
382
|
|
|
{ |
383
|
|
|
return $builder->whereNotNull('cancelled_at') |
384
|
|
|
->where('cancelled_at', '>', new Carbon($starts)) |
385
|
|
|
->where('cancelled_at', '<', new Carbon($ends)); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Check if the booking is cancelled. |
390
|
|
|
* |
391
|
|
|
* @return bool |
392
|
|
|
*/ |
393
|
|
|
public function isCancelled(): bool |
394
|
|
|
{ |
395
|
|
|
return (bool) $this->cancelled_at; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Check if the booking is past. |
400
|
|
|
* |
401
|
|
|
* @return bool |
402
|
|
|
*/ |
403
|
|
|
public function isPast(): bool |
404
|
|
|
{ |
405
|
|
|
return ! $this->isCancelled() && $this->ends_at->isPast(); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Check if the booking is future. |
410
|
|
|
* |
411
|
|
|
* @return bool |
412
|
|
|
*/ |
413
|
|
|
public function isFuture(): bool |
414
|
|
|
{ |
415
|
|
|
return ! $this->isCancelled() && $this->starts_at->isFuture(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Check if the booking is current. |
420
|
|
|
* |
421
|
|
|
* @return bool |
422
|
|
|
*/ |
423
|
|
|
public function isCurrent(): bool |
424
|
|
|
{ |
425
|
|
|
return ! $this->isCancelled() && Carbon::now()->between($this->starts_at, $this->ends_at); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.