144 lines
5.8 KiB
PHP
144 lines
5.8 KiB
PHP
<?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;
|
|
}
|
|
}
|