middleware('auth'); $this->middleware(function ($request, $next) { if (!HorizonReportAccess::canView()) { abort(403); } return $next($request); }); $this->_service = 'reports'; $this->_module = 'horizon_reports'; $this->_viewname = 'horizonreport'; $this->_title = 'Monthly Field Work Report'; $this->_base_url = '/horizonreport'; } public function list(Request $request) { return redirect('/horizonreport/fieldwork'); } public function fieldwork(Request $request) { $month = $this->month($request); $built = $this->build($month); $error = $built['error']; if (!$error && session('error')) { $error = session('error'); } return view('horizonreport.fieldwork', [ 'title' => $this->_title, 'month' => $month, 'months' => $this->months(), 'days' => $built['days'], 'dayLabels' => $built['day_labels'], 'rows' => $built['rows'], 'total' => $built['total'], 'access' => $built['access'], 'error' => $error, ]); } public function export(Request $request) { $month = $this->month($request); $built = $this->build($month); if ($built['error'] || $built['access'] === false) { $message = $built['error'] ?: 'You do not have access to this report.'; return redirect('/horizonreport/fieldwork?month=' . urlencode($month))->with('error', $message); } $slug = strtolower(preg_replace('/\s+/', '-', $month)); return Excel::download( new HorizonFieldWorkExport($built['days'], $built['rows']), 'horizon-field-work-' . $slug . '.xlsx' ); } public function pob(Request $request) { $from = trim((string) $request->input('from', '')); $to = trim((string) $request->input('to', '')); $filePath = ''; $error = null; $message = null; $access = HorizonReportAccess::canViewPob(); if ($access && ($from !== '' || $to !== '')) { if ($from === '' || $to === '') { $error = 'Please select a from date and a to date'; } elseif ($from > $to) { $error = 'From date must be on or before the to date'; } else { $built = $this->buildPob($from, $to); $error = $built['error']; $filePath = $built['file_path']; if ($built['access'] === false) { $access = false; } } } return view('horizonreport.pob', [ 'title' => 'POB Report', 'from' => $from, 'to' => $to, 'filePath' => $filePath, 'access' => $access, 'error' => $error, 'message' => $message, ]); } private function months(): array { $now = now()->startOfMonth(); $cursor = now()->setDate(2026, 11, 1)->startOfMonth(); if ($cursor->gt($now)) { return [$now->format('F Y')]; } $months = []; while ($cursor->lte($now)) { $months[] = $cursor->format('F Y'); $cursor->addMonth(); } return array_reverse($months); } private function month(Request $request): string { $months = $this->months(); $selected = (string) $request->input('month', ''); if (in_array($selected, $months, true)) { return $selected; } return $months[0] ?? now()->format('F Y'); } private function build(string $month): array { $empty = [ 'access' => true, 'days' => [], 'day_labels' => [], 'rows' => [], 'total' => 0, 'error' => null, ]; $response = LambdaRequest::signedrequest( 'get', 'reports', 'horizon_reports/field_work_report', ['month' => $month], 120 ); $response = $this->unwrap($response); if (!is_object($response) || ($response->status ?? '') !== 'success' || !is_object($response->data ?? null)) { $empty['error'] = is_object($response) ? ($response->message ?? 'Unable to load the field work report.') : 'Unable to load the field work report.'; return $empty; } $data = json_decode(json_encode($response->data), true); if (!is_array($data)) { $empty['error'] = 'Unable to load the field work report.'; return $empty; } $days = is_array($data['days'] ?? null) ? array_values($data['days']) : []; $rows = is_array($data['rows'] ?? null) ? array_values($data['rows']) : []; $total = 0; foreach ($rows as $row) { $total += (int) (is_array($row) ? ($row['total'] ?? 0) : 0); } $dayLabels = []; foreach ($days as $day) { $dayLabels[$day] = $this->dayLabel((string) $day); } return [ 'access' => ($data['access'] ?? true) !== false, 'days' => $days, 'day_labels' => $dayLabels, 'rows' => $rows, 'total' => $total, 'error' => null, ]; } private function buildPob(string $from, string $to): array { $empty = [ 'access' => true, 'file_path' => '', 'error' => null, ]; $response = LambdaRequest::signedrequest( 'get', 'reports', 'horizon_reports/pob_report', ['from' => $from, 'to' => $to], 120 ); $response = $this->unwrap($response); if (!is_object($response) || ($response->status ?? '') !== 'success' || !is_object($response->data ?? null)) { $empty['error'] = is_object($response) ? ($response->message ?? 'Unable to fetch the POB report') : 'Unable to fetch the POB report'; return $empty; } $data = json_decode(json_encode($response->data), true); if (!is_array($data)) { $empty['error'] = 'Unable to fetch the POB report'; return $empty; } if (($data['access'] ?? true) === false) { return [ 'access' => false, 'file_path' => '', 'error' => $response->message ?? 'You do not have access to this report.', ]; } $filePath = trim((string) ($data['file_path'] ?? '')); if ($filePath === '') { $empty['error'] = $response->message ?? 'No file returned for the selected dates'; return $empty; } return [ 'access' => true, 'file_path' => $filePath, 'error' => null, ]; } private function dayLabel(string $iso): string { $bits = explode('-', $iso); if (count($bits) !== 3) { return $iso; } return $bits[2] . '-' . $bits[1] . '-' . $bits[0]; } private function unwrap($response) { if (!is_object($response)) { return $response; } if (isset($response->body) && is_string($response->body) && !isset($response->data)) { $decoded = json_decode($response->body); if (is_object($decoded)) { return $decoded; } } return $response; } }