Files
siakad/app/Support/Legacy/EmployeeBiodata.php
2026-11-11 09:28:34 +07:00

309 lines
15 KiB
PHP

<?php
namespace App\Support\Legacy;
use App\Support\Legacy\Concerns\BiodataFields;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
/**
* Biodata lengkap pegawai DB lama (t_pegawai, dan t_dosen untuk dosen) untuk halaman detail operator: data pribadi, kontak
* dan alamat, kepegawaian, pendidikan dan rekening, serta data dosen. Dibaca dan ditulis per bagian dalam cakupan yang sama
* dengan daftar (EmployeeDirectory::scoped). Hanya kolom yang nilainya berubah yang ditulis. Akun login tidak disentuh dan
* jenis pegawai (dosen atau staf) tidak diubah di sini.
*/
final class EmployeeBiodata
{
use BiodataFields;
/** Bagian yang ditulis ke t_dosen (bukan t_pegawai); hanya ada untuk pegawai yang punya baris t_dosen. */
private const string DOSEN = 'dosen';
private const array ACTIVE = [1 => 'Aktif', 0 => 'Tidak aktif'];
/** @var array<string, array<int, string>>|null */
private ?array $lookups = null;
/** @var list<int> */
private array $prodi = [];
public function __construct(private readonly EmployeeDirectory $directory) {}
/**
* Bagian halaman; lihat BiodataFields untuk arti isian.
*
* @return array<string, array{title: string, icon: string, description: string, fields: list<array<string, mixed>>}>
*/
public static function sections(): array
{
return [
'pribadi' => [
'title' => 'Data pribadi',
'icon' => 'user',
'description' => 'Nama, gelar, dan identitas diri.',
'fields' => [
['name' => 'nama', 'label' => 'Nama', 'type' => 'text', 'max' => 50, 'required' => true],
['name' => 'gelardepan', 'label' => 'Gelar depan', 'type' => 'text', 'max' => 19, 'notnull' => true, 'affix' => 'front', 'placeholder' => 'Contoh: Dr. atau Prof. Dr.'],
['name' => 'gelarbelakang', 'label' => 'Gelar belakang', 'type' => 'text', 'max' => 23, 'notnull' => true, 'affix' => 'back', 'placeholder' => 'Contoh: S.Kom., M.T. (tanpa koma di depan)'],
['name' => 'kelamin', 'label' => 'Jenis kelamin', 'type' => 'select', 'lookup' => 'kelamin'],
['name' => 'tempatlahir', 'label' => 'Tempat lahir', 'type' => 'text', 'max' => 60],
['name' => 'tgllahir', 'label' => 'Tanggal lahir', 'type' => 'date'],
['name' => 'agama', 'label' => 'Agama', 'type' => 'select', 'lookup' => 'agama'],
['name' => 'no_id', 'label' => 'NIK', 'type' => 'text', 'max' => 16, 'alnum' => true],
['name' => 'jmlistri', 'label' => 'Jumlah istri atau suami', 'type' => 'number', 'min' => 0, 'max' => 20, 'required' => true],
['name' => 'jmlanak', 'label' => 'Jumlah anak', 'type' => 'number', 'min' => 0, 'max' => 30, 'required' => true],
],
],
'kontak' => [
'title' => 'Kontak dan alamat',
'icon' => 'map-pin',
'description' => 'Alamat tinggal dan cara menghubungi.',
'fields' => [
['name' => 'hp', 'label' => 'No. HP', 'type' => 'text', 'max' => 20],
['name' => 'telepon', 'label' => 'Telepon', 'type' => 'text', 'max' => 15],
['name' => 'email', 'label' => 'Email', 'type' => 'email', 'max' => 125],
['name' => 'kodepos', 'label' => 'Kode pos', 'type' => 'text', 'max' => 5, 'alnum' => true],
['name' => 'alamat', 'label' => 'Alamat', 'type' => 'textarea', 'max' => 255, 'wide' => true],
],
],
'kepegawaian' => [
'title' => 'Kepegawaian',
'icon' => 'briefcase',
'description' => 'Nomor induk, status, dan surat keputusan.',
'fields' => [
['name' => 'nip', 'label' => 'NIP', 'type' => 'text', 'max' => 20, 'required' => true],
['name' => 'nipnasional', 'label' => 'NIP nasional', 'type' => 'text', 'max' => 30],
['name' => 'nidn', 'label' => 'NIDN', 'type' => 'text', 'max' => 25],
['name' => 'idstatuspegawai', 'label' => 'Status kepegawaian', 'type' => 'select', 'lookup' => 'status'],
['name' => 'statusaktif', 'label' => 'Status aktif', 'type' => 'select', 'lookup' => 'aktif', 'required' => true, 'zero' => true],
['name' => 'skpegawai', 'label' => 'No. SK pegawai', 'type' => 'text', 'max' => 30],
['name' => 'tglskpegawai', 'label' => 'Tanggal SK pegawai', 'type' => 'date'],
['name' => 'npwp', 'label' => 'NPWP', 'type' => 'text', 'max' => 20],
['name' => 'no_bpjs', 'label' => 'No. BPJS', 'type' => 'text', 'max' => 20],
],
],
'pendidikan' => [
'title' => 'Pendidikan dan bank',
'icon' => 'academic-cap',
'description' => 'Pendidikan terakhir dan rekening gaji.',
'fields' => [
['name' => 'pendidikanterakhir', 'label' => 'Pendidikan terakhir', 'type' => 'select', 'lookup' => 'pendidikan'],
['name' => 'idbank', 'label' => 'Bank', 'type' => 'select', 'lookup' => 'bank'],
['name' => 'norekening', 'label' => 'No. rekening', 'type' => 'text', 'max' => 30],
],
],
self::DOSEN => [
'title' => 'Data dosen',
'icon' => 'identification',
'description' => 'Home base, pangkat, dan jabatan fungsional.',
'fields' => [
['name' => 'idprogdi', 'label' => 'Home base (program studi)', 'type' => 'select', 'lookup' => 'prodi', 'required' => true],
['name' => 'idpangkatdosen', 'label' => 'Pangkat', 'type' => 'select', 'lookup' => 'pangkat', 'required' => true],
['name' => 'idjabatanfungsional', 'label' => 'Jabatan fungsional', 'type' => 'select', 'lookup' => 'jabfung'],
['name' => 'nomerpa', 'label' => 'Nomor PA', 'type' => 'text', 'max' => 10],
],
],
];
}
/** @return list<string> */
public static function sectionKeys(): array
{
return array_keys(self::sections());
}
private function db(): ConnectionInterface
{
return DB::connection('untan');
}
/** Pilihan home base dibatasi pada program studi akun, jadi pilihan bergantung pada cakupan. */
private function scope(array $prodi): void
{
if ($this->prodi !== $prodi) {
$this->prodi = $prodi;
$this->lookups = null;
}
}
/**
* Pilihan select (kode => label). Kode 0 berarti "Tidak diisi" pada kolom yang memakainya di DB asli.
*
* @return array<string, array<int, string>>
*/
public function lookups(): array
{
if ($this->lookups !== null) {
return $this->lookups;
}
$zero = fn (array $options): array => [0 => 'Tidak diisi'] + array_diff_key($options, [0 => true]);
$names = fn (string $table, string $key, string $label, bool $upper = false): array => $this->db()->table($table)->orderBy($key)->pluck($label, $key)
->mapWithKeys(fn (?string $name, int|string $id): array => [(int) $id => $upper ? ucfirst(trim((string) $name)) : trim((string) $name)])->all();
// Pendidikan terakhir pegawai memakai skala id_simalum (1 SMP, 2 SMA, 3 D1 ... 7 S1, 8 S2, 9 S3), bukan idtingkat.
$education = $this->db()->table('t_tingkat_pendidikan')->where('id_simalum', '>', 0)->orderBy('id_simalum')->pluck('tingkatpendidikan', 'id_simalum')
->mapWithKeys(fn (string $name, int|string $id): array => [(int) $id => trim($name)])->all();
return $this->lookups = [
'kelamin' => EmployeeRecords::GENDER,
'agama' => $zero($names('xs_agama', 'idagama', 'agama')),
'aktif' => self::ACTIVE,
'status' => $names('t_statuspegawai', 'idstatuspegawai', 'statuspegawai', true),
'pendidikan' => $zero($education),
'bank' => $names('t_bank', 'idbank', 'namabank'),
'prodi' => $this->prodi === [] ? [] : $this->db()->table('t_bagian')->whereIn('idbagian', $this->prodi)->orderBy('namabagian')->pluck('namabagian', 'idbagian')
->mapWithKeys(fn (string $name, int|string $id): array => [(int) $id => trim($name)])->all(),
'pangkat' => $names('t_pangkatdosen', 'idpangkatdosen', 'pangkatdosen'),
'jabfung' => $names('t_jabatanfungsional', 'idjabatanfungsional', 'jabatan'),
];
}
/**
* @param list<int> $prodi
* @param list<int> $faculties
*/
public function exists(array $prodi, array $faculties, int $id): bool
{
return $this->directory->scoped($prodi, $faculties)->where('p.idpegawai', $id)->exists();
}
/**
* Biodata satu pegawai dalam cakupan: per bagian, nilai isian dan teks tampil. Bagian dosen hanya untuk dosen.
*
* @param list<int> $prodi
* @param list<int> $faculties
* @return array{sections: array<string, array<string, mixed>>, extra: array<string, mixed>}|null
*/
public function find(array $prodi, array $faculties, int $id): ?array
{
$this->scope($prodi);
$row = $this->row($prodi, $faculties, $id);
if ($row === null) {
return null;
}
$lookups = $this->lookups();
$sections = [];
foreach (self::sections() as $key => $section) {
if ($key === self::DOSEN && $row->dosen_id === null) {
continue;
}
$sections[$key] = $this->present($section, $row, $lookups) + ['key' => $key, 'title' => $section['title'], 'icon' => $section['icon'], 'description' => $section['description']];
}
return ['sections' => $sections, 'extra' => $this->extra($row, $id)];
}
/**
* Periksa isian satu bagian. NIP diperiksa tidak kembar dengan pegawai lain; home base hanya prodi akun.
*
* @param list<int> $prodi
* @param list<int> $faculties
* @param array<string, mixed> $input
* @return array<string, mixed>
*
* @throws ValidationException
*/
public function validated(array $prodi, array $faculties, int $id, string $key, array $input): array
{
$this->scope($prodi);
$row = $this->row($prodi, $faculties, $id) ?? throw new PortalException('Pegawai tidak ditemukan.');
if ($key === self::DOSEN && $row->dosen_id === null) {
throw new PortalException('Pegawai ini bukan dosen.');
}
$unique = function (string $attribute, mixed $value, \Closure $fail) use ($id): void {
$nip = trim((string) $value);
if ($this->db()->table('t_pegawai')->whereRaw('upper(trim(nip)) = ?', [mb_strtoupper($nip)])->where('idpegawai', '<>', $id)->exists()) {
$fail("NIP {$nip} sudah terdaftar.");
}
};
return $this->validateFields(self::sections()[$key]['fields'], $row, $input, ['nip' => [$unique]]);
}
/**
* Simpan satu bagian. Mengembalikan bagian yang sudah diperbarui (nilai isian + teks tampil) dan banyak kolom yang berubah.
*
* @param list<int> $prodi
* @param list<int> $faculties
* @param array<string, mixed> $input
* @return array{section: array<string, mixed>, changed: int}
*/
public function update(array $prodi, array $faculties, int $id, string $key, array $input, string $actor, ?string $ip): array
{
$this->scope($prodi);
$section = self::sections()[$key] ?? throw new PortalException('Bagian biodata tidak dikenal.');
$row = $this->row($prodi, $faculties, $id) ?? throw new PortalException('Pegawai tidak ditemukan.');
if ($key === self::DOSEN && $row->dosen_id === null) {
throw new PortalException('Pegawai ini bukan dosen.');
}
$lookups = $this->lookups();
$changes = $this->changes($section['fields'], $row, $input, $lookups);
if ($changes !== []) {
$this->db()->table($key === self::DOSEN ? 't_dosen' : 't_pegawai')->where('idpegawai', $id)->update($changes + [
'cname' => ActivityTypes::APP_NAME, 'cip' => $ip ?? '', 'uname' => mb_substr($actor, 0, 15), 'tanggal' => now(),
]);
}
return [
'section' => $this->present($section, $this->row($prodi, $faculties, $id), $lookups),
'changed' => count($changes),
];
}
/**
* @param list<int> $prodi
* @param list<int> $faculties
*/
private function row(array $prodi, array $faculties, int $id): ?object
{
$columns = [];
foreach (self::sections() as $key => $section) {
foreach ($section['fields'] as $field) {
$columns[] = ($key === self::DOSEN ? 'dd.' : 'p.').$field['name'];
}
}
return $this->directory->scoped($prodi, $faculties)
->leftJoin('t_dosen as dd', 'dd.idpegawai', '=', 'p.idpegawai')
->where('p.idpegawai', $id)
->first(array_merge($columns, ['p.idjenispegawai', 'dd.idpegawai as dosen_id']));
}
/**
* Keterangan yang hanya dibaca dan angka ringkasan: jenis pegawai, jabatan aktif, mahasiswa bimbingan, kelas diampu.
*
* @return array<string, mixed>
*/
private function extra(object $row, int $id): array
{
$type = $row->idjenispegawai === null ? null : $this->db()->table('t_jenispegawai')->where('idjenispegawai', $row->idjenispegawai)->value('jenispegawai');
$positions = $this->db()->table('t_jabatanpegawai as jp')
->join('t_jabatanbagian as jb', 'jb.idjabatanbagian', '=', 'jp.idjabatanbagian')
->leftJoin('t_jenisjabatan as jj', 'jj.idjenisjabatan', '=', 'jb.idjenisjabatan')
->leftJoin('t_bagian as b', 'b.idbagian', '=', 'jb.idbagian')
->where('jp.idpegawai', $id)->where('jp.aktif', 1)->orderBy('jp.idjabatan')
->get(['jj.namajabatan', 'b.namabagian'])
->map(fn (object $p): string => trim(trim((string) $p->namajabatan).' '.trim((string) $p->namabagian)))
->filter()->values();
return [
'jenis' => filled($type) ? trim($type) : null,
'dosen' => $row->dosen_id !== null,
'jabatan' => $positions->all(),
'bimbingan' => $this->count($this->db()->table('t_mahasiswa')->where('idpa', $id)->whereIn('stataktif', StudentDirectory::STATUS['aktif'])),
'kelas' => $this->count($this->db()->table('t_jadwaldosen')->where('iddosen', $id), 'idjadwal'),
];
}
private function count(Builder $query, ?string $distinct = null): int
{
return $distinct === null ? $query->count() : $query->distinct()->count($distinct);
}
}