get(); $periodeNow = optional(Periode::where('status', true)->first())->id_periode; return view('lainnya.simulasi-jabatan.index', compact('title', 'periodes', 'periodeNow')); } /** * 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')); } 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. $rows = $this->applyUnitJenisFilter($this->dataset($periode), $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'] 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)); $row['poin_grade_14_17'] = number_format((float) SimulasiJabatanExport::GRADE_1417_POIN, 2, ',', '.'); $row['total_poin'] = number_format($parseN($row['total_poin']) + SimulasiJabatanExport::GRADE_1417_POIN, 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); 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)); } $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()); } /** * 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): string { return "simulasi-jabatan:dataset:{$periode}"; } /** * 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)); $start = microtime(true); $rows = $this->dataset($periode); return response()->json([ 'success' => true, 'periode' => $periode, 'rows' => $rows->count(), 'seconds' => round(microtime(true) - $start, 1), ]); } public function dataset(int $periode): \Illuminate\Support\Collection { $key = self::datasetCacheKey($periode); 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) { return Cache::remember($key, now()->addMinutes(30), fn () => $this->buildDataset($periode)); }); } private function buildDataset(int $periode): \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)->mapWithKeys(function ($row) use ($format) { $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; } return [$nip => [ 'nip' => $nip, 'nama_pegawai' => $row['nama_pegawai'], 'jenis_pegawai' => mb_strtolower((string) $row['jenis_pegawai']), 'unit_kerja' => $row['unit_kerja'] === 'Tanpa Unit Kerja' ? '' : $row['unit_kerja'], 'unit_kerja_id' => (int) $row['raw_unit_kerja_id'], '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; $grade12 = $request->boolean('grade12'); $suffix = ($unitKerjaId ? '-unit'.$unitKerjaId : '').($grade12 ? '-grade12' : ''); return Excel::download(new SimulasiJabatanExport($periode, null, $jenis, $unitKerjaId, $grade12), 'simulasi-jabatan-'.$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): ?\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'; $rpb = DB::table('verifikator.remunerasi_per_bulans') ->whereIn('jabatan_pegawai_detail_id', $jp->detail->pluck('id')->all()) ->whereNull('deleted_at') ->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; } $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, &$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). $poin = $d->poin; $dp = is_string($poin?->detail_p2) ? json_decode($poin->detail_p2, true) : ($poin?->detail_p2 ?? []); $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) { $poinNonAdhocSim = $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. if ($fromRemun = $this->detailFromRemun($pegawai, $periode, $request->boolean('grade12'))) { 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, ',', '.')]); } }