| 1 |  |  | <?php namespace VojtaSvoboda\Reservations\Facades; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use October\Rain\Exception\ApplicationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use October\Rain\Exception\ValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use VojtaSvoboda\Reservations\Classes\DatesResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use VojtaSvoboda\Reservations\Mailers\ReservationAdminMailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use VojtaSvoboda\Reservations\Mailers\ReservationMailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use VojtaSvoboda\Reservations\Models\Reservation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use VojtaSvoboda\Reservations\Models\Settings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use VojtaSvoboda\Reservations\Models\Status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Public reservations facade. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Usage: App::make(ReservationsFacade::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package VojtaSvoboda\Reservations\Facades | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         // calculate reservations by same email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $sameCount = $this->getReservationsCountByMail($reservation->email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // send reservation confirmation to customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->mailer->send($reservation, $sameCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // send reservation confirmation to admin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->adminMailer->send($reservation, $sameCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->reservations->notCancelled()->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this->reservations->whereBetween('date', [$since, $till])->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Get reservations count by one email. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function getReservationsCountByMail($email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 1 |  |         return $this->reservations->where('email', $email)->notCancelled()->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  |         // when disabled, user is never returning | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  |         $threshold = Settings::get('returning_mark', 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         if ($threshold < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 1 |  |         // load emails count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         if ($this->returningUsersCache === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  |             $items = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 ->reservations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 ->select(DB::raw('email, count(*) as count')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 ->groupBy('email') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             // refactor to mapWithKeys after upgrade to Laravel 5.3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             foreach($items as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 $this->returningUsersCache[$item['email']] = $item['count']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $returning = $this->returningUsersCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $actual = isset($returning[$email]) ? $returning[$email] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         return $threshold > 0 && $actual >= $threshold; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         // validate date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         if (empty($data['date'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             throw new ApplicationException('You have to select pickup date!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 265 |  |  |         // validate time | 
            
                                                                        
                            
            
                                    
            
            
                | 266 | 6 |  |         if (empty($data['time'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |             throw new ApplicationException('You have to select pickup hour!'); | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 269 | 6 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |         $format = Config::get('vojtasvoboda.reservations::config.formats.datetime', 'd/m/Y H:i'); | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |         return Carbon::createFromFormat($format, trim($data['date'] . ' ' . $data['time'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 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() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         if ($this->isCreatedWhileAgo()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             throw new ApplicationException('You can sent only one reservation per 30 seconds, please wait a second.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |         // protection time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |         $time = Config::get('vojtasvoboda.reservations::config.protection_time', '-30 seconds'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         $timeLimit = Carbon::parse($time)->toDateTimeString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         // try to find some message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |         $item = $this->reservations->machine()->where('created_at', '>', $timeLimit)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |         return $item !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 327 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 328 |  |  |  |