47 lines
2.7 KiB
PHP
47 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\P1Dev;
|
|
|
|
use App\Models\Rekapitulasi\P1Dev;
|
|
use Carbon\Carbon;
|
|
|
|
class P1MonthlyDifferences
|
|
{
|
|
public function summarize(int $year, mixed $unit = null, string $employeeType = '', bool $includePppk2025 = false): array
|
|
{
|
|
$end = $year < now()->year ? 12 : ($year === now()->year ? now()->month : 0);
|
|
$records = P1Dev::where('tahun', $year)->whereBetween('bulan', [1, $end])
|
|
->when($unit !== null && $unit !== '', fn ($query) => $query->where('id_unit_kerja', $unit))
|
|
->when($employeeType !== '', fn ($query) => $query->whereRaw('LOWER(jenis_pegawai) = ?', [strtolower($employeeType)]))
|
|
->when(! $includePppk2025, fn ($query) => $query->where('nip', 'not like', '%202521%'))
|
|
->toBase()->get(['nip', 'bulan', 'paid_at', 'gaji_yang_dibayarkan', 'pph21', 'gaji_bersih', 'gaji_yang_dibayarkan_final', 'pph_21_final', 'gaji_bersih_final'])
|
|
->groupBy('bulan');
|
|
$store = new P1PaymentUploadStore;
|
|
$reviewer = new P1PaymentReview;
|
|
$result = [];
|
|
for ($month = 1; $month <= $end; $month++) {
|
|
$upload = $store->get($year, $month);
|
|
$snapshots = $store->snapshots($year, $month);
|
|
$row = ['bulan' => $month, 'label' => Carbon::create($year, $month, 1)->locale('id')->translatedFormat('F'),
|
|
'sudah' => ['kekurangan' => 0.0, 'kelebihan' => 0.0, 'jumlah' => 0],
|
|
'belum' => ['kekurangan' => 0.0, 'kelebihan' => 0.0, 'jumlah' => 0],
|
|
'tidak_diketahui' => 0, 'dibandingkan' => 0];
|
|
foreach ($records->get($month, collect()) as $record) {
|
|
// Zero-entitlement records without an actual payment are not payment candidates.
|
|
if ((float) $record->gaji_yang_dibayarkan == 0 && empty($record->paid_at) && ! isset($upload['payments'][ltrim((string) $record->nip, "'").':'.$month])) { continue; }
|
|
$payment = $upload['payments'][ltrim((string) $record->nip, "'").':'.$month] ?? null;
|
|
$comparison = $reviewer->forRecord($record, $payment, $snapshots);
|
|
if (! $comparison['known']) { $row['tidak_diketahui']++; continue; }
|
|
$row['dibandingkan']++;
|
|
$status = ! empty($record->paid_at) ? 'sudah' : 'belum';
|
|
$gap = $comparison['review']['difference']['netto'];
|
|
$row[$status]['kekurangan'] = round($row[$status]['kekurangan'] + max(0, $gap), 2);
|
|
$row[$status]['kelebihan'] = round($row[$status]['kelebihan'] + max(0, -$gap), 2);
|
|
if (abs($gap) >= 0.01) { $row[$status]['jumlah']++; }
|
|
}
|
|
$result[] = $row;
|
|
}
|
|
return $result;
|
|
}
|
|
}
|