|
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
|
|
|
/** |
|
50
|
|
|
* Build timetable. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Illuminate\Database\Eloquent\Collection $vacancies |
|
53
|
|
|
* @param string $starting |
|
54
|
|
|
* @param int $days |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function buildTimetable($vacancies, $starting = 'today', $days = 1) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->timetable |
|
61
|
|
|
->interval($this->interval) |
|
62
|
|
|
->format('date.service.time') |
|
63
|
|
|
->from($starting) |
|
64
|
|
|
->future($days) |
|
65
|
|
|
->init(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($vacancies as $vacancy) { |
|
68
|
|
|
$this->updateTimeslots($vacancy, $this->interval); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->timetable->get(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function updateTimeslots(Vacancy $vacancy, $step = 30) |
|
75
|
|
|
{ |
|
76
|
|
|
$fromTime = $vacancy->start_at; |
|
|
|
|
|
|
77
|
|
|
$toTime = $fromTime->copy(); |
|
78
|
|
|
$limit = $vacancy->finish_at; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
while ($fromTime <= $limit) { |
|
81
|
|
|
$toTime->addMinutes($step); |
|
82
|
|
|
|
|
83
|
|
|
$capacity = $vacancy->getAvailableCapacityBetween($fromTime->timezone('UTC'), $toTime->timezone('UTC')); |
|
84
|
|
|
|
|
85
|
|
|
$time = $fromTime->timezone($vacancy->business->timezone)->format('H:i:s'); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$this->timetable->capacity($vacancy->date, $time, $vacancy->service->slug, $capacity); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$fromTime->addMinutes($step); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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.