diff --git a/app/Exports/KeseluruhanExport.php b/app/Exports/KeseluruhanExport.php new file mode 100644 index 00000000..22333e1c --- /dev/null +++ b/app/Exports/KeseluruhanExport.php @@ -0,0 +1,107 @@ +rows = app(KeseluruhanController::class)->getRekapRows($tahun, $jenisPegawai, $unit); + } + + public function array(): array + { + return $this->rows; + } + + public function headings(): array + { + return [ + 'No.', + 'Nama', + 'NIP', + 'Jenis Pegawai', + 'Unit Kerja', + 'Jumlah P1', + 'Jumlah P2', + 'Jumlah THR', + 'Jumlah R13', + 'Jumlah Publikasi', + 'Total', + ]; + } + + public function map($row): array + { + static $no = 0; + $no++; + + return [ + $no, + $row['nama'], + $row['nip'], + $row['jenis_pegawai'] === 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] === 'tendik' ? 'Tendik' : '-'), + $row['nama_unit'], + $row['jumlah_p1'], + $row['jumlah_p2'], + $row['jumlah_thr'], + $row['jumlah_r13'], + $row['jumlah_publikasi'], + $row['total'], + ]; + } + + public function columnFormats(): array + { + $accountingFormat = '_("Rp"\ * #,##0.00_);_("Rp"\ * \-#,##0.00_);_("Rp"\ * "-"??_);_(@_)'; + + return [ + 'C' => '@', + 'F' => $accountingFormat, + 'G' => $accountingFormat, + 'H' => $accountingFormat, + 'I' => $accountingFormat, + 'J' => $accountingFormat, + 'K' => $accountingFormat, + ]; + } + + public function styles(Worksheet $sheet) + { + return [ + 1 => [ + 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => '4472C4'], + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ], + ]; + } +} diff --git a/app/Http/Controllers/Perhitungan/Remunerasi/KeseluruhanController.php b/app/Http/Controllers/Perhitungan/Remunerasi/KeseluruhanController.php new file mode 100644 index 00000000..2b713d1c --- /dev/null +++ b/app/Http/Controllers/Perhitungan/Remunerasi/KeseluruhanController.php @@ -0,0 +1,251 @@ +user()->hasRole('Superuser'), 403); + } + + public function index(): View + { + $this->authorizeAccess(); + + $title = 'Keseluruhan'; + + return view('perhitungan.remunerasi.keseluruhan.index', compact('title')); + } + + public function data(Request $request): JsonResponse + { + $this->authorizeAccess(); + + $tahun = (int) ($request->tahun ?: now()->year); + $jenisPegawai = $request->jenis_pegawai ?: null; + $unit = $request->unit; + + $rows = collect($this->getRekapRows($tahun, $jenisPegawai, $unit)); + + return DataTables::of($rows) + ->editColumn('jenis_pegawai', fn ($row) => $row['jenis_pegawai'] === 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] === 'tendik' ? 'Tendik' : '-')) + ->editColumn('jumlah_p1', fn ($row) => 'Rp. '.number_format($row['jumlah_p1'], 2, ',', '.')) + ->editColumn('jumlah_p2', fn ($row) => 'Rp. '.number_format($row['jumlah_p2'], 2, ',', '.')) + ->editColumn('jumlah_thr', fn ($row) => 'Rp. '.number_format($row['jumlah_thr'], 2, ',', '.')) + ->editColumn('jumlah_r13', fn ($row) => 'Rp. '.number_format($row['jumlah_r13'], 2, ',', '.')) + ->editColumn('jumlah_publikasi', fn ($row) => 'Rp. '.number_format($row['jumlah_publikasi'], 2, ',', '.')) + ->editColumn('total', fn ($row) => 'Rp. '.number_format($row['total'], 2, ',', '.')) + ->with([ + 'jumlahPegawai' => $rows->count(), + 'totalKeseluruhan' => number_format($rows->sum('total'), 2, ',', '.'), + ]) + ->make(); + } + + public function export(Request $request): BinaryFileResponse + { + $this->authorizeAccess(); + + set_time_limit(300); + + $tahun = (int) ($request->tahun ?: now()->year); + $jenisPegawai = $request->jenis_pegawai ?: null; + $unit = $request->unit; + + $unitName = '_Semua_Unit_Kerja'; + if ($unit !== null && $unit !== '') { + $unitKerja = UnitKerja::find((int) $unit === 99 ? 0 : (int) $unit); + if ($unitKerja) { + $unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama); + } + } + + $filename = "Remunerasi_Keseluruhan_{$tahun}{$unitName}.xlsx"; + + return Excel::download(new KeseluruhanExport($tahun, $jenisPegawai, $unit), $filename); + } + + /** + * Bangun daftar rekap per pegawai (Pegawai, Unit Kerja, Jumlah P1/P2/THR/R13/Publikasi) untuk + * tahun terpilih. Dipakai bersama oleh data() (DataTables) dan KeseluruhanExport supaya kedua + * sisi (tampilan web & export Excel) selalu konsisten. + */ + public function getRekapRows(int $tahun, ?string $jenisPegawai, mixed $unit): array + { + $rows = $this->buildRekap($tahun, $jenisPegawai); + + if ($unit !== null && $unit !== '') { + $unitBucket = (int) $unit; + $rows = $rows->filter(function ($row) use ($unitBucket) { + if ($unitBucket === 99) { + return $row['unit_kerja_id'] === null || ! in_array((int) $row['unit_kerja_id'], range(1, 9), true); + } + + return (int) $row['unit_kerja_id'] === $unitBucket; + })->values(); + } + + return $rows->sortBy('nama', SORT_NATURAL | SORT_FLAG_CASE)->values()->all(); + } + + private function buildRekap(int $tahun, ?string $jenisPegawai): Collection + { + $cacheKey = 'remunerasi_keseluruhan_v1_'.$tahun.'_'.($jenisPegawai ?: 'semua'); + + return collect(Cache::remember($cacheKey, now()->addHours(6), fn () => $this->computeRekap($tahun, $jenisPegawai))); + } + + private function computeRekap(int $tahun, ?string $jenisPegawai): array + { + $normalize = fn ($nip) => preg_replace("/['.\-]/", '', (string) $nip); + + // 1. P1 = akumulasi Januari s.d. bulan berjalan (atau Desember untuk tahun yang sudah lewat) + $bulanTerakhir = $tahun === (int) now()->year ? (int) now()->month : 12; + + $p1 = P1::query() + ->where('tahun', $tahun) + ->whereBetween('bulan', [1, $bulanTerakhir]) + ->when($jenisPegawai, fn ($q) => $q->whereRaw('LOWER(jenis_pegawai) = ?', [strtolower($jenisPegawai)])) + ->get() + ->groupBy(fn ($row) => $normalize($row->nip)) + ->map(fn ($rows) => (float) $rows->sum('gaji_yang_dibayarkan')); + + // 2. P2 = Semester 1 tahun terpilih + $periodeSemester1 = Periode::where('nama', 'LIKE', "%{$tahun} Semester 1%")->value('id_periode'); + + $p2 = collect(); + if ($periodeSemester1) { + $p2 = Pegawai::withoutGlobalScopes() + ->join('verifikator.jabatan_pegawais as jp', 'verifikator.pegawais.nip', '=', 'jp.nip') + ->join('verifikator.jabatan_pegawai_details as jpd', 'jp.id', '=', 'jpd.jabatan_pegawai_id') + ->join('verifikator.remunerasi_per_bulans as rpb', 'jpd.id', '=', 'rpb.jabatan_pegawai_detail_id') + ->select('verifikator.pegawais.nip') + ->selectRaw('SUM(rpb.p2_bruto) as total') + ->whereNull('rpb.deleted_at') + ->where('jp.periode', $periodeSemester1) + ->when($jenisPegawai, fn ($q) => $q->where('jpd.jenis_pegawai', $jenisPegawai)) + ->groupBy('verifikator.pegawais.nip') + ->havingRaw('SUM(rpb.p2_bruto) > 0') + ->get() + ->keyBy(fn ($row) => $normalize($row->nip)) + ->map(fn ($row) => (float) $row->total); + } + + // 3. THR & 4. R13 = data eksternal (API Dawai), untuk seluruh unit sekaligus lalu di-cache + // per hari seperti pola THRController/R13Controller. + $jenisPegawaiParam = $jenisPegawai ?? ''; + + $thrData = Cache::remember( + "keseluruhan_thr_{$jenisPegawaiParam}_{$tahun}", + now()->addDay(), + fn () => (new DawaiService)->getDataThr($jenisPegawaiParam, $tahun, null, false) + ); + $thr = collect($thrData)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_thr'] ?? 0)); + + $r13Data = Cache::remember( + "keseluruhan_r13_{$jenisPegawaiParam}_{$tahun}", + now()->addDay(), + fn () => (new DawaiService)->getDataR13($jenisPegawaiParam, $tahun, null, false) + ); + $r13 = collect($r13Data)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_r13'] ?? 0)); + + // 5. Publikasi = seluruh periode yang namanya memuat tahun terpilih (mis. "2026 Semester 1" + // maupun "2025/2026 Ganjil"), karena publikasi tidak terikat pada satu format periode tertentu. + $periodeIds = Periode::where('nama', 'LIKE', "%{$tahun}%")->pluck('id_periode'); + + $publikasiQuery = Publikasi::query() + ->whereIn('periode', $periodeIds) + ->where('poin_total', '>', 0) + ->select('nip') + ->selectRaw('SUM(biaya_remunerasi) as total'); + + if ($jenisPegawai) { + $publikasiQuery->whereHas('pegawai', function ($q) use ($jenisPegawai) { + $q->withoutGlobalScopes()->where('jenis_pegawai', $jenisPegawai); + }); + } + + $publikasi = $publikasiQuery->groupBy('nip')->get() + ->keyBy(fn ($row) => $normalize($row->nip)) + ->map(fn ($row) => (float) $row->total); + + $allNips = $p1->keys() + ->merge($p2->keys()) + ->merge($thr->keys()) + ->merge($r13->keys()) + ->merge($publikasi->keys()) + ->unique() + ->values(); + + if ($allNips->isEmpty()) { + return []; + } + + $pegawaiInfo = Pegawai::withoutGlobalScopes() + ->leftJoin('verifikator.unit_kerja_pegawais as ukp', 'verifikator.pegawais.unit_kerja_id', '=', 'ukp.id') + ->select([ + 'verifikator.pegawais.nip', + 'verifikator.pegawais.nama', + 'verifikator.pegawais.jenis_pegawai', + 'verifikator.pegawais.unit_kerja_id', + 'ukp.nama as nama_unit', + ]) + ->get() + ->keyBy(fn ($row) => $normalize($row->nip)); + + return $allNips->map(function ($nip) use ($pegawaiInfo, $p1, $p2, $thr, $r13, $publikasi, $jenisPegawai) { + $info = $pegawaiInfo->get($nip); + + if (! $info) { + return null; + } + + if ($jenisPegawai && strtolower((string) $info->jenis_pegawai) !== strtolower($jenisPegawai)) { + return null; + } + + $jumlahP1 = $p1->get($nip, 0.0); + $jumlahP2 = $p2->get($nip, 0.0); + $jumlahThr = $thr->get($nip, 0.0); + $jumlahR13 = $r13->get($nip, 0.0); + $jumlahPublikasi = $publikasi->get($nip, 0.0); + $total = $jumlahP1 + $jumlahP2 + $jumlahThr + $jumlahR13 + $jumlahPublikasi; + + if ($total <= 0) { + return null; + } + + return [ + 'nip' => ltrim($info->nip ?? $nip, "'"), + 'nama' => $info->nama ?? '-', + 'jenis_pegawai' => strtolower($info->jenis_pegawai ?? ''), + 'unit_kerja_id' => $info->unit_kerja_id, + 'nama_unit' => $info->nama_unit ?? '-', + 'jumlah_p1' => $jumlahP1, + 'jumlah_p2' => $jumlahP2, + 'jumlah_thr' => $jumlahThr, + 'jumlah_r13' => $jumlahR13, + 'jumlah_publikasi' => $jumlahPublikasi, + 'total' => $total, + ]; + })->filter()->values()->all(); + } +} diff --git a/resources/views/layouts/sidebar.blade.php b/resources/views/layouts/sidebar.blade.php index 8e6664bf..e0a257cd 100644 --- a/resources/views/layouts/sidebar.blade.php +++ b/resources/views/layouts/sidebar.blade.php @@ -553,6 +553,15 @@ @endrole + + @role('Superuser') +
| Pegawai | +Unit Kerja | +Jumlah P1 | +Jumlah P2 | +Jumlah THR | +Jumlah R13 | +Jumlah Publikasi | +Total | +
|---|