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; $includePppk2025 = $request->boolean('include_pppk_2025'); $includeP1Zero = $request->boolean('include_p1_zero'); $rows = collect($this->getRekapRows($tahun, $jenisPegawai, $unit, $includePppk2025, $includeP1Zero)); 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; $includePppk2025 = $request->boolean('include_pppk_2025'); $includeP1Zero = $request->boolean('include_p1_zero'); $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, $includePppk2025, $includeP1Zero), $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, bool $includePppk2025 = false, bool $includeP1Zero = false): 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(); } if (! $includePppk2025) { $rows = $rows->filter(fn ($row) => ! str_contains($row['nip'] ?? '', '202521'))->values(); } if (! $includeP1Zero) { $rows = $rows->filter(fn ($row) => ($row['jumlah_p1'] ?? 0) > 0)->values(); } return $rows->sortBy('nama', SORT_NATURAL | SORT_FLAG_CASE)->values()->all(); } private function buildRekap(int $tahun, ?string $jenisPegawai): Collection { $cacheKey = 'remunerasi_keseluruhan_v5_'.$tahun.'_'.($jenisPegawai ?: 'semua'); return collect(Cache::remember($cacheKey, now()->addHours(6), fn () => $this->computeRekap($tahun, $jenisPegawai))); } /** * Bulan terakhir yang diikutkan dalam akumulasi P1 "Januari - Sekarang": bulan berjalan kalau * tahun yang dipilih adalah tahun sekarang, atau Desember kalau tahun yang dipilih sudah lewat. * Dipakai bersama oleh computeRekap() dan KeseluruhanExport supaya jumlah kolom bulanan konsisten. */ public function getBulanTerakhir(int $tahun): int { return $tahun === (int) now()->year ? (int) now()->month : 12; } 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 = $this->getBulanTerakhir($tahun); $p1Rows = 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)); $p1 = $p1Rows->map(fn ($rows) => (float) $rows->sum('gaji_yang_dibayarkan')); // Rincian per bulan (dipakai untuk breakdown kolom bulanan di export Excel). $p1Bulanan = $p1Rows->map(fn ($rows) => $rows->groupBy('bulan')->map(fn ($bulanRows) => (float) $bulanRows->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) ->where('jp.active', true) ->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, $p1Bulanan, $bulanTerakhir, $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; } $bulananNip = $p1Bulanan->get($nip); $jumlahP1Bulanan = []; for ($bulan = 1; $bulan <= $bulanTerakhir; $bulan++) { $jumlahP1Bulanan[$bulan] = $bulananNip?->get($bulan, 0.0) ?? 0.0; } 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_p1_bulanan' => $jumlahP1Bulanan, 'jumlah_p2' => $jumlahP2, 'jumlah_thr' => $jumlahThr, 'jumlah_r13' => $jumlahR13, 'jumlah_publikasi' => $jumlahPublikasi, 'total' => $total, ]; })->filter()->values()->all(); } }