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