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