171 lines
7.2 KiB
PHP
171 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Tulis data pegawai DB lama (t_pegawai) dalam cakupan yang tampil di daftar operator (EmployeeDirectory::scoped).
|
|
* Tambah (sebagai dosen) dan hapus. Mengubah biodata dilakukan di halaman detail (EmployeeBiodata). Akun login (t_account)
|
|
* tidak disentuh, dan pegawai yang dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun tidak boleh dihapus.
|
|
*/
|
|
final class EmployeeRecords
|
|
{
|
|
public const array GENDER = [1 => 'Laki-laki', 2 => 'Perempuan'];
|
|
|
|
public const array ACTIVE = [1 => 'Aktif', 0 => 'Tidak aktif'];
|
|
|
|
/** t_jenispegawai: 1 Dosen, 2 Staff. */
|
|
private const int TYPE_LECTURER = 1;
|
|
|
|
/** [tabel, kolom rujukan ke idpegawai]; baris di salah satunya menghalangi penghapusan. */
|
|
private const array DEPENDENTS = [
|
|
['t_jabatanpegawai', 'idpegawai'], ['t_mastermk', 'idpegawai'], ['t_jadwaldosen', 'iddosen'], ['t_jadwal', 'idpegperkuliahan'],
|
|
['t_mahasiswa', 'idpa'], ['t_skripsi', 'idpegawai'], ['t_penguji_skripsi', 'iddosen'], ['t_jadwalprogdi', 'idpegawai'],
|
|
['t_perampingan', 'idpegawai'], ['t_pindah', 'idpegawai'], ['t_return', 'idpegawai'], ['pdpt_nidn', 'idpegawai'], ['xs_user_simalum', 'idpegawai'],
|
|
];
|
|
|
|
public function __construct(private readonly EmployeeDirectory $directory) {}
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
/** Pilihan status kepegawaian (tanpa pilihan "Belum diisi" yang berarti kosong). */
|
|
public function statusOptions(): array
|
|
{
|
|
return $this->directory->statusOptions()->except(0)->all();
|
|
}
|
|
|
|
/**
|
|
* Tambah pegawai baru sebagai dosen dengan home base salah satu prodi akun (t_dosen.idprogdi), sehingga langsung masuk
|
|
* cakupan daftar. Pegawai staf (bukan dosen) hanya tampil di daftar lewat jabatan aktif, yang tidak diatur di sini.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param array<string, mixed> $input
|
|
* @return int idpegawai baru
|
|
*/
|
|
public function create(array $prodi, int $homeBase, array $input, string $actor, ?string $ip): int
|
|
{
|
|
if (! in_array($homeBase, $prodi, true)) {
|
|
throw new PortalException('Home base harus dipilih dari program studi Anda.');
|
|
}
|
|
$values = $this->values($input);
|
|
|
|
return $this->db()->transaction(function () use ($values, $homeBase, $actor, $ip): int {
|
|
$id = (int) $this->db()->table('t_pegawai')->insertGetId($values + ['idjenispegawai' => self::TYPE_LECTURER] + $this->audit($actor, $ip), 'idpegawai');
|
|
$this->db()->table('t_dosen')->insert(['idpegawai' => $id, 'idprogdi' => $homeBase] + $this->audit($actor, $ip));
|
|
|
|
return $id;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hapus pegawai tanpa rujukan. Gagal seluruhnya bila ada yang dirujuk.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<int> $faculties
|
|
* @param list<int> $ids
|
|
*/
|
|
public function delete(array $prodi, array $faculties, array $ids): int
|
|
{
|
|
$rows = $this->owned($prodi, $faculties)->whereIn('p.idpegawai', array_map('intval', $ids))->get(['p.idpegawai', 'p.nama']);
|
|
if ($rows->isEmpty()) {
|
|
throw new PortalException('Pegawai tidak ditemukan.');
|
|
}
|
|
|
|
$blocked = $rows->filter(fn (object $p): bool => $this->isReferenced((int) $p->idpegawai))->map(fn (object $p): string => trim((string) $p->nama))->values();
|
|
if ($blocked->isNotEmpty()) {
|
|
$list = $blocked->take(3)->implode(', ').($blocked->count() > 3 ? ', dan '.($blocked->count() - 3).' lainnya' : '');
|
|
throw new PortalException("Pegawai {$list} masih dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun login sehingga tidak boleh dihapus.");
|
|
}
|
|
|
|
$keys = $rows->pluck('idpegawai')->map(fn ($id): int => (int) $id)->all();
|
|
|
|
return $this->db()->transaction(function () use ($keys): int {
|
|
$this->db()->table('t_dosen')->whereIn('idpegawai', $keys)->delete();
|
|
|
|
return $this->db()->table('t_pegawai')->whereIn('idpegawai', $keys)->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @param list<int> $faculties
|
|
*/
|
|
private function owned(array $prodi, array $faculties): Builder
|
|
{
|
|
return $this->directory->scoped($prodi, $faculties);
|
|
}
|
|
|
|
private function isReferenced(int $id): bool
|
|
{
|
|
$schema = $this->db()->getSchemaBuilder();
|
|
foreach (self::DEPENDENTS as [$table, $column]) {
|
|
if ($schema->hasColumn($table, $column) && $this->db()->table($table)->where($column, $id)->exists()) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return $this->db()->table('t_account')->where('jenis', 0)->where('identitas', $id)->exists();
|
|
}
|
|
|
|
/**
|
|
* Nilai kolom t_pegawai yang sudah diperiksa.
|
|
*
|
|
* @param array<string, mixed> $input
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function values(array $input): array
|
|
{
|
|
$text = fn (mixed $v): ?string => ($v = trim((string) $v)) === '' ? null : $v;
|
|
$nip = trim((string) ($input['nip'] ?? ''));
|
|
$nama = trim((string) ($input['nama'] ?? ''));
|
|
if ($nip === '' || $nama === '') {
|
|
throw new PortalException('NIP dan nama wajib diisi.');
|
|
}
|
|
if ($this->db()->table('t_pegawai')->whereRaw('upper(trim(nip)) = ?', [mb_strtoupper($nip)])->exists()) {
|
|
throw new PortalException("NIP {$nip} sudah terdaftar.");
|
|
}
|
|
$gender = filled($input['kelamin'] ?? null) ? (int) $input['kelamin'] : null;
|
|
if ($gender !== null && ! isset(self::GENDER[$gender])) {
|
|
throw new PortalException('Jenis kelamin tidak dikenal.');
|
|
}
|
|
$status = filled($input['status'] ?? null) ? (int) $input['status'] : null;
|
|
if ($status !== null && ! $this->db()->table('t_statuspegawai')->where('idstatuspegawai', $status)->exists()) {
|
|
throw new PortalException('Status kepegawaian tidak ditemukan.');
|
|
}
|
|
$email = $text($input['email'] ?? null);
|
|
if ($email !== null && ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
throw new PortalException('Format email tidak valid.');
|
|
}
|
|
$active = (int) ($input['aktif'] ?? 1);
|
|
if (! isset(self::ACTIVE[$active])) {
|
|
throw new PortalException('Status aktif tidak dikenal.');
|
|
}
|
|
|
|
return [
|
|
'nip' => $nip,
|
|
'nama' => $nama,
|
|
'gelardepan' => trim((string) ($input['gelardepan'] ?? '')),
|
|
'gelarbelakang' => trim((string) ($input['gelarbelakang'] ?? '')),
|
|
'kelamin' => $gender,
|
|
'nidn' => $text($input['nidn'] ?? null),
|
|
'hp' => $text($input['hp'] ?? null),
|
|
'telepon' => $text($input['telepon'] ?? null),
|
|
'email' => $email,
|
|
'idstatuspegawai' => $status,
|
|
'statusaktif' => $active,
|
|
];
|
|
}
|
|
|
|
/** Kolom audit seperti aplikasi lama: cname nama aplikasi, cip alamat IP, uname username (dipotong 15). */
|
|
private function audit(string $actor, ?string $ip): array
|
|
{
|
|
return ['cname' => ActivityTypes::APP_NAME, 'cip' => $ip ?? '', 'uname' => mb_substr($actor, 0, 15), 'tanggal' => now()];
|
|
}
|
|
}
|