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

65 lines
3.5 KiB
PHP

<?php
namespace App\Services\P1Dev;
class P1PaymentReview
{
public function forRecord(object $record, ?array $uploaded, array $snapshots = []): array
{
$calculated = ['bruto' => (float) $record->gaji_yang_dibayarkan, 'pajak' => (float) $record->pph21, 'netto' => (float) $record->gaji_bersih];
$paid = ! empty($record->paid_at);
$payment = $uploaded;
$source = 'Excel bulan ini';
$notice = null;
if ($paid) {
if ($record->gaji_yang_dibayarkan_final === null || $record->pph_21_final === null || $record->gaji_bersih_final === null) {
return ['review' => null, 'known' => false, 'source' => 'Database', 'notice' => 'Sudah ditandai dibayar, tetapi nominal final belum tersedia.'];
}
$actual = ['bruto' => (float) $record->gaji_yang_dibayarkan_final, 'pajak' => (float) $record->pph_21_final, 'netto' => (float) $record->gaji_bersih_final];
$source = 'Nominal final database';
$matches = fn ($candidate) => $candidate !== null && count(array_filter(['bruto', 'pajak', 'netto'], fn ($field) => round((float) ($candidate[$field] ?? 0), 2) !== round($actual[$field], 2))) === 0;
$payment = $matches($uploaded) ? array_replace($uploaded, $actual) : null;
if ($payment === null) {
$key = ltrim((string) $record->nip, "'").':'.(int) $record->bulan;
foreach ($snapshots as $snapshot) {
$candidate = $snapshot[$key] ?? null;
if ($matches($candidate)) { $payment = array_replace($candidate, $actual); break; }
}
$notice = 'Excel aktif berbeda atau tidak tersedia; memakai nominal final database.';
}
$payment ??= $actual;
}
if ($payment === null) {
return ['review' => null, 'known' => false, 'source' => null, 'notice' => 'Belum ada hasil pembayaran untuk dibandingkan.'];
}
$review = $this->compare($calculated, $payment);
$known = isset($payment['components']) || ! $review['needs_review'];
if (! $known) {
$notice = 'Nominal tersedia, tetapi rincian potongan/tambahan belum tersedia. Selisih belum dapat diklasifikasikan.';
}
return ['review' => $review, 'known' => $known, 'source' => $source, 'notice' => $notice];
}
public function compare(array $calculated, array $payment): array
{
$components = $payment['components'] ?? ['reguler' => array_intersect_key($payment, array_flip(['bruto', 'pajak', 'netto']))];
$difference = [];
foreach (['bruto', 'pajak', 'netto'] as $field) {
// Supplements settle this source month's entitlement. Recovery deductions
// settle earlier debt and must not create a new shortfall in this month.
$expected = (float) ($calculated[$field] ?? 0) + (float) ($components['potongan'][$field] ?? 0);
$difference[$field] = round($expected - (float) $payment[$field], 2);
}
$netDifference = $difference['netto'];
return [
'components' => $components,
'calculated' => $calculated,
'difference' => $difference,
'status' => $netDifference >= 0.01 ? 'kekurangan' : ($netDifference <= -0.01 ? 'kelebihan' : (count(array_filter($difference, fn ($amount) => abs($amount) >= 0.01)) ? 'periksa' : 'sesuai')),
'needs_review' => ! isset($components['reguler']) || count(array_filter($difference, fn ($amount) => abs($amount) >= 0.01)) > 0,
];
}
}