66 lines
3.2 KiB
PHP
66 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\P1Dev;
|
|
|
|
use App\Models\Rekapitulasi\P1Dev as P1;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class P1MarkPayment
|
|
{
|
|
public static function validateDifference(bool $hasDifference, int $year, int $month, bool $acknowledged): void
|
|
{
|
|
if (! $hasDifference) {
|
|
return;
|
|
}
|
|
if (! $acknowledged) {
|
|
throw ValidationException::withMessages(['acknowledge_difference' => 'Centang peringatan selisih untuk menggunakan nominal pembayaran dari Excel.']);
|
|
}
|
|
}
|
|
|
|
public function mark(array $ids, int $year, int $month, array $upload, ?int $unit, bool $acknowledged = false): int
|
|
{
|
|
if ((int) ($upload['meta']['tahun'] ?? 0) !== $year || (int) ($upload['meta']['bulan'] ?? 0) !== $month) {
|
|
throw ValidationException::withMessages(['file' => 'Unggah Excel pembayaran untuk tahun dan bulan yang dipilih.']);
|
|
}
|
|
|
|
return (new P1)->getConnection()->transaction(function () use ($ids, $year, $month, $upload, $unit, $acknowledged) {
|
|
$rows = P1::whereIn('id', $ids)->where('tahun', $year)->where('bulan', $month)
|
|
->when($unit, fn ($q) => $q->where('id_unit_kerja', $unit))->lockForUpdate()->get();
|
|
if ($rows->count() !== count($ids)) {
|
|
throw ValidationException::withMessages(['ids' => 'Ada baris di luar periode/unit yang dipilih atau sudah dihapus. Muat ulang tabel.']);
|
|
}
|
|
foreach ($rows as $row) {
|
|
$payment = $upload['payments'][ltrim($row->nip, "'").':'.$month] ?? null;
|
|
if ($row->paid_at || ! $payment) {
|
|
throw ValidationException::withMessages(['ids' => 'Ada baris yang sudah ditandai atau tidak memiliki pembayaran di Excel. Muat ulang tabel.']);
|
|
}
|
|
foreach (['bruto', 'pajak', 'netto'] as $field) {
|
|
if (! isset($payment[$field]) || ! is_numeric($payment[$field]) || ! is_finite((float) $payment[$field])) {
|
|
throw ValidationException::withMessages(['file' => 'Nominal pembayaran Excel tidak valid. Unggah ulang file.']);
|
|
}
|
|
}
|
|
if (abs((float) $payment['bruto'] - (float) $payment['pajak'] - (float) $payment['netto']) > 0.01) {
|
|
throw ValidationException::withMessages(['file' => 'Bruto, pajak, dan netto pembayaran tidak konsisten.']);
|
|
}
|
|
$review = (new P1PaymentReview)->compare([
|
|
'bruto' => (float) $row->gaji_yang_dibayarkan,
|
|
'pajak' => (float) $row->pph21,
|
|
'netto' => (float) $row->gaji_bersih,
|
|
], $payment);
|
|
self::validateDifference($review['needs_review'], $year, $month, $acknowledged);
|
|
}
|
|
foreach ($rows as $row) {
|
|
$payment = $upload['payments'][ltrim($row->nip, "'").':'.$month];
|
|
$row->update([
|
|
'paid_at' => now(),
|
|
'gaji_yang_dibayarkan_final' => $payment['bruto'],
|
|
'pph_21_final' => $payment['pajak'],
|
|
'gaji_bersih_final' => $payment['netto'],
|
|
]);
|
|
}
|
|
|
|
return $rows->count();
|
|
});
|
|
}
|
|
}
|