request = $request; } public function collection() { $query = Permohonan::query(); $query = $query->where('status', 'done'); // Apply date range filter if provided if ($this->request->has('start_date') || $this->request->has('end_date')) { $startDate = $this->request->start_date ?? '1900-01-01'; $endDate = $this->request->end_date ?? now()->toDateString(); $query->where(function ($q) use ($startDate, $endDate) { $q->whereHas('penilaian', function ($q2) use ($startDate, $endDate) { $q2->whereBetween('tanggal_kunjungan', [$startDate, $endDate]); }); // OR check if has penawaran with date in range $q->orWhereHas('penawaran', function ($q3) use ($startDate, $endDate) { $q3->whereBetween('tanggal_penilaian_sebelumnya', [$startDate, $endDate]); }); }); } // Apply branch filter if provided if ($this->request->has('branch_id') && !empty($this->request->branch_id)) { $query->where('branch_id', $this->request->branch_id); } if ($this->request->has('penilai_id') && !empty($this->request->penilai_id)) { $request = $this->request; // Store in a local variable $query->whereHas('penilaian._user_penilai.userPenilaiTeam', function ($q) use ($request) { $q->where('user_id', $request->penilai_id); }); } // Apply search filter if provided if ($this->request->has('search') && !empty($this->request->search)) { $search = $this->request->search; $query->where(function ($q) use ($search) { $q->where('nomor_registrasi', 'LIKE', '%' . $search . '%'); $q->orWhere('tanggal_permohonan', 'LIKE', '%' . $search . '%'); $q->orWhereRelation('user', 'name', 'LIKE', '%' . $search . '%'); $q->orWhereRelation('tujuanPenilaian', 'name', 'LIKE', '%' . $search . '%'); $q->orWhereRelation('debiture', DB::raw('LOWER(name)'), 'LIKE', '%' . strtolower($search) . '%'); $q->orWhereRelation('branch', 'name', 'LIKE', '%' . $search . '%'); $q->orWhereRelation('jenisFasilitasKredit', 'name', 'LIKE', '%' . $search . '%'); $q->orWhereRelation('jenisPenilaian', 'name', 'LIKE', '%' . $search . '%'); $q->orWhere('status', 'LIKE', '%' . $search . '%'); }); } // Default ordering $query->orderBy('nomor_registrasi', 'asc'); return $query->get(); } protected $rowNumber = 0; public function map($permohonan): array { $this->rowNumber++; $npw = 0; if (isset($permohonan->penilai->lpj)) { $lpj = json_decode($permohonan->penilai->lpj, true); $npw = str_replace('.', '', $lpj['total_nilai_pasar_wajar'] ?? 0); } return [ $this->rowNumber, $permohonan->nomor_registrasi, $permohonan->debiture->branch->name ?? '', $permohonan->debiture->name ?? '', $permohonan->user->name ?? $permohonan->mig_nama_ao ?? '', $permohonan->tanggal_permohonan ?? '', $permohonan->penilaian->_user_penilai->userPenilaiTeam->name ?? '', $permohonan->penilaian && $permohonan->penilaian->tanggal_kunjungan ? formatTanggalIndonesia($permohonan->penilaian->tanggal_kunjungan) : '', formatRupiah($npw, 2), ]; } public function headings(): array { return [ 'No', 'Nomor Registrasi', 'Cabang', 'Nama Debitur', 'Pemohon', 'Tanggal Permohonan', 'Nama Penilai', 'Tanggal Laporan', 'Nilai Pasar Wajar', ]; } /** * @return string */ public function title(): string { return 'Laporan User Limit'; } /** * @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:AH1'); $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:AH2'); $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 . ':AH' . $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 - fixed to handle columns beyond Z $highestColumn = $sheet->getHighestColumn(); $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); for ($i = 1; $i <= $highestColumnIndex; $i++) { $currentColumn = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($i); $sheet->getColumnDimension($currentColumn)->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); }, ]; } }