From bd4d7ce0c0e49b80c453948fc3142d9e4dad4b01 Mon Sep 17 00:00:00 2001 From: Ravi Gulhane Date: Thu, 3 Dec 2026 15:39:42 +0530 Subject: [PATCH] Changes --- app/Exports/HorizonFieldWorkExport.php | 272 +++++++++++++++++ app/Helpers/HorizonReportAccess.php | 89 ++++++ app/Http/Controllers/FormController.php | 28 +- .../Controllers/HorizonreportController.php | 275 ++++++++++++++++++ .../views/horizonreport/fieldwork.blade.php | 152 ++++++++++ resources/views/horizonreport/pob.blade.php | 86 ++++++ .../layouts/shared/left-sidebar.blade.php | 5 + 7 files changed, 904 insertions(+), 3 deletions(-) create mode 100644 app/Exports/HorizonFieldWorkExport.php create mode 100644 app/Helpers/HorizonReportAccess.php create mode 100644 app/Http/Controllers/HorizonreportController.php create mode 100644 resources/views/horizonreport/fieldwork.blade.php create mode 100644 resources/views/horizonreport/pob.blade.php diff --git a/app/Exports/HorizonFieldWorkExport.php b/app/Exports/HorizonFieldWorkExport.php new file mode 100644 index 00000000..0bcc2ad4 --- /dev/null +++ b/app/Exports/HorizonFieldWorkExport.php @@ -0,0 +1,272 @@ +days = array_values($days); + $this->rows = array_values($rows); + } + + public function title(): string + { + return 'Field Work'; + } + + public function array(): array + { + $identityCount = count(self::IDENTITY); + $lastColumn = $identityCount + count($this->days) * 2 + 1; + + $title = array_fill(0, $lastColumn, ''); + $title[0] = 'Monthly Field Work Report'; + + $header = array_fill(0, $lastColumn, ''); + foreach (self::IDENTITY as $index => $label) { + $header[$index] = $label; + } + foreach ($this->days as $index => $day) { + $header[$identityCount + $index * 2] = $this->dayLabel((string) $day); + } + $header[$lastColumn - 1] = 'RBM/ZBM wise Total'; + + $sub = array_fill(0, $lastColumn, ''); + foreach ($this->days as $index => $day) { + $column = $identityCount + $index * 2; + $sub[$column] = 'Link'; + $sub[$column + 1] = 'Time'; + } + + $grid = [$title, $header, $sub]; + foreach ($this->rows as $row) { + $line = array_fill(0, $lastColumn, ''); + $line[0] = (string) ($row['name'] ?? ''); + $line[1] = $this->codeValue($row['employee_code'] ?? ''); + $line[2] = (string) ($row['role_name'] ?? ''); + $line[3] = (string) ($row['position_name'] ?? ''); + $line[4] = $this->codeValue($row['manager_1_code'] ?? ''); + $line[5] = (string) ($row['manager_1_name'] ?? ''); + $line[6] = $this->codeValue($row['manager_2_code'] ?? ''); + $line[7] = (string) ($row['manager_2_name'] ?? ''); + + $cells = is_array($row['days'] ?? null) ? $row['days'] : []; + foreach ($this->days as $index => $day) { + $column = $identityCount + $index * 2; + $cell = is_array($cells[$day] ?? null) ? $cells[$day] : ['status' => 'blank']; + $status = $cell['status'] ?? 'blank'; + if ($status === 'link' && !empty($cell['link'])) { + $line[$column] = 'Link'; + $line[$column + 1] = (string) ($cell['time'] ?? ''); + } elseif ($status === 'no') { + $line[$column] = 'No'; + } + } + $line[$lastColumn - 1] = (int) ($row['total'] ?? 0); + $grid[] = $line; + } + + if (!empty($this->rows)) { + $totalLine = array_fill(0, $lastColumn, ''); + $totalLine[0] = 'Total'; + $sum = 0; + foreach ($this->rows as $row) { + $sum += (int) ($row['total'] ?? 0); + } + $totalLine[$lastColumn - 1] = $sum; + $grid[] = $totalLine; + } + + return $grid; + } + + public function registerEvents(): array + { + return [ + AfterSheet::class => function (AfterSheet $event) { + $sheet = $event->sheet->getDelegate(); + $identityCount = count(self::IDENTITY); + $dayCount = count($this->days); + $lastColumn = $identityCount + $dayCount * 2 + 1; + $lastLetter = Coordinate::stringFromColumnIndex($lastColumn); + $rowCount = count($this->rows); + $lastRow = $rowCount ? $rowCount + 4 : 3; + + $sheet->getStyle('A1')->applyFromArray([ + 'font' => ['bold' => true], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_LEFT, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]); + + for ($column = 1; $column <= $identityCount; $column++) { + $letter = Coordinate::stringFromColumnIndex($column); + $sheet->mergeCells($letter . '2:' . $letter . '3'); + } + foreach ($this->days as $index => $day) { + $start = Coordinate::stringFromColumnIndex($identityCount + $index * 2 + 1); + $end = Coordinate::stringFromColumnIndex($identityCount + $index * 2 + 2); + $sheet->mergeCells($start . '2:' . $end . '2'); + } + $sheet->mergeCells($lastLetter . '2:' . $lastLetter . '3'); + + $sheet->getStyle('A2:' . $lastLetter . '3')->applyFromArray([ + 'font' => [ + 'bold' => true, + 'color' => ['rgb' => '000000'], + ], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => '95B3D7'], + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + 'wrapText' => true, + ], + ]); + + if ($rowCount > 0) { + $dataEnd = $rowCount + 3; + $sheet->getStyle('A4:' . $lastLetter . $dataEnd)->applyFromArray([ + 'font' => [ + 'color' => ['rgb' => '000000'], + ], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => 'FFFFFF'], + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + 'wrapText' => true, + ], + ]); + + foreach ([1, 3, 4, 6, 8] as $column) { + $letter = Coordinate::stringFromColumnIndex($column); + $sheet->getStyle($letter . '4:' . $letter . $dataEnd) + ->getAlignment() + ->setHorizontal(Alignment::HORIZONTAL_LEFT); + } + + foreach ([2, 5, 7] as $column) { + $letter = Coordinate::stringFromColumnIndex($column); + for ($excelRow = 4; $excelRow <= $dataEnd; $excelRow++) { + $value = $sheet->getCell($letter . $excelRow)->getValue(); + if (is_int($value) || (is_string($value) && preg_match('/^-?\d+$/', $value))) { + $sheet->getStyle($letter . $excelRow)->getNumberFormat()->setFormatCode('0'); + } + } + } + + foreach ($this->rows as $rowIndex => $row) { + $excelRow = $rowIndex + 4; + $cells = is_array($row['days'] ?? null) ? $row['days'] : []; + foreach ($this->days as $index => $day) { + $column = $identityCount + $index * 2 + 1; + $cell = is_array($cells[$day] ?? null) ? $cells[$day] : ['status' => 'blank']; + $status = $cell['status'] ?? 'blank'; + $start = Coordinate::stringFromColumnIndex($column); + $end = Coordinate::stringFromColumnIndex($column + 1); + if ($status === 'link' && !empty($cell['link'])) { + $linkCell = $sheet->getCell($start . $excelRow); + $linkCell->getHyperlink()->setUrl((string) $cell['link']); + $sheet->getStyle($start . $excelRow)->applyFromArray([ + 'font' => [ + 'color' => ['rgb' => '0563C1'], + 'underline' => Font::UNDERLINE_SINGLE, + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]); + } else { + $sheet->mergeCells($start . $excelRow . ':' . $end . $excelRow); + } + } + } + + $totalRow = $rowCount + 4; + $sheet->getStyle('A' . $totalRow . ':' . $lastLetter . $totalRow)->applyFromArray([ + 'font' => [ + 'bold' => true, + 'color' => ['rgb' => '000000'], + ], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => '95B3D7'], + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]); + $sheet->getStyle('A' . $totalRow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); + $sheet->getStyle($lastLetter . $totalRow)->getNumberFormat()->setFormatCode('0'); + } + + $sheet->getStyle('A2:' . $lastLetter . $lastRow)->applyFromArray([ + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, + 'color' => ['rgb' => '000000'], + ], + ], + ]); + + for ($column = 1; $column <= $lastColumn; $column++) { + $letter = Coordinate::stringFromColumnIndex($column); + $sheet->getColumnDimension($letter)->setWidth($column <= $identityCount ? 22 : 14); + } + }, + ]; + } + + private function dayLabel(string $iso): string + { + $bits = explode('-', $iso); + if (count($bits) !== 3) { + return $iso; + } + + return $bits[2] . '-' . $bits[1] . '-' . $bits[0]; + } + + private function codeValue($value) + { + $text = trim((string) $value); + if ($text !== '' && preg_match('/^-?\d+$/', $text)) { + return (int) $text; + } + + return $text; + } +} diff --git a/app/Helpers/HorizonReportAccess.php b/app/Helpers/HorizonReportAccess.php new file mode 100644 index 00000000..bf199958 --- /dev/null +++ b/app/Helpers/HorizonReportAccess.php @@ -0,0 +1,89 @@ +domain ?? '') !== 'horizon') { + return false; + } + + $designation = strtoupper(trim(self::tokenDesignation($user->access_token ?? ''))); + if ($designation === 'RBM') { + return false; + } + + if (self::canViewPob()) { + return true; + } + + // Match web: show Custom Reports for designations above RBM/ABM/TM. + return $designation !== '' && $designation !== 'ABM' && $designation !== 'TM'; + } + + public static function canViewPob(): bool + { + $user = Auth::user(); + if (!$user || ($user->domain ?? '') !== 'horizon') { + return false; + } + + $permissions = self::permissions($user->permissions ?? []); + if (in_array('all_data_access', $permissions, true)) { + return true; + } + + $designation = strtoupper(trim(self::tokenDesignation($user->access_token ?? ''))); + + return $designation === 'HO'; + } + + private static function permissions($permissions): array + { + if (is_string($permissions)) { + $decoded = json_decode($permissions, true); + $permissions = is_array($decoded) ? $decoded : []; + } + if (!is_array($permissions)) { + return []; + } + + $names = []; + foreach ($permissions as $permission) { + if (is_scalar($permission) && (string) $permission !== '') { + $names[] = (string) $permission; + } + } + + return $names; + } + + private static function tokenDesignation(string $token): string + { + $token = trim($token); + if (stripos($token, 'Bearer ') === 0) { + $token = trim(substr($token, 7)); + } + $parts = explode('.', $token); + if (count($parts) < 2) { + return ''; + } + $segment = strtr($parts[1], '-_', '+/'); + $pad = strlen($segment) % 4; + if ($pad > 0) { + $segment .= str_repeat('=', 4 - $pad); + } + $payload = json_decode(base64_decode($segment)); + $user = is_object($payload) ? ($payload->user ?? null) : null; + if (!is_object($user)) { + return ''; + } + + return (string) ($user->designation ?? ''); + } +} diff --git a/app/Http/Controllers/FormController.php b/app/Http/Controllers/FormController.php index 709263ea..cc178473 100644 --- a/app/Http/Controllers/FormController.php +++ b/app/Http/Controllers/FormController.php @@ -124,7 +124,7 @@ class FormController extends Controller $supplyYes = ($post['supply_status'] ?? '') === 'Yes'; if (!$supplyYes) { $post['submission_status'] = 'Order Taken'; - return $post; + return $this->splitPobByContactType($post); } $actualUnits = 0; $actualValue = 0; @@ -142,6 +142,14 @@ class FormController extends Controller $post['total_actual_pob_value'] = (string) $actualValue; $post['supply_status'] = 'Yes'; $post['submission_status'] = $hasInvoice ? 'Invoice Uploaded' : 'Submitted'; + return $this->splitPobByContactType($post); + } + + protected function splitPobByContactType(array $post) { + $type = $post['contact_type'] ?? ''; + $actual = $post['total_actual_pob_value'] ?? ''; + $post['chemist_actual_pob_value'] = $type === 'ebt_chemist' ? $actual : ''; + $post['doctor_actual_pob_value'] = $type === 'ebt_doctor' ? $actual : ''; return $post; } @@ -9183,7 +9191,7 @@ class FormController extends Controller } $exceldata[] = $row; } - return Excel::download(new InvoicesExport($exceldata), $formdata->title.'.xlsx'); + return Excel::download(new InvoicesExport($exceldata), $this->excelExportFilename($formdata->title ?? '')); } if ($form_id === InceptNebAllocation::FORM_ID) { @@ -11488,7 +11496,7 @@ class FormController extends Controller response()->json(['status' => 'success']); } else { - return Excel::download(new InvoicesExport($exceldata), $formdata->title.'.xlsx'); + return Excel::download(new InvoicesExport($exceldata), $this->excelExportFilename($formdata->title ?? '')); } @@ -13835,6 +13843,20 @@ class FormController extends Controller Log::debug($cmd); } + /** + * Download names cannot contain "/" or "\". Form titles such as + * "View Galvus D/DM NPI Tracker" otherwise throw while building Content-Disposition. + */ + protected function excelExportFilename($title): string + { + $name = trim(preg_replace('/[\\\\\\/:*?"<>|]+/', '-', (string) $title)); + $name = trim(preg_replace('/\s+/', ' ', $name), " .-"); + if ($name === '') { + $name = 'report'; + } + return $name.'.xlsx'; + } + /** * True when the form Data Designer has at least one show_in_view field. * Opt-in only: empty designer or export-only fields keep the legacy list. diff --git a/app/Http/Controllers/HorizonreportController.php b/app/Http/Controllers/HorizonreportController.php new file mode 100644 index 00000000..0b03d91c --- /dev/null +++ b/app/Http/Controllers/HorizonreportController.php @@ -0,0 +1,275 @@ +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; + } +} diff --git a/resources/views/horizonreport/fieldwork.blade.php b/resources/views/horizonreport/fieldwork.blade.php new file mode 100644 index 00000000..f3259c9e --- /dev/null +++ b/resources/views/horizonreport/fieldwork.blade.php @@ -0,0 +1,152 @@ +@extends('layouts.vertical', ['page_title' => $title]) + +@section('css') + +@endsection + +@section('content') +
+
+
+
+
+

