|
1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Classes; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use Config; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
6
|
|
|
use VojtaSvoboda\Reservations\Models\Settings; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DatesResolver transform reservations to booked time slots, grouped by date. |
|
10
|
|
|
* |
|
11
|
|
|
* @package VojtaSvoboda\Reservations\Classes |
|
12
|
|
|
*/ |
|
13
|
|
|
class DatesResolver |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Returns reserved time slots from non cancelled reservations. |
|
17
|
|
|
* |
|
18
|
|
|
* If you have reservations at 13.10.2016 at 11:00 and also at 13:00, both with 2 hours lenght, this method return |
|
19
|
|
|
* all booked slots - from 09:15 to 14:45 (when you have 15 minutes slot lenght). |
|
20
|
|
|
* |
|
21
|
|
|
* ------------ 11:00 ------------- 13:00 -------------- |
|
22
|
|
|
* |
|
23
|
|
|
* Because nearest reservation can be done at 09:00 with 2 hours lenghts and next reservation at 15:00. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Collection $reservations |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getDatesFromReservations(Collection $reservations) |
|
30
|
|
|
{ |
|
31
|
|
|
// init |
|
32
|
|
|
$interval = $this->getInterval(); |
|
33
|
|
|
$length = $this->getLength(); |
|
34
|
|
|
$dateFormat = 'd/m/Y'; |
|
35
|
|
|
$timeFormat = 'H:i'; |
|
36
|
|
|
|
|
37
|
|
|
// sort reservations by date and count time slots before reservation and during the reservation |
|
38
|
|
|
$dates = []; |
|
39
|
|
|
foreach ($reservations as $reservation) { |
|
40
|
|
|
// init dates |
|
41
|
|
|
$startDate = $this->getStartDate($reservation, $length, $interval); |
|
42
|
|
|
$endDate = $this->getEndDate($reservation, $length); |
|
43
|
|
|
|
|
44
|
|
|
// save each booked interval |
|
45
|
|
|
while ($startDate < $endDate) { |
|
46
|
|
|
$time = $startDate->format($timeFormat); |
|
47
|
|
|
$dates[$startDate->format($dateFormat)][$time] = $time; |
|
48
|
|
|
$startDate->modify('+' . $interval . ' minutes'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $dates; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get booked interval around the given date. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Carbon $date |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getBoundaryDates(Carbon $date) |
|
63
|
11 |
|
{ |
|
64
|
|
|
// reservation length |
|
65
|
|
|
$length = $this->getLength(); |
|
66
|
11 |
|
|
|
67
|
|
|
// boundary dates before and after |
|
68
|
|
|
$startDatetime = $this->getBoundaryBefore($date, $length); |
|
69
|
11 |
|
$endDatetime = $this->getBoundaryAfter($date, $length); |
|
70
|
11 |
|
|
|
71
|
|
|
return [$startDatetime, $endDatetime]; |
|
72
|
11 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get boundary date before reservation date. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Carbon $date |
|
78
|
|
|
* @param string $length |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
private function getBoundaryBefore(Carbon $date, $length) |
|
83
|
11 |
|
{ |
|
84
|
|
|
$startDatetime = clone $date; |
|
85
|
11 |
|
$startDatetime->modify('-' . $length); |
|
86
|
11 |
|
$startDatetime->modify('+1 second'); |
|
87
|
11 |
|
|
|
88
|
|
|
return $startDatetime; |
|
89
|
11 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get boundary date after reservation date. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Carbon $date |
|
95
|
|
|
* @param string $length |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
private function getBoundaryAfter(Carbon $date, $length) |
|
100
|
11 |
|
{ |
|
101
|
|
|
$endDatetime = clone $date; |
|
102
|
11 |
|
$endDatetime->modify('+' . $length); |
|
103
|
11 |
|
$endDatetime->modify('-1 second'); |
|
104
|
11 |
|
|
|
105
|
|
|
return $endDatetime; |
|
106
|
11 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get reservation imaginary start date. |
|
110
|
|
|
* |
|
111
|
|
|
* @param $reservation |
|
112
|
|
|
* @param $length |
|
113
|
|
|
* @param $interval |
|
114
|
|
|
* |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function getStartDate($reservation, $length, $interval) |
|
118
|
|
|
{ |
|
119
|
|
|
$startDate = $reservation->date; |
|
120
|
|
|
$startDate->modify('-' . $length); |
|
121
|
|
|
$startDate->modify('+' . $interval . ' minutes'); |
|
122
|
|
|
|
|
123
|
|
|
return $startDate; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get reservation imaginary end date. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $reservation |
|
130
|
|
|
* @param $length |
|
131
|
|
|
* |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getEndDate($reservation, $length) |
|
135
|
|
|
{ |
|
136
|
|
|
$endDate = clone $reservation->date; |
|
137
|
|
|
$endDate->modify('+' . $length); |
|
138
|
|
|
|
|
139
|
|
|
return $endDate; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function getDateFormat() |
|
143
|
|
|
{ |
|
144
|
|
|
return Settings::get( |
|
145
|
|
|
'formats_date', |
|
146
|
|
|
Config::get('vojtasvoboda.reservations::config.formats.date', 'd/m/Y') |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
protected function getTimeFormat() |
|
151
|
|
|
{ |
|
152
|
|
|
return Settings::get( |
|
153
|
|
|
'formats_time', |
|
154
|
|
|
Config::get('vojtasvoboda.reservations::config.formats.time', 'H:i') |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
11 |
|
protected function getInterval() |
|
159
|
|
|
{ |
|
160
|
11 |
|
return Settings::get( |
|
161
|
|
|
'reservation_interval', |
|
162
|
|
|
Config::get('vojtasvoboda.reservations::config.reservation.interval', 15) |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
protected function getLength() |
|
167
|
|
|
{ |
|
168
|
|
|
return Settings::get( |
|
169
|
|
|
'reservation_length', |
|
170
|
|
|
(int)Config::get('vojtasvoboda.reservations::config.reservation.length', 15) |
|
171
|
|
|
).' hours'; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|