From ed20b7255b7bf933525e84b6e9689bdfd6f4697a Mon Sep 17 00:00:00 2001 From: Ariyutta Date: Mon, 12 Oct 2026 08:16:31 +0700 Subject: [PATCH] Pembaruan P1 --- .../Perhitungan/Remunerasi/P1Controller.php | 137 +++++++++++++----- .../perhitungan/remunerasi/p1/index.blade.php | 79 ++++++++++ routes/prefix/remunerasi.php | 1 + 3 files changed, 179 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php b/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php index 5516ad19..c8c8dfd6 100644 --- a/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php +++ b/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php @@ -60,41 +60,7 @@ class P1Controller extends Controller $cacheKey = "p1_rekap_compare_v2_{$jenisPegawai}_{$tahun}_{$bulan}_{$unit}"; $data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $bulan, $unit) { - $apiData = collect((new DawaiService)->getDataP1($jenisPegawai, $tahun, $bulan, $unit)) - ->keyBy(fn ($item) => preg_replace("/['\.\-]/", '', $item['nip'] ?? '')); - - return P1::query() - ->where('tahun', $tahun) - ->where('bulan', $bulan) - ->when($unit !== null && $unit !== '', function ($query) use ($unit) { - $idUnitPembayaran = (int) $unit === 0 ? 99 : (int) $unit; - $query->where('id_unit_pembayaran', $idUnitPembayaran); - }) - ->when($jenisPegawai !== '', function ($query) use ($jenisPegawai) { - $query->whereRaw('LOWER(jenis_pegawai) = ?', [strtolower($jenisPegawai)]); - }) - ->get() - ->map(function (P1 $pegawai) use ($apiData) { - $apiPegawai = $apiData->get(preg_replace("/['\.\-]/", '', $pegawai->nip)); - $differs = fn ($dbValue, $apiValue) => $apiPegawai !== null - && abs((float) $dbValue - (float) $apiValue) >= 0.01; - - return [ - 'nama_lengkap' => $pegawai->nama, - 'nip' => $pegawai->nip, - 'jenis_pegawai' => strtolower($pegawai->jenis_pegawai ?? ''), - 'nama_unit' => $pegawai->nama_unit_kerja, - 'nama_jafung' => $pegawai->nama_jabatan_fungsional, - 'nama_jtt' => $pegawai->nama_jabatan_tugas_tambahan, - 'harga_p1' => $pegawai->gaji_yang_dibayarkan, - 'pajak_p1' => $pegawai->pph21, - 'harga_p1_setelah_pajak' => $pegawai->gaji_bersih, - 'harga_p1_diff' => $differs($pegawai->gaji_yang_dibayarkan, $apiPegawai['harga_p1'] ?? 0), - 'pajak_p1_diff' => $differs($pegawai->pph21, $apiPegawai['pajak_p1'] ?? 0), - 'harga_p1_setelah_pajak_diff' => $differs($pegawai->gaji_bersih, $apiPegawai['harga_p1_setelah_pajak'] ?? 0), - ]; - }) - ->all(); + return $this->fetchCompareData($jenisPegawai, $tahun, $bulan, $unit); }); $includePppk2025 = $request->boolean('include_pppk_2025'); @@ -152,9 +118,7 @@ class P1Controller extends Controller $cacheKey = "p1_selisih_data_{$jenisPegawai}_{$tahun}_{$bulan}_{$unit}"; $data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $bulan, $unit) { - $dawaiService = new DawaiService; - - return $dawaiService->getDataP1Selisih($jenisPegawai, $tahun, $bulan, $unit); + return $this->fetchSelisihData($jenisPegawai, $tahun, $bulan, $unit); }); $filteredData = collect($data)->filter(function ($item) use ($jenisSelisih, $includePppk2025) { @@ -595,4 +559,101 @@ class P1Controller extends Controller return response()->json(['status' => 'success', 'message' => 'Log berhasil disimpan']); } + + public function refresh(Request $request): JsonResponse + { + $datenow = Carbon::now(); + $bulanNow = $datenow->format('m'); + $tahunNow = $datenow->format('Y'); + $team = Team::find(session('team_id')); + + if (! $team) { + return response()->json(['success' => false, 'message' => 'Team tidak ditemukan'], 403); + } + + if (! isset($team->unit_kerja_id) || $team->unit_kerja_id === null || $team->unit_kerja_id === 0) { + $unit = $request->unit; + } else { + $unit = $team->unit_kerja_id; + } + + $tahun = (int) ($request->tahun ?? $tahunNow); + $bulan = (int) ($request->bulan ?? $bulanNow); + + try { + // Bersihkan semua variasi cache terkait untuk tahun dan bulan terpilih + $jenisList = ['', 'dosen', 'jtt-univ', 'jtt', 'tendik']; + $unitList = array_unique([$unit, '', null, 99, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + + foreach ($unitList as $u) { + foreach ($jenisList as $j) { + Cache::forget("p1_rekap_compare_v2_{$j}_{$tahun}_{$bulan}_{$u}"); + Cache::forget("p1_rekap_compare_{$j}_{$tahun}_{$bulan}_{$u}"); + Cache::forget("p1_selisih_data_{$j}_{$tahun}_{$bulan}_{$u}"); + } + } + + return response()->json([ + 'success' => true, + 'message' => 'Cache data P1 berhasil dibersihkan.', + ]); + } catch (\Throwable $th) { + Log::error('P1 Refresh Error: '.$th->getMessage(), [ + 'exception' => $th, + ]); + + return response()->json([ + 'success' => false, + 'message' => 'Gagal memperbarui data cache: '.$th->getMessage(), + ], 500); + } + } + + private function fetchCompareData(string $jenisPegawai, int|string $tahun, int|string $bulan, mixed $unit): array + { + $dawaiUnit = ($unit !== null && $unit !== '') ? (int) $unit : null; + $apiData = collect((new DawaiService)->getDataP1($jenisPegawai, (int) $tahun, (int) $bulan, $dawaiUnit)) + ->keyBy(fn ($item) => preg_replace("/['\.\-]/", '', $item['nip'] ?? '')); + + return P1::query() + ->where('tahun', $tahun) + ->where('bulan', $bulan) + ->when($unit !== null && $unit !== '', function ($query) use ($unit) { + $idUnitPembayaran = (int) $unit === 0 ? 99 : (int) $unit; + $query->where('id_unit_pembayaran', $idUnitPembayaran); + }) + ->when($jenisPegawai !== '', function ($query) use ($jenisPegawai) { + $query->whereRaw('LOWER(jenis_pegawai) = ?', [strtolower($jenisPegawai)]); + }) + ->get() + ->map(function (P1 $pegawai) use ($apiData) { + $apiPegawai = $apiData->get(preg_replace("/['\.\-]/", '', $pegawai->nip)); + $differs = fn ($dbValue, $apiValue) => $apiPegawai !== null + && abs((float) $dbValue - (float) $apiValue) >= 0.01; + + return [ + 'nama_lengkap' => $pegawai->nama, + 'nip' => $pegawai->nip, + 'jenis_pegawai' => strtolower($pegawai->jenis_pegawai ?? ''), + 'nama_unit' => $pegawai->nama_unit_kerja, + 'nama_jafung' => $pegawai->nama_jabatan_fungsional, + 'nama_jtt' => $pegawai->nama_jabatan_tugas_tambahan, + 'harga_p1' => $pegawai->gaji_yang_dibayarkan, + 'pajak_p1' => $pegawai->pph21, + 'harga_p1_setelah_pajak' => $pegawai->gaji_bersih, + 'harga_p1_diff' => $differs($pegawai->gaji_yang_dibayarkan, $apiPegawai['harga_p1'] ?? 0), + 'pajak_p1_diff' => $differs($pegawai->pph21, $apiPegawai['pajak_p1'] ?? 0), + 'harga_p1_setelah_pajak_diff' => $differs($pegawai->gaji_bersih, $apiPegawai['harga_p1_setelah_pajak'] ?? 0), + ]; + }) + ->all(); + } + + private function fetchSelisihData(string $jenisPegawai, int|string $tahun, int|string $bulan, mixed $unit): array + { + $dawaiUnit = ($unit !== null && $unit !== '') ? (int) $unit : null; + $dawaiService = new DawaiService; + + return $dawaiService->getDataP1Selisih($jenisPegawai, (int) $tahun, (int) $bulan, $dawaiUnit) ?? []; + } } diff --git a/resources/views/perhitungan/remunerasi/p1/index.blade.php b/resources/views/perhitungan/remunerasi/p1/index.blade.php index 74f60507..4952613f 100644 --- a/resources/views/perhitungan/remunerasi/p1/index.blade.php +++ b/resources/views/perhitungan/remunerasi/p1/index.blade.php @@ -146,6 +146,9 @@ 'title' => 'Daftar P1', 'description' => 'Berikut adalah daftar perhitungan P1 untuk setiap pegawai', 'actionButton' => ' + Export Excel Export Excel Export Excel @@ -419,6 +422,23 @@ + @endsection @section('js') @@ -573,6 +593,65 @@ tablePerbandingan.ajax.reload(); }); + $('#btnPerbaruiData').on('click', function() { + var $btn = $(this); + var modalEl = document.getElementById('warmCacheModal'); + var modal = bootstrap.Modal.getOrCreateInstance(modalEl); + var $bar = $('#warm-cache-bar'); + var $pct = $('#warm-cache-pct'); + var pct = 0; + + function setPct(v) { + pct = v; + $bar.css('width', v + '%'); + $pct.text(v + '%'); + } + setPct(0); + $btn.prop('disabled', true); + modal.show(); + + // Progres merangkak sampai ~90% selama menunggu server. + var timer = setInterval(function() { + if (pct < 90) { + setPct(Math.min(90, pct + Math.max(1, Math.round((90 - pct) / 12)))); + } + }, 350); + + var postData = { + tahun: $('select#tahun').val(), + bulan: $('select#bulan').val(), + jenis_pegawai: $('select#jenis_pegawai').val() + }; + + @if (!$team || !isset($team->unit_kerja_id) || $team->unit_kerja_id === null || $team->unit_kerja_id === 0) + postData.unit = $('select#unit_kerja').val(); + @endif + + $.ajax({ + url: "{{ route('remunerasi.p1.refresh') }}", + method: 'POST', + data: postData + }).done(function(res) { + clearInterval(timer); + setPct(100); + setTimeout(function() { + modal.hide(); + showSuccess('Berhasil!', res.message || 'Data cache P1 berhasil diperbarui.'); + if (table) { table.ajax.reload(null, false); } + if (tableSelisih) { tableSelisih.ajax.reload(null, false); } + if (tablePerbandingan) { tablePerbandingan.ajax.reload(null, false); } + }, 450); + }).fail(function(xhr) { + clearInterval(timer); + modal.hide(); + var errorMsg = (xhr.responseJSON && xhr.responseJSON.message) ? + xhr.responseJSON.message : 'Terjadi kesalahan saat memperbarui data.'; + showError('Gagal', errorMsg); + }).always(function() { + $btn.prop('disabled', false); + }); + }); + $('a.export').on('click', function(e) { e.preventDefault(); var button = $(this); diff --git a/routes/prefix/remunerasi.php b/routes/prefix/remunerasi.php index 100667b5..32ffcd1c 100644 --- a/routes/prefix/remunerasi.php +++ b/routes/prefix/remunerasi.php @@ -21,6 +21,7 @@ Route::middleware('permission:Baca Pembayaran')->prefix('/remunerasi')->name('re Route::get('/export-selisih', [P1Controller::class, 'exportSelisih'])->name('export-selisih'); Route::get('/export-perbandingan', [P1Controller::class, 'exportPerbandingan'])->name('export-perbandingan'); Route::post('/logs', [P1Controller::class, 'logs'])->name('logs'); + Route::post('/refresh', [P1Controller::class, 'refresh'])->name('refresh'); }); Route::prefix('/p2')->name('p2.')->group(function () {