Files

111 lines
3.9 KiB
PHP

<?php
namespace App\Console\Commands\Rekapitulasi;
use App\Models\Verifikator\Rekapitulasi;
use App\Services\DawaiService;
use App\Services\P1DawaiSnapshot;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Throwable;
class RekapP1DawaiJsonCommand extends Command
{
protected $signature = 'rekap:p1-dawai-json {--tahun= : Tahun P1 (default: tahun sekarang)}';
protected $description = 'Ambil data P1 seluruh pegawai dari API Dawai (Januari s.d. sekarang) dan simpan ke JSON (tanpa menulis ke database)';
public function handle(): int
{
$tahun = (int) (filled($this->option('tahun')) ? $this->option('tahun') : Carbon::now()->year);
$bulanTerakhir = $tahun === Carbon::now()->year ? Carbon::now()->month : 12;
if ($tahun > Carbon::now()->year) {
$this->error('Tahun tidak boleh melebihi tahun sekarang.');
return self::FAILURE;
}
$units = Rekapitulasi::where('tahun', $tahun)->pluck('id_unit_kerja')
->map(fn ($id) => (int) $id === 0 ? 99 : (int) $id)
->unique()
->values();
if ($units->isEmpty()) {
$units = collect([99, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
$dawai = new DawaiService;
$pegawaiMap = [];
$gagal = [];
$this->info("Mengambil P1 Dawai tahun {$tahun} (Januari - bulan {$bulanTerakhir}) untuk {$units->count()} unit...");
$bar = $this->output->createProgressBar($bulanTerakhir * $units->count());
$bar->start();
for ($bulan = 1; $bulan <= $bulanTerakhir; $bulan++) {
foreach ($units as $unit) {
try {
$rows = $dawai->getDataP1(null, $tahun, $bulan, $unit);
} catch (Throwable $th) {
$gagal[] = "bulan {$bulan} unit {$unit}: ".$th->getMessage();
$bar->advance();
continue;
}
foreach ($rows as $row) {
$nip = preg_replace("/['.\-]/", '', (string) ($row['nip'] ?? ''));
if ($nip === '') {
continue;
}
$pegawaiMap[$nip] ??= [
'nama' => $row['nama_lengkap'] ?? null,
'jenis_pegawai' => strtolower((string) ($row['jenis_pegawai'] ?? '')),
'id_unit' => $row['id_unit'] ?? null,
'nama_unit' => $row['nama_unit'] ?? null,
'bulanan' => [],
];
$bulanan = &$pegawaiMap[$nip]['bulanan'][$bulan];
$bulanan ??= ['p1' => 0.0, 'pajak' => 0.0, 'netto' => 0.0];
$bulanan['p1'] += (float) ($row['harga_p1'] ?? 0);
$bulanan['pajak'] += (float) ($row['pajak_p1'] ?? 0);
$bulanan['netto'] += (float) ($row['harga_p1_setelah_pajak'] ?? 0);
unset($bulanan);
}
$bar->advance();
}
}
$bar->finish();
$this->newLine(2);
if ($gagal !== []) {
foreach ($gagal as $pesan) {
$this->warn($pesan);
}
$this->error('Sebagian data gagal diambil, file JSON TIDAK disimpan agar tidak lengkap. Coba jalankan ulang.');
return self::FAILURE;
}
$now = Carbon::now();
$path = P1DawaiSnapshot::path($tahun, $now->format('Ymd_His'));
File::ensureDirectoryExists(dirname($path));
File::put($path, json_encode([
'tahun' => $tahun,
'bulan_terakhir' => $bulanTerakhir,
'created_at' => $now->toDateTimeString(),
'data' => $pegawaiMap,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->info('✓ '.count($pegawaiMap)." pegawai disimpan ke {$path}");
return self::SUCCESS;
}
}