189 lines
6.5 KiB
PHP
189 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Lainnya\CekPoin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use App\Models\Verifikator\JabatanPegawai;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class CekPoinController extends Controller
|
|
{
|
|
private function isWakilDekan()
|
|
{
|
|
$nip = Auth::user()->nip;
|
|
|
|
return JabatanPegawai::whereHas('detail', function ($query) {
|
|
$query->where('jabatan_tambahan_id', 7);
|
|
})
|
|
->where('nip', $nip)
|
|
->where('active', true)
|
|
->exists();
|
|
}
|
|
|
|
function index()
|
|
{
|
|
$title = 'Cek Poin';
|
|
$isWakilDekan = $this->isWakilDekan();
|
|
|
|
return view('lainnya.cek-poin.index', compact('title', 'isWakilDekan'));
|
|
}
|
|
|
|
function data(Request $request)
|
|
{
|
|
// Check if request is for unit kerja options
|
|
if ($request->has('get_unit_kerja')) {
|
|
$unitKerja = Pegawai::where('jenis_pegawai', 'dosen')
|
|
->distinct()
|
|
->orderBy('unit_kerja_nama')
|
|
->pluck('unit_kerja_nama');
|
|
|
|
return response()->json(['unit_kerja' => $unitKerja]);
|
|
}
|
|
|
|
$nip = Auth::user()->nip;
|
|
$authUser = Pegawai::select('nip', 'nama', 'unit_kerja_id', 'unit_kerja_nama')->where('nip', $nip)->first();
|
|
$pegawais = Pegawai::select('nip', 'nama', 'unit_kerja_nama as unit_kerja')->where('jenis_pegawai', 'dosen');
|
|
|
|
// Check if user is Vice Dean (Wakil Dekan) with jabatan tugas tambahan id = 7
|
|
if ($this->isWakilDekan()) {
|
|
// Filter employees by authenticated user's unit kerja
|
|
$pegawais->where('unit_kerja_nama', $authUser->unit_kerja_nama);
|
|
}
|
|
|
|
// Apply unit kerja filter if provided
|
|
if ($request->filled('unit_kerja')) {
|
|
$pegawais->where('unit_kerja_nama', $request->unit_kerja);
|
|
}
|
|
|
|
$pegawais = $pegawais->orderBy('nama');
|
|
|
|
return DataTables::of($pegawais)
|
|
->addIndexColumn()
|
|
->addColumn('action', function ($row) {
|
|
return '
|
|
<button class="btn btn-sm btn-outline-primary detail-btn"
|
|
data-nip="' . $row->nip . '"
|
|
data-nama="' . $row->nama . '">
|
|
<i class="uil-info-circle"></i>
|
|
Detail
|
|
</button>
|
|
';
|
|
})
|
|
->filterColumn('unit_kerja', function ($query, $keyword) {
|
|
$query->where('unit_kerja_nama', 'like', "%{$keyword}%");
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
function detail(Request $request, $nip)
|
|
{
|
|
$title = 'Detail Cek Poin Pegawai';
|
|
|
|
// Get pegawai info
|
|
$pegawai = Pegawai::where('nip', $nip)->first();
|
|
|
|
if (!$pegawai) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pegawai tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Data berhasil diproses'
|
|
]);
|
|
}
|
|
|
|
// Call database function
|
|
$rawData = DB::select('SELECT * FROM func_mengajar_395_v1(?, ?)', [$nip, 'S1']);
|
|
$data = json_decode(json_encode($rawData), true);
|
|
|
|
return view('lainnya.cek-poin.detail', compact('title', 'pegawai', 'data'));
|
|
}
|
|
|
|
public function summary(Request $request)
|
|
{
|
|
$unitKerja = $request->input('unit_kerja');
|
|
|
|
// Check if user is Vice Dean - restrict to their unit kerja only
|
|
if ($this->isWakilDekan()) {
|
|
$nip = Auth::user()->nip;
|
|
$authUser = Pegawai::select('unit_kerja_nama')->where('nip', $nip)->first();
|
|
$unitKerja = $authUser->unit_kerja_nama;
|
|
}
|
|
|
|
$cacheKey = 'cek_poin_summary_' . md5($unitKerja ?: 'all');
|
|
|
|
// Check cache first (cache for 30 minutes for better performance)
|
|
$cachedResult = Cache::remember($cacheKey, 1800, function () use ($unitKerja) {
|
|
// Get all pegawai - filter by unit kerja if provided
|
|
$pegawaisQuery = Pegawai::where('jenis_pegawai', 'dosen');
|
|
|
|
if ($unitKerja) {
|
|
$pegawaisQuery->where('unit_kerja_nama', $unitKerja);
|
|
}
|
|
|
|
$pegawais = $pegawaisQuery->select('nip')->get();
|
|
$totalPegawai = $pegawais->count();
|
|
|
|
// For performance, if more than 300 pegawai and no specific unit, use sampling
|
|
if (!$unitKerja && $totalPegawai > 300) {
|
|
$pegawais = $pegawais->shuffle()->take(300);
|
|
$sampledCount = $pegawais->count();
|
|
} else {
|
|
$sampledCount = $totalPegawai;
|
|
}
|
|
|
|
// Process all pegawai in smaller chunks for better performance
|
|
$nipList = $pegawais->pluck('nip')->chunk(20);
|
|
$totalSelisih = 0;
|
|
|
|
foreach ($nipList as $nipChunk) {
|
|
try {
|
|
// Process each pegawai individually but in chunks to manage memory
|
|
foreach ($nipChunk as $nip) {
|
|
$rawData = DB::select('SELECT * FROM func_mengajar_395_v1(?, ?)', [$nip, 'S1']);
|
|
|
|
foreach ($rawData as $data) {
|
|
$harga392 = $data->harga_jabatan_392 ?? 0;
|
|
$harga395 = $data->harga_jabatan_395 ?? 0;
|
|
$selisih = $harga392 - $harga395;
|
|
$totalSelisih += $selisih;
|
|
}
|
|
}
|
|
} catch (Exception) {
|
|
// Skip if error occurs for this chunk
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// If we used sampling, extrapolate the result
|
|
if (!$unitKerja && $totalPegawai > 300) {
|
|
$totalSelisih = ($totalSelisih / $sampledCount) * $totalPegawai;
|
|
}
|
|
|
|
return [
|
|
'total_selisih' => $totalSelisih,
|
|
'total_pegawai' => $totalPegawai,
|
|
'unit_kerja' => $unitKerja ?: 'Semua Unit Kerja',
|
|
'sampled' => (!$unitKerja && $totalPegawai > 300) ? true : false,
|
|
'sample_size' => $sampledCount
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $cachedResult
|
|
]);
|
|
}
|
|
}
|