|
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 = [ |
|
20
|
|
|
'business_id', |
|
21
|
|
|
'service_id', |
|
22
|
|
|
'humanresource_id', |
|
23
|
|
|
'date', |
|
24
|
|
|
'start_at', |
|
25
|
|
|
'finish_at', |
|
26
|
|
|
'capacity', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The attributes that aren't mass assignable. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $guarded = ['id']; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The attributes that should be mutated to dates. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $dates = ['start_at', 'finish_at']; |
|
42
|
|
|
|
|
43
|
|
|
/////////////////// |
|
44
|
|
|
// Relationships // |
|
45
|
|
|
/////////////////// |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* belongs to Business. |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy belongs to Business query |
|
51
|
|
|
*/ |
|
52
|
16 |
|
public function business() |
|
53
|
|
|
{ |
|
54
|
16 |
|
return $this->belongsTo(Business::class); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* for Service. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy is for providing Service query |
|
61
|
|
|
*/ |
|
62
|
26 |
|
public function service() |
|
63
|
|
|
{ |
|
64
|
26 |
|
return $this->belongsTo(Service::class); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Humanresource. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
71
|
|
|
*/ |
|
72
|
18 |
|
public function humanresource() |
|
73
|
|
|
{ |
|
74
|
18 |
|
return $this->belongsTo(Humanresource::class); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* holds many Appointments. |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany Relationship Vacancy belongs to Business query |
|
81
|
|
|
*/ |
|
82
|
10 |
|
public function appointments() |
|
83
|
|
|
{ |
|
84
|
10 |
|
return $this->hasMany(Appointment::class); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Humanresource Slug. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function humanresourceSlug() |
|
93
|
|
|
{ |
|
94
|
|
|
if($this->humanresource) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
return $this->humanresource->slug; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return ''; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
//////////// |
|
103
|
|
|
// Scopes // |
|
104
|
|
|
//////////// |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Scope For Date. |
|
108
|
|
|
* |
|
109
|
|
|
* @param Illuminate\Database\Query $query |
|
110
|
|
|
* @param Carbon $date Date of inquiry |
|
111
|
|
|
* |
|
112
|
|
|
* @return Illuminate\Database\Query Scoped query |
|
113
|
|
|
*/ |
|
114
|
6 |
|
public function scopeForDate($query, Carbon $date) |
|
115
|
|
|
{ |
|
116
|
6 |
|
return $query->where('date', '=', $date->toDateString()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Scope For DateTime. |
|
121
|
|
|
* |
|
122
|
|
|
* @param Illuminate\Database\Query $query |
|
123
|
|
|
* @param Carbon $datetime Date and Time of inquiry |
|
124
|
|
|
* |
|
125
|
|
|
* @return Illuminate\Database\Query Scoped query |
|
126
|
|
|
*/ |
|
127
|
6 |
|
public function scopeForDateTime($query, Carbon $datetime) |
|
128
|
|
|
{ |
|
129
|
6 |
|
return $query->where('start_at', '<=', $datetime->toDateTimeString()) |
|
130
|
6 |
|
->where('finish_at', '>=', $datetime->toDateTimeString()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Scope only Future. |
|
135
|
|
|
* |
|
136
|
|
|
* @param Illuminate\Database\Query $query |
|
137
|
|
|
* |
|
138
|
|
|
* @return Illuminate\Database\Query Scoped query |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function scopeFuture($query) |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $query->where('date', '>', Carbon::now()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Scope For Service. |
|
147
|
|
|
* |
|
148
|
|
|
* @param Illuminate\Database\Query $query |
|
149
|
|
|
* @param int serviceId $service Inquired Service to filter |
|
150
|
|
|
* |
|
151
|
|
|
* @return Illuminate\Database\Query Scoped query |
|
152
|
|
|
*/ |
|
153
|
14 |
|
public function scopeForService($query, $serviceId) |
|
154
|
|
|
{ |
|
155
|
14 |
|
return $query->where('service_id', '=', $serviceId); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
///////////////////// |
|
159
|
|
|
// Soft Attributes // |
|
160
|
|
|
///////////////////// |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* is Holding Any Appointment for given User. |
|
164
|
|
|
* |
|
165
|
|
|
* ToDo: Remove from here as needs knowledge from User |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $userId User to check belonging Appointments |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool Vacancy holds at least one Appointment of User |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function isHoldingAnyFor($userId) |
|
172
|
|
|
{ |
|
173
|
2 |
|
$appointments = $this->appointments()->get(); |
|
174
|
|
|
|
|
175
|
2 |
|
foreach ($appointments as $appointment) { |
|
176
|
2 |
|
$contact = $appointment->contact()->first(); |
|
177
|
2 |
|
if ($contact->isProfileOf($userId)) { |
|
178
|
1 |
|
return true; |
|
179
|
|
|
} |
|
180
|
1 |
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* is Full. |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool Vacancy is fully booked |
|
189
|
|
|
*/ |
|
190
|
|
|
# public function isFull() |
|
|
|
|
|
|
191
|
|
|
# { |
|
192
|
|
|
# return $this->getFreeSlotsCount() <= 0; |
|
193
|
|
|
# } |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* get free slots count. |
|
197
|
|
|
* |
|
198
|
|
|
* @return int Count Capacity minus Used |
|
199
|
|
|
*/ |
|
200
|
|
|
# public function getFreeSlotsCount() |
|
|
|
|
|
|
201
|
|
|
# { |
|
202
|
|
|
# $count = $this->appointments()->active()->count(); |
|
203
|
|
|
# |
|
204
|
|
|
# return $this->capacity - $count; |
|
205
|
|
|
# } |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* get capacity. |
|
209
|
|
|
* |
|
210
|
|
|
* @return int Capacity of the vacancy (in appointment instances) |
|
211
|
|
|
*/ |
|
212
|
17 |
|
public function getCapacityAttribute() |
|
213
|
|
|
{ |
|
214
|
17 |
|
if ($this->humanresource) { |
|
|
|
|
|
|
215
|
|
|
return intval($this->humanresource->capacity); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
17 |
|
return intval($this->attributes['capacity']); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* has Room. |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool There is more capacity than used |
|
225
|
|
|
*/ |
|
226
|
4 |
|
public function hasRoom() |
|
227
|
|
|
{ |
|
228
|
4 |
|
return $this->capacity > $this->appointments()->active()->count(); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* has Room between time. |
|
233
|
|
|
* |
|
234
|
|
|
* @return bool There is more capacity than used |
|
235
|
|
|
*/ |
|
236
|
4 |
|
public function hasRoomBetween(Carbon $startAt, Carbon $finishAt) |
|
237
|
|
|
{ |
|
238
|
4 |
|
return $this->capacity > $this->business |
|
|
|
|
|
|
239
|
4 |
|
->bookings() |
|
240
|
4 |
|
->active() |
|
241
|
4 |
|
->affectingInterval($startAt, $finishAt) |
|
242
|
4 |
|
->affectingHumanresource($this->humanresource_id) |
|
|
|
|
|
|
243
|
4 |
|
->count() && |
|
244
|
4 |
|
($this->start_at <= $startAt && $this->finish_at >= $finishAt); |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get available capacity between time. |
|
249
|
|
|
* |
|
250
|
|
|
* @return int Available capacity |
|
251
|
|
|
*/ |
|
252
|
8 |
|
public function getAvailableCapacityBetween(Carbon $startAt, Carbon $finishAt) |
|
253
|
|
|
{ |
|
254
|
8 |
|
if (!($this->start_at <= $startAt && $this->finish_at >= $finishAt)) { |
|
|
|
|
|
|
255
|
7 |
|
return 0; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
8 |
|
$count = $this->business |
|
259
|
8 |
|
->bookings() |
|
260
|
8 |
|
->with('humanresource') |
|
261
|
8 |
|
->active() |
|
262
|
8 |
|
->affectingHumanresource($this->humanresource_id) |
|
|
|
|
|
|
263
|
8 |
|
->affectingInterval($startAt, $finishAt) |
|
264
|
8 |
|
->count(); |
|
265
|
|
|
|
|
266
|
8 |
|
return $this->capacity - intval($count); |
|
|
|
|
|
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
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.