| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Timegridio\Concierge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Timegridio\Concierge\Booking\BookingManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Timegridio\Concierge\Calendar\Calendar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Timegridio\Concierge\Models\Appointment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Timegridio\Concierge\Models\Business; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Timegridio\Concierge\Models\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Timegridio\Concierge\Vacancy\VacancyManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /******************************************************************************* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Concierge Service Layer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *     High level booking manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  ******************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Concierge extends Workspace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $timetable = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $calendar = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $booking = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $vacancies = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $appointment = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 6 |  |     protected function calendar() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 6 |  |         if ($this->calendar === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 6 |  |             $this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 6 |  |         return $this->calendar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |     public function timetable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         if ($this->timetable === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |             $this->timetable = new TimetableStrategy($this->business->strategy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         return $this->timetable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |     public function vacancies() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         if ($this->vacancies === null && $this->business !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             $this->vacancies = new VacancyManager($this->business); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         return $this->vacancies; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |     public function booking() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         if ($this->booking === null && $this->business !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |             $this->booking = new BookingManager($this->business); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         return $this->booking; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 6 |  |     public function takeReservation(array $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 6 |  |         $issuer = $request['issuer']; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 6 |  |         $service = $request['service']; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 6 |  |         $contact = $request['contact']; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 6 |  |         $comments = $request['comments']; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 6 |  |         $vacancies = $this->calendar() | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 6 |  |                           ->forService($service->id) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 6 |  |                           ->withDuration($service->duration) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 6 |  |                           ->forDate($request['date']) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 6 |  |                           ->atTime($request['time']) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 6 |  |                           ->find(); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 6 |  |         if ($vacancies->count() == 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             // TODO: Log failure feedback message / raise exception | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | //      DEBUG / INCONSISTENT DB RECORDS CHECK | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | //        if ($vacancies->count() > 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | //            // Log unexpected behavior message / raise exception | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | //            $vacancy = $vacancies->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | //        } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 4 |  |         if ($vacancies->count() == 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 4 |  |             $vacancy = $vacancies->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 4 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 4 |  |         $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 4 |  |         $finishAt = $startAt->copy()->addMinutes($service->duration); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 4 |  |         $appointment = $this->generateAppointment( | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 4 |  |             $issuer, | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 4 |  |             $this->business->id, | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 4 |  |             $contact->id, | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 4 |  |             $service->id, | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 4 |  |             $startAt, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 4 |  |             $finishAt, | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             $comments | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 4 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         /* Should be moved inside generateAppointment() */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 4 |  |         if ($appointment->duplicates()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 2 |  |             $this->appointment = $appointment; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 2 |  |             throw new DuplicatedAppointmentException($appointment->code); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         /* Should be moved inside generateAppointment() */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 4 |  |         $appointment->vacancy()->associate($vacancy); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 4 |  |         $appointment->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 4 |  |         return $appointment; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 4 |  |     protected function generateAppointment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $issuerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $businessId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $contactId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $serviceId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         Carbon $startAt, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         Carbon $finishAt, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $comments = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 4 |  |         $appointment = new Appointment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 4 |  |         $appointment->doReserve(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 4 |  |         $appointment->setStartAtAttribute($startAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 4 |  |         $appointment->setFinishAtAttribute($finishAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 4 |  |         $appointment->business()->associate($businessId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 4 |  |         $appointment->issuer()->associate($issuerId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 4 |  |         $appointment->contact()->associate($contactId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 4 |  |         $appointment->service()->associate($serviceId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 4 |  |         $appointment->comments = $comments; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 4 |  |         $appointment->doHash(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 4 |  |         return $appointment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * Determine if the Business has any published Vacancies available for booking. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 2 |  |     public function isBookable($fromDate = 'today', $days = 7) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 2 |  |         $timetable = $this->timetable()->buildTimetable($this->business->vacancies, $fromDate, $days); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 2 |  |         $timetable = Arr::flatten($timetable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 2 |  |         $sum = array_sum($timetable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 2 |  |         return $sum > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     ////////////////// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     // FOR REFACTOR // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     ////////////////// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 2 |  |     public function getActiveAppointments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  |         return $this->business | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |             ->bookings()->with('contact') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 2 |  |             ->with('business') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  |             ->with('service') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 2 |  |             ->active() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 2 |  |             ->orderBy('start_at') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 2 |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     public function getUnservedAppointments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return $this->business | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             ->bookings()->with('contact') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             ->with('business') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             ->with('service') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             ->unserved() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             ->orderBy('start_at') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 4 |  |     protected function makeDateTime($date, $time, $timezone = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 4 |  |         return Carbon::parse("{$date} {$time} {$timezone}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 4 |  |     protected function makeDateTimeUTC($date, $time, $timezone = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 4 |  |         return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     public function appointment() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         return $this->appointment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  | 
            
                        
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.