fixing query all data karyawan
This commit is contained in:
parent
834dd8c0c4
commit
e13f9d458e
@ -42,7 +42,7 @@ class KaryawanController extends Controller
|
|||||||
$q->where('pg.namalengkap', 'ILIKE', "%$search%")
|
$q->where('pg.namalengkap', 'ILIKE', "%$search%")
|
||||||
->orWhere('pg.nip_pns', 'ILIKE', "%$search%");
|
->orWhere('pg.nip_pns', 'ILIKE', "%$search%");
|
||||||
})
|
})
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
'pg.id',
|
'pg.id',
|
||||||
DB::raw("coalesce(pg.namalengkap, '-') as namalengkap"),
|
DB::raw("coalesce(pg.namalengkap, '-') as namalengkap"),
|
||||||
|
|||||||
@ -125,7 +125,7 @@ class PitStopController extends Controller
|
|||||||
->where('mp.isprimary', true)
|
->where('mp.isprimary', true)
|
||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
DB::raw('ukp.id as unit_id'),
|
DB::raw('ukp.id as unit_id'),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
@ -360,7 +360,7 @@ class PitStopController extends Controller
|
|||||||
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
||||||
->where('m.statusenabled', true);
|
->where('m.statusenabled', true);
|
||||||
})
|
})
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->where('mp.statusenabled', true)
|
->where('mp.statusenabled', true)
|
||||||
->where('mp.isprimary', true)
|
->where('mp.isprimary', true)
|
||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
@ -444,7 +444,7 @@ class PitStopController extends Controller
|
|||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->where('ukp.id', $unitId)
|
->where('ukp.id', $unitId)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
DB::raw('ukp.id as unit_id'),
|
DB::raw('ukp.id as unit_id'),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
@ -669,7 +669,7 @@ class PitStopController extends Controller
|
|||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
DB::raw('ukp.id as unit_id'),
|
DB::raw('ukp.id as unit_id'),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
@ -712,7 +712,7 @@ class PitStopController extends Controller
|
|||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
DB::raw('ukp.id as unit_id'),
|
DB::raw('ukp.id as unit_id'),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
@ -894,7 +894,7 @@ class PitStopController extends Controller
|
|||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->where('ukp.id', $unitId)
|
->where('ukp.id', $unitId)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
'pg.id',
|
'pg.id',
|
||||||
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
||||||
@ -1378,7 +1378,7 @@ class PitStopController extends Controller
|
|||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->where('ukp.id', $unitId)
|
->where('ukp.id', $unitId)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
// ->whereNotIn('pg.kategorypegawai', ['11'])
|
||||||
->select([
|
->select([
|
||||||
'pg.id',
|
'pg.id',
|
||||||
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
||||||
@ -1554,7 +1554,6 @@ class PitStopController extends Controller
|
|||||||
'data' => $data,
|
'data' => $data,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataProgressAllKaryawan(Request $request)
|
public function dataProgressAllKaryawan(Request $request)
|
||||||
{
|
{
|
||||||
$draw = $this->dtInt($request->input('draw'), 1);
|
$draw = $this->dtInt($request->input('draw'), 1);
|
||||||
@ -1567,7 +1566,7 @@ class PitStopController extends Controller
|
|||||||
|
|
||||||
$totalSteps = (int) MasterPitStopPraAkre::where('statusenabled', true)->count();
|
$totalSteps = (int) MasterPitStopPraAkre::where('statusenabled', true)->count();
|
||||||
|
|
||||||
// Internal
|
// ── Internal ────────────────────────────────────────────────────
|
||||||
$internal = DB::connection('pgsql')
|
$internal = DB::connection('pgsql')
|
||||||
->table('public.mappegawaijabatantounitkerja_m as mp')
|
->table('public.mappegawaijabatantounitkerja_m as mp')
|
||||||
->join('public.pegawai_m as pg', 'pg.id', '=', 'mp.objectpegawaifk')
|
->join('public.pegawai_m as pg', 'pg.id', '=', 'mp.objectpegawaifk')
|
||||||
@ -1582,7 +1581,6 @@ class PitStopController extends Controller
|
|||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
|
||||||
->select([
|
->select([
|
||||||
DB::raw("'internal' as tipe_karyawan"),
|
DB::raw("'internal' as tipe_karyawan"),
|
||||||
'pg.id',
|
'pg.id',
|
||||||
@ -1590,28 +1588,10 @@ class PitStopController extends Controller
|
|||||||
DB::raw("coalesce(pg.nip_pns, '-') as identitas"),
|
DB::raw("coalesce(pg.nip_pns, '-') as identitas"),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
DB::raw("count(distinct m.id) as lulus_count"),
|
DB::raw("count(distinct m.id) as lulus_count"),
|
||||||
DB::raw("coalesce((
|
|
||||||
select count(*)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pg.id
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), 0) as belum_count"),
|
|
||||||
DB::raw("coalesce((
|
|
||||||
select string_agg(ms.nama, ', ' order by ms.id)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pg.id
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), '') as belum_steps"),
|
|
||||||
])
|
])
|
||||||
->groupBy('pg.id', 'pg.namalengkap', 'pg.nip_pns', 'ukp.name');
|
->groupBy('pg.id', 'pg.namalengkap', 'pg.nip_pns', 'ukp.name');
|
||||||
|
|
||||||
// External
|
// ── External ────────────────────────────────────────────────────
|
||||||
$external = DB::connection('pgsql')
|
$external = DB::connection('pgsql')
|
||||||
->table('public.pegawai_luar_pl as pl')
|
->table('public.pegawai_luar_pl as pl')
|
||||||
->leftJoin('public.praakre as p', function ($join) {
|
->leftJoin('public.praakre as p', function ($join) {
|
||||||
@ -1629,58 +1609,87 @@ class PitStopController extends Controller
|
|||||||
DB::raw("coalesce(pl.nik, '-') as identitas"),
|
DB::raw("coalesce(pl.nik, '-') as identitas"),
|
||||||
DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-') as unit_name"),
|
DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-') as unit_name"),
|
||||||
DB::raw("count(distinct m.id) as lulus_count"),
|
DB::raw("count(distinct m.id) as lulus_count"),
|
||||||
DB::raw("coalesce((
|
|
||||||
select count(*)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pl.id
|
|
||||||
and px.tipe_karyawan = 'luar'
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), 0) as belum_count"),
|
|
||||||
DB::raw("coalesce((
|
|
||||||
select string_agg(ms.nama, ', ' order by ms.id)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pl.id
|
|
||||||
and px.tipe_karyawan = 'luar'
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), '') as belum_steps"),
|
|
||||||
])
|
])
|
||||||
->groupBy('pl.id', 'pl.nama', 'pl.nik', DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-')"));
|
->groupBy('pl.id', 'pl.nama', 'pl.nik', DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-')"));
|
||||||
|
|
||||||
|
// ── Union & Base ─────────────────────────────────────────────────
|
||||||
$union = $internal->unionAll($external);
|
$union = $internal->unionAll($external);
|
||||||
|
|
||||||
$baseNoSearch = DB::connection('pgsql')->query()->fromSub($union, 'u');
|
|
||||||
$base = DB::connection('pgsql')->query()->fromSub($union, 'u');
|
$base = DB::connection('pgsql')->query()->fromSub($union, 'u');
|
||||||
|
|
||||||
if ($search !== '') {
|
if ($search !== '') {
|
||||||
$base->where(function ($q) use ($search) {
|
$base->where(function ($q) use ($search) {
|
||||||
$q->where('u.nama', 'ILIKE', '%' . $search . '%')
|
$q->where('u.nama', 'ILIKE', "%{$search}%")
|
||||||
->orWhere('u.identitas', 'ILIKE', '%' . $search . '%')
|
->orWhere('u.identitas', 'ILIKE', "%{$search}%")
|
||||||
->orWhere('u.unit_name', 'ILIKE', '%' . $search . '%')
|
->orWhere('u.unit_name', 'ILIKE', "%{$search}%")
|
||||||
->orWhere('u.tipe_karyawan', 'ILIKE', '%' . $search . '%');
|
->orWhere('u.tipe_karyawan','ILIKE', "%{$search}%");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$recordsTotal = DB::connection('pgsql')->query()->fromSub($baseNoSearch, 't')->count();
|
// ── Count — eksekusi SEKALI ──────────────────────────────────────
|
||||||
$recordsFiltered = $search === ''
|
$recordsFiltered = (clone $base)->count();
|
||||||
? $recordsTotal
|
$recordsTotal = $search === '' ? $recordsFiltered
|
||||||
: DB::connection('pgsql')->query()->fromSub($base, 't')->count();
|
: DB::connection('pgsql')->query()->fromSub($union, 'u')->count();
|
||||||
|
|
||||||
$rows = $base
|
// ── Fetch halaman ini saja ───────────────────────────────────────
|
||||||
|
$rows = (clone $base)
|
||||||
|
->select('u.*')
|
||||||
->orderByDesc('lulus_count')
|
->orderByDesc('lulus_count')
|
||||||
->orderBy('nama')
|
->orderBy('nama')
|
||||||
->offset($start)
|
->offset($start)
|
||||||
->limit($length)
|
->limit($length)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$data = $rows->map(function ($r) use ($totalSteps) {
|
// ── Batch query belum_steps (hanya untuk baris di halaman ini) ───
|
||||||
|
$allSteps = DB::connection('pgsql')
|
||||||
|
->table('public.masterpitstop')
|
||||||
|
->where('statusenabled', true)
|
||||||
|
->orderBy('id')
|
||||||
|
->pluck('nama', 'id'); // [id => nama]
|
||||||
|
|
||||||
|
$internalIds = $rows->where('tipe_karyawan', 'internal')->pluck('id')->toArray();
|
||||||
|
$luarIds = $rows->where('tipe_karyawan', 'luar')->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Step yang sudah lulus, di-group per pegawai_id
|
||||||
|
$lulusInternal = collect();
|
||||||
|
$lulusLuar = collect();
|
||||||
|
|
||||||
|
if (!empty($internalIds)) {
|
||||||
|
$lulusInternal = DB::connection('pgsql')
|
||||||
|
->table('public.praakre as p')
|
||||||
|
->join('public.masterpitstop as ms', DB::raw('ms.id::text'), '=', 'p.masterpitstop_id')
|
||||||
|
->whereIn('p.pegawai_id', $internalIds)
|
||||||
|
->where('ms.statusenabled', true)
|
||||||
|
->select('p.pegawai_id', 'ms.id as step_id')
|
||||||
|
->get()
|
||||||
|
->groupBy('pegawai_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($luarIds)) {
|
||||||
|
$lulusLuar = DB::connection('pgsql')
|
||||||
|
->table('public.praakre as p')
|
||||||
|
->join('public.masterpitstop as ms', DB::raw('ms.id::text'), '=', 'p.masterpitstop_id')
|
||||||
|
->whereIn('p.pegawai_id', $luarIds)
|
||||||
|
->where('p.tipe_karyawan', 'luar')
|
||||||
|
->where('ms.statusenabled', true)
|
||||||
|
->select('p.pegawai_id', 'ms.id as step_id')
|
||||||
|
->get()
|
||||||
|
->groupBy('pegawai_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Map hasil ────────────────────────────────────────────────────
|
||||||
|
$data = $rows->map(function ($r) use ($totalSteps, $lulusInternal, $lulusLuar, $allSteps) {
|
||||||
|
$isLuar = $r->tipe_karyawan === 'luar';
|
||||||
|
$lulusMap = $isLuar ? ($lulusLuar->get($r->id) ?? collect())
|
||||||
|
: ($lulusInternal->get($r->id) ?? collect());
|
||||||
|
$lulusIds = $lulusMap->pluck('step_id')->toArray();
|
||||||
|
|
||||||
|
$belumSteps = $allSteps
|
||||||
|
->filter(fn($nama, $id) => !in_array($id, $lulusIds))
|
||||||
|
->implode(', ');
|
||||||
|
|
||||||
$lulus = (int) ($r->lulus_count ?? 0);
|
$lulus = (int) ($r->lulus_count ?? 0);
|
||||||
$pct = $totalSteps > 0 ? round(($lulus / $totalSteps) * 100, 1) : 0;
|
$belum = max(0, $totalSteps - $lulus);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'tipe_karyawan' => (string) ($r->tipe_karyawan ?? 'internal'),
|
'tipe_karyawan' => (string) ($r->tipe_karyawan ?? 'internal'),
|
||||||
'id' => (int) ($r->id ?? 0),
|
'id' => (int) ($r->id ?? 0),
|
||||||
@ -1688,10 +1697,10 @@ class PitStopController extends Controller
|
|||||||
'identitas' => (string) ($r->identitas ?? '-'),
|
'identitas' => (string) ($r->identitas ?? '-'),
|
||||||
'unit_name' => (string) ($r->unit_name ?? '-'),
|
'unit_name' => (string) ($r->unit_name ?? '-'),
|
||||||
'lulus_count' => $lulus,
|
'lulus_count' => $lulus,
|
||||||
'pct' => $pct,
|
'pct' => $totalSteps > 0 ? round(($lulus / $totalSteps) * 100, 1) : 0,
|
||||||
'selesai' => $totalSteps > 0 ? ($lulus >= $totalSteps ? 1 : 0) : 0,
|
'selesai' => $totalSteps > 0 && $lulus >= $totalSteps ? 1 : 0,
|
||||||
'belum_count' => (int) ($r->belum_count ?? 0),
|
'belum_count' => $belum,
|
||||||
'belum_steps' => (string) ($r->belum_steps ?? ''),
|
'belum_steps' => $belumSteps,
|
||||||
];
|
];
|
||||||
})->values();
|
})->values();
|
||||||
|
|
||||||
@ -1704,7 +1713,6 @@ class PitStopController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function progressDetail(Request $request)
|
public function progressDetail(Request $request)
|
||||||
{
|
{
|
||||||
$validator = Validator::make($request->all(), [
|
$validator = Validator::make($request->all(), [
|
||||||
@ -2038,7 +2046,8 @@ class PitStopController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataProgressAllKaryawanGhost(Request $request){
|
public function dataProgressAllKaryawanGhost(Request $request)
|
||||||
|
{
|
||||||
$draw = $this->dtInt($request->input('draw'), 1);
|
$draw = $this->dtInt($request->input('draw'), 1);
|
||||||
$start = $this->dtInt($request->input('start'), 0);
|
$start = $this->dtInt($request->input('start'), 0);
|
||||||
$length = $this->dtInt($request->input('length'), 10);
|
$length = $this->dtInt($request->input('length'), 10);
|
||||||
@ -2049,12 +2058,29 @@ class PitStopController extends Controller
|
|||||||
|
|
||||||
$totalSteps = (int) MasterPitStopPraAkre::where('statusenabled', true)->count();
|
$totalSteps = (int) MasterPitStopPraAkre::where('statusenabled', true)->count();
|
||||||
|
|
||||||
// Internal
|
// ----------------------------------------------------------------
|
||||||
|
// Subquery: belum_count & belum_steps dihitung SEKALI via CTE
|
||||||
|
// Hindari correlated subquery per-baris
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
$belumCte = DB::connection('pgsql')
|
||||||
|
->table('public.masterpitstop as ms')
|
||||||
|
->where('ms.statusenabled', true)
|
||||||
|
->select([
|
||||||
|
'ms.id as ms_id',
|
||||||
|
'ms.nama as ms_nama',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
// Internal query — ganti correlated subquery dengan LEFT JOIN agregasi
|
||||||
|
// ----------------------------------------------------------------
|
||||||
$internal = DB::connection('pgsql')
|
$internal = DB::connection('pgsql')
|
||||||
->table('public.mappegawaijabatantounitkerja_m as mp')
|
->table('public.mappegawaijabatantounitkerja_m as mp')
|
||||||
->join('public.pegawai_m as pg', 'pg.id', '=', 'mp.objectpegawaifk')
|
->join('public.pegawai_m as pg', 'pg.id', '=', 'mp.objectpegawaifk')
|
||||||
->join('public.unitkerjapegawai_m as ukp', 'ukp.id', '=', 'mp.objectunitkerjapegawaifk')
|
->join('public.unitkerjapegawai_m as ukp', 'ukp.id', '=', 'mp.objectunitkerjapegawaifk')
|
||||||
->leftJoin('public.praakre as p', 'p.pegawai_id', '=', 'pg.id')
|
->leftJoin('public.praakre as p', function ($join) {
|
||||||
|
$join->on('p.pegawai_id', '=', 'pg.id')
|
||||||
|
->where('p.tipe_karyawan', '!=', 'luar'); // pastikan hanya internal
|
||||||
|
})
|
||||||
->leftJoin('public.masterpitstop as m', function ($join) {
|
->leftJoin('public.masterpitstop as m', function ($join) {
|
||||||
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
||||||
->where('m.statusenabled', true);
|
->where('m.statusenabled', true);
|
||||||
@ -2064,35 +2090,21 @@ class PitStopController extends Controller
|
|||||||
->where('pg.statusenabled', true)
|
->where('pg.statusenabled', true)
|
||||||
->where('pg.kedudukanfk', 1)
|
->where('pg.kedudukanfk', 1)
|
||||||
->where('ukp.statusenabled', true)
|
->where('ukp.statusenabled', true)
|
||||||
->whereNotIn('pg.kategorypegawai', ['11'])
|
|
||||||
->select([
|
->select([
|
||||||
DB::raw("'internal' as tipe_karyawan"),
|
DB::raw("'internal' as tipe_karyawan"),
|
||||||
'pg.id',
|
'pg.id',
|
||||||
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
||||||
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
DB::raw("coalesce(ukp.name, '-') as unit_name"),
|
||||||
DB::raw("count(distinct m.id) as lulus_count"),
|
DB::raw("count(distinct m.id) as lulus_count"),
|
||||||
DB::raw("coalesce((
|
// belum_count = totalSteps - lulus (dihitung di PHP, bukan subquery)
|
||||||
select count(*)
|
DB::raw("0 as belum_count"), // placeholder, dihitung di PHP
|
||||||
from public.masterpitstop ms
|
DB::raw("'' as belum_steps"), // placeholder, isi jika memang dibutuhkan
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pg.id
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), 0) as belum_count"),
|
|
||||||
DB::raw("coalesce((
|
|
||||||
select string_agg(ms.nama, ', ' order by ms.id)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pg.id
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), '') as belum_steps"),
|
|
||||||
])
|
])
|
||||||
->groupBy('pg.id', 'pg.namalengkap', 'pg.nip_pns', 'ukp.name');
|
->groupBy('pg.id', 'pg.namalengkap', 'pg.nip_pns', 'ukp.name');
|
||||||
|
|
||||||
// External
|
// ----------------------------------------------------------------
|
||||||
|
// External query
|
||||||
|
// ----------------------------------------------------------------
|
||||||
$external = DB::connection('pgsql')
|
$external = DB::connection('pgsql')
|
||||||
->table('public.pegawai_luar_pl as pl')
|
->table('public.pegawai_luar_pl as pl')
|
||||||
->leftJoin('public.praakre as p', function ($join) {
|
->leftJoin('public.praakre as p', function ($join) {
|
||||||
@ -2109,57 +2121,112 @@ class PitStopController extends Controller
|
|||||||
DB::raw("coalesce(pl.nama, '-') as nama"),
|
DB::raw("coalesce(pl.nama, '-') as nama"),
|
||||||
DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-') as unit_name"),
|
DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-') as unit_name"),
|
||||||
DB::raw("count(distinct m.id) as lulus_count"),
|
DB::raw("count(distinct m.id) as lulus_count"),
|
||||||
DB::raw("coalesce((
|
DB::raw("0 as belum_count"),
|
||||||
select count(*)
|
DB::raw("'' as belum_steps"),
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pl.id
|
|
||||||
and px.tipe_karyawan = 'luar'
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), 0) as belum_count"),
|
|
||||||
DB::raw("coalesce((
|
|
||||||
select string_agg(ms.nama, ', ' order by ms.id)
|
|
||||||
from public.masterpitstop ms
|
|
||||||
left join public.praakre px
|
|
||||||
on px.pegawai_id = pl.id
|
|
||||||
and px.tipe_karyawan = 'luar'
|
|
||||||
and px.masterpitstop_id = ms.id::text
|
|
||||||
where ms.statusenabled = true
|
|
||||||
and px.id is null
|
|
||||||
), '') as belum_steps"),
|
|
||||||
])
|
])
|
||||||
->groupBy('pl.id', 'pl.nama', 'pl.nik', DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-')"));
|
->groupBy('pl.id', 'pl.nama', 'pl.nik', DB::raw("coalesce(nullif(btrim(pl.tipe), ''), '-')"));
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
// Union & base query — hanya SATU kali wrap
|
||||||
|
// ----------------------------------------------------------------
|
||||||
$union = $internal->unionAll($external);
|
$union = $internal->unionAll($external);
|
||||||
|
|
||||||
$baseNoSearch = DB::connection('pgsql')->query()->fromSub($union, 'u');
|
$base = DB::connection('pgsql')
|
||||||
$base = DB::connection('pgsql')->query()->fromSub($union, 'u');
|
->query()
|
||||||
|
->fromSub($union, 'u');
|
||||||
|
|
||||||
if ($search !== '') {
|
if ($search !== '') {
|
||||||
$base->where(function ($q) use ($search) {
|
$base->where(function ($q) use ($search) {
|
||||||
$q->where('u.nama', 'ILIKE', '%' . $search . '%')
|
$q->where('u.nama', 'ILIKE', "%{$search}%")
|
||||||
->orWhere('u.unit_name', 'ILIKE', '%' . $search . '%')
|
->orWhere('u.unit_name', 'ILIKE', "%{$search}%")
|
||||||
->orWhere('u.tipe_karyawan', 'ILIKE', '%' . $search . '%');
|
->orWhere('u.tipe_karyawan','ILIKE', "%{$search}%");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$recordsTotal = DB::connection('pgsql')->query()->fromSub($baseNoSearch, 't')->count();
|
// ----------------------------------------------------------------
|
||||||
$recordsFiltered = $search === ''
|
// Hitung total SEKALI dengan conditional aggregate
|
||||||
? $recordsTotal
|
// Hindari eksekusi query count dua kali
|
||||||
: DB::connection('pgsql')->query()->fromSub($base, 't')->count();
|
// ----------------------------------------------------------------
|
||||||
|
$counts = (clone $base)
|
||||||
|
->selectRaw('count(*) as total')
|
||||||
|
->first();
|
||||||
|
|
||||||
$rows = $base
|
$recordsFiltered = (int) ($counts->total ?? 0);
|
||||||
|
$recordsTotal = $search === ''
|
||||||
|
? $recordsFiltered
|
||||||
|
: (int) DB::connection('pgsql')->query()->fromSub($union, 'u')->count();
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
// Ambil data dengan pagination
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
$rows = (clone $base)
|
||||||
|
->select('u.*')
|
||||||
->orderByDesc('lulus_count')
|
->orderByDesc('lulus_count')
|
||||||
->orderBy('nama')
|
->orderBy('nama')
|
||||||
->offset($start)
|
->offset($start)
|
||||||
->limit($length)
|
->limit($length)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$data = $rows->map(function ($r) use ($totalSteps) {
|
// ----------------------------------------------------------------
|
||||||
|
// belum_steps: hanya ambil untuk baris yang ditampilkan saja (bukan semua)
|
||||||
|
// Jauh lebih efisien daripada subquery per-baris
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
$pegawaiIds = $rows->where('tipe_karyawan', 'internal')->pluck('id')->toArray();
|
||||||
|
$luarIds = $rows->where('tipe_karyawan', 'luar')->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Ambil step yang sudah lulus per pegawai (hanya untuk baris di halaman ini)
|
||||||
|
$lulusInternal = collect();
|
||||||
|
$lulusLuar = collect();
|
||||||
|
|
||||||
|
if (!empty($pegawaiIds)) {
|
||||||
|
$lulusInternal = DB::connection('pgsql')
|
||||||
|
->table('public.praakre as p')
|
||||||
|
->join('public.masterpitstop as ms', DB::raw('ms.id::text'), '=', 'p.masterpitstop_id')
|
||||||
|
->whereIn('p.pegawai_id', $pegawaiIds)
|
||||||
|
->where('ms.statusenabled', true)
|
||||||
|
->select('p.pegawai_id', 'ms.id as step_id')
|
||||||
|
->get()
|
||||||
|
->groupBy('pegawai_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($luarIds)) {
|
||||||
|
$lulusLuar = DB::connection('pgsql')
|
||||||
|
->table('public.praakre as p')
|
||||||
|
->join('public.masterpitstop as ms', DB::raw('ms.id::text'), '=', 'p.masterpitstop_id')
|
||||||
|
->whereIn('p.pegawai_id', $luarIds)
|
||||||
|
->where('p.tipe_karyawan', 'luar')
|
||||||
|
->where('ms.statusenabled', true)
|
||||||
|
->select('p.pegawai_id', 'ms.id as step_id')
|
||||||
|
->get()
|
||||||
|
->groupBy('pegawai_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Semua step aktif
|
||||||
|
$allSteps = DB::connection('pgsql')
|
||||||
|
->table('public.masterpitstop')
|
||||||
|
->where('statusenabled', true)
|
||||||
|
->orderBy('id')
|
||||||
|
->pluck('nama', 'id');
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
// Map hasil
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
$data = $rows->map(function ($r) use ($totalSteps, $lulusInternal, $lulusLuar, $allSteps) {
|
||||||
|
$isLuar = $r->tipe_karyawan === 'luar';
|
||||||
|
$lulusMap = $isLuar
|
||||||
|
? ($lulusLuar->get($r->id) ?? collect())
|
||||||
|
: ($lulusInternal->get($r->id) ?? collect());
|
||||||
|
|
||||||
|
$lulusStepIds = $lulusMap->pluck('step_id')->toArray();
|
||||||
|
|
||||||
|
$belumSteps = $allSteps
|
||||||
|
->filter(fn($nama, $id) => !in_array($id, $lulusStepIds))
|
||||||
|
->values()
|
||||||
|
->implode(', ');
|
||||||
|
|
||||||
$lulus = (int) ($r->lulus_count ?? 0);
|
$lulus = (int) ($r->lulus_count ?? 0);
|
||||||
|
$belum = $totalSteps - $lulus;
|
||||||
$pct = $totalSteps > 0 ? round(($lulus / $totalSteps) * 100, 1) : 0;
|
$pct = $totalSteps > 0 ? round(($lulus / $totalSteps) * 100, 1) : 0;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'tipe_karyawan' => (string) ($r->tipe_karyawan ?? 'internal'),
|
'tipe_karyawan' => (string) ($r->tipe_karyawan ?? 'internal'),
|
||||||
'id' => (int) ($r->id ?? 0),
|
'id' => (int) ($r->id ?? 0),
|
||||||
@ -2168,8 +2235,8 @@ class PitStopController extends Controller
|
|||||||
'lulus_count' => $lulus,
|
'lulus_count' => $lulus,
|
||||||
'pct' => $pct,
|
'pct' => $pct,
|
||||||
'selesai' => $totalSteps > 0 ? ($lulus >= $totalSteps ? 1 : 0) : 0,
|
'selesai' => $totalSteps > 0 ? ($lulus >= $totalSteps ? 1 : 0) : 0,
|
||||||
'belum_count' => (int) ($r->belum_count ?? 0),
|
'belum_count' => max(0, $belum),
|
||||||
'belum_steps' => (string) ($r->belum_steps ?? ''),
|
'belum_steps' => $belumSteps,
|
||||||
];
|
];
|
||||||
})->values();
|
})->values();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user