218 lines
7.2 KiB
PHP
218 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Models\MasterPitStopPraAkre;
|
|
use App\Models\PraAkre;
|
|
|
|
class PitStopController extends Controller
|
|
{
|
|
public function pitstop(){
|
|
$masterPitStop = MasterPitStopPraAkre::where('statusenabled', true)->get();
|
|
$data = [
|
|
'title' => 'Pit Stop',
|
|
'masterPitStop' => $masterPitStop
|
|
];
|
|
return view('pitstop.index', $data);
|
|
}
|
|
|
|
public function progress(Request $request)
|
|
{
|
|
$q = trim((string) $request->query('q', ''));
|
|
$perPage = (int) $request->query('per_page', 20);
|
|
if ($perPage < 1) $perPage = 20;
|
|
if ($perPage > 100) $perPage = 100;
|
|
|
|
$totalSteps = (int) MasterPitStopPraAkre::where('statusenabled', true)->count();
|
|
|
|
$baseQuery = DB::connection('pgsql')
|
|
->table('public.pegawai_m as pg')
|
|
->leftJoin('public.praakre as p', 'p.pegawai_id', '=', 'pg.id')
|
|
->leftJoin('public.masterpitstop as m', function ($join) {
|
|
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
|
->where('m.statusenabled', true);
|
|
})
|
|
->where('pg.statusenabled', true)
|
|
->where('pg.kedudukanfk', 1)
|
|
->when($q !== '', function ($query) use ($q) {
|
|
$query->where('pg.namalengkap', 'ILIKE', "%{$q}%");
|
|
})
|
|
->select([
|
|
'pg.id',
|
|
DB::raw("coalesce(pg.namalengkap, '-') as nama"),
|
|
DB::raw("count(distinct m.id) filter (where p.status='lulus') as lulus_count"),
|
|
])
|
|
->groupBy('pg.id', 'pg.namalengkap')
|
|
->orderByDesc('lulus_count')
|
|
->orderBy('pg.namalengkap');
|
|
|
|
$users = $baseQuery->paginate($perPage)->appends(['q' => $q, 'per_page' => $perPage]);
|
|
|
|
$totalUsers = $users->total();
|
|
$totalSelesai = DB::connection('pgsql')
|
|
->table('public.praakre as p')
|
|
->leftJoin('public.masterpitstop as m', function ($join) {
|
|
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
|
->where('m.statusenabled', true);
|
|
})
|
|
->where('p.status', 'lulus')
|
|
->whereNotNull('m.id')
|
|
->select(DB::raw('count(distinct (p.pegawai_id, m.id)) as c'))
|
|
->value('c');
|
|
|
|
if ($request->boolean('ajax')) {
|
|
return response()->json([
|
|
'error' => 0,
|
|
'data' => [
|
|
'totalUsers' => $totalUsers,
|
|
'table_html' => view('pitstop.partials.progress_rows', [
|
|
'users' => $users,
|
|
'totalSteps' => $totalSteps,
|
|
])->render(),
|
|
'pager_html' => view('pitstop.partials.progress_pager', [
|
|
'users' => $users,
|
|
])->render(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
return view('pitstop.progress', [
|
|
'title' => 'Dashboard Progress',
|
|
'q' => $q,
|
|
'perPage' => $perPage,
|
|
'totalSteps' => $totalSteps,
|
|
'totalUsers' => $totalUsers,
|
|
'totalSelesai' => $totalSelesai,
|
|
'users' => $users,
|
|
]);
|
|
}
|
|
|
|
public function progressDetail(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'pegawai_id' => 'required|integer|exists:pgsql.public.pegawai_m,id',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => 1,
|
|
'message' => 'Validasi gagal.',
|
|
'errors' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$pegawaiId = (int) $validator->validated()['pegawai_id'];
|
|
|
|
$rows = DB::connection('pgsql')
|
|
->table('public.praakre as p')
|
|
->leftJoin('public.masterpitstop as m', function ($join) {
|
|
$join->on(DB::raw('m.id::text'), '=', 'p.masterpitstop_id')
|
|
->where('m.statusenabled', true);
|
|
})
|
|
->where('p.pegawai_id', $pegawaiId)
|
|
->whereNotNull('m.id')
|
|
->select([
|
|
'p.id',
|
|
'p.masterpitstop_id',
|
|
DB::raw("coalesce(m.nama, '-') as step_nama"),
|
|
'p.status',
|
|
DB::raw("to_char(p.created_at, 'YYYY-MM-DD HH24:MI:SS') as created_at"),
|
|
])
|
|
->orderByDesc('p.id')
|
|
->limit(500)
|
|
->get();
|
|
|
|
return response()->json([
|
|
'error' => 0,
|
|
'data' => $rows,
|
|
]);
|
|
}
|
|
|
|
public function pegawaiSteps(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'pegawai_id' => 'required|integer|exists:pgsql.public.pegawai_m,id',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => 1,
|
|
'message' => 'Validasi gagal.',
|
|
'errors' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$pegawaiId = (int) $validator->validated()['pegawai_id'];
|
|
|
|
$lockedSteps = PraAkre::where('pegawai_id', $pegawaiId)
|
|
->where('status', 'lulus')
|
|
->distinct()
|
|
->pluck('masterpitstop_id');
|
|
|
|
return response()->json([
|
|
'error' => 0,
|
|
'data' => [
|
|
'locked_steps' => $lockedSteps,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function submit(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'karyawan_id' => 'required|integer|exists:pgsql.public.pegawai_m,id',
|
|
'step' => 'required',
|
|
'status' => 'required|in:lulus,tidak_lulus',
|
|
'unit_id' => 'nullable|string|max:15',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => 1,
|
|
'message' => 'Validasi gagal.',
|
|
'errors' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$payload = $validator->validated();
|
|
|
|
$pegawaiId = (int) $payload['karyawan_id'];
|
|
$masterPitstopId = (string) $payload['step'];
|
|
|
|
$alreadyPassed = PraAkre::where('pegawai_id', $pegawaiId)
|
|
->where('masterpitstop_id', $masterPitstopId)
|
|
->where('status', 'lulus')
|
|
->exists();
|
|
|
|
if ($alreadyPassed) {
|
|
return response()->json([
|
|
'error' => 1,
|
|
'message' => 'Step ini sudah lulus dan terkunci.',
|
|
], 409);
|
|
}
|
|
|
|
$row = PraAkre::create([
|
|
'pegawai_id' => $pegawaiId,
|
|
'masterpitstop_id' => $masterPitstopId,
|
|
'unit_id' => $payload['unit_id'] ?? null,
|
|
'status' => $payload['status'],
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'error' => 0,
|
|
'message' => 'Data berhasil disimpan.',
|
|
'data' => [
|
|
'id' => $row->id,
|
|
'pegawai_id' => $row->pegawai_id,
|
|
'masterpitstop_id' => $row->masterpitstop_id,
|
|
'status' => $row->status,
|
|
],
|
|
]);
|
|
}
|
|
|
|
}
|