218 lines
9.0 KiB
PHP
218 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Pengajuan cuti kuliah mahasiswa di DB lama (t_cuti). mulai/sampai = idperiode semester reguler.
|
|
* Aturan per prodi dari t_syaratcuti: lamacuti = maksimum semester per pengajuan, semester_ke = semester
|
|
* paling awal boleh cuti, jumlah = maksimum cuti yang pernah disetujui. Pengajuan baru berstatus 0 (diverifikasi
|
|
* fakultas); mahasiswa hanya dapat membatalkan yang masih 0 (menjadi 4). Verifikasi dilakukan di sisi operator.
|
|
*/
|
|
class LeaveRequests
|
|
{
|
|
public const int PENDING = 0;
|
|
|
|
public const int APPROVED = 2;
|
|
|
|
public const int REJECTED = 3;
|
|
|
|
public const int CANCELLED = 4;
|
|
|
|
/** Sama dengan t_mahasiswa.stataktif = 1. */
|
|
private const int ACTIVE_STUDENT = 1;
|
|
|
|
/** t_periodekuliah.perkuliahan = 1: semester reguler. */
|
|
private const int REGULAR = 1;
|
|
|
|
private const array STATUS = [
|
|
self::PENDING => ['Proses validasi', 'gold'],
|
|
self::APPROVED => ['Disetujui', 'success'],
|
|
self::REJECTED => ['Ditolak', 'danger'],
|
|
self::CANCELLED => ['Dibatalkan', null],
|
|
];
|
|
|
|
/**
|
|
* @return array{
|
|
* periode: string|null, bisa_ajukan: bool, alasan: string|null, maks_lama: int,
|
|
* alasan_cuti: Collection<int, string>, riwayat: Collection<int, array<string, mixed>>
|
|
* }
|
|
*/
|
|
public function forStudent(int $idmahasiswa): array
|
|
{
|
|
$ctx = $this->context($idmahasiswa);
|
|
$alasanCuti = $this->reasons();
|
|
$riwayat = $this->history($idmahasiswa, $alasanCuti);
|
|
|
|
if ($ctx === null) {
|
|
return ['periode' => null, 'bisa_ajukan' => false, 'alasan' => 'Data mahasiswa tidak ditemukan.', 'maks_lama' => 0, 'alasan_cuti' => $alasanCuti, 'riwayat' => $riwayat];
|
|
}
|
|
|
|
$syarat = $this->rule((int) $ctx->idprogdi);
|
|
$semester = LegacyFormat::semesterNumber($ctx->tahun, $ctx->smt_masuk, $ctx->thakad, $ctx->semester);
|
|
$disetujui = $riwayat->where('kode', self::APPROVED)->count();
|
|
$periode = $ctx->idperiode === null ? null : (int) $ctx->idperiode;
|
|
|
|
$berjalan = $periode === null ? null : $riwayat->first(
|
|
fn (array $r): bool => in_array($r['kode'], [self::PENDING, self::APPROVED], true) && $r['mulai_id'] <= $periode && $periode <= $r['sampai_id']
|
|
);
|
|
|
|
$alasan = match (true) {
|
|
(int) $ctx->stataktif !== self::ACTIVE_STUDENT => 'Hanya mahasiswa berstatus aktif yang dapat mengajukan cuti.',
|
|
$periode === null => 'Program studi Anda belum memiliki periode perkuliahan aktif.',
|
|
(int) $ctx->perkuliahan !== self::REGULAR => 'Cuti hanya dapat diajukan pada semester reguler.',
|
|
$syarat === null => 'Aturan cuti program studi Anda belum ditetapkan. Hubungi bagian akademik fakultas.',
|
|
$berjalan !== null && $berjalan['kode'] === self::APPROVED => 'Anda sedang dalam masa cuti pada semester ini.',
|
|
$berjalan !== null => 'Anda sudah memiliki pengajuan cuti yang sedang diproses untuk semester ini.',
|
|
$semester !== null && $semester < (int) $syarat->semester_ke => 'Cuti baru dapat diajukan mulai semester '.(int) $syarat->semester_ke.'.',
|
|
$disetujui >= (int) $syarat->jumlah => 'Jumlah cuti Anda telah mencapai batas '.(int) $syarat->jumlah.' kali.',
|
|
default => null,
|
|
};
|
|
|
|
return [
|
|
'periode' => $periode === null ? null : LegacyFormat::periodLabel($ctx->thakad, $ctx->semester, $ctx->perkuliahan),
|
|
'bisa_ajukan' => $alasan === null,
|
|
'alasan' => $alasan,
|
|
'maks_lama' => $syarat === null ? 0 : (int) $syarat->lamacuti,
|
|
'alasan_cuti' => $alasanCuti,
|
|
'riwayat' => $riwayat,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Ajukan cuti mulai semester berjalan selama $lama semester reguler.
|
|
*
|
|
* @return array{mulai: string, sampai: string} label periode cuti
|
|
*
|
|
* @throws LeaveRequestException
|
|
*/
|
|
public function request(int $idmahasiswa, int $alasan, int $lama): array
|
|
{
|
|
$plan = $this->forStudent($idmahasiswa);
|
|
|
|
if (! $plan['bisa_ajukan']) {
|
|
throw new LeaveRequestException($plan['alasan']);
|
|
}
|
|
|
|
if (! $plan['alasan_cuti']->has($alasan)) {
|
|
throw new LeaveRequestException('Alasan cuti tidak dikenal.');
|
|
}
|
|
|
|
if ($lama < 1 || $lama > $plan['maks_lama']) {
|
|
throw new LeaveRequestException("Lama cuti harus 1 sampai {$plan['maks_lama']} semester.");
|
|
}
|
|
|
|
$mulai = (int) $this->context($idmahasiswa)->idperiode;
|
|
$periods = $this->db()->table('t_periodekuliah')
|
|
->where('perkuliahan', self::REGULAR)->where('idperiode', '>=', $mulai)
|
|
->orderBy('idperiode')->limit($lama)
|
|
->get(['idperiode', 'thakad', 'semester', 'perkuliahan']);
|
|
|
|
if ($periods->count() < $lama) {
|
|
throw new LeaveRequestException('Semester untuk masa cuti yang diminta belum tersedia di kalender akademik.');
|
|
}
|
|
|
|
$this->db()->table('t_cuti')->insert([
|
|
'idmahasiswa' => $idmahasiswa,
|
|
'mulai' => $mulai,
|
|
'sampai' => (int) $periods->last()->idperiode,
|
|
'alasancuti' => $alasan,
|
|
'stat' => self::PENDING,
|
|
'tglmohon' => now()->toDateString(),
|
|
]);
|
|
|
|
return ['mulai' => $this->periodLabel($periods->first()), 'sampai' => $this->periodLabel($periods->last())];
|
|
}
|
|
|
|
/**
|
|
* Batalkan pengajuan yang masih diproses (stat 0 menjadi 4).
|
|
*
|
|
* @throws LeaveRequestException
|
|
*/
|
|
public function cancel(int $idmahasiswa, int $idcuti): void
|
|
{
|
|
$changed = $this->db()->table('t_cuti')
|
|
->where('idcuti', $idcuti)->where('idmahasiswa', $idmahasiswa)->where('stat', self::PENDING)
|
|
->update(['stat' => self::CANCELLED]);
|
|
|
|
if ($changed === 0) {
|
|
throw new LeaveRequestException('Pengajuan ini tidak ditemukan atau sudah diverifikasi sehingga tidak dapat dibatalkan.');
|
|
}
|
|
}
|
|
|
|
private function context(int $idmahasiswa): ?object
|
|
{
|
|
return $this->db()->table('t_mahasiswa as m')
|
|
->leftJoin('t_angkatanmhs as g', 'g.idangkatan', '=', 'm.idangkatan')
|
|
->leftJoin('t_periodekuliahaktif as a', 'a.idprogdi', '=', 'm.idprogdi')
|
|
->leftJoin('t_periodekuliah as p', 'p.idperiode', '=', 'a.idperiode')
|
|
->where('m.idmahasiswa', $idmahasiswa)
|
|
->first([
|
|
'm.idprogdi', 'm.stataktif', 'g.tahun', 'g.semester as smt_masuk',
|
|
'a.idperiode', 'p.thakad', 'p.semester', 'p.perkuliahan',
|
|
]);
|
|
}
|
|
|
|
private function rule(int $idprogdi): ?object
|
|
{
|
|
return $this->db()->table('t_syaratcuti')->where('idprogdi', $idprogdi)
|
|
->orderByDesc('idsyaratcuti')->first(['lamacuti', 'semester_ke', 'jumlah']);
|
|
}
|
|
|
|
/** @return Collection<int, string> */
|
|
private function reasons(): Collection
|
|
{
|
|
return $this->db()->table('xs_alasan_cuti')->orderBy('idjalasan')->pluck('alasan', 'idjalasan')
|
|
->mapWithKeys(fn (string $alasan, int|string $id): array => [(int) $id => trim($alasan)]);
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, string> $alasanCuti
|
|
* @return Collection<int, array<string, mixed>>
|
|
*/
|
|
private function history(int $idmahasiswa, Collection $alasanCuti): Collection
|
|
{
|
|
$rows = $this->db()->table('t_cuti')->where('idmahasiswa', $idmahasiswa)
|
|
->orderByDesc('idcuti')
|
|
->get(['idcuti', 'mulai', 'sampai', 'alasancuti', 'stat', 'tglmohon', 'tglverifikasi']);
|
|
|
|
$periods = $this->db()->table('t_periodekuliah')
|
|
->whereIn('idperiode', $rows->pluck('mulai')->merge($rows->pluck('sampai'))->filter()->unique()->all())
|
|
->get(['idperiode', 'thakad', 'semester', 'perkuliahan'])->keyBy('idperiode');
|
|
|
|
return $rows->map(function (object $row) use ($alasanCuti, $periods): array {
|
|
$kode = (int) $row->stat;
|
|
$sampai = (int) ($row->sampai ?? $row->mulai);
|
|
[$status, $nada] = self::STATUS[$kode] ?? ['Kode '.$kode, null];
|
|
|
|
return [
|
|
'id' => (int) $row->idcuti,
|
|
'kode' => $kode,
|
|
'mulai_id' => (int) $row->mulai,
|
|
'sampai_id' => $sampai,
|
|
'mulai' => isset($periods[$row->mulai]) ? $this->periodLabel($periods[$row->mulai]) : 'Periode '.$row->mulai,
|
|
'sampai' => isset($periods[$sampai]) ? $this->periodLabel($periods[$sampai]) : 'Periode '.$sampai,
|
|
'alasan' => $alasanCuti[(int) $row->alasancuti] ?? '-',
|
|
'diajukan' => $row->tglmohon,
|
|
'diverifikasi' => $row->tglverifikasi,
|
|
'status' => $status,
|
|
'nada' => $nada,
|
|
'bisa_batal' => $kode === self::PENDING,
|
|
];
|
|
});
|
|
}
|
|
|
|
private function periodLabel(object $period): string
|
|
{
|
|
return LegacyFormat::periodLabel($period->thakad, $period->semester, $period->perkuliahan);
|
|
}
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
}
|