54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
/**
|
|
* Snapshot JSON P1 hasil tarikan API Dawai (read-only terhadap DB): resources/data/{tahun}/dawai_p1/latest_p1_dawai_{Ymd_His}.json.
|
|
* Format: {"tahun": 2026, "bulan_terakhir": 9, "created_at": "...", "data": {"<nip>": {"nama", "jenis_pegawai",
|
|
* "id_unit", "nama_unit", "bulanan": {"<bulan>": {"p1", "pajak", "netto"}}}}}.
|
|
*/
|
|
class P1DawaiSnapshot
|
|
{
|
|
public static function directory(int $tahun): string
|
|
{
|
|
return resource_path("data/{$tahun}/dawai_p1");
|
|
}
|
|
|
|
public static function path(int $tahun, string $tanggal): string
|
|
{
|
|
return self::directory($tahun)."/latest_p1_dawai_{$tanggal}.json";
|
|
}
|
|
|
|
/**
|
|
* File snapshot terbaru (berdasarkan tanggal & jam di nama file) untuk tahun tertentu, atau null bila belum ada.
|
|
*/
|
|
public static function latestPath(int $tahun): ?string
|
|
{
|
|
$files = glob(self::directory($tahun).'/latest_p1_dawai_*.json') ?: [];
|
|
|
|
if ($files === []) {
|
|
return null;
|
|
}
|
|
|
|
// Urutkan berdasarkan tanggal(+jam) di nama file, apa pun formatnya (2026-09-29, 20260929_185856, dst.);
|
|
// bila sama, yang terakhir diubah dianggap terbaru.
|
|
$stamp = fn (string $file) => str_pad(substr(preg_replace('/\D/', '', basename($file)), 0, 14), 14, '0');
|
|
usort($files, fn ($a, $b) => [$stamp($a), filemtime($a)] <=> [$stamp($b), filemtime($b)]);
|
|
|
|
return end($files);
|
|
}
|
|
|
|
public static function load(int $tahun): ?array
|
|
{
|
|
$path = self::latestPath($tahun);
|
|
|
|
if ($path === null) {
|
|
return null;
|
|
}
|
|
|
|
$decoded = json_decode((string) file_get_contents($path), true);
|
|
|
|
return is_array($decoded) ? $decoded + ['_file' => basename($path)] : null;
|
|
}
|
|
}
|