1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
7
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter; |
8
|
|
|
use Timegridio\Concierge\Presenters\AppointmentPresenter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An Appointment can be understood as a reservation of a given Service, |
12
|
|
|
* provided by a given Business, targeted to a Contact, which will take place |
13
|
|
|
* on a determined Date and Time, and might have a duration and or comments. |
14
|
|
|
* |
15
|
|
|
* The Appointment can be issued by the Contact's User or by the Business owner. |
16
|
|
|
*/ |
17
|
|
|
class Appointment extends EloquentModel implements HasPresenter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The attributes that are mass assignable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fillable = [ |
25
|
|
|
'issuer_id', |
26
|
|
|
'contact_id', |
27
|
|
|
'business_id', |
28
|
|
|
'service_id', |
29
|
|
|
'resource_id', |
30
|
|
|
'start_at', |
31
|
|
|
'finish_at', |
32
|
|
|
'duration', |
33
|
|
|
'comments', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The attributes that aren't mass assignable. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $guarded = ['id', 'hash', 'status', 'vacancy_id']; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The attributes that should be mutated to dates. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $dates = ['start_at', 'finish_at']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Appointment Hard Status Constants. |
52
|
|
|
*/ |
53
|
|
|
const STATUS_RESERVED = 'R'; |
54
|
|
|
const STATUS_CONFIRMED = 'C'; |
55
|
|
|
const STATUS_CANCELED = 'A'; |
56
|
|
|
const STATUS_SERVED = 'S'; |
57
|
|
|
|
58
|
|
|
/////////////// |
59
|
|
|
// PRESENTER // |
60
|
|
|
/////////////// |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get Presenter Class. |
64
|
|
|
* |
65
|
|
|
* @return App\Presenters\AppointmentPresenter |
66
|
|
|
*/ |
67
|
1 |
|
public function getPresenterClass() |
68
|
|
|
{ |
69
|
1 |
|
return AppointmentPresenter::class; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generate hash and save the model to the database. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
30 |
|
public function save(array $options = []) |
80
|
|
|
{ |
81
|
30 |
|
$this->doHash(); |
82
|
|
|
|
83
|
30 |
|
return parent::save($options); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/////////////////// |
87
|
|
|
// Relationships // |
88
|
|
|
/////////////////// |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the issuer (the User that generated the Appointment). |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
94
|
|
|
*/ |
95
|
4 |
|
public function issuer() |
96
|
|
|
{ |
97
|
4 |
|
return $this->belongsTo(config('auth.providers.users.model')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the target Contact (for whom is reserved the Appointment). |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
104
|
|
|
*/ |
105
|
4 |
|
public function contact() |
106
|
|
|
{ |
107
|
4 |
|
return $this->belongsTo(Contact::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the holding Business (that has taken the reservation). |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
114
|
|
|
*/ |
115
|
10 |
|
public function business() |
116
|
|
|
{ |
117
|
10 |
|
return $this->belongsTo(Business::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the reserved Service. |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
124
|
|
|
*/ |
125
|
8 |
|
public function service() |
126
|
|
|
{ |
127
|
8 |
|
return $this->belongsTo(Service::class); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Humanresource. |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
134
|
|
|
*/ |
135
|
4 |
|
public function humanresource() |
136
|
|
|
{ |
137
|
4 |
|
return $this->belongsTo(Humanresource::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the Vacancy (that justifies the availability of resources for the |
142
|
|
|
* Appointment generation). |
143
|
|
|
* |
144
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
145
|
|
|
*/ |
146
|
6 |
|
public function vacancy() |
147
|
|
|
{ |
148
|
6 |
|
return $this->belongsTo(Vacancy::class); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/////////// |
152
|
|
|
// Other // |
153
|
|
|
/////////// |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the User through Contact. |
157
|
|
|
* |
158
|
|
|
* @return User |
159
|
|
|
*/ |
160
|
2 |
|
public function user() |
161
|
|
|
{ |
162
|
2 |
|
return $this->contact->user; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Determine if the new Appointment will hash-crash with another existing |
167
|
|
|
* Appointment. |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
5 |
|
public function duplicates() |
172
|
|
|
{ |
173
|
5 |
|
return !self::where('hash', $this->hash)->get()->isEmpty(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
public function duration() |
177
|
|
|
{ |
178
|
2 |
|
return $this->finish_at->diffInMinutes($this->start_at); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/////////////// |
182
|
|
|
// Accessors // |
183
|
|
|
/////////////// |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get Hash. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
8 |
|
public function getHashAttribute() |
191
|
|
|
{ |
192
|
8 |
|
return isset($this->attributes['hash']) |
193
|
8 |
|
? $this->attributes['hash'] |
194
|
8 |
|
: $this->doHash(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get Finish At: |
199
|
|
|
* Calculates the start_at time plus duration in minutes. |
200
|
|
|
* |
201
|
|
|
* @return Carbon |
202
|
|
|
*/ |
203
|
3 |
|
public function getFinishAtAttribute() |
204
|
|
|
{ |
205
|
3 |
|
if (array_get($this->attributes, 'finish_at') !== null) { |
206
|
|
|
return Carbon::parse($this->attributes['finish_at']); |
207
|
|
|
} |
208
|
|
|
|
209
|
3 |
|
if (is_numeric($this->duration)) { |
|
|
|
|
210
|
3 |
|
return $this->start_at->addMinutes($this->duration); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->start_at; |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get cancellation deadline (target date). |
218
|
|
|
* |
219
|
|
|
* @return Carbon\Carbon |
220
|
|
|
*/ |
221
|
|
|
public function getCancellationDeadlineAttribute() |
222
|
|
|
{ |
223
|
|
|
$hours = $this->business->pref('appointment_cancellation_pre_hs'); |
|
|
|
|
224
|
|
|
|
225
|
|
|
return $this->start_at |
|
|
|
|
226
|
|
|
->subHours($hours) |
227
|
|
|
->timezone($this->business->timezone); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the human readable status name. |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
public function getStatusLabelAttribute() |
236
|
|
|
{ |
237
|
|
|
$labels = [ |
238
|
|
|
Self::STATUS_RESERVED => 'reserved', |
239
|
|
|
Self::STATUS_CONFIRMED => 'confirmed', |
240
|
|
|
Self::STATUS_CANCELED => 'canceled', |
241
|
|
|
Self::STATUS_SERVED => 'served', |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
return array_key_exists($this->status, $labels) |
|
|
|
|
245
|
|
|
? $labels[$this->status] |
|
|
|
|
246
|
|
|
: ''; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get the date of the Appointment. |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
3 |
|
public function getDateAttribute() |
255
|
|
|
{ |
256
|
3 |
|
return $this->start_at |
|
|
|
|
257
|
3 |
|
->timezone($this->business->timezone) |
|
|
|
|
258
|
3 |
|
->toDateString(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get user-friendly unique identification code. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
2 |
|
public function getCodeAttribute() |
267
|
|
|
{ |
268
|
2 |
|
$length = $this->business->pref('appointment_code_length'); |
|
|
|
|
269
|
|
|
|
270
|
2 |
|
return strtoupper(substr($this->hash, 0, $length)); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
////////////// |
274
|
|
|
// Mutators // |
275
|
|
|
////////////// |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Generate Appointment hash. |
279
|
|
|
* |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
30 |
|
public function doHash() |
283
|
|
|
{ |
284
|
30 |
|
return $this->attributes['hash'] = md5( |
285
|
30 |
|
$this->start_at.'/'. |
|
|
|
|
286
|
30 |
|
$this->contact_id.'/'. |
|
|
|
|
287
|
30 |
|
$this->business_id.'/'. |
|
|
|
|
288
|
30 |
|
$this->service_id |
|
|
|
|
289
|
30 |
|
); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set start at. |
294
|
|
|
* |
295
|
|
|
* @param Carbon $datetime |
296
|
|
|
*/ |
297
|
30 |
|
public function setStartAtAttribute(Carbon $datetime) |
298
|
|
|
{ |
299
|
30 |
|
$this->attributes['start_at'] = $datetime; |
300
|
30 |
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set finish_at attribute. |
304
|
|
|
* |
305
|
|
|
* @param Carbon $datetime |
306
|
|
|
*/ |
307
|
11 |
|
public function setFinishAtAttribute(Carbon $datetime) |
308
|
|
|
{ |
309
|
11 |
|
$this->attributes['finish_at'] = $datetime; |
310
|
11 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set Comments. |
314
|
|
|
* |
315
|
|
|
* @param string $comments |
316
|
|
|
*/ |
317
|
30 |
|
public function setCommentsAttribute($comments) |
318
|
|
|
{ |
319
|
30 |
|
$this->attributes['comments'] = trim($comments) ?: null; |
320
|
30 |
|
} |
321
|
|
|
|
322
|
|
|
///////////////// |
323
|
|
|
// HARD STATUS // |
324
|
|
|
///////////////// |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Determine if is Reserved. |
328
|
|
|
* |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
1 |
|
public function isReserved() |
332
|
|
|
{ |
333
|
1 |
|
return $this->status == Self::STATUS_RESERVED; |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/////////////////////////// |
337
|
|
|
// Calculated attributes // |
338
|
|
|
/////////////////////////// |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Appointment Status Workflow. |
342
|
|
|
* |
343
|
|
|
* Hard Status: Those concrete values stored in DB |
344
|
|
|
* Soft Status: Those values calculated from stored values in DB |
345
|
|
|
* |
346
|
|
|
* Suggested transitions (Binding is not mandatory) |
347
|
|
|
* Reserved -> Confirmed -> Served |
348
|
|
|
* Reserved -> Served |
349
|
|
|
* Reserved -> Canceled |
350
|
|
|
* Reserved -> Confirmed -> Canceled |
351
|
|
|
* |
352
|
|
|
* Soft Status |
353
|
|
|
* (Active) [ Reserved | Confirmed ] |
354
|
|
|
* (InActive) [ Canceled | Served ] |
355
|
|
|
*/ |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Determine if is Active. |
359
|
|
|
* |
360
|
|
|
* @return bool |
361
|
|
|
*/ |
362
|
5 |
|
public function isActive() |
363
|
|
|
{ |
364
|
|
|
return |
365
|
5 |
|
$this->status == Self::STATUS_CONFIRMED || |
|
|
|
|
366
|
5 |
|
$this->status == Self::STATUS_RESERVED; |
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Determine if is Pending. |
371
|
|
|
* |
372
|
|
|
* @return bool |
373
|
|
|
*/ |
374
|
3 |
|
public function isPending() |
375
|
|
|
{ |
376
|
3 |
|
return $this->isActive() && $this->isFuture(); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Determine if is Future. |
381
|
|
|
* |
382
|
|
|
* @return bool |
383
|
|
|
*/ |
384
|
4 |
|
public function isFuture() |
385
|
|
|
{ |
386
|
4 |
|
return !$this->isDue(); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Determine if is due. |
391
|
|
|
* |
392
|
|
|
* @return bool |
393
|
|
|
*/ |
394
|
5 |
|
public function isDue() |
395
|
|
|
{ |
396
|
5 |
|
return $this->start_at->isPast(); |
|
|
|
|
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
//////////// |
400
|
|
|
// Scopes // |
401
|
|
|
//////////// |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Scope to Business. |
405
|
|
|
* |
406
|
|
|
* @param Illuminate\Database\Query $query |
407
|
|
|
* |
408
|
|
|
* @return Illuminate\Database\Query |
409
|
|
|
*/ |
410
|
1 |
|
public function scopeOfBusiness($query, $businessId) |
411
|
|
|
{ |
412
|
1 |
|
return $query->where('business_id', $businessId); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
///////////////////////// |
416
|
|
|
// Hard Status Scoping // |
417
|
|
|
///////////////////////// |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Scope to Contacts Collection. |
421
|
|
|
* |
422
|
|
|
* @param Illuminate\Database\Query $query |
423
|
|
|
* |
424
|
|
|
* @return Illuminate\Database\Query |
425
|
|
|
*/ |
426
|
|
|
public function scopeForContacts($query, $contacts) |
427
|
|
|
{ |
428
|
|
|
return $query->whereIn('contact_id', $contacts->pluck('id')); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Scope to Unarchived Appointments. |
433
|
|
|
* |
434
|
|
|
* @param Illuminate\Database\Query $query |
435
|
|
|
* |
436
|
|
|
* @return Illuminate\Database\Query |
437
|
|
|
*/ |
438
|
1 |
|
public function scopeUnarchived($query) |
439
|
|
|
{ |
440
|
1 |
|
$carbon = Carbon::parse('today midnight')->timezone('UTC'); |
441
|
|
|
|
442
|
|
|
return $query |
443
|
|
|
->where(function ($query) use ($carbon) { |
444
|
|
|
|
445
|
1 |
|
$query->whereIn('status', [Self::STATUS_RESERVED, Self::STATUS_CONFIRMED]) |
446
|
1 |
|
->where('start_at', '<=', $carbon) |
447
|
|
|
->orWhere(function ($query) use ($carbon) { |
448
|
1 |
|
$query->where('start_at', '>=', $carbon); |
449
|
1 |
|
}); |
450
|
1 |
|
}); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Scope to Served Appointments. |
455
|
|
|
* |
456
|
|
|
* @param Illuminate\Database\Query $query |
457
|
|
|
* |
458
|
|
|
* @return Illuminate\Database\Query |
459
|
|
|
*/ |
460
|
|
|
public function scopeServed($query) |
461
|
|
|
{ |
462
|
|
|
return $query->where('status', '=', Self::STATUS_SERVED); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Scope to Canceled Appointments. |
467
|
|
|
* |
468
|
|
|
* @param Illuminate\Database\Query $query |
469
|
|
|
* |
470
|
|
|
* @return Illuminate\Database\Query |
471
|
|
|
*/ |
472
|
|
|
public function scopeCanceled($query) |
473
|
|
|
{ |
474
|
|
|
return $query->where('status', '=', Self::STATUS_CANCELED); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
///////////////////////// |
478
|
|
|
// Soft Status Scoping // |
479
|
|
|
///////////////////////// |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Scope to not Served Appointments. |
483
|
|
|
* |
484
|
|
|
* @param Illuminate\Database\Query $query |
485
|
|
|
* |
486
|
|
|
* @return Illuminate\Database\Query |
487
|
|
|
*/ |
488
|
|
|
public function scopeUnServed($query) |
489
|
|
|
{ |
490
|
|
|
return $query->where('status', '<>', Self::STATUS_SERVED); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Scope to Active Appointments. |
495
|
|
|
* |
496
|
|
|
* @param Illuminate\Database\Query $query |
497
|
|
|
* |
498
|
|
|
* @return Illuminate\Database\Query |
499
|
|
|
*/ |
500
|
18 |
|
public function scopeActive($query) |
501
|
|
|
{ |
502
|
18 |
|
return $query->whereIn('status', [Self::STATUS_RESERVED, Self::STATUS_CONFIRMED]); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Scope of date. |
507
|
|
|
* |
508
|
|
|
* @param Illuminate\Database\Query $query |
509
|
|
|
* @param Carbon $date |
510
|
|
|
* |
511
|
|
|
* @return Illuminate\Database\Query |
512
|
|
|
*/ |
513
|
|
|
public function scopeOfDate($query, Carbon $date) |
514
|
|
|
{ |
515
|
|
|
$date->timezone('UTC'); |
516
|
|
|
|
517
|
|
|
return $query->whereRaw('date(`start_at`) = ?', [$date->toDateString()]); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Soft check of time interval affectation |
522
|
|
|
* |
523
|
|
|
* @param Illuminate\Database\Query $query |
524
|
|
|
* @param Carbon $startAt |
525
|
|
|
* @param Carbon $finishAt |
526
|
|
|
* |
527
|
|
|
* @return Illuminate\Database\Query |
528
|
|
|
*/ |
529
|
12 |
|
public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
530
|
|
|
{ |
531
|
12 |
|
$startAt->timezone('UTC'); |
532
|
12 |
|
$finishAt->timezone('UTC'); |
533
|
|
|
|
534
|
|
|
return $query |
535
|
|
|
->where(function ($query) use ($startAt, $finishAt) { |
536
|
|
|
|
537
|
|
|
$query->where(function ($query) use ($startAt, $finishAt) { |
538
|
12 |
|
$query->where('finish_at', '>=', $finishAt) |
539
|
12 |
|
->where('start_at', '<', $startAt); |
540
|
12 |
|
}) |
541
|
|
|
->orWhere(function ($query) use ($startAt, $finishAt) { |
542
|
12 |
|
$query->where('finish_at', '<=', $finishAt) |
543
|
12 |
|
->where('finish_at', '>', $startAt); |
544
|
12 |
|
}) |
545
|
12 |
|
->orWhere(function ($query) use ($startAt, $finishAt) { |
546
|
12 |
|
$query->where('start_at', '>=', $startAt) |
547
|
12 |
|
->where('start_at', '<', $finishAt); |
548
|
12 |
|
}); |
549
|
|
|
// ->orWhere(function ($query) use ($startAt, $finishAt) { |
|
|
|
|
550
|
|
|
// $query->where('start_at', '>', $startAt) |
551
|
|
|
// ->where('finish_at', '>', $finishAt); |
552
|
|
|
// }); |
553
|
|
|
|
554
|
12 |
|
}); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Scope Affecting Humanresource. |
559
|
|
|
* |
560
|
|
|
* @param Illuminate\Database\Query $query |
561
|
|
|
* |
562
|
|
|
* @return Illuminate\Database\Query |
563
|
|
|
*/ |
564
|
12 |
|
public function scopeAffectingHumanresource($query, $humanresourceId) |
565
|
|
|
{ |
566
|
12 |
|
if (is_null($humanresourceId)) { |
567
|
12 |
|
return $query; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
return $query->where('humanresource_id', $humanresourceId); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
////////////////////////// |
574
|
|
|
// Soft Status Checkers // |
575
|
|
|
////////////////////////// |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* User is target contact of the appointment. |
579
|
|
|
* |
580
|
|
|
* @param int $userId |
581
|
|
|
* |
582
|
|
|
* @return bool |
583
|
|
|
*/ |
584
|
|
|
public function isTarget($userId) |
585
|
|
|
{ |
586
|
|
|
return $this->contact->isProfileOf($userId); |
|
|
|
|
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* User is issuer of the appointment. |
591
|
|
|
* |
592
|
|
|
* @param int $userId |
593
|
|
|
* |
594
|
|
|
* @return bool |
595
|
|
|
*/ |
596
|
|
|
public function isIssuer($userId) |
597
|
|
|
{ |
598
|
|
|
return $this->issuer ? $this->issuer->id == $userId : false; |
|
|
|
|
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* User is owner of business. |
603
|
|
|
* |
604
|
|
|
* @param int $userId |
605
|
|
|
* |
606
|
|
|
* @return bool |
607
|
|
|
*/ |
608
|
|
|
public function isOwner($userId) |
609
|
|
|
{ |
610
|
|
|
return $this->business->owners->contains($userId); |
|
|
|
|
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* can be canceled by user. |
615
|
|
|
* |
616
|
|
|
* @param int $userId |
617
|
|
|
* |
618
|
|
|
* @return bool |
619
|
|
|
*/ |
620
|
|
|
public function canCancel($userId) |
621
|
|
|
{ |
622
|
|
|
return $this->isOwner($userId) || |
623
|
|
|
($this->isIssuer($userId) && $this->isOnTimeToCancel()) || |
624
|
|
|
($this->isTarget($userId) && $this->isOnTimeToCancel()); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Determine if it is still possible to cancel according business policy. |
629
|
|
|
* |
630
|
|
|
* @return bool |
631
|
|
|
*/ |
632
|
|
|
public function isOnTimeToCancel() |
633
|
|
|
{ |
634
|
|
|
$graceHours = $this->business->pref('appointment_cancellation_pre_hs'); |
|
|
|
|
635
|
|
|
|
636
|
|
|
$diff = $this->start_at->diffInHours(Carbon::now()); |
|
|
|
|
637
|
|
|
|
638
|
|
|
return intval($diff) >= intval($graceHours); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* can Serve. |
643
|
|
|
* |
644
|
|
|
* @param int $userId |
645
|
|
|
* |
646
|
|
|
* @return bool |
647
|
|
|
*/ |
648
|
|
|
public function canServe($userId) |
649
|
|
|
{ |
650
|
|
|
return $this->isOwner($userId); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* can confirm. |
655
|
|
|
* |
656
|
|
|
* @param int $userId |
657
|
|
|
* |
658
|
|
|
* @return bool |
659
|
|
|
*/ |
660
|
|
|
public function canConfirm($userId) |
661
|
|
|
{ |
662
|
|
|
return $this->isIssuer($userId) || $this->isOwner($userId); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* is Serveable by user. |
667
|
|
|
* |
668
|
|
|
* @param int $userId |
669
|
|
|
* |
670
|
|
|
* @return bool |
671
|
|
|
*/ |
672
|
|
|
public function isServeableBy($userId) |
673
|
|
|
{ |
674
|
|
|
return $this->isServeable() && $this->canServe($userId); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* is Confirmable By user. |
679
|
|
|
* |
680
|
|
|
* @param int $userId |
681
|
|
|
* |
682
|
|
|
* @return bool |
683
|
|
|
*/ |
684
|
|
|
public function isConfirmableBy($userId) |
685
|
|
|
{ |
686
|
|
|
return |
687
|
|
|
$this->isConfirmable() && |
688
|
|
|
$this->shouldConfirmBy($userId) && |
689
|
|
|
$this->canConfirm($userId); |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* is cancelable By user. |
694
|
|
|
* |
695
|
|
|
* @param int $userId |
696
|
|
|
* |
697
|
|
|
* @return bool |
698
|
|
|
*/ |
699
|
|
|
public function isCancelableBy($userId) |
700
|
|
|
{ |
701
|
|
|
return $this->isCancelable() && $this->canCancel($userId); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Determine if the queried userId may confirm the appointment or not. |
706
|
|
|
* |
707
|
|
|
* @param int $userId |
708
|
|
|
* |
709
|
|
|
* @return bool |
710
|
|
|
*/ |
711
|
|
|
public function shouldConfirmBy($userId) |
712
|
|
|
{ |
713
|
|
|
return ($this->isSelfIssued() && $this->isOwner($userId)) || $this->isIssuer($userId); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Determine if the target Contact's User is the same of the Appointment |
718
|
|
|
* issuer User. |
719
|
|
|
* |
720
|
|
|
* @return bool |
721
|
|
|
*/ |
722
|
|
|
public function isSelfIssued() |
723
|
|
|
{ |
724
|
|
|
if (!$this->issuer) { |
|
|
|
|
725
|
|
|
return false; |
726
|
|
|
} |
727
|
|
|
if (!$this->contact) { |
|
|
|
|
728
|
|
|
return false; |
729
|
|
|
} |
730
|
|
|
if (!$this->contact->user) { |
|
|
|
|
731
|
|
|
return false; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
return $this->issuer->id == $this->contact->user->id; |
|
|
|
|
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* Determine if the Serve action can be performed. |
739
|
|
|
* |
740
|
|
|
* @return bool |
741
|
|
|
*/ |
742
|
1 |
|
public function isServeable() |
743
|
|
|
{ |
744
|
1 |
|
return $this->isActive() && $this->isDue(); |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Determine if the Confirm action can be performed. |
749
|
|
|
* |
750
|
|
|
* @return bool |
751
|
|
|
*/ |
752
|
1 |
|
public function isConfirmable() |
753
|
|
|
{ |
754
|
1 |
|
return $this->status == self::STATUS_RESERVED && $this->isFuture(); |
|
|
|
|
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Determine if the cancelable action can be performed. |
759
|
|
|
* |
760
|
|
|
* @return bool |
761
|
|
|
*/ |
762
|
1 |
|
public function isCancelable() |
763
|
|
|
{ |
764
|
1 |
|
return $this->isActive(); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
///////////////////////// |
768
|
|
|
// Hard Status Actions // |
769
|
|
|
///////////////////////// |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* Check and perform Confirm action. |
773
|
|
|
* |
774
|
|
|
* @return $this |
775
|
|
|
*/ |
776
|
5 |
|
public function doReserve() |
777
|
|
|
{ |
778
|
5 |
|
if ($this->status === null) { |
|
|
|
|
779
|
5 |
|
$this->status = self::STATUS_RESERVED; |
|
|
|
|
780
|
5 |
|
} |
781
|
|
|
|
782
|
5 |
|
return $this; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Check and perform Confirm action. |
787
|
|
|
* |
788
|
|
|
* @return $this |
789
|
|
|
*/ |
790
|
2 |
|
public function doConfirm() |
791
|
|
|
{ |
792
|
2 |
|
if ($this->isConfirmable()) { |
793
|
2 |
|
$this->status = self::STATUS_CONFIRMED; |
|
|
|
|
794
|
|
|
|
795
|
2 |
|
$this->save(); |
796
|
2 |
|
} |
797
|
|
|
|
798
|
2 |
|
return $this; |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* Check and perform cancel action. |
803
|
|
|
* |
804
|
|
|
* @return $this |
805
|
|
|
*/ |
806
|
2 |
|
public function doCancel() |
807
|
|
|
{ |
808
|
2 |
|
if ($this->isCancelable()) { |
809
|
1 |
|
$this->status = self::STATUS_CANCELED; |
|
|
|
|
810
|
|
|
|
811
|
1 |
|
$this->save(); |
812
|
1 |
|
} |
813
|
|
|
|
814
|
2 |
|
return $this; |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* Check and perform Serve action. |
819
|
|
|
* |
820
|
|
|
* @return $this |
821
|
|
|
*/ |
822
|
3 |
|
public function doServe() |
823
|
|
|
{ |
824
|
3 |
|
if ($this->isServeable()) { |
825
|
1 |
|
$this->status = self::STATUS_SERVED; |
|
|
|
|
826
|
|
|
|
827
|
1 |
|
$this->save(); |
828
|
1 |
|
} |
829
|
|
|
|
830
|
3 |
|
return $this; |
831
|
|
|
} |
832
|
|
|
} |
833
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.