Penambahn P1 Dev
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\P1Dev;
|
||||
|
||||
use App\Http\Controllers\Perhitungan\Remunerasi\P1DevController as P1Controller;
|
||||
use App\Models\Rekapitulasi\P1Dev as P1;
|
||||
use App\Models\Rekapitulasi\P1Penyesuaian;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class P1TmtService
|
||||
{
|
||||
public function effectiveMonth(string $tmt): CarbonImmutable
|
||||
{
|
||||
$date = CarbonImmutable::parse($tmt, 'Asia/Jakarta');
|
||||
|
||||
return $date->day <= 10 ? $date->startOfMonth() : $date->startOfMonth()->addMonth();
|
||||
}
|
||||
|
||||
public function snapshot(P1 $record): array
|
||||
{
|
||||
$harga = max((float) $record->gaji_jabatan_fungsional, (float) $record->gaji_jabatan_tugas_tambahan);
|
||||
|
||||
return [
|
||||
'jafung' => $record->nama_jabatan_fungsional ?: '-',
|
||||
'jtt' => $record->nama_jabatan_tugas_tambahan ?: '-',
|
||||
'grade_jafung' => $record->grade_jabatan_fungsional,
|
||||
'grade_jtt' => $record->grade_jabatan_tugas_tambahan,
|
||||
'golongan' => $record->golongan,
|
||||
'is_cpns' => $harga > 0 && abs((float) $record->gaji_yang_dibayarkan - 2 * $harga * 0.8) < 1,
|
||||
'bruto' => (float) $record->gaji_yang_dibayarkan,
|
||||
'pajak' => (float) $record->pph21,
|
||||
'netto' => (float) $record->gaji_bersih,
|
||||
];
|
||||
}
|
||||
|
||||
/** Null TMT leaves that attribute untouched. All supplied ranges end today. */
|
||||
public function update(P1 $pegawai, array $tmt, array $attributes, CarbonImmutable $today): array
|
||||
{
|
||||
$starts = collect($tmt)->filter()->map(fn ($date) => $this->effectiveMonth($date));
|
||||
if ($starts->isEmpty()) {
|
||||
throw ValidationException::withMessages(['tmt' => 'Isi minimal satu TMT untuk menerapkan perubahan.']);
|
||||
}
|
||||
foreach ($starts as $field => $date) {
|
||||
if ($date->greaterThan($today->startOfMonth())) {
|
||||
throw ValidationException::withMessages([$field.'_tmt' => 'Bulan efektif TMT melewati bulan sekarang.']);
|
||||
}
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($pegawai, $tmt, $attributes, $today, $starts) {
|
||||
$first = $starts->sortBy(fn ($date) => $date->timestamp)->first();
|
||||
$records = P1::where('nip', $pegawai->nip)
|
||||
->whereRaw('(tahun * 100 + bulan) >= ?', [$first->year * 100 + $first->month])
|
||||
->whereRaw('(tahun * 100 + bulan) <= ?', [$today->year * 100 + $today->month])
|
||||
->orderBy('tahun')->orderBy('bulan')->lockForUpdate()->get();
|
||||
|
||||
// Never silently skip a missing month in the requested calculation.
|
||||
$periods = $records->map(fn ($r) => sprintf('%04d-%02d', $r->tahun, $r->bulan));
|
||||
for ($month = $first; $month->lessThanOrEqualTo($today->startOfMonth()); $month = $month->addMonth()) {
|
||||
if (! $periods->contains($month->format('Y-m'))) {
|
||||
throw ValidationException::withMessages(['tmt' => 'Rekap P1 '.$month->translatedFormat('F Y').' belum tersedia untuk pegawai ini.']);
|
||||
}
|
||||
}
|
||||
|
||||
$updated = [];
|
||||
foreach ($records as $record) {
|
||||
$month = CarbonImmutable::create((int) $record->tahun, (int) $record->bulan, 1, 0, 0, 0, 'Asia/Jakarta');
|
||||
$before = $this->snapshot($record);
|
||||
$cpns = $before['is_cpns'];
|
||||
foreach ($starts as $field => $start) {
|
||||
if ($month->lessThan($start)) {
|
||||
continue;
|
||||
}
|
||||
if ($field === 'cpns') {
|
||||
$cpns = $attributes['cpns'];
|
||||
} else {
|
||||
$record->fill($attributes[$field]);
|
||||
}
|
||||
}
|
||||
$calc = P1Controller::calculateP1($record->gaji_jabatan_fungsional, $record->gaji_jabatan_tugas_tambahan, $record->golongan, $cpns);
|
||||
$record->fill(['gaji_yang_dibayarkan' => $calc['bruto'], 'pph21' => $calc['pajak'], 'gaji_bersih' => $calc['netto']]);
|
||||
if (! $record->isDirty()) {
|
||||
continue;
|
||||
}
|
||||
$key = ['nip' => $record->nip, 'tahun' => $record->tahun, 'bulan' => $record->bulan,
|
||||
'tahun_pembayaran' => $today->year, 'bulan_pembayaran' => $today->month];
|
||||
$adjustment = P1Penyesuaian::firstOrNew($key);
|
||||
// Keep the original baseline across repeated saves in this payment month.
|
||||
if (! $adjustment->exists) {
|
||||
$adjustment->sebelum = $before;
|
||||
}
|
||||
$adjustment->sesudah = $this->snapshot($record);
|
||||
$adjustment->tmt = array_filter(array_merge($adjustment->tmt ?? [], array_filter($tmt)));
|
||||
$adjustment->tst = $today->toDateString();
|
||||
$adjustment->save();
|
||||
$record->save();
|
||||
$updated[] = ['tahun' => $record->tahun, 'bulan' => $record->bulan, 'unit' => $record->id_unit_kerja,
|
||||
'unit_pembayaran' => $record->id_unit_pembayaran];
|
||||
}
|
||||
|
||||
return $updated;
|
||||
});
|
||||
}
|
||||
|
||||
/** Combine local corrections with Dawai by source month, never add the same correction twice. */
|
||||
public function mergeAdjustments(array $dawai, iterable $local): array
|
||||
{
|
||||
$entries = collect($dawai)->keyBy(fn ($r) => $r['tahun'].'-'.$r['bulan']);
|
||||
foreach ($local as $adjustment) {
|
||||
$key = $adjustment->tahun.'-'.$adjustment->bulan;
|
||||
$existing = $entries->get($key);
|
||||
// If Dawai already has an adjustment for this month, retain its paid baseline.
|
||||
$before = $existing['p1_lama'] ?? $adjustment->sebelum;
|
||||
$after = $adjustment->sesudah;
|
||||
$bruto = round($after['bruto'] - $before['bruto'], 2);
|
||||
$netto = round($after['netto'] - $before['netto'], 2);
|
||||
$status = $bruto != 0 ? ($bruto > 0 ? 'kekurangan' : 'kelebihan') : ($netto > 0 ? 'kekurangan' : ($netto < 0 ? 'kelebihan' : ''));
|
||||
$entries->put($key, [
|
||||
'tahun' => (int) $adjustment->tahun, 'bulan' => (int) $adjustment->bulan,
|
||||
'nama_bulan' => CarbonImmutable::create($adjustment->tahun, $adjustment->bulan, 1)->locale('id')->translatedFormat('F Y'),
|
||||
'status' => $status, 'status_label' => ucfirst($status), 'sumber' => 'lokal',
|
||||
'selisih_bruto' => $bruto, 'selisih_pajak' => round(($after['pajak'] - $before['pajak']), 2),
|
||||
'selisih_netto' => $netto, 'p1_lama' => $before, 'p1_baru' => $after,
|
||||
]);
|
||||
}
|
||||
|
||||
return $entries->sortBy(fn ($r) => $r['tahun'] * 100 + $r['bulan'])->values()->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user