authorizeAbility($request, 'reports.view'); $organization = $this->organization($request); $owner = $this->ownerRef($request); [$from, $to] = $this->dateRange($request); $branchId = app(OrganizationResolver::class)->branchScope($this->member($request)); return view('frontdesk.reports.index', [ 'organization' => $organization, 'from' => $from->toDateString(), 'to' => $to->toDateString(), 'summary' => $reports->summary($owner, $organization, $from, $to, $branchId), 'peakHours' => $reports->peakHours($owner, $organization, $from, $to, $branchId), 'departments' => $reports->visitsByDepartment($owner, $organization, $from, $to, $branchId), 'frequentVisitors' => $reports->frequentVisitors($owner, $organization), 'security' => $reports->securityIncidents($owner, $organization, $from, $to), 'dailyCounts' => $reports->dailyCounts($owner, $organization, $from, $to, $branchId), 'canExport' => app(FrontdeskPermissions::class)->can($this->member($request), 'reports.export'), ]); } public function export(Request $request, ReportService $reports): StreamedResponse { $this->authorizeAbility($request, 'reports.export'); $organization = $this->organization($request); $owner = $this->ownerRef($request); [$from, $to] = $this->dateRange($request); $branchId = app(OrganizationResolver::class)->branchScope($this->member($request)); $summary = $reports->summary($owner, $organization, $from, $to, $branchId); $departments = $reports->visitsByDepartment($owner, $organization, $from, $to, $branchId); $filename = 'frontdesk-report-'.$from->format('Y-m-d').'-'.$to->format('Y-m-d').'.csv'; return response()->streamDownload(function () use ($summary, $departments, $from, $to) { $handle = fopen('php://output', 'w'); fputcsv($handle, ['Frontdesk report', $from->toDateString(), 'to', $to->toDateString()]); fputcsv($handle, []); fputcsv($handle, ['Metric', 'Value']); foreach ($summary as $key => $value) { fputcsv($handle, [str_replace('_', ' ', $key), $value]); } fputcsv($handle, []); fputcsv($handle, ['Department', 'Visits']); foreach ($departments as $row) { fputcsv($handle, [$row->department, $row->count]); } fclose($handle); }, $filename, ['Content-Type' => 'text/csv']); } /** @return array{0: Carbon, 1: Carbon} */ protected function dateRange(Request $request): array { $from = $request->filled('from') ? Carbon::parse($request->string('from'))->startOfDay() : now()->subDays(30)->startOfDay(); $to = $request->filled('to') ? Carbon::parse($request->string('to'))->endOfDay() : now()->endOfDay(); return [$from, $to]; } }