diff --git a/app/Exports/LaporanPenilaianJaminanExport.php b/app/Exports/LaporanPenilaianJaminanExport.php index 8f696ed..6e6e9f1 100644 --- a/app/Exports/LaporanPenilaianJaminanExport.php +++ b/app/Exports/LaporanPenilaianJaminanExport.php @@ -5,10 +5,16 @@ use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMapping; + use Maatwebsite\Excel\Concerns\WithTitle; + use Maatwebsite\Excel\Concerns\WithCustomStartCell; + use Maatwebsite\Excel\Concerns\WithEvents; + use Maatwebsite\Excel\Events\AfterSheet; use Modules\Lpj\Models\Permohonan; + use Modules\Lpj\Models\Branch; use Illuminate\Support\Facades\Auth; + use Carbon\Carbon; - class LaporanPenilaianJaminanExport implements FromCollection, WithHeadings, WithMapping + class LaporanPenilaianJaminanExport implements FromCollection, WithHeadings, WithMapping, WithTitle, WithCustomStartCell, WithEvents { protected $request; @@ -56,8 +62,11 @@ return $query->with(['debiture.branch'])->get(); } + protected $rowNumber = 0; + public function map($permohonan): array { + $this->rowNumber++; $luas_tanah = 0; $luas_bangunan = 0; $nilai_tanah = 0; @@ -77,7 +86,7 @@ } return [ - $permohonan->id, + $this->rowNumber, $permohonan->nomor_registrasi, $permohonan->tanggal_permohonan, $permohonan->debiture->branch->name, @@ -103,7 +112,7 @@ public function headings(): array { return [ - 'ID', + 'No', 'Nomor Registrasi', 'Tanggal Permohonan', 'Cabang', @@ -123,4 +132,105 @@ 'Nama Penilai', ]; } + + /** + * @return string + */ + public function title(): string + { + return 'Laporan Penilaian Jaminan'; + } + + /** + * @return string + */ + public function startCell(): string + { + return 'A7'; + } + + /** + * @return array + */ + public function registerEvents(): array + { + return [ + AfterSheet::class => function(AfterSheet $event) { + // Get the sheet + $sheet = $event->sheet->getDelegate(); + + // Set the title + $sheet->setCellValue('A1', 'LAPORAN PENILAIAN JAMINAN'); + $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16); + + // Merge cells for title + $sheet->mergeCells('A1:R1'); + $sheet->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); + + // Set the branch information if filtered + $branchInfo = ''; + if ($this->request->has('branch_id') && !empty($this->request->branch_id)) { + $branch = Branch::find($this->request->branch_id); + if ($branch) { + $branchInfo = 'Cabang: ' . $branch->name; + $sheet->setCellValue('A2', $branchInfo); + $sheet->mergeCells('A2:R2'); + $sheet->getStyle('A2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); + $sheet->getStyle('A2')->getFont()->setBold(true); + } + } + + // Set the period + $startDate = $this->request->start_date ?? ''; + $endDate = $this->request->end_date ?? ''; + + $rowIndex = $branchInfo ? 3 : 2; + + if ($startDate && $endDate) { + $startDateFormatted = Carbon::parse($startDate)->format('d F Y'); + $endDateFormatted = Carbon::parse($endDate)->format('d F Y'); + $sheet->setCellValue('A' . $rowIndex, 'Periode: ' . $startDateFormatted . ' - ' . $endDateFormatted); + } else { + $sheet->setCellValue('A' . $rowIndex, 'Periode: Semua Data'); + } + $sheet->mergeCells('A' . $rowIndex . ':R' . $rowIndex); + $sheet->getStyle('A' . $rowIndex)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); + + // Set the date of export + $rowIndex++; + $sheet->setCellValue('A' . $rowIndex, 'Tanggal Export: ' . Carbon::now()->format('d F Y H:i:s')); + + // Set the user who exported + $rowIndex++; + $userName = Auth::user() ? Auth::user()->name : 'System'; + $sheet->setCellValue('A' . $rowIndex, 'Diexport oleh: ' . $userName); + + // Add a blank line + $rowIndex++; + $sheet->setCellValue('A' . $rowIndex, ''); + + // Style the header row + $headerRange = 'A7:' . $sheet->getHighestColumn() . '7'; + $sheet->getStyle($headerRange)->getFont()->setBold(true); + $sheet->getStyle($headerRange)->getFill() + ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) + ->getStartColor()->setARGB('FFCCCCCC'); + + // Auto-size columns + foreach(range('A', $sheet->getHighestColumn()) as $column) { + $sheet->getColumnDimension($column)->setAutoSize(true); + } + + // Add borders to all cells with data + $dataRange = 'A7:' . $sheet->getHighestColumn() . $sheet->getHighestRow(); + $sheet->getStyle($dataRange)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); + + // Center align the header row + $sheet->getStyle($headerRange)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); + + // Set text wrap for header cells + $sheet->getStyle($headerRange)->getAlignment()->setWrapText(true); + }, + ]; + } }