1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property Timegridio\Concierge\Models\Business $business |
10
|
|
|
* @property string $date Local timezone date of the published Vacancy |
11
|
|
|
*/ |
12
|
|
|
class Vacancy extends EloquentModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The attributes that are mass assignable. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $fillable = ['business_id', 'service_id', 'date', 'start_at', 'finish_at', 'capacity']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The attributes that aren't mass assignable. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $guarded = ['id']; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The attributes that should be mutated to dates. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $dates = ['start_at', 'finish_at']; |
34
|
|
|
|
35
|
|
|
/////////////////// |
36
|
|
|
// Relationships // |
37
|
|
|
/////////////////// |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* belongs to Business. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy belongs to Business query |
43
|
|
|
*/ |
44
|
12 |
|
public function business() |
45
|
|
|
{ |
46
|
12 |
|
return $this->belongsTo(Business::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* for Service. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy is for providing Service query |
53
|
|
|
*/ |
54
|
12 |
|
public function service() |
55
|
|
|
{ |
56
|
12 |
|
return $this->belongsTo(Service::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* holds many Appointments. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany Relationship Vacancy belongs to Business query |
63
|
|
|
*/ |
64
|
12 |
|
public function appointments() |
65
|
|
|
{ |
66
|
12 |
|
return $this->hasMany(Appointment::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//////////// |
70
|
|
|
// Scopes // |
71
|
|
|
//////////// |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Scope For Date. |
75
|
|
|
* |
76
|
|
|
* @param Illuminate\Database\Query $query |
77
|
|
|
* @param Carbon $date Date of inquiry |
78
|
|
|
* |
79
|
|
|
* @return Illuminate\Database\Query Scoped query |
80
|
|
|
*/ |
81
|
4 |
|
public function scopeForDate($query, Carbon $date) |
82
|
|
|
{ |
83
|
4 |
|
return $query->where('date', '=', $date->toDateString()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Scope For DateTime. |
88
|
|
|
* |
89
|
|
|
* @param Illuminate\Database\Query $query |
90
|
|
|
* @param Carbon $datetime Date and Time of inquiry |
91
|
|
|
* |
92
|
|
|
* @return Illuminate\Database\Query Scoped query |
93
|
|
|
*/ |
94
|
6 |
|
public function scopeForDateTime($query, Carbon $datetime) |
95
|
|
|
{ |
96
|
6 |
|
return $query->where('start_at', '<=', $datetime->toDateTimeString()) |
97
|
6 |
|
->where('finish_at', '>=', $datetime->toDateTimeString()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Scope For Service. |
102
|
|
|
* |
103
|
|
|
* @param Illuminate\Database\Query $query |
104
|
|
|
* @param int serviceId $service Inquired Service to filter |
105
|
|
|
* |
106
|
|
|
* @return Illuminate\Database\Query Scoped query |
107
|
|
|
*/ |
108
|
12 |
|
public function scopeForService($query, $serviceId) |
109
|
|
|
{ |
110
|
12 |
|
return $query->where('service_id', '=', $serviceId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
///////////////////// |
114
|
|
|
// Soft Attributes // |
115
|
|
|
///////////////////// |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* is Holding Any Appointment for given User. |
119
|
|
|
* |
120
|
|
|
* ToDo: Remove from here as needs knowledge from User |
121
|
|
|
* |
122
|
|
|
* @param int $userId User to check belonging Appointments |
123
|
|
|
* |
124
|
|
|
* @return bool Vacancy holds at least one Appointment of User |
125
|
|
|
*/ |
126
|
2 |
|
public function isHoldingAnyFor($userId) |
127
|
|
|
{ |
128
|
2 |
|
$appointments = $this->appointments()->get(); |
129
|
|
|
|
130
|
2 |
|
foreach ($appointments as $appointment) { |
131
|
2 |
|
$contact = $appointment->contact()->first(); |
132
|
2 |
|
if ($contact->isProfileOf($userId)) { |
133
|
1 |
|
return true; |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
|
137
|
1 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* get capacity. |
142
|
|
|
* |
143
|
|
|
* @return int Capacity of the vacancy (in appointment instances) |
144
|
|
|
*/ |
145
|
12 |
|
public function getCapacityAttribute() |
146
|
|
|
{ |
147
|
12 |
|
return intval($this->attributes['capacity']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* has Room. |
152
|
|
|
* |
153
|
|
|
* @return bool There is more capacity than used |
154
|
|
|
*/ |
155
|
4 |
|
public function hasRoom() |
156
|
|
|
{ |
157
|
4 |
|
return $this->capacity > $this->appointments()->active()->count(); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* has Room between time. |
162
|
|
|
* |
163
|
|
|
* @return bool There is more capacity than used |
164
|
|
|
*/ |
165
|
2 |
|
public function hasRoomBetween(Carbon $startAt, Carbon $finishAt) |
166
|
|
|
{ |
167
|
2 |
|
return $this->capacity > $this->appointments() |
|
|
|
|
168
|
2 |
|
->active() |
169
|
2 |
|
->affectingInterval($startAt, $finishAt) |
170
|
2 |
|
->count() && |
171
|
2 |
|
($this->start_at <= $startAt && $this->finish_at >= $finishAt); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get available capacity between time. |
176
|
|
|
* |
177
|
|
|
* @return int Available capacity |
178
|
|
|
*/ |
179
|
6 |
|
public function getAvailableCapacityBetween(Carbon $startAt, Carbon $finishAt) |
180
|
|
|
{ |
181
|
6 |
|
if (!($this->start_at <= $startAt && $this->finish_at >= $finishAt)) { |
|
|
|
|
182
|
5 |
|
return 0; |
183
|
|
|
} |
184
|
|
|
|
185
|
6 |
|
$count = $this->appointments()->active()->affectingInterval($startAt, $finishAt)->count(); |
186
|
|
|
|
187
|
6 |
|
return $this->capacity - intval($count); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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.