627 lines
36 KiB
PHP
627 lines
36 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Lainnya\SimulasiJabatan;
|
|
|
|
use App\Helpers\JenjangPendidikanHelper;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\JabatanPegawaiDetailPoin;
|
|
use App\Models\Verifikator\JabatanFungsionalRiwayat;
|
|
use App\Models\Verifikator\JabatanPegawai;
|
|
use App\Models\Verifikator\JabatanPegawaiDetail;
|
|
use App\Models\Verifikator\JabatanTugasTambahanRiwayat;
|
|
use App\Models\Verifikator\Periode;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exports\SimulasiJabatanExport;
|
|
use App\Exports\SimulasiJabatanTotalExport;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class SimulasiJabatanController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$title = 'Simulasi Jabatan';
|
|
$periodes = Periode::orderByDesc('id_periode')->get();
|
|
$periodeNow = optional(Periode::where('status', true)->first())->id_periode;
|
|
$jenjangPendidikanList = JenjangPendidikanHelper::options();
|
|
|
|
return view('lainnya.simulasi-jabatan.index', compact('title', 'periodes', 'periodeNow', 'jenjangPendidikanList'));
|
|
}
|
|
|
|
/**
|
|
* Filter unit kerja & jenis pegawai atas collection dataset yang sudah di-cache.
|
|
* Unit kerja mengikuti menu Remunerasi > P2: fakultas 1-9, atau 99 = Universitas
|
|
* (seluruh unit non-fakultas).
|
|
*/
|
|
private function applyUnitJenisFilter(\Illuminate\Support\Collection $rows, Request $request): \Illuminate\Support\Collection
|
|
{
|
|
$unit = $request->input('unit_kerja_id');
|
|
if ((string) $unit === (string) SimulasiJabatanExport::UNIT_UNIVERSITAS) {
|
|
$rows = $rows->whereNotIn('unit_kerja_id', SimulasiJabatanExport::FAKULTAS_IDS);
|
|
} elseif ($unit !== null && $unit !== '') {
|
|
$rows = $rows->where('unit_kerja_id', (int) $unit);
|
|
}
|
|
|
|
if ($request->filled('jenis_pegawai')) {
|
|
$rows = $rows->where('jenis_pegawai', $request->input('jenis_pegawai'));
|
|
}
|
|
|
|
if ($request->filled('jenjang_pendidikan')) {
|
|
$rows = JenjangPendidikanHelper::applyCollectionFilter($rows, 'jenjang_pendidikan', $request->input('jenjang_pendidikan'));
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
@set_time_limit(300);
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
$search = trim((string) data_get($request->all(), 'search.value', ''));
|
|
$start = max(0, (int) $request->input('start', 0));
|
|
$length = (int) $request->input('length', 10);
|
|
|
|
// Seluruh dataset (perhitungan berat) dibangun sekali lalu di-cache.
|
|
// Filter unit kerja / jenis pegawai / pencarian + paginasi dilakukan
|
|
// di memori supaya tiap request berikutnya nyaris tanpa beban DB.
|
|
$splitUnit = $request->boolean('split_unit');
|
|
$rows = $this->applyUnitJenisFilter($this->dataset($periode, $splitUnit), $request);
|
|
$recordsTotal = $rows->count();
|
|
|
|
if ($search !== '') {
|
|
$needle = mb_strtolower($search);
|
|
$rows = $rows->filter(function ($row) use ($needle) {
|
|
foreach (['nama_pegawai', 'nip', 'unit_kerja', 'jenis_pegawai', 'unit_pembayaran'] as $field) {
|
|
if (str_contains(mb_strtolower((string) ($row[$field] ?? '')), $needle)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
$recordsFiltered = $rows->count();
|
|
|
|
$page = $length < 0 ? $rows->values() : $rows->slice($start, $length)->values();
|
|
$g12 = $request->boolean('grade12');
|
|
|
|
return response()->json([
|
|
'draw' => (int) $request->input('draw'),
|
|
'recordsTotal' => $recordsTotal,
|
|
'recordsFiltered' => $recordsFiltered,
|
|
'data' => $page->map(function ($row, $index) use ($start, $g12) {
|
|
if ($g12) {
|
|
$row['p2_total_374'] = $row['p2_total_374_g12'];
|
|
$row['p2_total_377'] = $row['p2_total_377_g12'];
|
|
$row['p2_total_excel'] = $row['p2_total_excel_g12'];
|
|
if (! empty($row['grade12_eligible'])) {
|
|
$parseN = fn ($v) => (float) str_replace(',', '.', str_replace('.', '', $v));
|
|
// Total Poin dosen mentok di 68 (aturan yang sama dengan
|
|
// rumus P2: min(68, ...)). Tambahan +12 hanya berlaku
|
|
// sampai plafon itu, jadi dosen yang sudah 68 tidak
|
|
// bertambah dan yang mendekati 68 hanya naik sebagian.
|
|
$totalPoin = $parseN($row['total_poin']);
|
|
$tambahanEfektif = max(0.0, min((float) SimulasiJabatanExport::TOTAL_POIN_MAX, $totalPoin + SimulasiJabatanExport::GRADE_1417_POIN) - $totalPoin);
|
|
$row['poin_grade_14_17'] = number_format($tambahanEfektif, 2, ',', '.');
|
|
$row['total_poin'] = number_format($totalPoin + $tambahanEfektif, 2, ',', '.');
|
|
}
|
|
}
|
|
return array_merge($row, ['DT_RowIndex' => $start + $index + 1]);
|
|
})->all(),
|
|
]);
|
|
}
|
|
|
|
public function summary(Request $request)
|
|
{
|
|
@set_time_limit(300);
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
$search = trim((string) $request->input('search'));
|
|
|
|
$rows = $this->applyUnitJenisFilter($this->dataset($periode, $request->boolean('split_unit')), $request);
|
|
if ($search !== '') {
|
|
$needle = mb_strtolower($search);
|
|
$rows = $rows->filter(fn ($row) => str_contains(mb_strtolower((string) $row['nama_pegawai']), $needle)
|
|
|| str_contains(mb_strtolower((string) $row['nip']), $needle)
|
|
|| str_contains(mb_strtolower((string) $row['unit_kerja']), $needle)
|
|
|| str_contains(mb_strtolower((string) ($row['unit_pembayaran'] ?? '')), $needle));
|
|
}
|
|
|
|
$parse = fn ($value) => (float) str_replace(',', '.', str_replace('.', '', $value));
|
|
$suffix = $request->boolean('grade12') ? '_g12' : '';
|
|
$totals = collect(['p2_total_374', 'p2_total_377', 'p2_total_excel'])
|
|
->mapWithKeys(fn ($key) => [$key => number_format($rows->sum(fn ($row) => $parse($row[$key.$suffix])), 2, ',', '.')]);
|
|
|
|
return response()->json($totals->all());
|
|
}
|
|
|
|
/**
|
|
* Agregat Total P2 per unit kerja untuk visualisasi grafik batang.
|
|
* Pengelompokan mengikuti menu Remunerasi > P2: fakultas 1-9 + "Universitas"
|
|
* (seluruh unit non-fakultas). Filter unit kerja diabaikan supaya seluruh
|
|
* unit tetap tampil; filter periode & jenis pegawai tetap dihormati.
|
|
*/
|
|
public function chart(Request $request)
|
|
{
|
|
@set_time_limit(300);
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
|
|
$rows = $this->dataset($periode);
|
|
if ($request->filled('jenis_pegawai')) {
|
|
$rows = $rows->where('jenis_pegawai', $request->input('jenis_pegawai'));
|
|
}
|
|
if ($request->filled('jenjang_pendidikan')) {
|
|
$rows = JenjangPendidikanHelper::applyCollectionFilter($rows, 'jenjang_pendidikan', $request->input('jenjang_pendidikan'));
|
|
}
|
|
|
|
$parse = fn ($value) => (float) str_replace(',', '.', str_replace('.', '', (string) $value));
|
|
|
|
$labels = [
|
|
1 => 'Fakultas Hukum',
|
|
2 => 'Fakultas Ekonomi dan Bisnis',
|
|
3 => 'Fakultas Pertanian',
|
|
4 => 'Fakultas Teknik',
|
|
5 => 'Fakultas Ilmu Sosial dan Ilmu Politik',
|
|
6 => 'Fakultas Keguruan dan Ilmu Pendidikan',
|
|
7 => 'Fakultas Kehutanan',
|
|
8 => 'Fakultas Matematika dan Ilmu Pengetahuan Alam',
|
|
9 => 'Fakultas Kedokteran',
|
|
SimulasiJabatanExport::UNIT_UNIVERSITAS => 'Universitas',
|
|
];
|
|
$short = [
|
|
1 => 'FH', 2 => 'FEB', 3 => 'FP', 4 => 'FT', 5 => 'FISIP',
|
|
6 => 'FKIP', 7 => 'FHUT', 8 => 'FMIPA', 9 => 'FK',
|
|
SimulasiJabatanExport::UNIT_UNIVERSITAS => 'UNIV',
|
|
];
|
|
|
|
$groups = collect($labels)->map(function ($nama, $id) use ($rows, $parse, $short) {
|
|
$subset = (int) $id === SimulasiJabatanExport::UNIT_UNIVERSITAS
|
|
? $rows->whereNotIn('unit_kerja_id', SimulasiJabatanExport::FAKULTAS_IDS)
|
|
: $rows->where('unit_kerja_id', (int) $id);
|
|
|
|
$sum = fn ($key) => round($subset->sum(fn ($r) => $parse($r[$key])), 2);
|
|
|
|
return [
|
|
'unit_kerja' => $nama,
|
|
'unit_kerja_short' => $short[$id],
|
|
'pegawai' => $subset->count(),
|
|
'p2_374' => $sum('p2_total_374'),
|
|
'p2_377' => $sum('p2_total_377'),
|
|
'p2_excel' => $sum('p2_total_excel'),
|
|
'p2_374_g12' => $sum('p2_total_374_g12'),
|
|
'p2_377_g12' => $sum('p2_total_377_g12'),
|
|
'p2_excel_g12' => $sum('p2_total_excel_g12'),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json($groups->all());
|
|
}
|
|
|
|
/**
|
|
* Bangun dan cache seluruh baris simulasi untuk satu periode.
|
|
*
|
|
* Perhitungan harga/P2 memakai pendekatan batch yang sama dengan ekspor
|
|
* Excel (SimulasiJabatanSheet::rows) sehingga angka pada tabel, kartu
|
|
* ringkasan, dan file ekspor konsisten. Satu baris per pegawai; Total P2
|
|
* sudah menjumlah seluruh bulan jabatan pada periode tersebut. Hasilnya
|
|
* berupa collection ter-key berdasarkan NIP.
|
|
*/
|
|
public static function datasetCacheKey(int $periode, bool $splitUnit = false): string
|
|
{
|
|
// v3: aturan normalisasi jenjang_pendidikan berubah (mis. Profesi/Sp-1 -> S1,
|
|
// DIII -> D3) — versi dinaikkan supaya cache lama tidak dipakai ulang.
|
|
return "simulasi-jabatan:dataset:v3:{$periode}".($splitUnit ? ':split-unit' : '');
|
|
}
|
|
|
|
/**
|
|
* Hitung ulang & simpan cache dataset untuk periode terpilih (tombol
|
|
* "Perbarui Data" di halaman). Dipakai setelah data poin/Remunerasi berubah.
|
|
*/
|
|
public function warmCache(Request $request)
|
|
{
|
|
@set_time_limit(300);
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
|
|
Cache::forget(self::datasetCacheKey($periode, false));
|
|
Cache::forget(self::datasetCacheKey($periode, true));
|
|
$start = microtime(true);
|
|
$rows = $this->dataset($periode, false);
|
|
$this->dataset($periode, true);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'periode' => $periode,
|
|
'rows' => $rows->count(),
|
|
'seconds' => round(microtime(true) - $start, 1),
|
|
]);
|
|
}
|
|
|
|
public function dataset(int $periode, bool $splitUnit = false): \Illuminate\Support\Collection
|
|
{
|
|
$key = self::datasetCacheKey($periode, $splitUnit);
|
|
|
|
if ($cached = Cache::get($key)) {
|
|
return $cached;
|
|
}
|
|
|
|
// Halaman memuat tabel + kartu ringkasan sekaligus; keduanya butuh
|
|
// dataset yang sama. Lock supaya proses berat hanya berjalan sekali
|
|
// saat cache dingin, request lain menunggu lalu memakai hasilnya.
|
|
return Cache::lock($key.':lock', 180)->block(185, function () use ($key, $periode, $splitUnit) {
|
|
return Cache::remember($key, now()->addMinutes(30), fn () => $this->buildDataset($periode, $splitUnit));
|
|
});
|
|
}
|
|
|
|
private function buildDataset(int $periode, bool $splitUnit = false): \Illuminate\Support\Collection
|
|
{
|
|
// Pastikan file kelas ekspor (yang juga mendefinisikan
|
|
// SimulasiJabatanSheet) sudah ter-autoload.
|
|
class_exists(SimulasiJabatanExport::class);
|
|
|
|
$format = fn ($value) => number_format((float) $value, 2, ',', '.');
|
|
|
|
return \App\Exports\SimulasiJabatanSheet::rows($periode, null, null, null, true, false, $splitUnit)->mapWithKeys(function ($row) use ($format, $splitUnit) {
|
|
$nip = ltrim((string) $row['nip'], "'");
|
|
|
|
// Bila perhitungan Remunerasi sudah ada: pakai nilai P2 riil (satu
|
|
// harga jabatan, tanpa dipecah JF/JTT). Tendik tanpa Remunerasi -> 0.
|
|
// Dosen tanpa Remunerasi -> model lama (P2 JF + P2 JTT + adhoc).
|
|
// Untuk tiap versi P2 disiapkan 2 nilai: normal & varian "+12 poin
|
|
// grade" (dosen bergrade < 14). Frontend memilih sesuai checkbox.
|
|
if (! empty($row['raw_has_remun'])) {
|
|
$p2_374 = (float) $row['raw_p2_final_374'];
|
|
$p2_377 = (float) $row['raw_p2_final_377'];
|
|
$p2_excel = (float) $row['raw_p2_final_excel'];
|
|
$p2_374_g12 = (float) $row['raw_p2_final_374_g12'];
|
|
$p2_377_g12 = (float) $row['raw_p2_final_377_g12'];
|
|
$p2_excel_g12 = (float) $row['raw_p2_final_excel_g12'];
|
|
} elseif (! empty($row['raw_is_tendik'])) {
|
|
$p2_374 = $p2_377 = $p2_excel = 0.0;
|
|
$p2_374_g12 = $p2_377_g12 = $p2_excel_g12 = 0.0;
|
|
} else {
|
|
$adhocHargaP2 = (float) $row['raw_adhoc_p2'];
|
|
$p2_374 = $p2_374_g12 = $row['total_p2_jf_374'] + $row['total_p2_jtt_374'] + $adhocHargaP2;
|
|
$p2_377 = $p2_377_g12 = $row['total_p2_jf_377'] + $row['total_p2_jtt_377'] + $adhocHargaP2;
|
|
$p2_excel = $p2_excel_g12 = $row['total_p2_jf_excel'] + $row['total_p2_jtt_excel'] + $adhocHargaP2;
|
|
}
|
|
|
|
$unitPembayaranId = $row['unit_pembayaran_id'] ?? null;
|
|
$key = $splitUnit && $unitPembayaranId !== null ? $nip.'|'.$unitPembayaranId : $nip;
|
|
|
|
return [$key => [
|
|
'nip' => $nip,
|
|
'nama_pegawai' => $row['nama_pegawai'],
|
|
'jenis_pegawai' => mb_strtolower((string) $row['jenis_pegawai']),
|
|
'jenjang_pendidikan' => JenjangPendidikanHelper::normalize((string) ($row['raw_pendidikan_terakhir'] ?? '')),
|
|
'unit_kerja' => $row['unit_kerja'] === 'Tanpa Unit Kerja' ? '' : $row['unit_kerja'],
|
|
'unit_kerja_id' => (int) $row['raw_unit_kerja_id'],
|
|
'unit_pembayaran' => $row['unit_pembayaran'] ?? null,
|
|
'unit_pembayaran_id' => $unitPembayaranId,
|
|
'grade12_eligible' => (bool) ($row['raw_grade12_eligible'] ?? false),
|
|
'poin_kegiatan' => $format($row['raw_poin_kegiatan']),
|
|
'poin_adhoc' => $format($row['raw_poin_adhoc']),
|
|
'poin_absen' => $format($row['raw_poin_absen']),
|
|
'poin_skp' => $format($row['raw_poin_skp']),
|
|
'poin_grade_14_17' => $format($row['raw_poin_grade_14_17']),
|
|
'total_poin' => $format($row['raw_total_poin']),
|
|
'p2_total_374' => $format($p2_374),
|
|
'p2_total_377' => $format($p2_377),
|
|
'p2_total_excel' => $format($p2_excel),
|
|
'p2_total_374_g12' => $format($p2_374_g12),
|
|
'p2_total_377_g12' => $format($p2_377_g12),
|
|
'p2_total_excel_g12' => $format($p2_excel_g12),
|
|
]];
|
|
});
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
// Ekspor membuat beberapa sheet dan melakukan kalkulasi per pegawai.
|
|
// Naikkan batas waktu hanya untuk proses ini, bukan seluruh aplikasi.
|
|
@set_time_limit(300);
|
|
@ini_set('max_execution_time', '300');
|
|
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
$jenis = $request->input('jenis_pegawai') ?: null;
|
|
$unitKerjaId = $request->input('unit_kerja_id') ?: null;
|
|
$jenjangPendidikan = $request->input('jenjang_pendidikan') ?: null;
|
|
$grade12 = $request->boolean('grade12');
|
|
$suffix = ($unitKerjaId ? '-unit'.$unitKerjaId : '').($grade12 ? '-grade12' : '');
|
|
return Excel::download(new SimulasiJabatanExport($periode, null, $jenis, $unitKerjaId, $grade12, $jenjangPendidikan), 'simulasi-jabatan-'.$periode.$suffix.'.xlsx');
|
|
}
|
|
|
|
/**
|
|
* Rekap Excel "Total": ringkas — satu baris per pegawai (atau per unit
|
|
* pembayaran bila mode pecah aktif) berisi Total P2 (2022), P2 (2023),
|
|
* P2 (Excel), plus satu baris TOTAL keseluruhan. Sumber angka sama dengan
|
|
* tabel & kartu ringkasan (dataset ter-cache), menghormati filter periode,
|
|
* unit kerja, jenis pegawai, simulasi +12 poin grade, dan unit pembayaran.
|
|
*/
|
|
public function exportTotal(Request $request)
|
|
{
|
|
@set_time_limit(300);
|
|
@ini_set('max_execution_time', '300');
|
|
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
$splitUnit = $request->boolean('split_unit');
|
|
$grade12 = $request->boolean('grade12');
|
|
$unitKerjaId = $request->input('unit_kerja_id') ?: null;
|
|
|
|
$key = $grade12 ? '_g12' : '';
|
|
$parse = fn ($value) => (float) str_replace(',', '.', str_replace('.', '', (string) $value));
|
|
|
|
$rows = $this->applyUnitJenisFilter($this->dataset($periode, $splitUnit), $request)
|
|
->values()
|
|
->map(function ($row) use ($parse, $key, $splitUnit) {
|
|
$out = [
|
|
'nama_pegawai' => $row['nama_pegawai'],
|
|
'nip' => "'".$row['nip'],
|
|
'jenis_pegawai' => strtoupper((string) $row['jenis_pegawai']),
|
|
'unit_kerja' => $row['unit_kerja'] ?: 'Tanpa Unit Kerja',
|
|
];
|
|
if ($splitUnit) {
|
|
$out['unit_pembayaran'] = $row['unit_pembayaran'] ?: '-';
|
|
}
|
|
$out['p2_2022'] = $parse($row['p2_total_374'.$key]);
|
|
$out['p2_2023'] = $parse($row['p2_total_377'.$key]);
|
|
$out['p2_excel'] = $parse($row['p2_total_excel'.$key]);
|
|
// Poin P2 baru terbentuk bila Total Poin melewati ambang 12
|
|
// (rumus P2 memakai ...-12). Bila di bawah itu, atau ketiga versi
|
|
// P2 nol, tandai supaya mudah difilter di Excel.
|
|
$out['keterangan'] = ($parse($row['total_poin']) < 12
|
|
|| ($out['p2_2022'] == 0.0 && $out['p2_2023'] == 0.0 && $out['p2_excel'] == 0.0))
|
|
? 'Poin P2 tidak mencukupi 12 Poin'
|
|
: '';
|
|
|
|
return $out;
|
|
});
|
|
|
|
$suffix = ($unitKerjaId ? '-unit'.$unitKerjaId : '').($grade12 ? '-grade12' : '').($splitUnit ? '-unit-pembayaran' : '');
|
|
|
|
return Excel::download(new SimulasiJabatanTotalExport($rows, $splitUnit), 'simulasi-jabatan-total-'.$periode.$suffix.'.xlsx');
|
|
}
|
|
|
|
/**
|
|
* Rincian modal mengikuti aturan menu Remunerasi > P2: seluruh Poin P2 dikali
|
|
* SATU harga jabatan — tendik: JTT bila ada & harga 377-nya > 0 (selain itu
|
|
* JF); dosen: JF — tanpa dipecah. P2 (377) = nilai riil Remunerasi per bulan
|
|
* (di luar porsi Adhoc yang tarifnya tetap); versi 374 & Excel diskala menurut
|
|
* rasio harga jabatan. Return null bila data Remunerasi belum ada (pakai cara lama).
|
|
*/
|
|
private function detailFromRemun(Pegawai $pegawai, int $periode, bool $grade12 = false, int|string|null $unitPembayaran = null): ?\Illuminate\Http\JsonResponse
|
|
{
|
|
$jp = JabatanPegawai::with(['detail.fungsional', 'detail.tambahan', 'detail.poin'])
|
|
->where('nip', $pegawai->nip)->where('periode', $periode)->where('active', true)->first();
|
|
|
|
if (! $jp || $jp->detail->isEmpty()) {
|
|
return null;
|
|
}
|
|
$isTendik = mb_strtolower((string) ($jp->detail->first()->jenis_pegawai ?? $pegawai->jenis_pegawai)) === 'tendik';
|
|
|
|
// Bila dibuka dari baris "pecah per unit pembayaran": batasi rincian ke
|
|
// remunerasi yang dibayar unit tsb saja.
|
|
$unitScoped = $unitPembayaran !== null && $unitPembayaran !== '';
|
|
$unitId = (int) $unitPembayaran;
|
|
|
|
$rpb = DB::table('verifikator.remunerasi_per_bulans')
|
|
->whereIn('jabatan_pegawai_detail_id', $jp->detail->pluck('id')->all())
|
|
->whereNull('deleted_at')
|
|
->when($unitScoped, fn ($q) => $q->where('unit_pembayaran_id', $unitId))
|
|
->selectRaw('jabatan_pegawai_detail_id, SUM(poin_p2) as poin_p2, SUM(p2_bruto) as p2_bruto')
|
|
->groupBy('jabatan_pegawai_detail_id')
|
|
->get()->keyBy('jabatan_pegawai_detail_id');
|
|
if ($rpb->isEmpty() || (float) $rpb->sum('p2_bruto') <= 0) {
|
|
return null;
|
|
}
|
|
|
|
if ($unitScoped) {
|
|
$jp->setRelation('detail', $jp->detail->filter(fn ($d) => $rpb->has($d->id))->values());
|
|
if ($jp->detail->isEmpty()) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
$excel = collect(json_decode(file_get_contents(public_path('data-jabatan-excel.json')), true) ?: [])
|
|
->keyBy(fn ($x) => mb_strtolower(trim($x['nama_jabatan'] ?? '')));
|
|
$jfRiw = fn ($id, $p, $col = 'harga') => $id ? (JabatanFungsionalRiwayat::where('jabatan_fungsional_id', $id)->where('periode', $p)->value($col)) : null;
|
|
$jttRiw = fn ($id, $p, $col = 'harga') => $id ? (JabatanTugasTambahanRiwayat::where('jtt_id', $id)->where('periode', $p)->value($col)) : null;
|
|
$fmt = fn ($v) => number_format((float) $v, 2, ',', '.');
|
|
$parse = fn ($v) => (float) str_replace(',', '.', str_replace('.', '', $v));
|
|
|
|
// Simulasi opsional "+12 poin grade" — dosen yang TIDAK punya grade 14-17
|
|
// pada jabatan fungsional maupun jabatan tugas tambahan.
|
|
$firstD = $jp->detail->sortBy('bulan')->first();
|
|
$gJf = $jfRiw($firstD->jabatan_fungsional_id, 377, 'grade') ?? $jfRiw($firstD->jabatan_fungsional_id, 374, 'grade');
|
|
$gJtt = $jttRiw($firstD->jabatan_tambahan_id, 377, 'grade') ?? $jttRiw($firstD->jabatan_tambahan_id, 374, 'grade');
|
|
$is1417 = fn ($g) => is_numeric($g) && (int) $g >= 14;
|
|
$eligibleG12 = $grade12 && ! $isTendik && ! $is1417($gJf) && ! $is1417($gJtt);
|
|
|
|
$adhocRows = collect();
|
|
$data = $jp->detail->sortBy('bulan')->values()->map(function ($d) use ($rpb, $excel, $jfRiw, $jttRiw, $fmt, $isTendik, $eligibleG12, $unitScoped, $unitId, &$adhocRows) {
|
|
$r = $rpb->get($d->id);
|
|
$poinP2 = $r ? (float) $r->poin_p2 : 0.0;
|
|
$bruto377 = $r ? (float) $r->p2_bruto : 0.0;
|
|
|
|
// Porsi Adhoc bulan ini (tarif tetap Rp 660.000, tidak diskala harga jabatan).
|
|
// detail_p2 dikelompokkan per unit pembayaran; saat baris dibuka dari
|
|
// mode "pecah per unit", hanya adhoc unit tsb yang dihitung.
|
|
$poin = $d->poin;
|
|
$dp = is_string($poin?->detail_p2) ? json_decode($poin->detail_p2, true) : ($poin?->detail_p2 ?? []);
|
|
$dp = $dp ?: [];
|
|
if ($unitScoped) {
|
|
$dp = array_key_exists($unitId, $dp) ? [$unitId => $dp[$unitId]] : [];
|
|
}
|
|
$adhocPoin = collect($dp)->sum(fn ($u) => collect($u['adhoc_kegiatan'] ?? [])->sum('poin_p2'));
|
|
// Porsi Adhoc dikunci maksimal sebesar P2 bulan itu (kasus kontrak/non-PNS).
|
|
$adhocHarga = min((float) $adhocPoin * 660000 / 6, $bruto377);
|
|
if ($adhocPoin > 0) {
|
|
$adhocRows->push([
|
|
'bulan' => $d->bulan, 'kegiatan' => 'Adhoc',
|
|
'poin_p2' => $fmt($adhocPoin),
|
|
'harga_kegiatan' => $fmt(660000),
|
|
'harga_p2_adhoc' => $fmt($adhocHarga),
|
|
]);
|
|
}
|
|
|
|
$jf374 = (float) ($jfRiw($d->jabatan_fungsional_id, 374) ?? 0);
|
|
$jf377 = (float) ($jfRiw($d->jabatan_fungsional_id, 377) ?? 0);
|
|
$jfName = $d->fungsional?->nama ?? '-';
|
|
$jfExcel = (float) ($excel->get(mb_strtolower(trim($jfName)))['harga_jabatan'] ?? 0);
|
|
$jtt374 = (float) ($jttRiw($d->jabatan_tambahan_id, 374) ?? 0);
|
|
$jtt377 = (float) ($jttRiw($d->jabatan_tambahan_id, 377) ?? 0);
|
|
$jttName = $d->tambahan?->nama ?? '-';
|
|
$jttExcel = (float) ($excel->get(mb_strtolower(trim($jttName)))['harga_jabatan'] ?? 0);
|
|
|
|
// Poin dibagi SETELAH poin Adhoc dikeluarkan. Poin JTT: tendik =
|
|
// seluruh poin P2 non-Adhoc; dosen = min(28, poin P2 non-Adhoc).
|
|
$hasJtt = $d->jabatan_tambahan_id && $jtt377 > 0;
|
|
$poinP2NonAdhoc = max(0.0, $poinP2 - (float) $adhocPoin);
|
|
$jttPoin = $hasJtt ? ($isTendik ? $poinP2NonAdhoc : min(28.0, $poinP2NonAdhoc)) : 0.0;
|
|
$jfPoin = max(0.0, $poinP2NonAdhoc - $jttPoin);
|
|
// Porsi JTT tidak boleh melebihi P2 bulan itu (di luar Adhoc); sisanya
|
|
// jadi porsi JF/kegiatan. Menjaga total per bulan tetap = p2_bruto riil.
|
|
$jttPortion = min($jttPoin * $jtt377 / 6, max(0.0, $bruto377 - $adhocHarga));
|
|
$jfPortion = max(0.0, $bruto377 - $jttPortion - $adhocHarga); // menyerap distribusi kegiatan
|
|
$ratio = fn ($num, $den) => $den > 0 ? $num / $den : 0.0;
|
|
|
|
// Tambahan "+12 poin grade" (opsional) — dihargai tarif dasar jabatan.
|
|
if ($eligibleG12) {
|
|
// Plafon 68 tetap berlaku (net 68-12 di luar poin wajib), jadi
|
|
// bulan yang poin P2-nya sudah mentok tidak ikut naik.
|
|
$poinNonAdhocSim = min((float) (SimulasiJabatanExport::TOTAL_POIN_MAX - SimulasiJabatanExport::GRADE_1417_POIN), $poinP2NonAdhoc + SimulasiJabatanExport::GRADE_1417_POIN);
|
|
$jttPoinSim = $hasJtt ? min(28.0, $poinNonAdhocSim) : 0.0;
|
|
$jfPoinSim = max(0.0, $poinNonAdhocSim - $jttPoinSim);
|
|
$jttPortion += ($jttPoinSim - $jttPoin) * $jtt377 / 6;
|
|
$jfPortion += ($jfPoinSim - $jfPoin) * $jf377 / 6;
|
|
$jttPoin = $jttPoinSim;
|
|
$jfPoin = $jfPoinSim;
|
|
}
|
|
|
|
return [
|
|
'bulan' => $d->bulan,
|
|
'jabatan' => $jfName,
|
|
'grade' => $jfRiw($d->jabatan_fungsional_id, 377, 'grade') ?? $jfRiw($d->jabatan_fungsional_id, 374, 'grade') ?? '-',
|
|
'poin_kegiatan' => $fmt($jfPoin),
|
|
'harga_374' => $fmt($jf374), 'harga_p2_374' => $fmt($jfPortion * $ratio($jf374, $jf377)),
|
|
'harga_377' => $fmt($jf377), 'harga_p2_377' => $fmt($jfPortion),
|
|
'harga_excel' => $fmt($jfExcel), 'harga_p2_excel' => $fmt($jfPortion * $ratio($jfExcel, $jf377)),
|
|
'jabatan_tambahan' => $jttName,
|
|
'grade_tambahan' => $jttRiw($d->jabatan_tambahan_id, 377, 'grade') ?? $jttRiw($d->jabatan_tambahan_id, 374, 'grade') ?? '-',
|
|
'poin_tambahan' => $fmt($jttPoin),
|
|
'harga_tambahan_374' => $fmt($jtt374), 'harga_p2_tambahan_374' => $fmt($jttPortion * $ratio($jtt374, $jtt377)),
|
|
'harga_tambahan_377' => $fmt($jtt377), 'harga_p2_tambahan_377' => $fmt($jttPortion),
|
|
'harga_tambahan_excel' => $fmt($jttExcel), 'harga_p2_tambahan_excel' => $fmt($jttPortion * $ratio($jttExcel, $jtt377)),
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'pegawai' => $pegawai->nama,
|
|
'data' => $data,
|
|
'adhoc' => $adhocRows->values(),
|
|
'total_harga_p2_374' => $fmt($data->sum(fn ($x) => $parse($x['harga_p2_374']))),
|
|
'total_harga_p2_377' => $fmt($data->sum(fn ($x) => $parse($x['harga_p2_377']))),
|
|
'total_harga_p2_excel' => $fmt($data->sum(fn ($x) => $parse($x['harga_p2_excel']))),
|
|
'total_harga_p2_tambahan_374' => $fmt($data->sum(fn ($x) => $parse($x['harga_p2_tambahan_374']))),
|
|
'total_harga_p2_tambahan_377' => $fmt($data->sum(fn ($x) => $parse($x['harga_p2_tambahan_377']))),
|
|
]);
|
|
}
|
|
|
|
public function detail(Request $request, string $nip)
|
|
{
|
|
$pegawai = Pegawai::where('nip', $nip)->firstOrFail();
|
|
$periode = (int) ($request->input('periode') ?: optional(Periode::where('status', true)->first())->id_periode);
|
|
|
|
// Rincian mengikuti aturan Remunerasi > P2 bila datanya sudah ada.
|
|
// Jatuh ke perhitungan lama hanya bila Remunerasi belum dihitung.
|
|
$unitPembayaran = $request->boolean('split_unit') ? $request->input('unit') : null;
|
|
if ($fromRemun = $this->detailFromRemun($pegawai, $periode, $request->boolean('grade12'), $unitPembayaran)) {
|
|
return $fromRemun;
|
|
}
|
|
|
|
$jabatanExcel = collect(json_decode(file_get_contents(public_path('data-jabatan-excel.json')), true) ?: [])
|
|
->keyBy(fn ($item) => mb_strtolower(trim($item['nama_jabatan'] ?? '')));
|
|
$details = JabatanPegawai::with(['detail.fungsional'])->where('nip', $nip)->where('active', true)->latest('periode')->first()?->detail ?? collect();
|
|
$data = collect($details)->map(function ($detail) use ($request, $jabatanExcel) {
|
|
$riwayat374 = $detail->jabatan_fungsional_id ? JabatanFungsionalRiwayat::where('jabatan_fungsional_id', $detail->jabatan_fungsional_id)->where('periode', 374)->first() : null;
|
|
$riwayat377 = $detail->jabatan_fungsional_id ? JabatanFungsionalRiwayat::where('jabatan_fungsional_id', $detail->jabatan_fungsional_id)->where('periode', 377)->first() : null;
|
|
$harga374 = (float) ($riwayat374?->harga ?? 0);
|
|
$harga377 = (float) ($riwayat377?->harga ?? 0);
|
|
$tambahan374 = $detail->jabatan_tambahan_id ? JabatanTugasTambahanRiwayat::where('jtt_id', $detail->jabatan_tambahan_id)->where('periode', 374)->first() : null;
|
|
$tambahan377 = $detail->jabatan_tambahan_id ? JabatanTugasTambahanRiwayat::where('jtt_id', $detail->jabatan_tambahan_id)->where('periode', 377)->first() : null;
|
|
$hargaTambahan374 = (float) ($tambahan374?->harga ?? 0);
|
|
$hargaTambahan377 = (float) ($tambahan377?->harga ?? 0);
|
|
$gradeTambahan = (int) ($tambahan377?->grade ?? $tambahan374?->grade ?? 0);
|
|
$parseRequestPoint = function ($value) {
|
|
if (is_numeric($value)) {
|
|
return (float) $value;
|
|
}
|
|
|
|
return (float) str_replace(',', '.', str_replace('.', '', (string) $value));
|
|
};
|
|
$poinKegiatan = $parseRequestPoint($request->input('poin_kegiatan', 0));
|
|
$totalPoin = $parseRequestPoint($request->input('total_poin', 0));
|
|
$poinAdhoc = $parseRequestPoint($request->input('poin_adhoc', 0));
|
|
$grade = (int) ($riwayat377?->grade ?? $riwayat374?->grade ?? 0);
|
|
$tambahan = $detail->jabatan_tambahan_id ? 28 : 0;
|
|
$tambahanGrade1417 = in_array($gradeTambahan, [14, 15, 16, 17], true) ? 12 : 0;
|
|
$poinDasarP2 = ($tambahanGrade1417 + $totalPoin) - $tambahan - $poinAdhoc - 12;
|
|
$jabatan = $detail->fungsional?->nama ?? '-';
|
|
$excel = $jabatanExcel->get(mb_strtolower(trim($jabatan)));
|
|
$hargaExcel = (float) ($excel['harga_jabatan'] ?? 0);
|
|
return ['jabatan' => $jabatan, 'grade' => $grade ?: '-', 'harga_374' => number_format($harga374, 2, ',', '.'), 'harga_p2_374' => number_format((1 / 6) * $poinDasarP2 * $harga374, 2, ',', '.'), 'harga_377' => number_format($harga377, 2, ',', '.'), 'harga_p2_377' => number_format((1 / 6) * $poinDasarP2 * $harga377, 2, ',', '.'), 'harga_excel' => number_format($hargaExcel, 2, ',', '.'), 'harga_p2_excel' => number_format((1 / 6) * $poinDasarP2 * $hargaExcel, 2, ',', '.'), 'harga_tambahan_374' => number_format($hargaTambahan374, 2, ',', '.'), 'harga_p2_tambahan_374' => number_format((1 / 6) * $poinDasarP2 * $hargaTambahan374, 2, ',', '.'), 'harga_tambahan_377' => number_format($hargaTambahan377, 2, ',', '.'), 'harga_p2_tambahan_377' => number_format((1 / 6) * $poinDasarP2 * $hargaTambahan377, 2, ',', '.'), 'poin_kegiatan' => number_format($poinDasarP2, 2, ',', '.'), 'bulan' => $detail->bulan];
|
|
})->values();
|
|
$data = $data->map(function ($item) use ($nip) {
|
|
$item['jabatan_tambahan'] = JabatanPegawai::with('detail.tambahan')->where('nip', $nip)->where('active', true)->latest('periode')->first()?->detail->firstWhere('bulan', $item['bulan'])?->tambahan?->nama ?? '-';
|
|
return $item;
|
|
});
|
|
$data = $data->map(function ($item) use ($jabatanExcel, $nip, $request) {
|
|
$parsePoint = function ($value) {
|
|
if (is_numeric($value)) {
|
|
return (float) $value;
|
|
}
|
|
|
|
return (float) str_replace(',', '.', str_replace('.', '', (string) $value));
|
|
};
|
|
$item['poin_tambahan'] = number_format(28, 2, ',', '.');
|
|
$detailTambahan = JabatanPegawai::with('detail')->where('nip', $nip)->where('active', true)->latest('periode')->first()?->detail->firstWhere('bulan', $item['bulan']);
|
|
$riwayatTambahan = $detailTambahan?->jabatan_tambahan_id ? JabatanTugasTambahanRiwayat::where('jtt_id', $detailTambahan->jabatan_tambahan_id)->where('periode', 377)->first() : null;
|
|
$item['grade_tambahan'] = $riwayatTambahan?->grade ?? '-';
|
|
$excel = $jabatanExcel->get(mb_strtolower(trim($item['jabatan_tambahan'] ?? '')));
|
|
$harga = (float) ($excel['harga_jabatan'] ?? 0);
|
|
$poin = (float) str_replace(',', '.', str_replace('.', '', $item['poin_kegiatan']));
|
|
$totalPoin = $parsePoint($request->input('total_poin', 0));
|
|
$adhocPoin = $parsePoint($request->input('poin_adhoc', 0));
|
|
$poinP2Total = max(0, min(68, $totalPoin - $adhocPoin) - 12);
|
|
if (in_array((int) ($item['grade_tambahan'] ?? 0), [14, 15, 16, 17], true)) {
|
|
$poinP2Total = max(0, min(68, ($totalPoin - $adhocPoin) + 12) - 12);
|
|
}
|
|
$poinTambahan = $item['jabatan_tambahan'] !== '-' ? min(28, $poinP2Total) : 0;
|
|
$poinFungsional = max(0, $poinP2Total - $poinTambahan);
|
|
$item['poin_kegiatan'] = number_format($poinFungsional, 2, ',', '.');
|
|
$item['poin_tambahan'] = number_format($poinTambahan, 2, ',', '.');
|
|
$item['harga_tambahan_excel'] = number_format($harga, 2, ',', '.');
|
|
$item['harga_p2_tambahan_excel'] = number_format((1 / 6) * $poin * $harga, 2, ',', '.');
|
|
foreach ([['harga_374', 'harga_p2_374', $poinFungsional], ['harga_377', 'harga_p2_377', $poinFungsional], ['harga_excel', 'harga_p2_excel', $poinFungsional], ['harga_tambahan_374', 'harga_p2_tambahan_374', $poinTambahan], ['harga_tambahan_377', 'harga_p2_tambahan_377', $poinTambahan], ['harga_tambahan_excel', 'harga_p2_tambahan_excel', $poinTambahan]] as [$priceKey, $p2Key, $point]) {
|
|
$item[$p2Key] = number_format((1 / 6) * $point * $parsePoint($item[$priceKey] ?? 0), 2, ',', '.');
|
|
}
|
|
return $item;
|
|
});
|
|
$parse = fn ($value) => (float) str_replace(',', '.', str_replace('.', '', $value));
|
|
$adhoc = collect($details)->map(function ($detail) {
|
|
$poin = $detail->poin;
|
|
$detailP2 = is_string($poin?->detail_p2) ? json_decode($poin->detail_p2, true) : ($poin?->detail_p2 ?? []);
|
|
$poinAdhoc = collect($detailP2 ?: [])->sum(fn ($unit) => collect($unit['adhoc_kegiatan'] ?? [])->sum('poin_p2'));
|
|
|
|
return [
|
|
'bulan' => $detail->bulan,
|
|
'kegiatan' => 'Adhoc',
|
|
'poin_p2' => number_format((float) $poinAdhoc, 2, ',', '.'),
|
|
'harga_kegiatan' => number_format(660000, 2, ',', '.'),
|
|
'harga_p2_adhoc' => number_format((float) $poinAdhoc * 660000 / 6, 2, ',', '.'),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json(['pegawai' => $pegawai->nama, 'data' => $data, 'adhoc' => $adhoc, 'total_harga_p2_374' => number_format($data->sum(fn ($item) => $parse($item['harga_p2_374'])), 2, ',', '.'), 'total_harga_p2_377' => number_format($data->sum(fn ($item) => $parse($item['harga_p2_377'])), 2, ',', '.'), 'total_harga_p2_excel' => number_format($data->sum(fn ($item) => $parse($item['harga_p2_excel'])), 2, ',', '.'), 'total_harga_p2_tambahan_374' => number_format($data->sum(fn ($item) => $parse($item['harga_p2_tambahan_374'])), 2, ',', '.'), 'total_harga_p2_tambahan_377' => number_format($data->sum(fn ($item) => $parse($item['harga_p2_tambahan_377'])), 2, ',', '.')]);
|
|
}
|
|
}
|