52 lines
2.4 KiB
PHP
52 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\P1Dev;
|
|
|
|
class P1PaymentBalance
|
|
{
|
|
private array $monthLabels = [];
|
|
|
|
public function calculate(iterable $records, array $payments, int $month, array $changeHistory = []): array
|
|
{
|
|
$entries = [];
|
|
$changesByPeriod = [];
|
|
foreach ($changeHistory as $change) {
|
|
$changesByPeriod[(int) ($change['tahun'] ?? 0).':'.(int) ($change['bulan'] ?? 0)] = $change;
|
|
}
|
|
$totals = ['bruto' => 0.0, 'pajak' => 0.0, 'netto' => 0.0];
|
|
foreach ($records as $record) {
|
|
if ((int) $record->bulan > $month) {
|
|
continue;
|
|
}
|
|
$payment = $payments[ltrim((string) $record->nip, "'").':'.(int) $record->bulan] ?? null;
|
|
// Missing Excel months are unknown, never assume that nothing was paid.
|
|
if ($payment === null) {
|
|
continue;
|
|
}
|
|
$current = ['bruto' => (float) $record->gaji_yang_dibayarkan, 'pajak' => (float) $record->pph21, 'netto' => (float) $record->gaji_bersih];
|
|
$delta = [];
|
|
foreach ($totals as $field => $value) {
|
|
$delta[$field] = round($current[$field] - (float) $payment[$field], 2);
|
|
$totals[$field] = round($value + $delta[$field], 2);
|
|
}
|
|
$direction = $delta['bruto'] ?: ($delta['netto'] ?: -$delta['pajak']);
|
|
$period = (int) $record->tahun.':'.(int) $record->bulan;
|
|
$change = $changesByPeriod[$period] ?? [];
|
|
$monthLabel = $this->monthLabels[$period] ??= \Carbon\Carbon::create($record->tahun, $record->bulan, 1)
|
|
->locale('id')->translatedFormat('F Y');
|
|
$entries[] = [
|
|
'tahun' => (int) $record->tahun, 'bulan' => (int) $record->bulan,
|
|
'nama_bulan' => $monthLabel,
|
|
'status' => $direction > 0 ? 'kekurangan' : ($direction < 0 ? 'kelebihan' : ''),
|
|
'selisih_bruto' => $delta['bruto'], 'selisih_pajak' => $delta['pajak'], 'selisih_netto' => $delta['netto'],
|
|
'saldo' => $totals,
|
|
// Excel supplies amounts, while DAWAI/local corrections supply job/status history.
|
|
'p1_lama' => array_replace($change['p1_lama'] ?? [], $payment),
|
|
'p1_baru' => array_replace($change['p1_baru'] ?? [], $current),
|
|
];
|
|
}
|
|
|
|
return ['totals' => $totals, 'entries' => $entries];
|
|
}
|
|
}
|