*/ public function summary( string $ownerRef, Organization $organization, Carbon $from, Carbon $to, ?int $branchId = null, ): array { $events = $this->eventsQuery($ownerRef, $organization->id, $from, $to, $branchId); return [ 'sign_ins' => (clone $events)->where('event', EmployeePresenceEvent::EVENT_SIGN_IN)->count(), 'sign_outs' => (clone $events)->where('event', EmployeePresenceEvent::EVENT_SIGN_OUT)->count(), 'step_outs' => (clone $events)->where('event', EmployeePresenceEvent::EVENT_STEP_OUT)->count(), 'staff_on_site_now' => EmployeePresence::query() ->where('status', EmployeePresence::STATUS_ON_SITE) ->whereHas('employee', function ($q) use ($ownerRef, $organization, $branchId) { $q->owned($ownerRef)->where('organization_id', $organization->id)->where('active', true); if ($branchId) { $q->where('branch_id', $branchId); } }) ->count(), ]; } /** @return Collection */ public function attendanceLog( string $ownerRef, Organization $organization, Carbon $from, Carbon $to, ?int $branchId = null, ): Collection { return $this->eventsQuery($ownerRef, $organization->id, $from, $to, $branchId) ->with(['employee']) ->orderByDesc('created_at') ->limit(200) ->get(); } /** @return Collection */ public function stepOutLog( string $ownerRef, Organization $organization, Carbon $from, Carbon $to, ?int $branchId = null, ): Collection { return $this->eventsQuery($ownerRef, $organization->id, $from, $to, $branchId) ->where('event', EmployeePresenceEvent::EVENT_STEP_OUT) ->with(['employee']) ->orderByDesc('created_at') ->limit(100) ->get(); } protected function eventsQuery( string $ownerRef, int $organizationId, Carbon $from, Carbon $to, ?int $branchId, ) { $query = EmployeePresenceEvent::owned($ownerRef) ->where('organization_id', $organizationId) ->whereBetween('created_at', [$from, $to]); if ($branchId) { $query->where('branch_id', $branchId); } return $query; } }