1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Booking\Strategies; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Timegridio\Concierge\Booking\Timetable; |
8
|
|
|
use Timegridio\Concierge\Models\Appointment; |
9
|
|
|
use Timegridio\Concierge\Models\Business; |
10
|
|
|
use Timegridio\Concierge\Models\Contact; |
11
|
|
|
use Timegridio\Concierge\Models\Service; |
12
|
|
|
use Timegridio\Concierge\Models\Vacancy; |
13
|
|
|
|
14
|
|
|
class BookingTimeslotStrategy implements BookingStrategyInterface |
15
|
|
|
{ |
16
|
|
|
private $timetable; |
17
|
|
|
|
18
|
|
|
private $interval = 30; |
19
|
|
|
|
20
|
|
|
public function __construct(Timetable $timetable) |
21
|
|
|
{ |
22
|
|
|
$this->timetable = $timetable; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function generateAppointment( |
26
|
|
|
$issuerId, |
27
|
|
|
Business $business, |
28
|
|
|
Contact $contact, |
29
|
|
|
Service $service, |
30
|
|
|
Carbon $datetime, |
31
|
|
|
$comments = null |
32
|
|
|
) { |
33
|
|
|
$appointment = new Appointment(); |
34
|
|
|
|
35
|
|
|
$appointment->doReserve(); |
36
|
|
|
$appointment->setStartAtAttribute($datetime); |
37
|
|
|
$appointment->setFinishAtAttribute($datetime->copy()->addMinutes($service->duration)); |
|
|
|
|
38
|
|
|
$appointment->duration = $service->duration; |
|
|
|
|
39
|
|
|
$appointment->business()->associate($business); |
40
|
|
|
$appointment->issuer()->associate($issuerId); |
41
|
|
|
$appointment->contact()->associate($contact); |
42
|
|
|
$appointment->service()->associate($service); |
43
|
|
|
$appointment->comments = $comments; |
|
|
|
|
44
|
|
|
$appointment->doHash(); |
45
|
|
|
|
46
|
|
|
return $appointment; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function hasRoom(Appointment $appointment, Vacancy $vacancy) |
50
|
|
|
{ |
51
|
|
|
return $vacancy->hasRoomBetween($appointment->start_at, $appointment->finish_at); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* [removeBookedVacancies description]. |
56
|
|
|
* |
57
|
|
|
* @param Collection $vacancies |
58
|
|
|
* |
59
|
|
|
* @return Illuminate\Database\Eloquent\Collection |
60
|
|
|
*/ |
61
|
|
|
public function removeBookedVacancies(Collection $vacancies) |
62
|
|
|
{ |
63
|
|
|
//$vacancies = $vacancies->reject(function ($vacancy) { |
|
|
|
|
64
|
|
|
// return $vacancy->isFull(); |
|
|
|
|
65
|
|
|
//}); |
66
|
|
|
|
67
|
|
|
return $vacancies; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* [removeBookedVacancies description]. |
72
|
|
|
* |
73
|
|
|
* @param Collection $vacancies |
74
|
|
|
* |
75
|
|
|
* @return Illuminate\Database\Eloquent\Collection |
76
|
|
|
*/ |
77
|
|
|
public function removeSelfBooked(Collection $vacancies, $userId) |
78
|
|
|
{ |
79
|
|
|
//$vacancies = $vacancies->reject(function ($vacancy) use ($user) { |
|
|
|
|
80
|
|
|
// return $vacancy->isHoldingAnyFor($user); |
|
|
|
|
81
|
|
|
//}); |
82
|
|
|
|
83
|
|
|
return $vacancies; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Build timetable. |
88
|
|
|
* |
89
|
|
|
* @param Illuminate\Database\Eloquent\Collection $vacancies |
90
|
|
|
* @param string $starting |
91
|
|
|
* @param int $days |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function buildTimetable($vacancies, $starting = 'today', $days = 1) |
96
|
|
|
{ |
97
|
|
|
$this->timetable |
98
|
|
|
->interval($this->interval) |
99
|
|
|
->format('date.service.time') |
100
|
|
|
->from($starting) |
101
|
|
|
->future($days) |
102
|
|
|
->init(); |
103
|
|
|
|
104
|
|
|
foreach ($vacancies as $vacancy) { |
105
|
|
|
$this->updateTimeslots($vacancy, $this->interval); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->timetable->get(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function updateTimeslots(Vacancy $vacancy, $step = 30) |
112
|
|
|
{ |
113
|
|
|
$fromTime = $vacancy->start_at; |
|
|
|
|
114
|
|
|
$toTime = $fromTime->copy(); |
115
|
|
|
$limit = $vacancy->finish_at; |
|
|
|
|
116
|
|
|
|
117
|
|
|
while ($fromTime <= $limit) { |
118
|
|
|
$toTime->addMinutes($step); |
119
|
|
|
|
120
|
|
|
$capacity = $vacancy->getAvailableCapacityBetween($fromTime->timezone('UTC'), $toTime->timezone('UTC')); |
121
|
|
|
|
122
|
|
|
$time = $fromTime->timezone($vacancy->business->timezone)->format('H:i:s'); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$this->timetable->capacity($vacancy->date, $time, $vacancy->service->slug, $capacity); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$fromTime->addMinutes($step); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.