'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April', 5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember', ]; private const PENUNJANG_ORDER = [ 'penunjang_luar_homebase_universitas' => 'Penunjang (LHB Univ)', 'penunjang_luar_homebase_fakultas' => 'Penunjang (LHB Fak)', 'penunjang_homebase' => 'Penunjang (HB)', ]; /** @var array */ private array $penunjangLimits = []; public function handle(): int { $nip = $this->option('nip'); $unitPembayaranId = $this->option('unit-pembayaran-id'); $unitPembayaranId = $unitPembayaranId !== null ? (int) $unitPembayaranId : null; $periode = (int) ($this->option('periode') ?? Periode::where('status', true)->value('id_periode')); if (! $periode) { $this->error('Tidak ada periode aktif. Gunakan --periode=.'); return Command::FAILURE; } $bulan = $periode % 2 === 1 ? [1, 2, 3, 4, 5, 6] : [7, 8, 9, 10, 11, 12]; $this->loadPenunjangLimits(); $this->displayHeader($periode, $nip, $unitPembayaranId); $nips = $this->resolveNips($nip, $periode, $unitPembayaranId); if ($nips->isEmpty()) { $this->warn('Tidak ada pegawai ditemukan untuk filter yang diberikan.'); return Command::SUCCESS; } $this->info(sprintf(' Memproses %d pegawai...', $nips->count())); $this->newLine(); $service = new PerhitunganKegiatanService; $grandTotal = ['bruto' => 0, 'tax' => 0, 'netto' => 0]; $num = 0; foreach ($nips as $pegawai) { $num++; DB::beginTransaction(); try { $kegiatans = $service->listKegiatan($pegawai->nip, $periode, $bulan, $unitPembayaranId, false); } catch (Exception) { DB::rollBack(); $this->warn(" [{$num}] {$pegawai->nip} - {$pegawai->nama} → Gagal dihitung, dilewati."); continue; } DB::rollBack(); if (empty($kegiatans)) { continue; } $this->displayPegawai($num, $pegawai, $kegiatans, $grandTotal); } $this->displayGrandTotal($grandTotal, $num); return Command::SUCCESS; } private function resolveNips(?string $nip, int $periode, ?int $unitPembayaranId) { return Pegawai::query() ->select('verifikator.pegawais.nip', 'verifikator.pegawais.nama') ->join('verifikator.jabatan_pegawais as jp', 'jp.nip', '=', 'verifikator.pegawais.nip') ->join('verifikator.jabatan_pegawai_details as jpd', 'jpd.jabatan_pegawai_id', '=', 'jp.id') ->where('jp.periode', $periode) ->where('jp.active', true) ->when($nip, fn ($q) => $q->where('verifikator.pegawais.nip', $nip)) ->when($unitPembayaranId !== null && $unitPembayaranId !== 0, fn ($q) => $q->where('jpd.unit_pembayaran_id', $unitPembayaranId)) ->orderBy('verifikator.pegawais.nip') ->distinct() ->get(); } private function displayPegawai(int $num, Pegawai $pegawai, array $kegiatans, array &$grandTotal): void { $this->line(sprintf(' [%d] %s — %s', $num, $pegawai->nip, $pegawai->nama)); $this->newLine(); ksort($kegiatans); $unitTotals = []; foreach ($kegiatans as $bulan => $data) { $this->displayBulanSection($bulan, $data, $unitTotals); } // Filter unit yang ada nilainya $activeUnits = array_filter($unitTotals, fn ($u) => $u['bruto'] > 0); $multiUnit = count($activeUnits) > 1; $totalBruto = $totalTax = $totalNetto = 0; $this->line(' TOTAL KESELURUHAN'); foreach ($activeUnits as $unitId => $unit) { $label = $multiUnit ? sprintf(' [%s] %-30s', $unitId, $unit['nama']) : ' Total '; $this->line(sprintf( '%s Bruto: Rp %s │ Pajak: Rp %s │ Netto: Rp %s', $label, number_format($unit['bruto'], 0, ',', '.'), number_format($unit['tax'], 0, ',', '.'), number_format($unit['netto'], 0, ',', '.'), )); $totalBruto += $unit['bruto']; $totalTax += $unit['tax']; $totalNetto += $unit['netto']; } $this->line(' ──────────────────────────────────────────────────────────'); $this->newLine(); $grandTotal['bruto'] += $totalBruto; $grandTotal['tax'] += $totalTax; $grandTotal['netto'] += $totalNetto; } private function displayBulanSection(int $bulan, array $data, array &$unitTotals): void { $namaBulan = self::NAMA_BULAN[$bulan] ?? "Bulan $bulan"; $this->line(sprintf(' ┌─ %s %s', $namaBulan, str_repeat('─', max(0, 50 - mb_strlen($namaBulan))))); // Kegiatan detail table $kegiatanRows = $this->buildKegiatanRows($data); if (! empty($kegiatanRows)) { $this->table(['#', 'Kegiatan', 'Poin Total', 'Poin Remun', 'Poin P2', 'Nominal (Rp)'], $kegiatanRows); } else { $this->line(' (Tidak ada kegiatan dengan poin)'); $this->newLine(); } // Ringkasan poin & batas $this->displayRingkasanPoin($data); // P2 per unit (hanya yang non-zero) $units = collect($data['units'] ?? [])->filter(fn ($u) => ($u['p2']['ratio'] ?? 0) > 0); if ($units->isNotEmpty()) { foreach ($units as $unit) { $unitId = $unit['id']; if (! isset($unitTotals[$unitId])) { $unitTotals[$unitId] = ['nama' => $unit['nama'], 'bruto' => 0, 'tax' => 0, 'netto' => 0]; } $unitTotals[$unitId]['bruto'] += $unit['p2']['ratio'] ?? 0; $unitTotals[$unitId]['tax'] += $unit['p2']['tax'] ?? 0; $unitTotals[$unitId]['netto'] += $unit['p2']['netto'] ?? 0; $rasio = $unit['rasio'] ?? 1; $this->line(sprintf( ' P2 [%s] %-25s Rasio: %s%% │ Bruto: Rp %s │ Pajak: Rp %s │ Netto: Rp %s', $unitId, mb_substr($unit['nama'], 0, 25), number_format($rasio * 100, 0), number_format($unit['p2']['ratio'] ?? 0, 0, ',', '.'), number_format($unit['p2']['tax'] ?? 0, 0, ',', '.'), number_format($unit['p2']['netto'] ?? 0, 0, ',', '.'), )); } } $this->newLine(); } private function buildKegiatanRows(array $data): array { $grouped = $this->collectKegiatanByGroup($data); // Nilai per poin: distribusikan total P2 (ratio) proporsional ke tiap poin_p2 kegiatan $totalP2Ratio = collect($data['units'] ?? [])->sum(fn ($u) => $u['p2']['ratio'] ?? 0); $totalPoinP2Kegiatan = (float) ($data['total_poin']['poin_p2']['kegiatan'] ?? 0); $nilaiPerPoin = $totalPoinP2Kegiatan > 0 ? $totalP2Ratio / $totalPoinP2Kegiatan : 0; $rows = []; $isFirstGroup = true; foreach ($grouped as $label => $items) { if (empty($items)) { continue; } if (! $isFirstGroup) { $rows[] = new TableSeparator; } $isFirstGroup = false; $num = 0; $rows[] = ['', " $label ", '', '', '', '']; $currentSubGroup = null; $isFirstSubGroup = true; $groupTotals = ['poin_total' => 0.0, 'poin_remun' => 0.0, 'poin_p2' => 0.0, 'nominal' => 0.0]; $subGroupTotals = ['poin_total' => 0.0, 'poin_remun' => 0.0, 'poin_p2' => 0.0, 'nominal' => 0.0, 'count' => 0]; foreach ($items as $item) { // Sub-group header (mis. per kategori penunjang atau per jenjang) if (($item['sub_group'] ?? null) !== $currentSubGroup) { // Flush subtotal sub-group sebelumnya if ($currentSubGroup !== null && $subGroupTotals['count'] > 0) { $rows[] = $this->buildSubtotalRow("Subtotal {$currentSubGroup}", $subGroupTotals, 'subgroup'); $subGroupTotals = ['poin_total' => 0.0, 'poin_remun' => 0.0, 'poin_p2' => 0.0, 'nominal' => 0.0, 'count' => 0]; } $currentSubGroup = $item['sub_group'] ?? null; if ($currentSubGroup !== null) { if (! $isFirstSubGroup) { $rows[] = new TableSeparator; } $isFirstSubGroup = false; $limitStr = $item['sub_limit'] !== null ? ' [Maks: '.(int) $item['sub_limit'].' pengajuan]' : ''; $rows[] = ['', " {$currentSubGroup}{$limitStr}", '', '', '', '']; $num = 0; } } $num++; $nominal = $item['poin_p2'] * $nilaiPerPoin; $cut = ($item['poin_total'] - $item['poin_remun']) > 0.001; $p1Cut = ! $cut && $item['poin_p2'] < 0.001; if ($cut) { $rows[] = [ "{$num}", ''.$this->truncateName($item['nama']).'', ''.number_format($item['poin_total'], 2, ',', '.').'', ''.number_format($item['poin_remun'], 2, ',', '.').' ↓', ''.number_format($item['poin_p2'], 2, ',', '.').'', ''.number_format($nominal, 0, ',', '.').'', ]; } elseif ($p1Cut) { $rows[] = [ "{$num}", ''.$this->truncateName($item['nama']).'', ''.number_format($item['poin_total'], 2, ',', '.').'', ''.number_format($item['poin_remun'], 2, ',', '.').'', ''.number_format($item['poin_p2'], 2, ',', '.').' ↓P1', ''.number_format($nominal, 0, ',', '.').'', ]; } else { $rows[] = [ $num, $this->truncateName($item['nama']), number_format($item['poin_total'], 2, ',', '.'), number_format($item['poin_remun'], 2, ',', '.'), number_format($item['poin_p2'], 2, ',', '.'), number_format($nominal, 0, ',', '.'), ]; } $subGroupTotals['poin_total'] += $item['poin_total']; $subGroupTotals['poin_remun'] += $item['poin_remun']; $subGroupTotals['poin_p2'] += $item['poin_p2']; $subGroupTotals['nominal'] += $nominal; $subGroupTotals['count']++; $groupTotals['poin_total'] += $item['poin_total']; $groupTotals['poin_remun'] += $item['poin_remun']; $groupTotals['poin_p2'] += $item['poin_p2']; $groupTotals['nominal'] += $nominal; } // Flush subtotal sub-group terakhir if ($currentSubGroup !== null && $subGroupTotals['count'] > 0) { $rows[] = $this->buildSubtotalRow("Subtotal {$currentSubGroup}", $subGroupTotals, 'subgroup'); } // Total per group (kategori) $rows[] = new TableSeparator; $rows[] = $this->buildSubtotalRow("Total {$label}", $groupTotals, 'group'); } return $rows; } /** @param array{poin_total: float, poin_remun: float, poin_p2: float, nominal: float} $totals */ private function buildSubtotalRow(string $label, array $totals, string $type): array { $color = $type === 'group' ? '#FF69B4' : 'cyan'; return [ '', " {$label} ", ''.number_format($totals['poin_total'], 2, ',', '.').'', ''.number_format($totals['poin_remun'], 2, ',', '.').'', ''.number_format($totals['poin_p2'], 2, ',', '.').'', ''.number_format($totals['nominal'], 0, ',', '.').'', ]; } private function collectKegiatanByGroup(array $data): array { // Urutan grup mencerminkan urutan pemotongan di PerhitunganKegiatanService $groups = [ 'Mengajar (HB)' => [], 'Membimbing (HB)' => [], 'Menguji (HB)' => [], 'Lainnya (HB)' => [], 'Mengajar (LHB)' => [], 'Membimbing (LHB)' => [], 'Menguji (LHB)' => [], 'Lainnya (LHB)' => [], 'Penunjang (LHB Univ)' => [], 'Penunjang (LHB Fak)' => [], 'Penunjang (HB)' => [], 'Semester Antara (HB)' => [], 'Semester Antara (LHB)' => [], ]; // HB Mengajar, Membimbing, Menguji — per jenjang sesuai urutan service foreach (['S1', 'S2', 'S3', 'MKWU'] as $jenjang) { foreach ($data['tri_dharma_homebase']['mengajar'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Mengajar (HB)', $jenjang); } } foreach (['Akademik', 'D3', 'PROFESI', 'S1', 'S2', 'S3'] as $jenjang) { foreach ($data['tri_dharma_homebase']['membimbing'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Membimbing (HB)', $jenjang); } } foreach (['D3', 'PROFESI', 'S1', 'S2', 'S3'] as $jenjang) { foreach ($data['tri_dharma_homebase']['menguji'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Menguji (HB)', $jenjang); } } // HB Lainnya (Collection langsung, tidak per jenjang) foreach (['mengajarLainnya', 'membimbingLainnya', 'mengujiLainnya', 'pengabdian', 'penelitian', 'asesor', 'reviewer', 'modul'] as $subKey) { foreach ($data['tri_dharma_homebase'][$subKey] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Lainnya (HB)'); } } foreach ($data['tri_dharma_homebase']['buku_ajar'] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Buku Ajar (HB)'); } // LHB Mengajar, Membimbing, Menguji — per jenjang foreach (['S1', 'S2', 'S3', 'MKWU'] as $jenjang) { foreach ($data['tri_dharma_luar_homebase']['mengajar'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Mengajar (LHB)', $jenjang); } } foreach (['Akademik', 'D3', 'PROFESI', 'S1', 'S2', 'S3'] as $jenjang) { foreach ($data['tri_dharma_luar_homebase']['membimbing'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Membimbing (LHB)', $jenjang); } } foreach (['D3', 'PROFESI', 'S1', 'S2', 'S3'] as $jenjang) { foreach ($data['tri_dharma_luar_homebase']['menguji'][$jenjang] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Menguji (LHB)', $jenjang); } } // LHB Lainnya foreach (['mengajarLainnya', 'membimbingLainnya', 'mengujiLainnya', 'pengabdian', 'penelitian'] as $subKey) { foreach ($data['tri_dharma_luar_homebase'][$subKey] ?? [] as $item) { $this->addItemToGroup($item, $groups, 'Lainnya (LHB)'); } } // Penunjang: LHB Univ → LHB Fak → HB (sesuai urutan service) foreach (self::PENUNJANG_ORDER as $key => $groupName) { foreach (collect($data[$key] ?? []) as $item) { $katId = (int) ($item->id_kategori ?? 0); $katInfo = $this->penunjangLimits[$katId] ?? ['nama' => 'Penunjang', 'limit' => null]; $this->addItemToGroup($item, $groups, $groupName, $katInfo['nama'], $katInfo['limit'], $katId); } usort($groups[$groupName], fn ($a, $b) => ($a['sub_group_order'] ?? 0) <=> ($b['sub_group_order'] ?? 0)); } // Semester Antara: HB → LHB foreach (collect($data['semester_antara_homebase'] ?? []) as $item) { $this->addItemToGroup($item, $groups, 'Semester Antara (HB)'); } foreach (collect($data['semester_antara_luar_homebase'] ?? []) as $item) { $this->addItemToGroup($item, $groups, 'Semester Antara (LHB)'); } return $groups; } private function addItemToGroup( mixed $item, array &$groups, string $groupName, ?string $subGroup = null, ?float $subLimit = null, ?int $subGroupOrder = null, ): void { if ($item instanceof Collection) { $item = (object) $item->all(); } $poinTotal = (float) ($item->poin_total ?? 0); // Tampilkan semua item yang punya poin_total > 0, termasuk yang dipotong ke 0 if ($poinTotal == 0) { return; } $rawNama = $item->nama_kegiatan ?? $item->kegiatan?->nama_kegiatan ?? $item->nama ?? $item->judul ?? '-'; $groups[$groupName][] = [ 'nama' => preg_replace('/\s+/', ' ', trim((string) $rawNama)), 'poin_total' => $poinTotal, 'poin_remun' => (float) ($item->poin_remunerasi ?? 0), 'poin_p2' => (float) ($item->poin_p2 ?? 0), 'sub_group' => $subGroup, 'sub_limit' => $subLimit, 'sub_group_order' => $subGroupOrder, ]; } private function truncateName(string $nama, int $max = 50): string { if (mb_strlen($nama) <= $max) { return $nama; } return mb_substr($nama, 0, $max - 3).'...'; } private function loadPenunjangLimits(): void { $this->penunjangLimits = KategoriRubrik::with('limitPenunjang') ->get() ->keyBy('id') ->map(fn ($k) => [ 'nama' => $k->nama ?? '-', 'limit' => $k->limitPenunjang?->limit !== null ? (float) $k->limitPenunjang->limit : null, ]) ->all(); } private function displayRingkasanPoin(array $data): void { $totalPoin = $data['total_poin'] ?? []; $poinRemun = $totalPoin['poin_remunerasi'] ?? []; $poinP2 = $totalPoin['poin_p2'] ?? []; $limitKegiatan = $totalPoin['limit']['poin_kegiatan'] ?? null; $jenis = $data['jenis_pegawai'] ?? 'dosen'; $cap = $jenis === 'dosen' ? 68 : 54; $totalRemun = array_sum($poinRemun); $totalP2 = array_sum($poinP2); $setelahCap = min($totalRemun, $cap); $p1Deduction = max(0, round($setelahCap - $totalP2, 2)); $labels = [ 'kegiatan' => 'Kegiatan', 'skp' => 'SKP', 'presensi' => 'Presensi / Absen', 'grade_14_17' => 'Grade 14-17', ]; $rows = []; foreach ($labels as $key => $label) { $remun = (float) ($poinRemun[$key] ?? 0); $p2 = (float) ($poinP2[$key] ?? 0); if ($remun == 0 && $p2 == 0) { continue; } $rows[] = [$label, number_format($remun, 2, ',', '.'), number_format($p2, 2, ',', '.')]; } if (empty($rows)) { return; } $rows[] = ['Total', number_format($totalRemun, 2, ',', '.'), number_format($totalP2, 2, ',', '.')]; $this->table(['Komponen Poin', 'Poin Remun', 'Poin P2'], $rows); // Info batas $batasInfo = sprintf('Batas maks: %d', $cap); if ($limitKegiatan !== null) { $batasInfo .= sprintf( ' | Limit kegiatan: %s (tercapai: %s)', number_format($limitKegiatan['limit'], 0, ',', '.'), number_format($limitKegiatan['current'], 2, ',', '.'), ); } $batasInfo .= sprintf(' | Potongan P1: %s', number_format($p1Deduction, 2, ',', '.')); $this->line(sprintf(' %s', $batasInfo)); $this->newLine(); } private function displayHeader(int $periode, ?string $nip, ?int $unitPembayaranId): void { $this->newLine(); $this->info('══════════════════════════════════════════════════════════════'); $this->info(' CEK PERHITUNGAN REMUNERASI (SEMENTARA / PREVIEW) '); $this->info('══════════════════════════════════════════════════════════════'); $this->info(sprintf(' Periode : %s', $periode)); $this->info(sprintf(' NIP : %s', $nip ?? 'Semua')); $this->info(sprintf(' Unit Pembayaran : %s', $unitPembayaranId ?? 'Semua')); $this->info(' * Tidak ada data yang disimpan ke database '); $this->info('══════════════════════════════════════════════════════════════'); $this->newLine(); } private function displayGrandTotal(array $grandTotal, int $totalPegawai): void { $this->info('══════════════════════════════════════════════════════════════'); $this->info(sprintf(' TOTAL PEGAWAI DIPROSES : %d', $totalPegawai)); $this->info(sprintf(' GRAND TOTAL BRUTO : Rp %s', number_format($grandTotal['bruto'], 0, ',', '.'))); $this->info(sprintf(' GRAND TOTAL PAJAK : Rp %s', number_format($grandTotal['tax'], 0, ',', '.'))); $this->info(sprintf(' GRAND TOTAL NETTO : Rp %s', number_format($grandTotal['netto'], 0, ',', '.'))); $this->info('══════════════════════════════════════════════════════════════'); } }