team = Team::find(session('team_id')); } public function export(): Spreadsheet { $spreadsheet = new Spreadsheet; $sheet = $spreadsheet->getActiveSheet(); $spreadsheet->getProperties() ->setCreator('Verifikator System') ->setTitle('Laporan Kegiatan Kompetisi - Diterima') ->setSubject('Export Laporan Kegiatan Kompetisi') ->setDescription('Data laporan kegiatan kompetisi yang telah diterima beserta anggotanya, diekspor dari sistem verifikator'); $headers = [ 'A1' => 'No', 'B1' => 'Unit Kerja', 'C1' => 'Nama Kegiatan/SK', 'D1' => 'Tanggal Kegiatan', 'E1' => 'NIP Anggota', 'F1' => 'Nama Anggota', 'G1' => 'Posisi', 'H1' => 'Jumlah Satuan', 'I1' => 'Jangka Waktu', 'J1' => 'Poin', 'K1' => 'Status Verifikasi', ]; foreach ($headers as $cell => $header) { $sheet->setCellValue($cell, $header); } // Kolom NIP harus diformat sebagai TEXT sebelum diisi, agar Excel tidak mengubahnya menjadi angka/notasi ilmiah $sheet->getStyle('E:E')->getNumberFormat()->setFormatCode('@'); $sheet->getStyle('A1:K1')->applyFromArray([ 'font' => [ 'bold' => true, 'color' => ['rgb' => 'FFFFFF'], ], 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '4472C4'], ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => '000000'], ], ], ]); $data = $this->getData(); $row = 2; $no = 1; $currentKegiatanId = null; $kegiatanStartRow = 2; foreach ($data as $item) { if ($currentKegiatanId !== null && $currentKegiatanId !== $item['kegiatan_id'] && $row - 1 > $kegiatanStartRow) { $sheet->mergeCells('A'.$kegiatanStartRow.':A'.($row - 1)); $sheet->mergeCells('B'.$kegiatanStartRow.':B'.($row - 1)); $sheet->mergeCells('C'.$kegiatanStartRow.':C'.($row - 1)); $sheet->mergeCells('D'.$kegiatanStartRow.':D'.($row - 1)); } if ($currentKegiatanId !== $item['kegiatan_id']) { $currentKegiatanId = $item['kegiatan_id']; $kegiatanStartRow = $row; $sheet->setCellValue('A'.$row, $no); $sheet->setCellValue('B'.$row, $item['unit_kerja']); $sheet->setCellValue('C'.$row, $item['nama_kegiatan']); $sheet->setCellValue('D'.$row, $item['tanggal_kegiatan']); $no++; } $sheet->setCellValueExplicit('E'.$row, $item['nip_anggota'], DataType::TYPE_STRING); $sheet->setCellValue('F'.$row, $item['nama_anggota']); $sheet->setCellValue('G'.$row, $item['posisi']); $sheet->setCellValue('H'.$row, $item['jumlah_satuan']); $sheet->setCellValue('I'.$row, $item['jangka_waktu']); $sheet->setCellValue('J'.$row, $item['poin']); $sheet->setCellValue('K'.$row, $item['status_verifikasi']); $row++; } if ($currentKegiatanId !== null && $row - 1 > $kegiatanStartRow) { $sheet->mergeCells('A'.$kegiatanStartRow.':A'.($row - 1)); $sheet->mergeCells('B'.$kegiatanStartRow.':B'.($row - 1)); $sheet->mergeCells('C'.$kegiatanStartRow.':C'.($row - 1)); $sheet->mergeCells('D'.$kegiatanStartRow.':D'.($row - 1)); } if ($row > 2) { $sheet->getStyle('A2:K'.($row - 1))->applyFromArray([ 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => '000000'], ], ], 'alignment' => [ 'vertical' => Alignment::VERTICAL_CENTER, ], ]); $sheet->getStyle('J2:J'.($row - 1))->getNumberFormat()->setFormatCode('#,##0.00'); $sheet->getStyle('K2:K'.($row - 1))->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); } foreach (range('A', 'K') as $column) { $sheet->getColumnDimension($column)->setAutoSize(true); } return $spreadsheet; } protected function getData(): array { $periode = Periode::where('status', true)->first()->id_periode; $pengajuans = Kegiatan::with('unit:id,nama') ->with(['syarats' => function ($query) { return $query->select('id', 'id_kegiatan', 'id_rubrik', 'id_kategori', 'id_pelaksana', 'nama_pelaksana', 'posisi', 'poin_total', 'jumlah_satuan', 'satuan', 'jangka_waktu_per_semester', 'status_verifikasi_id') ->whereNotIn('id_kategori', [1, 2, 3, 5, 6]) ->whereIn('status_verifikasi_id', [6, 7]); }]) ->whereHas('syarats', function ($subquery) { return $subquery->whereIn('status_verifikasi_id', [6, 7])->whereNotIn('id_kategori', [1, 2, 3, 5, 6]); }) ->whereDoesntHave('syarats', function ($subquery) { return $subquery->whereIn('status_verifikasi_id', [3, 4])->whereNotIn('id_kategori', [1, 2, 3, 5, 6]); }) ->select('id', 'id_periode', 'id_unit_kerja_pimpinan as id_unit_kerja', 'nama_sk', 'nama_kegiatan', 'tanggal_kegiatan') ->where('id_periode', $periode) ->where(function ($query) { $query->where('status', 1)->orWhereNull('status'); }) ->whereHas('syarats', function ($query) { return $query->whereNotIn('id_kategori', [1, 2, 3, 5, 6]) ->whereIn('id_rubrik', config('rubrik.kompetisi')); }) ->when(Auth::user()->id == 1 && $this->unitKerjaFilter !== null && $this->unitKerjaFilter !== '', function ($query) { $unitId = (int) $this->unitKerjaFilter; if ($unitId >= 1 && $unitId <= 9) { return $query->where('id_unit_kerja_pimpinan', $unitId + 1); } if ($unitId === 0) { return $query->where(function ($subquery) { return $subquery->where('id_unit_kerja_pimpinan', 1) ->orWhere('id_unit_kerja_pimpinan', '>', 10); }); } }) ->when(Auth::user()->id != 1 && $this->team, function ($query) { $this->applyUnitKerjaFilter($query, $this->team, $this->unitKerjaFilter); }) ->orderBy('nama_kegiatan') ->get(); $data = []; foreach ($pengajuans as $kegiatan) { foreach ($kegiatan->syarats as $syarat) { $data[] = [ 'kegiatan_id' => $kegiatan->id, 'unit_kerja' => $kegiatan->unit->nama ?? '', 'nama_kegiatan' => $kegiatan->nama_kegiatan ?? '', 'tanggal_kegiatan' => $kegiatan->tanggal_kegiatan ? Carbon::parse($kegiatan->tanggal_kegiatan)->format('d/m/Y') : '', 'nip_anggota' => $syarat->id_pelaksana ?? '', 'nama_anggota' => $syarat->nama_pelaksana ?? '', 'posisi' => $syarat->posisi ?? '', 'jumlah_satuan' => ($syarat->jumlah_satuan ?? 0).' '.ucwords(str_replace('Per ', '', $syarat->satuan ?? '')), 'jangka_waktu' => (($syarat->jangka_waktu_per_semester ?? 0) * 6).' Bulan', 'poin' => $syarat->poin_total ?? 0, 'status_verifikasi' => 'Telah Dipleno dan Diterima', ]; } } return $data; } protected function applyUnitKerjaFilter($query, Team $team, ?string $specificUnitId = null): void { $unitKerjaIds = $team->teamUnitKerjas()->pluck('unit_kerja_id')->toArray(); if (empty($unitKerjaIds)) { return; } if (in_array(null, $unitKerjaIds, true)) { if ($specificUnitId !== null && $specificUnitId !== '') { $unitId = (int) $specificUnitId; if ($unitId === 0) { $query->where(function ($sub) { $sub->where('id_unit_kerja_pimpinan', 1)->orWhere('id_unit_kerja_pimpinan', '>', 10); }); } else { $query->where('id_unit_kerja_pimpinan', $unitId + 1); } } return; } if ($specificUnitId !== null && $specificUnitId !== '' && in_array((int) $specificUnitId, $unitKerjaIds)) { $unitId = (int) $specificUnitId; if ($unitId === 0) { $query->where(function ($sub) { $sub->where('id_unit_kerja_pimpinan', 1)->orWhere('id_unit_kerja_pimpinan', '>', 10); }); } else { $query->where('id_unit_kerja_pimpinan', $unitId + 1); } return; } $query->where(function ($q) use ($unitKerjaIds) { foreach ($unitKerjaIds as $unitId) { if ($unitId === 0) { $q->orWhere(function ($sub) { $sub->where('id_unit_kerja_pimpinan', 1)->orWhere('id_unit_kerja_pimpinan', '>', 10); }); } else { $q->orWhere('id_unit_kerja_pimpinan', $unitId + 1); } } }); } public function download(?string $filename = null): StreamedResponse { $spreadsheet = $this->export(); if (! $filename) { $filename = 'laporan-kegiatan-kompetisi-diterima-'.date('Y-m-d-H-i-s').'.xlsx'; } return response()->streamDownload(function () use ($spreadsheet) { $writer = new Xlsx($spreadsheet); $writer->save('php://output'); }, $filename, [ 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Cache-Control' => 'max-age=0', ]); } }