1 | <?php |
||
15 | abstract class BookableBooking extends Model |
||
16 | { |
||
17 | use ValidatingTrait; |
||
18 | use CacheableEloquent; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | protected $fillable = [ |
||
24 | 'bookable_id', |
||
25 | 'bookable_type', |
||
26 | 'customer_id', |
||
27 | 'customer_type', |
||
28 | 'starts_at', |
||
29 | 'ends_at', |
||
30 | 'price', |
||
31 | 'currency', |
||
32 | 'formula', |
||
33 | 'actual_paid', |
||
34 | 'canceled_at', |
||
35 | 'options', |
||
36 | 'notes', |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | protected $casts = [ |
||
43 | 'bookable_id' => 'integer', |
||
44 | 'bookable_type' => 'string', |
||
45 | 'customer_id' => 'integer', |
||
46 | 'customer_type' => 'string', |
||
47 | 'starts_at' => 'datetime', |
||
48 | 'ends_at' => 'datetime', |
||
49 | 'price' => 'float', |
||
50 | 'currency' => 'string', |
||
51 | 'formula' => 'json', |
||
52 | 'actual_paid' => 'float', |
||
53 | 'canceled_at' => 'datetime', |
||
54 | 'options' => 'array', |
||
55 | 'notes' => 'string', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | protected $observables = [ |
||
62 | 'validating', |
||
63 | 'validated', |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * The default rules that the model will validate against. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $rules = [ |
||
72 | 'bookable_id' => 'required|integer', |
||
73 | 'bookable_type' => 'required|string', |
||
74 | 'customer_id' => 'required|integer', |
||
75 | 'customer_type' => 'required|string', |
||
76 | 'starts_at' => 'required|date', |
||
77 | 'ends_at' => 'required|date', |
||
78 | 'price' => 'required|numeric', |
||
79 | 'currency' => 'required|alpha|size:3', |
||
80 | 'formula' => 'nullable|array', |
||
81 | 'actual_paid' => 'required|numeric', |
||
82 | 'canceled_at' => 'nullable|date', |
||
83 | 'options' => 'nullable|array', |
||
84 | 'notes' => 'nullable|string|max:10000', |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * Whether the model should throw a |
||
89 | * ValidationException if it fails validation. |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $throwValidationExceptions = true; |
||
94 | |||
95 | /** |
||
96 | * Create a new Eloquent model instance. |
||
97 | * |
||
98 | * @param array $attributes |
||
99 | */ |
||
100 | public function __construct(array $attributes = []) |
||
106 | |||
107 | /** |
||
108 | * @TODO: refactor |
||
|
|||
109 | * |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | protected static function boot() |
||
125 | |||
126 | /** |
||
127 | * Get options attributes. |
||
128 | * |
||
129 | * @return \Spatie\SchemalessAttributes\SchemalessAttributes |
||
130 | */ |
||
131 | public function getOptionsAttribute(): SchemalessAttributes |
||
135 | |||
136 | /** |
||
137 | * Scope with options attributes. |
||
138 | * |
||
139 | * @return \Illuminate\Database\Eloquent\Builder |
||
140 | */ |
||
141 | public function scopeWithOptions(): Builder |
||
145 | |||
146 | /** |
||
147 | * @TODO: implement rates, availabilites, minimum & maximum units |
||
148 | * |
||
149 | * Calculate the booking price. |
||
150 | * |
||
151 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
152 | * @param \Carbon\Carbon $startsAt |
||
153 | * @param \Carbon\Carbon $endsAt |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function calculatePrice(Model $bookable, Carbon $startsAt, Carbon $endsAt = null): array |
||
178 | |||
179 | /** |
||
180 | * Get the owning resource model. |
||
181 | * |
||
182 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
183 | */ |
||
184 | public function bookable(): MorphTo |
||
188 | |||
189 | /** |
||
190 | * Get the booking customer. |
||
191 | * |
||
192 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
193 | */ |
||
194 | public function customer(): MorphTo |
||
198 | |||
199 | /** |
||
200 | * Get bookings of the given resource. |
||
201 | * |
||
202 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
203 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
204 | * |
||
205 | * @return \Illuminate\Database\Eloquent\Builder |
||
206 | */ |
||
207 | public function scopeOfBookable(Builder $builder, Model $bookable): Builder |
||
211 | |||
212 | /** |
||
213 | * Get bookings of the given customer. |
||
214 | * |
||
215 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
216 | * @param \Illuminate\Database\Eloquent\Model $customer |
||
217 | * |
||
218 | * @return \Illuminate\Database\Eloquent\Builder |
||
219 | */ |
||
220 | public function scopeOfCustomer(Builder $builder, Model $customer): Builder |
||
224 | |||
225 | /** |
||
226 | * Get the past bookings. |
||
227 | * |
||
228 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
229 | * |
||
230 | * @return \Illuminate\Database\Eloquent\Builder |
||
231 | */ |
||
232 | public function scopePast(Builder $builder): Builder |
||
238 | |||
239 | /** |
||
240 | * Get the future bookings. |
||
241 | * |
||
242 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
243 | * |
||
244 | * @return \Illuminate\Database\Eloquent\Builder |
||
245 | */ |
||
246 | public function scopeFuture(Builder $builder): Builder |
||
252 | |||
253 | /** |
||
254 | * Get the current bookings. |
||
255 | * |
||
256 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
257 | * |
||
258 | * @return \Illuminate\Database\Eloquent\Builder |
||
259 | */ |
||
260 | public function scopeCurrent(Builder $builder): Builder |
||
268 | |||
269 | /** |
||
270 | * Get the cancelled bookings. |
||
271 | * |
||
272 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
273 | * |
||
274 | * @return \Illuminate\Database\Eloquent\Builder |
||
275 | */ |
||
276 | public function scopeCancelled(Builder $builder): Builder |
||
280 | |||
281 | /** |
||
282 | * Get bookings starts before the given date. |
||
283 | * |
||
284 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
285 | * @param string $date |
||
286 | * |
||
287 | * @return \Illuminate\Database\Eloquent\Builder |
||
288 | */ |
||
289 | public function scopeStartsBefore(Builder $builder, string $date): Builder |
||
295 | |||
296 | /** |
||
297 | * Get bookings starts after the given date. |
||
298 | * |
||
299 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
300 | * @param string $date |
||
301 | * |
||
302 | * @return \Illuminate\Database\Eloquent\Builder |
||
303 | */ |
||
304 | public function scopeStartsAfter(Builder $builder, string $date): Builder |
||
310 | |||
311 | /** |
||
312 | * Get bookings starts between the given dates. |
||
313 | * |
||
314 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
315 | * @param string $startsAt |
||
316 | * @param string $endsAt |
||
317 | * |
||
318 | * @return \Illuminate\Database\Eloquent\Builder |
||
319 | */ |
||
320 | public function scopeStartsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
327 | |||
328 | /** |
||
329 | * Get bookings ends before the given date. |
||
330 | * |
||
331 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
332 | * @param string $date |
||
333 | * |
||
334 | * @return \Illuminate\Database\Eloquent\Builder |
||
335 | */ |
||
336 | public function scopeEndsBefore(Builder $builder, string $date): Builder |
||
342 | |||
343 | /** |
||
344 | * Get bookings ends after the given date. |
||
345 | * |
||
346 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
347 | * @param string $date |
||
348 | * |
||
349 | * @return \Illuminate\Database\Eloquent\Builder |
||
350 | */ |
||
351 | public function scopeEndsAfter(Builder $builder, string $date): Builder |
||
357 | |||
358 | /** |
||
359 | * Get bookings ends between the given dates. |
||
360 | * |
||
361 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
362 | * @param string $startsAt |
||
363 | * @param string $endsAt |
||
364 | * |
||
365 | * @return \Illuminate\Database\Eloquent\Builder |
||
366 | */ |
||
367 | public function scopeEndsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
374 | |||
375 | /** |
||
376 | * Get bookings cancelled before the given date. |
||
377 | * |
||
378 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
379 | * @param string $date |
||
380 | * |
||
381 | * @return \Illuminate\Database\Eloquent\Builder |
||
382 | */ |
||
383 | public function scopeCancelledBefore(Builder $builder, string $date): Builder |
||
388 | |||
389 | /** |
||
390 | * Get bookings cancelled after the given date. |
||
391 | * |
||
392 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
393 | * @param string $date |
||
394 | * |
||
395 | * @return \Illuminate\Database\Eloquent\Builder |
||
396 | */ |
||
397 | public function scopeCancelledAfter(Builder $builder, string $date): Builder |
||
402 | |||
403 | /** |
||
404 | * Get bookings cancelled between the given dates. |
||
405 | * |
||
406 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
407 | * @param string $startsAt |
||
408 | * @param string $endsAt |
||
409 | * |
||
410 | * @return \Illuminate\Database\Eloquent\Builder |
||
411 | */ |
||
412 | public function scopeCancelledBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
418 | |||
419 | /** |
||
420 | * Get bookings between the given dates. |
||
421 | * |
||
422 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
423 | * @param string $startsAt |
||
424 | * @param string $endsAt |
||
425 | * |
||
426 | * @return \Illuminate\Database\Eloquent\Builder |
||
427 | */ |
||
428 | public function scopeRange(Builder $builder, string $startsAt, string $endsAt): Builder |
||
441 | |||
442 | /** |
||
443 | * Check if the booking is cancelled. |
||
444 | * |
||
445 | * @return bool |
||
446 | */ |
||
447 | public function isCancelled(): bool |
||
451 | |||
452 | /** |
||
453 | * Check if the booking is past. |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | public function isPast(): bool |
||
461 | |||
462 | /** |
||
463 | * Check if the booking is future. |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | public function isFuture(): bool |
||
471 | |||
472 | /** |
||
473 | * Check if the booking is current. |
||
474 | * |
||
475 | * @return bool |
||
476 | */ |
||
477 | public function isCurrent(): bool |
||
481 | } |
||
482 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.