1 | <?php |
||
14 | abstract class BookableBooking extends Model |
||
15 | { |
||
16 | use ValidatingTrait; |
||
17 | use CacheableEloquent; |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | protected $fillable = [ |
||
23 | 'bookable_id', |
||
24 | 'bookable_type', |
||
25 | 'customer_id', |
||
26 | 'customer_type', |
||
27 | 'starts_at', |
||
28 | 'ends_at', |
||
29 | 'price', |
||
30 | 'currency', |
||
31 | 'actual_paid', |
||
32 | 'price_equation', |
||
33 | 'is_approved', |
||
34 | 'is_confirmed', |
||
35 | 'is_attended', |
||
36 | 'canceled_at', |
||
37 | 'notes', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | protected $casts = [ |
||
44 | 'bookable_id' => 'integer', |
||
45 | 'bookable_type' => 'string', |
||
46 | 'customer_id' => 'integer', |
||
47 | 'customer_type' => 'string', |
||
48 | 'starts_at' => 'datetime', |
||
49 | 'ends_at' => 'datetime', |
||
50 | 'price' => 'float', |
||
51 | 'currency' => 'string', |
||
52 | 'actual_paid' => 'float', |
||
53 | 'price_equation' => 'json', |
||
54 | 'is_approved' => 'boolean', |
||
55 | 'is_confirmed' => 'boolean', |
||
56 | 'is_attended' => 'boolean', |
||
57 | 'canceled_at' => 'datetime', |
||
58 | 'notes' => 'string', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | protected $observables = [ |
||
65 | 'validating', |
||
66 | 'validated', |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * The default rules that the model will validate against. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $rules = [ |
||
75 | 'bookable_id' => 'required|integer', |
||
76 | 'bookable_type' => 'required|string', |
||
77 | 'customer_id' => 'required|integer', |
||
78 | 'customer_type' => 'required|string', |
||
79 | 'starts_at' => 'required|date', |
||
80 | 'ends_at' => 'required|date', |
||
81 | 'price' => 'required|numeric', |
||
82 | 'currency' => 'required|alpha|size:3', |
||
83 | 'actual_paid' => 'required|numeric', |
||
84 | 'price_equation' => 'nullable|array', |
||
85 | 'is_approved' => 'sometimes|boolean', |
||
86 | 'is_confirmed' => 'sometimes|boolean', |
||
87 | 'is_attended' => 'sometimes|boolean', |
||
88 | 'canceled_at' => 'nullable|date', |
||
89 | 'notes' => 'nullable|string|max:10000', |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * Whether the model should throw a |
||
94 | * ValidationException if it fails validation. |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $throwValidationExceptions = true; |
||
99 | |||
100 | /** |
||
101 | * Create a new Eloquent model instance. |
||
102 | * |
||
103 | * @param array $attributes |
||
104 | */ |
||
105 | public function __construct(array $attributes = []) |
||
111 | |||
112 | /** |
||
113 | * @TODO: refactor |
||
|
|||
114 | * |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | protected static function boot() |
||
130 | |||
131 | /** |
||
132 | * @TODO: implement rates, availabilites, minimum & maximum units |
||
133 | * |
||
134 | * Calculate the booking price. |
||
135 | * |
||
136 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
137 | * @param \Carbon\Carbon $startsAt |
||
138 | * @param \Carbon\Carbon $endsAt |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function calculatePrice(Model $bookable, Carbon $startsAt, Carbon $endsAt = null): array |
||
163 | |||
164 | /** |
||
165 | * Get the owning resource model. |
||
166 | * |
||
167 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
168 | */ |
||
169 | public function bookable(): MorphTo |
||
173 | |||
174 | /** |
||
175 | * Get the booking customer. |
||
176 | * |
||
177 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
178 | */ |
||
179 | public function customer(): MorphTo |
||
183 | |||
184 | /** |
||
185 | * Get bookings of the given resource. |
||
186 | * |
||
187 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
188 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
189 | * |
||
190 | * @return \Illuminate\Database\Eloquent\Builder |
||
191 | */ |
||
192 | public function scopeOfBookable(Builder $builder, Model $bookable): Builder |
||
196 | |||
197 | /** |
||
198 | * Get bookings of the given customer. |
||
199 | * |
||
200 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
201 | * @param \Illuminate\Database\Eloquent\Model $customer |
||
202 | * |
||
203 | * @return \Illuminate\Database\Eloquent\Builder |
||
204 | */ |
||
205 | public function scopeOfCustomer(Builder $builder, Model $customer): Builder |
||
209 | |||
210 | /** |
||
211 | * Get the past bookings. |
||
212 | * |
||
213 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
214 | * |
||
215 | * @return \Illuminate\Database\Eloquent\Builder |
||
216 | */ |
||
217 | public function scopePast(Builder $builder): Builder |
||
223 | |||
224 | /** |
||
225 | * Get the future bookings. |
||
226 | * |
||
227 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
228 | * |
||
229 | * @return \Illuminate\Database\Eloquent\Builder |
||
230 | */ |
||
231 | public function scopeFuture(Builder $builder): Builder |
||
237 | |||
238 | /** |
||
239 | * Get the current bookings. |
||
240 | * |
||
241 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
242 | * |
||
243 | * @return \Illuminate\Database\Eloquent\Builder |
||
244 | */ |
||
245 | public function scopeCurrent(Builder $builder): Builder |
||
253 | |||
254 | /** |
||
255 | * Get the cancelled bookings. |
||
256 | * |
||
257 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
258 | * |
||
259 | * @return \Illuminate\Database\Eloquent\Builder |
||
260 | */ |
||
261 | public function scopeCancelled(Builder $builder): Builder |
||
265 | |||
266 | /** |
||
267 | * Get bookings starts before the given date. |
||
268 | * |
||
269 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
270 | * @param string $date |
||
271 | * |
||
272 | * @return \Illuminate\Database\Eloquent\Builder |
||
273 | */ |
||
274 | public function scopeStartsBefore(Builder $builder, string $date): Builder |
||
280 | |||
281 | /** |
||
282 | * Get bookings starts after 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 scopeStartsAfter(Builder $builder, string $date): Builder |
||
295 | |||
296 | /** |
||
297 | * Get bookings starts between the given dates. |
||
298 | * |
||
299 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
300 | * @param string $startsAt |
||
301 | * @param string $endsAt |
||
302 | * |
||
303 | * @return \Illuminate\Database\Eloquent\Builder |
||
304 | */ |
||
305 | public function scopeStartsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
312 | |||
313 | /** |
||
314 | * Get bookings ends before the given date. |
||
315 | * |
||
316 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
317 | * @param string $date |
||
318 | * |
||
319 | * @return \Illuminate\Database\Eloquent\Builder |
||
320 | */ |
||
321 | public function scopeEndsBefore(Builder $builder, string $date): Builder |
||
327 | |||
328 | /** |
||
329 | * Get bookings ends after 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 scopeEndsAfter(Builder $builder, string $date): Builder |
||
342 | |||
343 | /** |
||
344 | * Get bookings ends between the given dates. |
||
345 | * |
||
346 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
347 | * @param string $startsAt |
||
348 | * @param string $endsAt |
||
349 | * |
||
350 | * @return \Illuminate\Database\Eloquent\Builder |
||
351 | */ |
||
352 | public function scopeEndsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
359 | |||
360 | /** |
||
361 | * Get bookings cancelled before the given date. |
||
362 | * |
||
363 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
364 | * @param string $date |
||
365 | * |
||
366 | * @return \Illuminate\Database\Eloquent\Builder |
||
367 | */ |
||
368 | public function scopeCancelledBefore(Builder $builder, string $date): Builder |
||
373 | |||
374 | /** |
||
375 | * Get bookings cancelled after the given date. |
||
376 | * |
||
377 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
378 | * @param string $date |
||
379 | * |
||
380 | * @return \Illuminate\Database\Eloquent\Builder |
||
381 | */ |
||
382 | public function scopeCancelledAfter(Builder $builder, string $date): Builder |
||
387 | |||
388 | /** |
||
389 | * Get bookings cancelled between the given dates. |
||
390 | * |
||
391 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
392 | * @param string $startsAt |
||
393 | * @param string $endsAt |
||
394 | * |
||
395 | * @return \Illuminate\Database\Eloquent\Builder |
||
396 | */ |
||
397 | public function scopeCancelledBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
403 | |||
404 | /** |
||
405 | * Get bookings between the given dates. |
||
406 | * |
||
407 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
408 | * @param string $startsAt |
||
409 | * @param string $endsAt |
||
410 | * |
||
411 | * @return \Illuminate\Database\Eloquent\Builder |
||
412 | */ |
||
413 | public function scopeRange(Builder $builder, string $startsAt, string $endsAt): Builder |
||
426 | |||
427 | /** |
||
428 | * Check if the booking is cancelled. |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | public function isCancelled(): bool |
||
436 | |||
437 | /** |
||
438 | * Check if the booking is past. |
||
439 | * |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function isPast(): bool |
||
446 | |||
447 | /** |
||
448 | * Check if the booking is future. |
||
449 | * |
||
450 | * @return bool |
||
451 | */ |
||
452 | public function isFuture(): bool |
||
456 | |||
457 | /** |
||
458 | * Check if the booking is current. |
||
459 | * |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function isCurrent(): bool |
||
466 | } |
||
467 |
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.