110 lines
4.3 KiB
PHP
110 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Tagihan dan pembayaran mahasiswa di DB lama (t_keuanganmhs): jenis 1 = tagihan, jenis 2 = pembayaran
|
|
* (f_totaltagihanmhs dan f_totalpembayaran). Jenis 3 dan 4 (pengembalian/potongan) tidak dihitung.
|
|
* Hanya baca; DB lama tidak punya tabel bukti bayar atau virtual account.
|
|
*/
|
|
class StudentFinance
|
|
{
|
|
private const int BILL = 1;
|
|
|
|
private const int PAYMENT = 2;
|
|
|
|
/** Jumlah riwayat pembayaran yang ditampilkan. */
|
|
private const int HISTORY = 30;
|
|
|
|
/**
|
|
* @return array{periode: Collection<int, array<string, mixed>>, riwayat: Collection<int, array<string, mixed>>, sisa: float}
|
|
*/
|
|
public function forStudent(int $idmahasiswa): array
|
|
{
|
|
$totals = $this->db()->table('t_keuanganmhs as k')
|
|
->leftJoin('t_komponenpembayaran as c', 'c.idkomponen', '=', 'k.idkomponen')
|
|
->where('k.idmahasiswa', $idmahasiswa)
|
|
->whereIn('k.jenis', [self::BILL, self::PAYMENT])
|
|
->groupBy('k.idperiode', 'k.idkomponen', 'c.namakomponen', 'k.jenis')
|
|
->selectRaw('k.idperiode, k.idkomponen, c.namakomponen, k.jenis, sum(k.jumlah) as total')
|
|
->get();
|
|
|
|
$labels = $this->periodLabels($totals->pluck('idperiode')->unique()->all());
|
|
|
|
$periode = $totals->groupBy('idperiode')->map(function (Collection $rows, int|string $id) use ($labels): array {
|
|
$komponen = $rows->groupBy('idkomponen')->map(function (Collection $items): array {
|
|
$tagihan = (float) $items->where('jenis', self::BILL)->sum('total');
|
|
$dibayar = (float) $items->where('jenis', self::PAYMENT)->sum('total');
|
|
|
|
return [
|
|
'nama' => trim((string) ($items->first()->namakomponen ?? 'Komponen lain')),
|
|
'tagihan' => $tagihan,
|
|
'dibayar' => $dibayar,
|
|
'sisa' => max($tagihan - $dibayar, 0.0),
|
|
];
|
|
})->sortBy('nama')->values();
|
|
|
|
$tagihan = (float) $komponen->sum('tagihan');
|
|
$dibayar = (float) $komponen->sum('dibayar');
|
|
|
|
return [
|
|
'id' => (int) $id,
|
|
'label' => $labels[(int) $id] ?? 'Periode '.$id,
|
|
'komponen' => $komponen,
|
|
'tagihan' => $tagihan,
|
|
'dibayar' => $dibayar,
|
|
'sisa' => max($tagihan - $dibayar, 0.0),
|
|
'lunas' => $tagihan > 0 && $dibayar >= $tagihan,
|
|
];
|
|
})->sortByDesc('id')->values();
|
|
|
|
return [
|
|
'periode' => $periode,
|
|
'riwayat' => $this->history($idmahasiswa),
|
|
'sisa' => (float) $periode->sum('sisa'),
|
|
];
|
|
}
|
|
|
|
/** @return Collection<int, array<string, mixed>> */
|
|
private function history(int $idmahasiswa): Collection
|
|
{
|
|
$rows = $this->db()->table('t_keuanganmhs as k')
|
|
->leftJoin('t_komponenpembayaran as c', 'c.idkomponen', '=', 'k.idkomponen')
|
|
->where('k.idmahasiswa', $idmahasiswa)
|
|
->where('k.jenis', self::PAYMENT)
|
|
->orderByDesc('k.tgltransaksi')->orderByDesc('k.idkeuanganmhs')
|
|
->limit(self::HISTORY)
|
|
->get(['k.idperiode', 'k.tgltransaksi', 'k.nokuitansi', 'k.jumlah', 'c.namakomponen']);
|
|
|
|
$labels = $this->periodLabels($rows->pluck('idperiode')->unique()->all());
|
|
|
|
return $rows->map(fn (object $row): array => [
|
|
'tanggal' => $row->tgltransaksi,
|
|
'periode' => $labels[(int) $row->idperiode] ?? '',
|
|
'komponen' => trim((string) ($row->namakomponen ?? 'Komponen lain')),
|
|
'kuitansi' => filled($row->nokuitansi) ? trim($row->nokuitansi) : null,
|
|
'jumlah' => (float) $row->jumlah,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param list<int|string> $ids
|
|
* @return Collection<int, string>
|
|
*/
|
|
private function periodLabels(array $ids): Collection
|
|
{
|
|
return $this->db()->table('t_periodekuliah')->whereIn('idperiode', $ids)
|
|
->get(['idperiode', 'thakad', 'semester', 'perkuliahan'])
|
|
->mapWithKeys(fn (object $row): array => [(int) $row->idperiode => LegacyFormat::periodLabel($row->thakad, $row->semester, $row->perkuliahan)]);
|
|
}
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
}
|