{{ $title }}

+ @if ($access === false) + + @else + @if (!empty($error)) + + @endif +
+
+ + +
+
+ +
+ @if (empty($error) && count($rows)) + + @endif +
+ @endif +
+
+
+
+ + @if ($access !== false && empty($error)) +
+
+
+
+
+ + + + + + + + + + + + @foreach ($days as $day) + + @endforeach + + + + @foreach ($days as $day) + + + @endforeach + + + + @forelse ($rows as $row) + + + + + + + + + + @foreach ($days as $day) + @php + $cell = $row['days'][$day] ?? ['status' => 'blank']; + $status = $cell['status'] ?? 'blank'; + @endphp + @if ($status === 'link' && !empty($cell['link'])) + + + @elseif ($status === 'no') + + @else + + @endif + @endforeach + + + @empty + + + + @endforelse + @if (count($rows)) + + + + + @endif + +
RBM/ZBM NameEmp CodeRole NamePosition NameManager 1 CodeManager 1 NameManager 2 CodeManager 2 Name{{ $dayLabels[$day] ?? $day }}RBM/ZBM wise Total
LinkTime
{{ $row['name'] ?? '' }}{{ $row['employee_code'] ?? '' }}{{ $row['role_name'] ?? '' }}{{ $row['position_name'] ?? '' }}{{ $row['manager_1_code'] ?? '' }}{{ $row['manager_1_name'] ?? '' }}{{ $row['manager_2_code'] ?? '' }}{{ $row['manager_2_name'] ?? '' }} + Link + {{ $cell['time'] ?? '' }}No{{ $row['total'] ?? 0 }}
No rows for this month.
Total{{ $total }}
+
+
+
+
+
+ @endif +
+@endsection diff --git a/resources/views/horizonreport/pob.blade.php b/resources/views/horizonreport/pob.blade.php new file mode 100644 index 00000000..e4b94226 --- /dev/null +++ b/resources/views/horizonreport/pob.blade.php @@ -0,0 +1,86 @@ +@extends('layouts.vertical', ['page_title' => $title]) + +@section('content') +
+
+
+
+
+

{{ $title }}

+ @if ($access === false) + + @else + @if (!empty($error)) + + @endif + @if (!empty($message)) + + @endif +
+
+ + +
+
+ + +
+
+ @if (!empty($filePath)) + Download + @else + + @endif +
+
+ @endif +
+
+
+
+
+@endsection + +@section('script') + +@endsection diff --git a/resources/views/layouts/shared/left-sidebar.blade.php b/resources/views/layouts/shared/left-sidebar.blade.php index ff5f6896..3b505b90 100644 --- a/resources/views/layouts/shared/left-sidebar.blade.php +++ b/resources/views/layouts/shared/left-sidebar.blade.php @@ -219,6 +219,11 @@
  • Monthly Field Work Report
  • + @if(\App\Helpers\HorizonReportAccess::canViewPob()) +
  • + POB Report +
  • + @endif