| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace plunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Auth\Authenticatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Auth\Passwords\CanResetPassword; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Foundation\Auth\Access\Authorizable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class Company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package plunner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Claudio Cardinale <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @copyright 2015 Claudio Cardinale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @version 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @property integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property string $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @property string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property boolean $verified | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @property string $remember_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @property \Carbon\Carbon $created_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @property \Carbon\Carbon $updated_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @property-read \Illuminate\Database\Eloquent\Collection|Employee[] $employees | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @property-read \Illuminate\Database\Eloquent\Collection|Group[] $groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereId($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereName($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereEmail($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company wherePassword($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereVerified($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereRememberToken($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereCreatedAt($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @method static \Illuminate\Database\Query\Builder|\plunner\Company whereUpdatedAt($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | class Company extends Model implements AuthenticatableContract, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                                     AuthorizableContract, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                                     CanResetPasswordContract, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                                     PolicyCheckable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     use Authenticatable, Authorizable, CanResetPassword; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * The attributes that are mass assignable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $fillable = ['name', 'email', 'password']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * The attributes excluded from the model's JSON form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected $hidden = ['password', 'remember_token']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 82 |  |     public function employees() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 82 |  |         return $this->hasMany(Employee::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 38 |  |     public function groups() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 38 |  |         return $this->hasMany(Group::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param Group $group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 16 |  |     public function verifyGroup(Group $group) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 16 |  |         return $group->company_id === $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param Employee $employee | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 16 |  |     public function verifyEmployee(Employee $employee) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 16 |  |         return $employee->company_id === $this->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param Company $company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function verifyCompany(Company $company) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $company->id === $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param Calendar $calendar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function verifyCalendar(Calendar $calendar) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         //TODO test this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $calendar->employee->company->id == $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param Meeting $meeting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function verifyMeeting(Meeting $meeting) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         //TODO implement and test this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param \DateTime $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param \DateTime $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return \Illuminate\Support\Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |     public function getEmployeesTimeSlots($from, $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |         return \DB::table('employees') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |             ->join('calendars', 'employees.id', '=', 'calendars.employee_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 2 |  |             ->join('timeslots', 'calendars.id', '=', 'timeslots.calendar_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 2 |  |             ->where('calendars.enabled', '=', '1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 2 |  |             ->where('timeslots.time_start', '>=', $from) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |             ->where('timeslots.time_end', '<=', $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 2 |  |             ->where('employees.company_id','=', $this->id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  |             ->select('employees.id','timeslots.time_start','timeslots.time_end') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param \DateTime $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param \DateTime $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return \Illuminate\Support\Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |     public function getMeetingsTimeSlots($from, $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 2 |  |         return \DB::table('meetings') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |             ->join('groups', 'meetings.group_id', '=', 'groups.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |             ->join('meeting_timeslots', 'meetings.id', '=', 'meeting_timeslots.meeting_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 2 |  |             ->where('meeting_timeslots.time_start', '>=', $from) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |             ->where('meeting_timeslots.time_end', '<=', $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |             ->where('groups.company_id','=', $this->id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 2 |  |             ->where('meetings.start_time','=', NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  |             ->select('meetings.id', 'meetings.duration','meeting_timeslots.time_start','meeting_timeslots.time_end') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param array $users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param array $meetings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @return \Illuminate\Support\Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 2 |  |     public function getUsersMeetings(array $users, array $meetings) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 2 |  |         return \DB::table('meetings') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  |             ->join('groups', 'meetings.group_id', '=', 'groups.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 2 |  |             ->join('employee_group', 'employee_group.group_id', '=', 'groups.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |             ->join('employees', 'employee_group.employee_id', '=', 'employees.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 2 |  |             ->whereIn('employees.id', $users) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 2 |  |             ->whereIn('meetings.id', $meetings) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 2 |  |             ->where('groups.company_id','=', $this->id) //this is not needed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 2 |  |             ->where('employees.company_id','=', $this->id) //this is not needed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  |             ->select('employees.id as employee_id', 'meetings.id as meeting_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |