1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Timegridio\Concierge\Calendar\Calendar; |
7
|
|
|
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException; |
8
|
|
|
use Timegridio\Concierge\Models\Appointment; |
9
|
|
|
use Timegridio\Concierge\Models\Business; |
10
|
|
|
use Timegridio\Concierge\Models\Service; |
11
|
|
|
|
12
|
|
|
/******************************************************************************* |
13
|
|
|
* Concierge Service Layer |
14
|
|
|
* High level booking manager |
15
|
|
|
******************************************************************************/ |
16
|
|
|
class Concierge extends Workspace |
17
|
|
|
{ |
18
|
|
|
protected $calendar = null; |
19
|
|
|
|
20
|
|
|
protected $booking = null; |
21
|
|
|
|
22
|
6 |
|
protected function calendar() |
23
|
|
|
{ |
24
|
6 |
|
if ($this->calendar === null) { |
25
|
6 |
|
$this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); |
26
|
6 |
|
} |
27
|
|
|
|
28
|
6 |
|
return $this->calendar; |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
public function takeReservation(array $request) |
32
|
|
|
{ |
33
|
6 |
|
$issuer = $request['issuer']; |
34
|
6 |
|
$service = $request['service']; |
35
|
6 |
|
$contact = $request['contact']; |
36
|
6 |
|
$comments = $request['comments']; |
37
|
|
|
|
38
|
6 |
|
$vacancies = $this->calendar() |
39
|
6 |
|
->forService($service->id) |
40
|
6 |
|
->forDate($request['date']) |
41
|
6 |
|
->atTime($request['time']) |
42
|
6 |
|
->find(); |
43
|
|
|
|
44
|
6 |
|
if ($vacancies->count() == 0) { |
45
|
|
|
// TODO: Log failure feedback message / raise exception |
46
|
2 |
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// DEBUG / INCONSISTENT DB RECORDS CHECK |
|
|
|
|
50
|
|
|
// if ($vacancies->count() > 1) { |
51
|
|
|
// // Log unexpected behavior message / raise exception |
52
|
|
|
// $vacancy = $vacancies->first(); |
53
|
|
|
// } |
54
|
|
|
|
55
|
4 |
|
if ($vacancies->count() == 1) { |
56
|
4 |
|
$vacancy = $vacancies->first(); |
57
|
4 |
|
} |
58
|
|
|
|
59
|
4 |
|
$startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
60
|
4 |
|
$finishAt = $startAt->copy()->addMinutes($service->duration); |
61
|
|
|
|
62
|
4 |
|
$appointment = $this->generateAppointment( |
63
|
4 |
|
$issuer, |
64
|
4 |
|
$this->business->id, |
65
|
4 |
|
$contact->id, |
66
|
4 |
|
$service->id, |
67
|
4 |
|
$startAt, |
68
|
4 |
|
$finishAt, |
69
|
|
|
$comments |
70
|
4 |
|
); |
71
|
|
|
|
72
|
|
|
/* Should be moved inside generateAppointment() */ |
73
|
4 |
|
if ($appointment->duplicates()) { |
74
|
2 |
|
throw new DuplicatedAppointmentException(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* Should be moved inside generateAppointment() */ |
78
|
4 |
|
$appointment->vacancy()->associate($vacancy); |
|
|
|
|
79
|
4 |
|
$appointment->save(); |
80
|
|
|
|
81
|
4 |
|
return $appointment; |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
protected function generateAppointment( |
85
|
|
|
$issuerId, |
86
|
|
|
$businessId, |
87
|
|
|
$contactId, |
88
|
|
|
$serviceId, |
89
|
|
|
Carbon $startAt, |
90
|
|
|
Carbon $finishAt, |
91
|
|
|
$comments = null) |
92
|
|
|
{ |
93
|
4 |
|
$appointment = new Appointment(); |
94
|
|
|
|
95
|
4 |
|
$appointment->doReserve(); |
96
|
4 |
|
$appointment->setStartAtAttribute($startAt); |
97
|
4 |
|
$appointment->setFinishAtAttribute($finishAt); |
98
|
4 |
|
$appointment->business()->associate($businessId); |
99
|
4 |
|
$appointment->issuer()->associate($issuerId); |
100
|
4 |
|
$appointment->contact()->associate($contactId); |
101
|
4 |
|
$appointment->service()->associate($serviceId); |
102
|
4 |
|
$appointment->comments = $comments; |
|
|
|
|
103
|
4 |
|
$appointment->doHash(); |
104
|
|
|
|
105
|
4 |
|
return $appointment; |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
protected function makeDateTime($date, $time, $timezone = null) |
109
|
|
|
{ |
110
|
4 |
|
return Carbon::parse("{$date} {$time} {$timezone}"); |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
protected function makeDateTimeUTC($date, $time, $timezone = null) |
114
|
|
|
{ |
115
|
4 |
|
return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.