140 lines
5.0 KiB
PHP
140 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Verifikasi\TriDharma\Concerns;
|
|
|
|
use App\Models\Sidia\Pegawai as SidiaPegawai;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Kolom & filter "Status Aktif" dari view pegawai SIDIA (dipakai di list Pengajuan & Pleno Tri Dharma).
|
|
*
|
|
* SIDIA ada di server database berbeda, jadi tidak bisa di-join:
|
|
* - filter: ambil dulu daftar NIP berstatus X dari SIDIA, lalu whereIn ke query utama;
|
|
* - kolom: setelah DataTables selesai, ambil status untuk baris di halaman ini dalam 1 query.
|
|
*/
|
|
trait HasStatusAktifSidia
|
|
{
|
|
/**
|
|
* Nilai kolom `status` di view pegawai SIDIA => [label, warna badge].
|
|
*/
|
|
private const STATUS_AKTIF_SIDIA = [
|
|
'aktif' => ['Aktif', 'success'],
|
|
'tubel' => ['Tubel', 'info'],
|
|
'tubelmandiri' => ['Tubel Mandiri', 'purple'],
|
|
'nonaktif' => ['Nonaktif', 'warning'],
|
|
'mutkel' => ['Mutasi Keluar', 'secondary'],
|
|
'pensiun' => ['Pensiun', 'danger'],
|
|
'wafat' => ['Wafat', 'dark'],
|
|
];
|
|
|
|
/**
|
|
* Opsi dropdown filter: [value => label].
|
|
*/
|
|
protected function statusAktifOptions(): Collection
|
|
{
|
|
return collect(self::STATUS_AKTIF_SIDIA)->map(fn ($item) => $item[0]);
|
|
}
|
|
|
|
protected function applyStatusAktifFilter(Builder $query, ?string $statusAktif): Builder
|
|
{
|
|
if (! $statusAktif || ! array_key_exists($statusAktif, self::STATUS_AKTIF_SIDIA)) {
|
|
return $query;
|
|
}
|
|
|
|
try {
|
|
$nips = SidiaPegawai::query()
|
|
->where('status', $statusAktif)
|
|
->get(['nip', 'nip_siakad'])
|
|
->flatMap(fn ($p) => [$p->nip, $p->nip_siakad])
|
|
->filter()
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
} catch (Exception $e) {
|
|
Log::warning('Gagal mengambil filter status aktif SIDIA: '.$e->getMessage());
|
|
$nips = [];
|
|
}
|
|
|
|
return $query->where(fn ($q) => $q->whereIn('nip', $nips)->orWhereIn('nip_siakad', $nips));
|
|
}
|
|
|
|
/**
|
|
* Tambahkan kolom `status_aktif` (HTML badge) ke setiap baris response DataTables.
|
|
*/
|
|
protected function attachStatusAktifSidia(JsonResponse $response): JsonResponse
|
|
{
|
|
$payload = $response->getData(true);
|
|
$rows = $payload['data'] ?? [];
|
|
|
|
$nips = collect($rows)
|
|
->flatMap(fn ($row) => [$row['nip'] ?? null, $row['nip_siakad'] ?? null])
|
|
->filter()
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
|
|
$statusByNip = [];
|
|
if (! empty($nips)) {
|
|
try {
|
|
SidiaPegawai::query()
|
|
->select('nip', 'nip_siakad', 'status', 'tmt_status', 'tst_status')
|
|
->where(fn ($q) => $q->whereIn('nip', $nips)->orWhereIn('nip_siakad', $nips))
|
|
->get()
|
|
->each(function ($sidia) use (&$statusByNip) {
|
|
foreach ([$sidia->nip, $sidia->nip_siakad] as $key) {
|
|
if ($key && ! isset($statusByNip[$key])) {
|
|
$statusByNip[$key] = $sidia;
|
|
}
|
|
}
|
|
});
|
|
} catch (Exception $e) {
|
|
Log::warning('Gagal mengambil status aktif SIDIA: '.$e->getMessage());
|
|
}
|
|
}
|
|
|
|
foreach ($rows as $i => $row) {
|
|
$sidia = $statusByNip[$row['nip'] ?? ''] ?? $statusByNip[$row['nip_siakad'] ?? ''] ?? null;
|
|
$rows[$i]['status_aktif'] = $this->renderStatusAktif($sidia);
|
|
}
|
|
|
|
$payload['data'] = $rows;
|
|
$response->setData($payload);
|
|
|
|
return $response;
|
|
}
|
|
|
|
protected function renderStatusAktif(?SidiaPegawai $sidia): string
|
|
{
|
|
if (! $sidia || ! $sidia->status) {
|
|
return '<span class="text-muted">-</span>';
|
|
}
|
|
|
|
$status = strtolower($sidia->status);
|
|
[$label, $color] = self::STATUS_AKTIF_SIDIA[$status] ?? [ucfirst($status), 'secondary'];
|
|
|
|
// Bootstrap tidak punya bg-purple, jadi ungu diberi inline style (fill terang).
|
|
$html = $color === 'purple'
|
|
? '<span class="badge rounded-pill fs-6 px-2 py-1" style="background-color: #e9dffa; color: #5a32a3;">'.e($label).'</span>'
|
|
: '<span class="badge bg-'.$color.' rounded-pill fs-6 px-2 py-1">'.e($label).'</span>';
|
|
|
|
if ($status !== 'aktif' && $sidia->tmt_status) {
|
|
$tanggal = Carbon::parse($sidia->tmt_status)->format('d/m/Y');
|
|
|
|
// Pensiun/wafat/mutasi keluar cukup satu tanggal; status lain pakai rentang jika ada TST
|
|
$satuTanggal = in_array($status, ['pensiun', 'wafat', 'mutkel'], true);
|
|
if (! $satuTanggal && $sidia->tst_status) {
|
|
$tanggal .= ' - '.Carbon::parse($sidia->tst_status)->format('d/m/Y');
|
|
}
|
|
|
|
$html .= '<small class="d-block text-muted mt-1">'.$tanggal.'</small>';
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
}
|