1 | <?php namespace VojtaSvoboda\Reservations\Facades; |
||
25 | class ReservationsFacade |
||
26 | { |
||
27 | /** @var Reservation $reservations */ |
||
28 | private $reservations; |
||
29 | |||
30 | /** @var Status $statuses */ |
||
31 | private $statuses; |
||
32 | |||
33 | /** @var DatesResolver $datesResolver */ |
||
34 | private $datesResolver; |
||
35 | |||
36 | /** @var array $returningUsersCache */ |
||
37 | private $returningUsersCache; |
||
38 | |||
39 | /** @var ReservationMailer $mailer */ |
||
40 | private $mailer; |
||
41 | |||
42 | /** @var ReservationAdminMailer $adminMailer */ |
||
43 | private $adminMailer; |
||
44 | |||
45 | /** |
||
46 | * ReservationsFacade constructor. |
||
47 | * |
||
48 | * @param Reservation $reservations |
||
49 | * @param Status $statuses |
||
50 | * @param DatesResolver $resolver |
||
51 | * @param ReservationMailer $mailer |
||
52 | * @param ReservationAdminMailer $adminMailer |
||
53 | */ |
||
54 | 9 | public function __construct( |
|
55 | Reservation $reservations, Status $statuses, DatesResolver $resolver, |
||
56 | ReservationMailer $mailer, ReservationAdminMailer $adminMailer |
||
57 | ) { |
||
58 | 9 | $this->reservations = $reservations; |
|
59 | 9 | $this->statuses = $statuses; |
|
60 | 9 | $this->datesResolver = $resolver; |
|
61 | 9 | $this->mailer = $mailer; |
|
62 | 9 | $this->adminMailer = $adminMailer; |
|
63 | 9 | } |
|
64 | |||
65 | /** |
||
66 | * Create and store reservation. |
||
67 | * |
||
68 | * @param array $data |
||
69 | * |
||
70 | * @return Reservation $reservation |
||
71 | * |
||
72 | * @throws ApplicationException |
||
73 | * @throws ValidationException |
||
74 | */ |
||
75 | 8 | public function storeReservation($data) |
|
76 | { |
||
77 | // check number of sends |
||
78 | 8 | $this->checkLimits(); |
|
79 | |||
80 | // transform date and time to Carbon |
||
81 | 6 | $data['date'] = $this->transformDateTime($data); |
|
82 | |||
83 | // place for extending |
||
84 | 6 | Event::fire('vojtasvoboda.reservations.processReservation', [&$data]); |
|
85 | |||
86 | // create reservation |
||
87 | 6 | $reservation = $this->reservations->create($data); |
|
88 | |||
89 | // send mails to client and admin |
||
90 | 6 | $this->sendMails($reservation); |
|
91 | |||
92 | return $reservation; |
||
93 | 6 | } |
|
94 | |||
95 | 6 | /** |
|
96 | * Send mail to client and admin. |
||
97 | * |
||
98 | * @param Reservation $reservation |
||
99 | */ |
||
100 | public function sendMails($reservation) |
||
111 | |||
112 | /** |
||
113 | * Get all reservations. |
||
114 | * |
||
115 | * @return Collection |
||
116 | */ |
||
117 | public function getReservations() |
||
118 | { |
||
119 | return $this->reservations->all(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get all active (not cancelled) reservations. |
||
124 | * |
||
125 | * @return Collection |
||
126 | */ |
||
127 | public function getActiveReservations() |
||
131 | |||
132 | /** |
||
133 | * Get all reserved time slots. |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public function getReservedDates() |
||
138 | { |
||
139 | $reservations = $this->getActiveReservations(); |
||
140 | |||
141 | return $this->datesResolver->getDatesFromReservations($reservations); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get all reservations by given date interval. |
||
146 | * |
||
147 | * @param Carbon $since Date from. |
||
148 | * @param Carbon $till Date to. |
||
149 | * |
||
150 | 6 | * @return mixed |
|
151 | */ |
||
152 | 6 | public function getReservationsByInterval(Carbon $since, Carbon $till) |
|
156 | |||
157 | /** |
||
158 | * Get reservations count by one email. |
||
159 | * |
||
160 | * @param $email |
||
161 | * |
||
162 | 1 | * @return int |
|
163 | */ |
||
164 | public function getReservationsCountByMail($email) |
||
168 | |||
169 | /** |
||
170 | * Is user returning or not? You have to set this parameter at Backend Reservations setting. |
||
171 | 1 | * |
|
172 | * @param $email |
||
173 | 1 | * |
|
174 | 1 | * @return bool |
|
175 | 1 | */ |
|
176 | 1 | public function isUserReturning($email) |
|
202 | |||
203 | /** |
||
204 | * Bulk reservation state change. |
||
205 | * |
||
206 | * @param array $ids |
||
207 | * @param string $ident |
||
208 | */ |
||
209 | public function bulkStateChange($ids, $ident) |
||
210 | { |
||
211 | // get state |
||
212 | $status = $this->statuses->where('ident', $ident)->first(); |
||
213 | if (!$status) { |
||
214 | return; |
||
215 | } |
||
216 | |||
217 | // go through reservations |
||
218 | foreach ($ids as $id) |
||
219 | { |
||
220 | $reservation = $this->reservations->find($id); |
||
221 | if (!$reservation) { |
||
222 | continue; |
||
223 | } |
||
224 | |||
225 | $reservation->status = $status; |
||
226 | $reservation->save(); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Bulk reservations delete. |
||
232 | * |
||
233 | * @param array $ids |
||
234 | */ |
||
235 | public function bulkDelete($ids) |
||
236 | { |
||
237 | // go through reservations |
||
238 | foreach ($ids as $id) |
||
239 | { |
||
240 | $reservation = $this->reservations->find($id); |
||
241 | if (!$reservation) { |
||
242 | continue; |
||
243 | } |
||
244 | 9 | ||
245 | $reservation->delete(); |
||
246 | } |
||
247 | 9 | } |
|
248 | 1 | ||
249 | /** |
||
250 | 8 | * Transform date and time to DateTime string. |
|
251 | 1 | * |
|
252 | * @param $data |
||
253 | * |
||
254 | 7 | * @return Carbon |
|
255 | * |
||
256 | 7 | * @throws ApplicationException |
|
257 | */ |
||
258 | public function transformDateTime($data) |
||
274 | 6 | ||
275 | 1 | /** |
|
276 | * Returns if given date is available. |
||
277 | 6 | * |
|
278 | * @param Carbon $date |
||
279 | * @param int $exceptId Except reservation ID. |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function isDateAvailable($date, $exceptId = null) |
||
284 | { |
||
285 | // get boundary dates for given reservation date |
||
286 | 6 | $boundaries = $this->datesResolver->getBoundaryDates($date); |
|
287 | |||
288 | // get all reservations in this date |
||
289 | 6 | $query = $this->reservations->notCancelled()->whereBetween('date', $boundaries); |
|
290 | |||
291 | // if updating reservation, we should skip existing reservation |
||
292 | 6 | if ($exceptId !== null) { |
|
293 | 6 | $query->where('id', '!=', $exceptId); |
|
294 | 6 | } |
|
295 | |||
296 | return $query->count() === 0; |
||
297 | 6 | } |
|
298 | 6 | ||
299 | 6 | /** |
|
300 | * Check reservations amount limit per time. |
||
301 | * |
||
302 | 6 | * @throws ApplicationException |
|
303 | */ |
||
304 | 6 | private function checkLimits() |
|
310 | |||
311 | /** |
||
312 | 6 | * Try to find some reservation in less then given limit (default 30 seconds). |
|
313 | * |
||
314 | 6 | * @return boolean |
|
315 | */ |
||
316 | public function isCreatedWhileAgo() |
||
327 | } |
||
328 |