Penghapusan menu Pengajuan Aktivitas yg sudah tidak terpakai
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Verifikator\Pagu;
|
||||
use App\Models\Verifikator\PaguRiwayat;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PaguMutasiService
|
||||
{
|
||||
/**
|
||||
* Tambah/kurangi saldo pagu semester dan catat riwayatnya dalam satu transaksi.
|
||||
*/
|
||||
public function catat(Pagu $pagu, int $semester, string $jenis, float $jumlah, ?string $keterangan = null): PaguRiwayat
|
||||
{
|
||||
if (! in_array($semester, [1, 2], true)) {
|
||||
throw new InvalidArgumentException('Semester harus 1 atau 2.');
|
||||
}
|
||||
if (! in_array($jenis, [PaguRiwayat::JENIS_PENAMBAHAN, PaguRiwayat::JENIS_PENGURANGAN], true)) {
|
||||
throw new InvalidArgumentException('Jenis mutasi tidak valid.');
|
||||
}
|
||||
if ($jumlah <= 0) {
|
||||
throw new InvalidArgumentException('Jumlah harus lebih dari 0.');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($pagu, $semester, $jenis, $jumlah, $keterangan) {
|
||||
$locked = Pagu::whereKey($pagu->id)->lockForUpdate()->firstOrFail();
|
||||
$kolom = 'pagu_smt'.$semester;
|
||||
$sebelum = (float) ($locked->{$kolom} ?? 0);
|
||||
$sesudah = $jenis === PaguRiwayat::JENIS_PENAMBAHAN ? $sebelum + $jumlah : $sebelum - $jumlah;
|
||||
|
||||
if ($sesudah < 0) {
|
||||
throw new InvalidArgumentException('Pengurangan melebihi saldo pagu semester '.$semester.'.');
|
||||
}
|
||||
|
||||
$locked->update([$kolom => $sesudah]);
|
||||
$pagu->setAttribute($kolom, $sesudah);
|
||||
|
||||
return $this->simpanRiwayat($locked, $semester, $jenis, $jumlah, $sebelum, $sesudah, $keterangan);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Catat selisih saldo semester yang sudah diubah langsung (mis. lewat Edit Pagu).
|
||||
*/
|
||||
public function catatSelisih(Pagu $pagu, int $semester, float $sebelum, float $sesudah, ?string $keterangan = null): ?PaguRiwayat
|
||||
{
|
||||
$selisih = $sesudah - $sebelum;
|
||||
if (abs($selisih) < 0.005) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$jenis = $selisih > 0 ? PaguRiwayat::JENIS_PENAMBAHAN : PaguRiwayat::JENIS_PENGURANGAN;
|
||||
|
||||
return $this->simpanRiwayat($pagu, $semester, $jenis, abs($selisih), $sebelum, $sesudah, $keterangan);
|
||||
}
|
||||
|
||||
private function simpanRiwayat(Pagu $pagu, int $semester, string $jenis, float $jumlah, float $sebelum, float $sesudah, ?string $keterangan): PaguRiwayat
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
return PaguRiwayat::create([
|
||||
'pagu_id' => $pagu->id,
|
||||
'unit_pembayaran_id' => $pagu->unit_pembayaran_id,
|
||||
'tahun' => $pagu->tahun,
|
||||
'semester' => $semester,
|
||||
'jenis' => $jenis,
|
||||
'jumlah' => $jumlah,
|
||||
'saldo_sebelum' => $sebelum,
|
||||
'saldo_sesudah' => $sesudah,
|
||||
'keterangan' => $keterangan,
|
||||
'created_by_nip' => $user?->nip,
|
||||
'created_by_nama' => $user?->name,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user