Files
verifikator/app/Services/P1Dev/P1PaymentUploadStore.php
2026-10-20 10:24:22 +07:00

98 lines
4.0 KiB
PHP

<?php
namespace App\Services\P1Dev;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class P1PaymentUploadStore
{
private function directory(int $year, int $month): string
{
if ($year < 2025 || $year > 2100 || $month < 1 || $month > 12) {
throw ValidationException::withMessages(['bulan' => 'Pilih tahun dan bulan pembayaran yang valid.']);
}
return sprintf('p1-dev/payments/%04d/%02d', $year, $month);
}
public function put(int $year, int $month, UploadedFile $file, array $upload): array
{
$directory = $this->directory($year, $month);
$payments = array_filter($upload['payments'] ?? [], fn ($key) => (int) substr($key, strrpos($key, ':') + 1) === $month, ARRAY_FILTER_USE_KEY);
if (! $payments) {
throw ValidationException::withMessages(['file' => 'Tidak ditemukan pembayaran untuk bulan yang dipilih. Periksa kolom Bulan atau nama sheet.']);
}
$id = (string) Str::uuid();
$path = $file->storeAs($directory, $id.'.'.$file->extension(), 'local');
if (! $path) {
throw new \RuntimeException('File pembayaran tidak dapat disimpan.');
}
$upload['meta'] = array_merge($upload['meta'], [
'tahun' => $year, 'bulan' => $month, 'upload_id' => $id,
'filename' => $file->getClientOriginalName(), 'path' => $path,
'uploaded_at' => now()->toIso8601String(),
]);
$upload['payments'] = $payments;
$upload['meta']['pegawai'] = count($payments);
$upload['meta']['totals'] = ['bruto' => 0.0, 'pajak' => 0.0, 'netto' => 0.0, 'tambahan' => 0.0, 'potongan' => 0.0];
foreach ($payments as $payment) {
foreach (['bruto', 'pajak', 'netto'] as $field) {
$upload['meta']['totals'][$field] = round($upload['meta']['totals'][$field] + (float) ($payment[$field] ?? 0), 2);
}
$upload['meta']['totals']['tambahan'] += (float) ($payment['components']['tambahan']['netto'] ?? 0);
$upload['meta']['totals']['potongan'] += abs((float) ($payment['components']['potongan']['netto'] ?? 0));
}
try {
// Replace the active period atomically; old Excel files remain available in storage.
$json = json_encode($upload, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
File::replace(Storage::disk('local')->path($directory.'/'.$id.'.json'), $json);
File::replace(Storage::disk('local')->path($directory.'/current.json'), $json);
} catch (\Throwable $e) {
Storage::disk('local')->delete($path);
throw $e;
}
return $upload;
}
public function period(int $year, int $month): array
{
$path = $this->directory($year, $month).'/current.json';
$disk = Storage::disk('local');
if (! $disk->exists($path)) {
return [];
}
return json_decode($disk->get($path), true, 512, JSON_THROW_ON_ERROR);
}
public function get(int $year, int $month): array
{
return $this->period($year, $month) ?: ['meta' => [], 'payments' => []];
}
public function snapshots(int $year, int $month): array
{
$snapshots = [];
foreach (Storage::disk('local')->files($this->directory($year, $month)) as $path) {
if (str_ends_with($path, '.json') && basename($path) !== 'current.json') {
$snapshot = json_decode(Storage::disk('local')->get($path), true, 512, JSON_THROW_ON_ERROR);
$snapshots[] = $snapshot['payments'] ?? [];
}
}
return $snapshots;
}
public function forget(int $year, int $month): void
{
$path = $this->directory($year, $month).'/current.json';
if (Storage::disk('local')->exists($path) && ! Storage::disk('local')->delete($path)) {
throw new \RuntimeException('Pembanding pembayaran tidak dapat dihapus.');
}
}
}