Merge pull request 'Penambahan Kekurangan P2 Manual' (#39) from development into main

Reviewed-on: #39
This commit was merged in pull request #39.
This commit is contained in:
2026-09-30 03:22:11 +07:00
4 changed files with 219 additions and 10 deletions
@@ -0,0 +1,143 @@
<?php
namespace App\Console\Commands\Remunerasi;
use App\Models\Verifikator\Periode;
use App\Models\Verifikator\View\Pegawai;
use App\Services\PerhitunganKegiatanService;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class GenerateKekuranganP2JsonCommand extends Command
{
protected $signature = 'remunerasi:p2-json
{nip* : NIP pegawai (boleh lebih dari satu)}
{--periode= : ID periode (default: periode aktif)}
{--tahun= : Tahun folder JSON (default: diambil dari nama periode)}
{--dry-run : Tampilkan hasil saja, jangan tulis file JSON}';
protected $description = 'Hitung P2 pegawai lewat listKegiatan (save=false) lalu simpan/perbarui ke JSON kekurangan P2 (resources/data/{tahun}/kekurangan_p2_{periode}.json). Tidak menyimpan ke database.';
public function handle(): int
{
$periode = (int) ($this->option('periode') ?? Periode::where('status', true)->value('id_periode'));
if (! $periode) {
$this->error('Tidak ada periode aktif. Gunakan --periode=<id_periode>.');
return self::FAILURE;
}
$namaPeriode = (string) Periode::where('id_periode', $periode)->value('nama');
$tahun = (int) ($this->option('tahun') ?: (preg_match('/\d{4}/', $namaPeriode, $m) ? $m[0] : 0));
if (! $tahun) {
$this->error('Tahun tidak dapat ditentukan dari nama periode. Gunakan --tahun=<tahun>.');
return self::FAILURE;
}
$bulan = $periode % 2 === 1 ? [1, 2, 3, 4, 5, 6] : [7, 8, 9, 10, 11, 12];
$service = new PerhitunganKegiatanService;
$hasil = [];
foreach ($this->argument('nip') as $nip) {
$nip = preg_replace("/['.\-\s]/", '', $nip);
$pegawai = Pegawai::withoutGlobalScope('aktif')
->with([
'jabatans' => fn ($q) => $q->where('periode', $periode),
'jabatans.detail' => fn ($q) => $q->whereIn('bulan', $bulan),
])
->where(fn ($q) => $q->where('nip', $nip)->orWhere('nip_siakad', $nip))
->first();
if (! $pegawai) {
$this->warn("{$nip}: pegawai tidak ditemukan, dilewati.");
continue;
}
// listKegiatan menulis ke DB (hapus/buat remunerasi) untuk bulan tanpa p2_jf/p2_jtt walau save=false.
// Karena itu hanya bulan yang berhak P2 (p2_jf atau p2_jtt true) yang dihitung; bulan lain dilewati
// (nilai P2-nya memang 0) sehingga tidak ada penulisan ke DB.
$details = ($pegawai->jabatans->first()?->detail ?? collect());
$bulanLayak = $details->filter(fn ($d) => $d->p2_jf === true || $d->p2_jtt === true)->pluck('bulan')->sort()->values()->all();
$bulanDilewati = $details->pluck('bulan')->diff($bulanLayak)->sort()->values()->all();
if ($bulanLayak === []) {
$this->warn("{$nip} ({$pegawai->nama}): tidak ada bulan yang berhak P2 di periode ini, dilewati.");
continue;
}
if ($bulanDilewati !== []) {
$this->line(" {$nip}: bulan ".implode(', ', $bulanDilewati).' dilewati (tanpa p2_jf/p2_jtt); dihitung bulan '.implode(', ', $bulanLayak).'.');
}
try {
$kegiatans = $service->listKegiatan($pegawai->nip, $periode, $bulanLayak, null, false);
} catch (Exception $e) {
$this->warn("{$nip} ({$pegawai->nama}): gagal dihitung - {$e->getMessage()}");
continue;
}
// Jumlahkan P2 seluruh unit pembayaran pada semua bulan di periode.
$total = ['p2_bruto' => 0.0, 'p2_pajak' => 0.0, 'p2_netto' => 0.0];
foreach ($kegiatans ?: [] as $dataBulan) {
foreach ($dataBulan['units'] ?? [] as $unit) {
$total['p2_bruto'] += (float) ($unit['p2']['ratio'] ?? 0);
$total['p2_pajak'] += (float) ($unit['p2']['tax'] ?? 0);
$total['p2_netto'] += (float) ($unit['p2']['netto'] ?? 0);
}
}
$hasil[$pegawai->nip] = array_map(fn ($v) => $this->rapikan($v), $total);
$this->line(sprintf(
'%s %-35s bruto: Rp %s | pajak: Rp %s | netto: Rp %s',
$pegawai->nip,
mb_substr((string) $pegawai->nama, 0, 35),
number_format($total['p2_bruto'], 2, ',', '.'),
number_format($total['p2_pajak'], 2, ',', '.'),
number_format($total['p2_netto'], 2, ',', '.'),
));
}
if ($hasil === []) {
$this->warn('Tidak ada data yang bisa disimpan.');
return self::FAILURE;
}
if ($this->option('dry-run')) {
$this->info('Dry-run: file JSON tidak diubah.');
return self::SUCCESS;
}
$path = resource_path("data/{$tahun}/kekurangan_p2_{$periode}.json");
$existing = is_file($path) ? (json_decode((string) file_get_contents($path), true) ?: []) : [];
$merged = array_replace($existing, $hasil);
ksort($merged, SORT_STRING);
File::ensureDirectoryExists(dirname($path));
File::put($path, json_encode($merged, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->info(sprintf('✓ %d pegawai disimpan/diperbarui di %s (total %d entri)', count($hasil), $path, count($merged)));
return self::SUCCESS;
}
/**
* Bulatkan 2 desimal dan buang ".0" pada bilangan bulat agar format sama dengan isi file yang sudah ada.
*/
private function rapikan(float $nilai): int|float
{
$nilai = round($nilai, 2);
return $nilai == floor($nilai) ? (int) $nilai : $nilai;
}
}
@@ -227,7 +227,11 @@ class KeseluruhanController extends Controller
*/
private function rekapCacheKey(int $tahun, ?string $jenisPegawai, string $sumberP1): string
{
$key = 'remunerasi_keseluruhan_v8_'.$tahun.'_'.($jenisPegawai ?: 'semua');
$key = 'remunerasi_keseluruhan_v9_'.$tahun.'_'.($jenisPegawai ?: 'semua');
// Cache ikut berganti bila JSON kekurangan P2 diubah.
$mtimeP2 = collect(glob(resource_path("data/{$tahun}/kekurangan_p2_*.json")) ?: [])->map(fn ($f) => filemtime($f))->max();
$key .= '_p2json_'.($mtimeP2 ?? 0);
if ($sumberP1 === 'dawai') {
$file = P1DawaiSnapshot::latestPath($tahun);
@@ -341,11 +345,11 @@ class KeseluruhanController extends Controller
$p2Pajak = $p2Rows->map(fn ($row) => (float) $row->total_pajak);
$p2Netto = $p2Rows->map(fn ($row) => (float) $row->total_netto);
// Fallback untuk pegawai (CPNS) yang P2-nya belum tersimpan di remunerasi_per_bulans:
// ambil dari snapshot JSON hasil perhitungan (read-only), tanpa menulis ke DB.
// Snapshot JSON hasil perhitungan (read-only, tanpa menulis ke DB): bila pegawai ada di JSON
// dengan P2 > 0, nilai JSON menimpa nilai dari DB (juga untuk pegawai yang belum ada di DB, mis. CPNS).
foreach ($this->loadP2Snapshot($tahun, $periodeSemester1) as $nip => $nilai) {
$key = $normalize($nip);
if (! $p2->has($key) && ($nilai['p2_bruto'] ?? 0) > 0) {
if (($nilai['p2_bruto'] ?? 0) > 0) {
$p2->put($key, (float) $nilai['p2_bruto']);
$p2Pajak->put($key, (float) ($nilai['p2_pajak'] ?? 0));
$p2Netto->put($key, (float) ($nilai['p2_netto'] ?? 0));
@@ -374,9 +378,9 @@ class KeseluruhanController extends Controller
$r13 = collect($r13Data)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_r13'] ?? 0));
$r13Netto = collect($r13Data)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_r13_setelah_pajak'] ?? 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');
// 5. Publikasi = hanya periode Semester 1 tahun terpilih (mis. 401 untuk 2026), sama seperti P2;
// Semester 2 belum waktunya perhitungan dan periode Ganjil/Genap tahun sebelumnya tidak dihitung.
$periodeIds = collect($periodeSemester1 ? [$periodeSemester1] : []);
$publikasiQuery = Publikasi::query()
->whereIn('periode', $periodeIds)
@@ -1,4 +1,64 @@
{
"196801131992032001": {
"p2_bruto": 4936330,
"p2_pajak": 246816.5,
"p2_netto": 4689513.5
},
"196802291993032001": {
"p2_bruto": 11615019,
"p2_pajak": 580750.95,
"p2_netto": 11034268.05
},
"196803052014091004": {
"p2_bruto": 12094075.5,
"p2_pajak": 604703.77,
"p2_netto": 11489371.73
},
"196804012000032001": {
"p2_bruto": 17887577.5,
"p2_pajak": 894378.88,
"p2_netto": 16993198.63
},
"196804152007012001": {
"p2_bruto": 10354750,
"p2_pajak": 517737.5,
"p2_netto": 9837012.5
},
"196804241987021001": {
"p2_bruto": 23308480,
"p2_pajak": 1165424,
"p2_netto": 22143056
},
"196806011987031001": {
"p2_bruto": 5432000,
"p2_pajak": 271600,
"p2_netto": 5160400
},
"196808191996031003": {
"p2_bruto": 13853527.5,
"p2_pajak": 692676.38,
"p2_netto": 13160851.13
},
"198006032005012002": {
"p2_bruto": 14041210,
"p2_pajak": 702060.5,
"p2_netto": 13339149.5
},
"198410182008012002": {
"p2_bruto": 3184142.5,
"p2_pajak": 159207.13,
"p2_netto": 3024935.38
},
"198601172012122004": {
"p2_bruto": 8219175,
"p2_pajak": 410958.75,
"p2_netto": 7808216.25
},
"198908032022032005": {
"p2_bruto": 3387288,
"p2_pajak": 169364.4,
"p2_netto": 3217923.6
},
"199002132025062005": {
"p2_bruto": 0,
"p2_pajak": 0,
@@ -211,12 +211,12 @@
'"></i> ' + jenis.toUpperCase() + '</small>';
}
// Penanda pegawai yang P2-nya diambil dari snapshot JSON (CPNS), warna tosca.
// Penanda pegawai yang P2-nya diambil dari snapshot JSON, warna tosca.
function renderCpnsTag(sumber) {
if (sumber !== 'json') {
return '';
}
return ' <small class="d-inline-block fw-bold" title="P2 dari snapshot JSON (belum tersimpan di DB)" style="padding: 1px 18px 1px 6px; color: #0fb5a5; background: linear-gradient(to right, rgba(20, 184, 166, 0.25), rgba(20, 184, 166, 0)); clip-path: polygon(0 0, 100% 0, calc(100% - 20px) 100%, 0 100%);"><i class="uil-file-check-alt"></i> CPNS</small>';
return ' <small class="d-inline-block fw-bold" title="P2 diambil dari snapshot JSON" style="padding: 1px 18px 1px 6px; color: #0fb5a5; background: linear-gradient(to right, rgba(20, 184, 166, 0.25), rgba(20, 184, 166, 0)); clip-path: polygon(0 0, 100% 0, calc(100% - 20px) 100%, 0 100%);"><i class="uil-file-check-alt"></i> P2 JSON</small>';
}
var table = $('#keseluruhan-table').DataTable({
@@ -257,7 +257,9 @@
{ data: 'jumlah_thr', name: 'jumlah_thr', className: 'text-end font-monospace' },
{ data: 'jumlah_r13', name: 'jumlah_r13', className: 'text-end font-monospace' },
{ data: 'jumlah_publikasi', name: 'jumlah_publikasi', className: 'text-end font-monospace' },
{ data: 'total', name: 'total', className: 'text-end font-monospace fw-bold' }
{ data: 'total', name: 'total', className: 'text-end font-monospace fw-bold' },
// Kolom tersembunyi supaya pencarian tabel juga bisa memakai NIP.
{ data: 'nip', name: 'nip', visible: false, searchable: true, orderable: false }
],
order: [],
language: {