Penambahan Kekurangan P2 Manual
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user