|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Timegridio\Concierge\Calendar\Calendar; |
|
8
|
|
|
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException; |
|
9
|
|
|
use Timegridio\Concierge\Models\Appointment; |
|
10
|
|
|
use Timegridio\Concierge\Models\Business; |
|
11
|
|
|
use Timegridio\Concierge\Models\Service; |
|
12
|
|
|
use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy; |
|
13
|
|
|
use Timegridio\Concierge\Vacancy\VacancyManager; |
|
14
|
|
|
|
|
15
|
|
|
/******************************************************************************* |
|
16
|
|
|
* Concierge Service Layer |
|
17
|
|
|
* High level booking manager |
|
18
|
|
|
******************************************************************************/ |
|
19
|
|
|
class Concierge extends Workspace |
|
20
|
|
|
{ |
|
21
|
|
|
protected $timetable = null; |
|
22
|
|
|
|
|
23
|
|
|
protected $calendar = null; |
|
24
|
|
|
|
|
25
|
|
|
protected $booking = null; |
|
26
|
|
|
|
|
27
|
|
|
protected $vacancies = null; |
|
28
|
|
|
|
|
29
|
6 |
|
protected function calendar() |
|
30
|
|
|
{ |
|
31
|
6 |
|
if ($this->calendar === null) { |
|
32
|
6 |
|
$this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
return $this->calendar; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function timetable() |
|
39
|
|
|
{ |
|
40
|
2 |
|
if ($this->timetable === null) { |
|
41
|
2 |
|
$this->timetable = new TimetableStrategy($this->business->strategy); |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $this->timetable; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function vacancies() |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->vacancies === null && $this->business !== null) { |
|
50
|
|
|
$this->vacancies = new VacancyManager($this->business); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->vacancies; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
public function takeReservation(array $request) |
|
57
|
|
|
{ |
|
58
|
6 |
|
$issuer = $request['issuer']; |
|
59
|
6 |
|
$service = $request['service']; |
|
60
|
6 |
|
$contact = $request['contact']; |
|
61
|
6 |
|
$comments = $request['comments']; |
|
62
|
|
|
|
|
63
|
6 |
|
$vacancies = $this->calendar() |
|
64
|
6 |
|
->forService($service->id) |
|
65
|
6 |
|
->withDuration($service->duration) |
|
66
|
6 |
|
->forDate($request['date']) |
|
67
|
6 |
|
->atTime($request['time']) |
|
68
|
6 |
|
->find(); |
|
69
|
|
|
|
|
70
|
6 |
|
if ($vacancies->count() == 0) { |
|
71
|
|
|
// TODO: Log failure feedback message / raise exception |
|
72
|
2 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// DEBUG / INCONSISTENT DB RECORDS CHECK |
|
76
|
|
|
// if ($vacancies->count() > 1) { |
|
77
|
|
|
// // Log unexpected behavior message / raise exception |
|
78
|
|
|
// $vacancy = $vacancies->first(); |
|
79
|
|
|
// } |
|
80
|
|
|
|
|
81
|
4 |
|
if ($vacancies->count() == 1) { |
|
82
|
4 |
|
$vacancy = $vacancies->first(); |
|
83
|
4 |
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
$startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
|
86
|
4 |
|
$finishAt = $startAt->copy()->addMinutes($service->duration); |
|
87
|
|
|
|
|
88
|
4 |
|
$appointment = $this->generateAppointment( |
|
89
|
4 |
|
$issuer, |
|
90
|
4 |
|
$this->business->id, |
|
91
|
4 |
|
$contact->id, |
|
92
|
4 |
|
$service->id, |
|
93
|
4 |
|
$startAt, |
|
94
|
4 |
|
$finishAt, |
|
95
|
|
|
$comments |
|
96
|
4 |
|
); |
|
97
|
|
|
|
|
98
|
|
|
/* Should be moved inside generateAppointment() */ |
|
99
|
4 |
|
if ($appointment->duplicates()) { |
|
100
|
2 |
|
throw new DuplicatedAppointmentException(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* Should be moved inside generateAppointment() */ |
|
104
|
4 |
|
$appointment->vacancy()->associate($vacancy); |
|
|
|
|
|
|
105
|
4 |
|
$appointment->save(); |
|
106
|
|
|
|
|
107
|
4 |
|
return $appointment; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
protected function generateAppointment( |
|
111
|
|
|
$issuerId, |
|
112
|
|
|
$businessId, |
|
113
|
|
|
$contactId, |
|
114
|
|
|
$serviceId, |
|
115
|
|
|
Carbon $startAt, |
|
116
|
|
|
Carbon $finishAt, |
|
117
|
|
|
$comments = null) |
|
118
|
|
|
{ |
|
119
|
4 |
|
$appointment = new Appointment(); |
|
120
|
|
|
|
|
121
|
4 |
|
$appointment->doReserve(); |
|
122
|
4 |
|
$appointment->setStartAtAttribute($startAt); |
|
123
|
4 |
|
$appointment->setFinishAtAttribute($finishAt); |
|
124
|
4 |
|
$appointment->business()->associate($businessId); |
|
125
|
4 |
|
$appointment->issuer()->associate($issuerId); |
|
126
|
4 |
|
$appointment->contact()->associate($contactId); |
|
127
|
4 |
|
$appointment->service()->associate($serviceId); |
|
128
|
4 |
|
$appointment->comments = $comments; |
|
|
|
|
|
|
129
|
4 |
|
$appointment->doHash(); |
|
130
|
|
|
|
|
131
|
4 |
|
return $appointment; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Determine if the Business has any published Vacancies available for booking. |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function isBookable($fromDate = 'today', $days = 7) |
|
140
|
|
|
{ |
|
141
|
2 |
|
$timetable = $this->timetable()->buildTimetable($this->business->vacancies, $fromDate, $days); |
|
142
|
|
|
|
|
143
|
2 |
|
$timetable = Arr::flatten($timetable); |
|
144
|
|
|
|
|
145
|
2 |
|
$sum = array_sum($timetable); |
|
146
|
|
|
|
|
147
|
2 |
|
return $sum > 0; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
////////////////// |
|
151
|
|
|
// FOR REFACTOR // |
|
152
|
|
|
////////////////// |
|
153
|
|
|
|
|
154
|
|
|
public function getActiveAppointments() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->business |
|
157
|
|
|
->bookings()->with('contact') |
|
158
|
|
|
->with('business') |
|
159
|
|
|
->with('service') |
|
160
|
|
|
->active() |
|
161
|
|
|
->orderBy('start_at') |
|
162
|
|
|
->get(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getUnservedAppointments() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->business |
|
168
|
|
|
->bookings()->with('contact') |
|
169
|
|
|
->with('business') |
|
170
|
|
|
->with('service') |
|
171
|
|
|
->unserved() |
|
172
|
|
|
->orderBy('start_at') |
|
173
|
|
|
->get(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
4 |
|
protected function makeDateTime($date, $time, $timezone = null) |
|
177
|
|
|
{ |
|
178
|
4 |
|
return Carbon::parse("{$date} {$time} {$timezone}"); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
4 |
|
protected function makeDateTimeUTC($date, $time, $timezone = null) |
|
182
|
|
|
{ |
|
183
|
4 |
|
return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: