Penambahan Halaman Keseluruhan
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Perhitungan\Remunerasi;
|
||||
|
||||
use App\Exports\KeseluruhanExport;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Rekapitulasi\P1;
|
||||
use App\Models\Verifikator\Periode;
|
||||
use App\Models\Verifikator\Publikasi;
|
||||
use App\Models\Verifikator\UnitKerja;
|
||||
use App\Models\Verifikator\View\Pegawai;
|
||||
use App\Services\DawaiService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\View\View;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class KeseluruhanController extends Controller
|
||||
{
|
||||
private function authorizeAccess(): void
|
||||
{
|
||||
abort_unless(auth()->user()->hasRole('Superuser'), 403);
|
||||
}
|
||||
|
||||
public function index(): View
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
$title = 'Keseluruhan';
|
||||
|
||||
return view('perhitungan.remunerasi.keseluruhan.index', compact('title'));
|
||||
}
|
||||
|
||||
public function data(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
$tahun = (int) ($request->tahun ?: now()->year);
|
||||
$jenisPegawai = $request->jenis_pegawai ?: null;
|
||||
$unit = $request->unit;
|
||||
|
||||
$rows = collect($this->getRekapRows($tahun, $jenisPegawai, $unit));
|
||||
|
||||
return DataTables::of($rows)
|
||||
->editColumn('jenis_pegawai', fn ($row) => $row['jenis_pegawai'] === 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] === 'tendik' ? 'Tendik' : '-'))
|
||||
->editColumn('jumlah_p1', fn ($row) => 'Rp. '.number_format($row['jumlah_p1'], 2, ',', '.'))
|
||||
->editColumn('jumlah_p2', fn ($row) => 'Rp. '.number_format($row['jumlah_p2'], 2, ',', '.'))
|
||||
->editColumn('jumlah_thr', fn ($row) => 'Rp. '.number_format($row['jumlah_thr'], 2, ',', '.'))
|
||||
->editColumn('jumlah_r13', fn ($row) => 'Rp. '.number_format($row['jumlah_r13'], 2, ',', '.'))
|
||||
->editColumn('jumlah_publikasi', fn ($row) => 'Rp. '.number_format($row['jumlah_publikasi'], 2, ',', '.'))
|
||||
->editColumn('total', fn ($row) => 'Rp. '.number_format($row['total'], 2, ',', '.'))
|
||||
->with([
|
||||
'jumlahPegawai' => $rows->count(),
|
||||
'totalKeseluruhan' => number_format($rows->sum('total'), 2, ',', '.'),
|
||||
])
|
||||
->make();
|
||||
}
|
||||
|
||||
public function export(Request $request): BinaryFileResponse
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
set_time_limit(300);
|
||||
|
||||
$tahun = (int) ($request->tahun ?: now()->year);
|
||||
$jenisPegawai = $request->jenis_pegawai ?: null;
|
||||
$unit = $request->unit;
|
||||
|
||||
$unitName = '_Semua_Unit_Kerja';
|
||||
if ($unit !== null && $unit !== '') {
|
||||
$unitKerja = UnitKerja::find((int) $unit === 99 ? 0 : (int) $unit);
|
||||
if ($unitKerja) {
|
||||
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
||||
}
|
||||
}
|
||||
|
||||
$filename = "Remunerasi_Keseluruhan_{$tahun}{$unitName}.xlsx";
|
||||
|
||||
return Excel::download(new KeseluruhanExport($tahun, $jenisPegawai, $unit), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bangun daftar rekap per pegawai (Pegawai, Unit Kerja, Jumlah P1/P2/THR/R13/Publikasi) untuk
|
||||
* tahun terpilih. Dipakai bersama oleh data() (DataTables) dan KeseluruhanExport supaya kedua
|
||||
* sisi (tampilan web & export Excel) selalu konsisten.
|
||||
*/
|
||||
public function getRekapRows(int $tahun, ?string $jenisPegawai, mixed $unit): array
|
||||
{
|
||||
$rows = $this->buildRekap($tahun, $jenisPegawai);
|
||||
|
||||
if ($unit !== null && $unit !== '') {
|
||||
$unitBucket = (int) $unit;
|
||||
$rows = $rows->filter(function ($row) use ($unitBucket) {
|
||||
if ($unitBucket === 99) {
|
||||
return $row['unit_kerja_id'] === null || ! in_array((int) $row['unit_kerja_id'], range(1, 9), true);
|
||||
}
|
||||
|
||||
return (int) $row['unit_kerja_id'] === $unitBucket;
|
||||
})->values();
|
||||
}
|
||||
|
||||
return $rows->sortBy('nama', SORT_NATURAL | SORT_FLAG_CASE)->values()->all();
|
||||
}
|
||||
|
||||
private function buildRekap(int $tahun, ?string $jenisPegawai): Collection
|
||||
{
|
||||
$cacheKey = 'remunerasi_keseluruhan_v1_'.$tahun.'_'.($jenisPegawai ?: 'semua');
|
||||
|
||||
return collect(Cache::remember($cacheKey, now()->addHours(6), fn () => $this->computeRekap($tahun, $jenisPegawai)));
|
||||
}
|
||||
|
||||
private function computeRekap(int $tahun, ?string $jenisPegawai): array
|
||||
{
|
||||
$normalize = fn ($nip) => preg_replace("/['.\-]/", '', (string) $nip);
|
||||
|
||||
// 1. P1 = akumulasi Januari s.d. bulan berjalan (atau Desember untuk tahun yang sudah lewat)
|
||||
$bulanTerakhir = $tahun === (int) now()->year ? (int) now()->month : 12;
|
||||
|
||||
$p1 = P1::query()
|
||||
->where('tahun', $tahun)
|
||||
->whereBetween('bulan', [1, $bulanTerakhir])
|
||||
->when($jenisPegawai, fn ($q) => $q->whereRaw('LOWER(jenis_pegawai) = ?', [strtolower($jenisPegawai)]))
|
||||
->get()
|
||||
->groupBy(fn ($row) => $normalize($row->nip))
|
||||
->map(fn ($rows) => (float) $rows->sum('gaji_yang_dibayarkan'));
|
||||
|
||||
// 2. P2 = Semester 1 tahun terpilih
|
||||
$periodeSemester1 = Periode::where('nama', 'LIKE', "%{$tahun} Semester 1%")->value('id_periode');
|
||||
|
||||
$p2 = collect();
|
||||
if ($periodeSemester1) {
|
||||
$p2 = Pegawai::withoutGlobalScopes()
|
||||
->join('verifikator.jabatan_pegawais as jp', 'verifikator.pegawais.nip', '=', 'jp.nip')
|
||||
->join('verifikator.jabatan_pegawai_details as jpd', 'jp.id', '=', 'jpd.jabatan_pegawai_id')
|
||||
->join('verifikator.remunerasi_per_bulans as rpb', 'jpd.id', '=', 'rpb.jabatan_pegawai_detail_id')
|
||||
->select('verifikator.pegawais.nip')
|
||||
->selectRaw('SUM(rpb.p2_bruto) as total')
|
||||
->whereNull('rpb.deleted_at')
|
||||
->where('jp.periode', $periodeSemester1)
|
||||
->when($jenisPegawai, fn ($q) => $q->where('jpd.jenis_pegawai', $jenisPegawai))
|
||||
->groupBy('verifikator.pegawais.nip')
|
||||
->havingRaw('SUM(rpb.p2_bruto) > 0')
|
||||
->get()
|
||||
->keyBy(fn ($row) => $normalize($row->nip))
|
||||
->map(fn ($row) => (float) $row->total);
|
||||
}
|
||||
|
||||
// 3. THR & 4. R13 = data eksternal (API Dawai), untuk seluruh unit sekaligus lalu di-cache
|
||||
// per hari seperti pola THRController/R13Controller.
|
||||
$jenisPegawaiParam = $jenisPegawai ?? '';
|
||||
|
||||
$thrData = Cache::remember(
|
||||
"keseluruhan_thr_{$jenisPegawaiParam}_{$tahun}",
|
||||
now()->addDay(),
|
||||
fn () => (new DawaiService)->getDataThr($jenisPegawaiParam, $tahun, null, false)
|
||||
);
|
||||
$thr = collect($thrData)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_thr'] ?? 0));
|
||||
|
||||
$r13Data = Cache::remember(
|
||||
"keseluruhan_r13_{$jenisPegawaiParam}_{$tahun}",
|
||||
now()->addDay(),
|
||||
fn () => (new DawaiService)->getDataR13($jenisPegawaiParam, $tahun, null, false)
|
||||
);
|
||||
$r13 = collect($r13Data)->keyBy(fn ($row) => $normalize($row['nip'] ?? ''))->map(fn ($row) => (float) ($row['total_r13'] ?? 0));
|
||||
|
||||
// 5. Publikasi = seluruh periode yang namanya memuat tahun terpilih (mis. "2026 Semester 1"
|
||||
// maupun "2025/2026 Ganjil"), karena publikasi tidak terikat pada satu format periode tertentu.
|
||||
$periodeIds = Periode::where('nama', 'LIKE', "%{$tahun}%")->pluck('id_periode');
|
||||
|
||||
$publikasiQuery = Publikasi::query()
|
||||
->whereIn('periode', $periodeIds)
|
||||
->where('poin_total', '>', 0)
|
||||
->select('nip')
|
||||
->selectRaw('SUM(biaya_remunerasi) as total');
|
||||
|
||||
if ($jenisPegawai) {
|
||||
$publikasiQuery->whereHas('pegawai', function ($q) use ($jenisPegawai) {
|
||||
$q->withoutGlobalScopes()->where('jenis_pegawai', $jenisPegawai);
|
||||
});
|
||||
}
|
||||
|
||||
$publikasi = $publikasiQuery->groupBy('nip')->get()
|
||||
->keyBy(fn ($row) => $normalize($row->nip))
|
||||
->map(fn ($row) => (float) $row->total);
|
||||
|
||||
$allNips = $p1->keys()
|
||||
->merge($p2->keys())
|
||||
->merge($thr->keys())
|
||||
->merge($r13->keys())
|
||||
->merge($publikasi->keys())
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($allNips->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$pegawaiInfo = Pegawai::withoutGlobalScopes()
|
||||
->leftJoin('verifikator.unit_kerja_pegawais as ukp', 'verifikator.pegawais.unit_kerja_id', '=', 'ukp.id')
|
||||
->select([
|
||||
'verifikator.pegawais.nip',
|
||||
'verifikator.pegawais.nama',
|
||||
'verifikator.pegawais.jenis_pegawai',
|
||||
'verifikator.pegawais.unit_kerja_id',
|
||||
'ukp.nama as nama_unit',
|
||||
])
|
||||
->get()
|
||||
->keyBy(fn ($row) => $normalize($row->nip));
|
||||
|
||||
return $allNips->map(function ($nip) use ($pegawaiInfo, $p1, $p2, $thr, $r13, $publikasi, $jenisPegawai) {
|
||||
$info = $pegawaiInfo->get($nip);
|
||||
|
||||
if (! $info) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($jenisPegawai && strtolower((string) $info->jenis_pegawai) !== strtolower($jenisPegawai)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$jumlahP1 = $p1->get($nip, 0.0);
|
||||
$jumlahP2 = $p2->get($nip, 0.0);
|
||||
$jumlahThr = $thr->get($nip, 0.0);
|
||||
$jumlahR13 = $r13->get($nip, 0.0);
|
||||
$jumlahPublikasi = $publikasi->get($nip, 0.0);
|
||||
$total = $jumlahP1 + $jumlahP2 + $jumlahThr + $jumlahR13 + $jumlahPublikasi;
|
||||
|
||||
if ($total <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'nip' => ltrim($info->nip ?? $nip, "'"),
|
||||
'nama' => $info->nama ?? '-',
|
||||
'jenis_pegawai' => strtolower($info->jenis_pegawai ?? ''),
|
||||
'unit_kerja_id' => $info->unit_kerja_id,
|
||||
'nama_unit' => $info->nama_unit ?? '-',
|
||||
'jumlah_p1' => $jumlahP1,
|
||||
'jumlah_p2' => $jumlahP2,
|
||||
'jumlah_thr' => $jumlahThr,
|
||||
'jumlah_r13' => $jumlahR13,
|
||||
'jumlah_publikasi' => $jumlahPublikasi,
|
||||
'total' => $total,
|
||||
];
|
||||
})->filter()->values()->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user