306 lines
14 KiB
PHP
306 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Verifikator\Rekapitulasi;
|
|
use App\Models\Rekapitulasi\THR;
|
|
use App\Services\DawaiService;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProcessRekapThrJob implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
protected $sessionId;
|
|
|
|
/**
|
|
* The number of seconds the job can run before timing out.
|
|
*
|
|
* @var int
|
|
*/
|
|
public $timeout = 600; // 10 menit
|
|
|
|
/**
|
|
* The number of times the job may be attempted.
|
|
*
|
|
* @var int
|
|
*/
|
|
public $tries = 1;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct($sessionId)
|
|
{
|
|
$this->sessionId = $sessionId;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
try {
|
|
$tahunSekarang = Carbon::now()->year;
|
|
$rekapitulasi = Rekapitulasi::where('tahun', $tahunSekarang)->get();
|
|
|
|
// Ambil data rekening dari DawaiService
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 0,
|
|
'current' => 0,
|
|
'total' => 0,
|
|
'status' => 'processing',
|
|
'message' => 'Mengambil data rekening dari DAWAI...',
|
|
'detail' => 'Memulai proses...'
|
|
], 3600);
|
|
|
|
$dawaiService = new DawaiService();
|
|
$dataRekening = $dawaiService->listRekening();
|
|
|
|
// Buat mapping NIP => data rekening untuk akses cepat
|
|
$rekeningMap = [];
|
|
foreach ($dataRekening as $rekening) {
|
|
$nip = preg_replace("/['\.\-]/", "", $rekening['nip'] ?? '');
|
|
if ($nip) {
|
|
$rekeningMap[$nip] = $rekening;
|
|
}
|
|
}
|
|
|
|
Log::info("ProcessRekapThrJob: Data rekening dimuat (" . count($rekeningMap) . " pegawai)");
|
|
|
|
// Hitung total pegawai/riwayat terlebih dahulu dari semua unit
|
|
$totalRiwayatKeseluruhan = 0;
|
|
$unitDataCache = []; // Cache untuk menyimpan data per unit
|
|
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 0,
|
|
'current' => 0,
|
|
'total' => 0,
|
|
'status' => 'processing',
|
|
'message' => 'Menghitung total pegawai...',
|
|
'detail' => 'Memproses data dari API...'
|
|
], 3600);
|
|
|
|
// Loop pertama: hitung total riwayat dan cache data
|
|
foreach ($rekapitulasi as $unitKerja) {
|
|
$idUnitApi = ($unitKerja->id_unit_kerja == 0) ? 99 : $unitKerja->id_unit_kerja;
|
|
$unitDataCache[$unitKerja->id] = [
|
|
'unit' => $unitKerja,
|
|
'id_api' => $idUnitApi,
|
|
'response' => null
|
|
];
|
|
|
|
$response = Http::timeout(30)->get("http://172.16.40.128/api/v1/thrApi/$tahunSekarang/?id_unit=$idUnitApi")->json();
|
|
|
|
if (isset($response['data']) && is_array($response['data'])) {
|
|
// Hitung total riwayat
|
|
foreach ($response['data'] as $pegawai) {
|
|
if (isset($pegawai['riwayat']) && is_array($pegawai['riwayat'])) {
|
|
$totalRiwayatKeseluruhan += count($pegawai['riwayat']);
|
|
}
|
|
}
|
|
$unitDataCache[$unitKerja->id]['response'] = $response;
|
|
}
|
|
}
|
|
|
|
// Update cache dengan total pegawai
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 0,
|
|
'current' => 0,
|
|
'total' => $totalRiwayatKeseluruhan,
|
|
'status' => 'processing',
|
|
'message' => 'Memproses data pegawai...',
|
|
'detail' => "Total {$totalRiwayatKeseluruhan} riwayat akan diproses"
|
|
], 3600);
|
|
|
|
Log::info("ProcessRekapThrJob: Total riwayat keseluruhan = {$totalRiwayatKeseluruhan}");
|
|
|
|
$processedRiwayatKeseluruhan = 0;
|
|
|
|
// Loop kedua: proses data pegawai dengan progress berdasarkan total riwayat
|
|
foreach ($unitDataCache as $unitData) {
|
|
$unitKerja = $unitData['unit'];
|
|
$idUnitApi = $unitData['id_api'];
|
|
$response = $unitData['response'];
|
|
|
|
if (!$response || !isset($response['data'])) {
|
|
continue;
|
|
}
|
|
|
|
// Insert data pegawai ke tabel rekapitulasi THR
|
|
foreach ($response['data'] as $pegawai) {
|
|
// Loop melalui setiap riwayat untuk insert sebagai record terpisah
|
|
if (isset($pegawai['riwayat']) && is_array($pegawai['riwayat'])) {
|
|
foreach ($pegawai['riwayat'] as $riwayat) {
|
|
// Hitung total pajak THR (P1 + P2)
|
|
$pajakThrP1 = $riwayat['harga_thr_p1_pajak'] ?? 0;
|
|
$pajakThrP2 = $riwayat['harga_thr_p2_pajak'] ?? 0;
|
|
$totalPajakPegawai = $pajakThrP1 + $pajakThrP2;
|
|
|
|
// Total THR = THR P1 + THR P2 (simpan nilai asli dari API)
|
|
$totalThrPegawai = ($riwayat['harga_thr_p1'] ?? 0) + ($riwayat['harga_thr_p2'] ?? 0);
|
|
$totalThrSetelahPajakPegawai = ($riwayat['harga_thr_p1_setelah_pajak'] ?? 0) + ($riwayat['harga_thr_p2_setelah_pajak'] ?? 0);
|
|
|
|
// Ambil data rekening dari mapping berdasarkan NIP
|
|
$nipBersih = preg_replace("/['\.\-]/", "", $riwayat['nip'] ?? '');
|
|
$dataRekeningPegawai = $rekeningMap[$nipBersih] ?? null;
|
|
|
|
// Ambil NPWP dan nomor rekening dari data rekening, jika tidak ada ambil dari API pegawai
|
|
$npwp = null;
|
|
$nomorRekening = null;
|
|
|
|
if ($dataRekeningPegawai) {
|
|
$npwp = isset($dataRekeningPegawai['npwp']) ? preg_replace("/['\.\-]/", "", $dataRekeningPegawai['npwp']) : null;
|
|
$nomorRekening = isset($dataRekeningPegawai['rekening']) ? preg_replace("/['\.\-]/", "", $dataRekeningPegawai['rekening']) : null;
|
|
} else {
|
|
// Fallback ke data pegawai dari API
|
|
$npwp = isset($pegawai['npwp']) ? preg_replace("/['\.\-]/", "", $pegawai['npwp']) : null;
|
|
$nomorRekening = isset($pegawai['rekening']) ? preg_replace("/['\.\-]/", "", $pegawai['rekening']) : null;
|
|
}
|
|
|
|
THR::updateOrCreate(
|
|
[
|
|
'nip' => $nipBersih,
|
|
'tahun' => $riwayat['tahun'] ?? $tahunSekarang,
|
|
'bulan' => $riwayat['bulan'] ?? '3'
|
|
],
|
|
[
|
|
'nama' => $pegawai['nama'] ?? '',
|
|
'jenis_pegawai' => isset($pegawai['jenis_pegawai']) ? ucfirst(strtolower($pegawai['jenis_pegawai'])) : null,
|
|
'id_unit_kerja' => $unitKerja->id_unit_kerja,
|
|
'nama_unit_kerja' => $pegawai['nama_unit'] ?? '',
|
|
'id_unit_pembayaran' => $idUnitApi,
|
|
'pangkat' => '',
|
|
'golongan' => $riwayat['golongan'] ?? '',
|
|
'id_jabatan_fungsional' => $riwayat['jafung_id'] ?? null,
|
|
'nama_jabatan_fungsional' => null,
|
|
'grade_jabatan_fungsional' => null,
|
|
'gaji_jabatan_fungsional' => $riwayat['harga_jafung'] ?? 0,
|
|
'id_jabatan_tugas_tambahan' => $riwayat['jtt_id'] ?? null,
|
|
'nama_jabatan_tugas_tambahan' => $pegawai['jtt'] ?? null,
|
|
'grade_jabatan_tugas_tambahan' => null,
|
|
'gaji_jabatan_tugas_tambahan' => $riwayat['harga_jtt'] ?? 0,
|
|
'gaji_yang_dibayarkan' => $totalThrPegawai,
|
|
'pph21' => $totalPajakPegawai,
|
|
'gaji_bersih' => $totalThrSetelahPajakPegawai,
|
|
'npwp' => $npwp,
|
|
'nomor_rekening' => $nomorRekening,
|
|
'keterangan' => 'THR ' . $tahunSekarang,
|
|
]
|
|
);
|
|
|
|
$processedRiwayatKeseluruhan++;
|
|
|
|
// Hitung progress berdasarkan total riwayat keseluruhan
|
|
$overallProgress = 0;
|
|
if ($totalRiwayatKeseluruhan > 0) {
|
|
$overallProgress = round(($processedRiwayatKeseluruhan / $totalRiwayatKeseluruhan) * 100, 2);
|
|
}
|
|
|
|
// Pastikan progress tidak pernah mundur
|
|
$cachedProgress = cache()->get("rekap_thr_progress_{$this->sessionId}");
|
|
if ($cachedProgress && isset($cachedProgress['progress']) && $overallProgress < $cachedProgress['progress']) {
|
|
$overallProgress = $cachedProgress['progress'];
|
|
}
|
|
|
|
// Log untuk debugging (setiap 50 riwayat)
|
|
if ($processedRiwayatKeseluruhan % 50 == 0) {
|
|
Log::info("Progress THR: {$processedRiwayatKeseluruhan}/{$totalRiwayatKeseluruhan} = {$overallProgress}%");
|
|
}
|
|
|
|
// Update cache setiap riwayat diproses (dengan throttling)
|
|
$shouldUpdate = ($processedRiwayatKeseluruhan % 10 == 0) ||
|
|
($overallProgress >= ($cachedProgress['progress'] ?? 0) + 1) ||
|
|
($processedRiwayatKeseluruhan == 1);
|
|
|
|
if ($shouldUpdate) {
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => $overallProgress,
|
|
'current' => $processedRiwayatKeseluruhan,
|
|
'total' => $totalRiwayatKeseluruhan,
|
|
'status' => 'processing',
|
|
'message' => "Memproses riwayat ke-{$processedRiwayatKeseluruhan} dari {$totalRiwayatKeseluruhan}",
|
|
'detail' => "{$unitKerja->nama_unit_kerja} - {$pegawai['nama']}"
|
|
], 3600);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update total_thr berdasarkan sum gaji_yang_dibayarkan dari tabel THR
|
|
foreach ($rekapitulasi as $unitKerja) {
|
|
// Kondisi: jika unit kerja == 0 maka ambil dari id_unit_pembayaran 99, selainnya sama
|
|
$idUnitPembayaran = ($unitKerja->id_unit_kerja == 0) ? 99 : $unitKerja->id_unit_kerja;
|
|
|
|
// Hitung total gaji_yang_dibayarkan dari tabel THR (dibulatkan ke bilangan bulat)
|
|
$totalThrFromDb = round(THR::where('id_unit_pembayaran', $idUnitPembayaran)
|
|
->where('tahun', $tahunSekarang)
|
|
->sum('gaji_yang_dibayarkan'));
|
|
|
|
// Ambil total kekurangan dari API
|
|
$response = Http::timeout(30)->get("http://172.16.40.128/api/v1/thrApi/$tahunSekarang/?id_unit=$idUnitPembayaran")->json();
|
|
$totalKekuranganThr = $response['total_kekurangan_thr'] ?? 0;
|
|
|
|
// Update rekapitulasi dengan total dari database
|
|
$rekapitulasiUpdate = Rekapitulasi::find($unitKerja->id);
|
|
if ($rekapitulasiUpdate) {
|
|
$rekapitulasiUpdate->total_thr = $totalThrFromDb;
|
|
$rekapitulasiUpdate->total_kekurangan_thr = $totalKekuranganThr;
|
|
$rekapitulasiUpdate->save();
|
|
}
|
|
}
|
|
|
|
// Mark as completed
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 100,
|
|
'current' => $totalRiwayatKeseluruhan,
|
|
'total' => $totalRiwayatKeseluruhan,
|
|
'status' => 'completed',
|
|
'message' => 'Rekapitulasi THR berhasil dijalankan!',
|
|
'detail' => "Semua {$totalRiwayatKeseluruhan} riwayat berhasil diproses"
|
|
], 3600);
|
|
|
|
} catch (\Exception $e) {
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 0,
|
|
'current' => 0,
|
|
'total' => 0,
|
|
'status' => 'error',
|
|
'message' => 'Terjadi kesalahan: ' . $e->getMessage(),
|
|
'detail' => ''
|
|
], 3600);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a job failure.
|
|
*
|
|
* @param \Throwable $exception
|
|
* @return void
|
|
*/
|
|
public function failed(\Throwable $exception): void
|
|
{
|
|
// Update cache dengan status error ketika job gagal atau timeout
|
|
$errorMessage = $exception->getMessage();
|
|
|
|
// Cek apakah ini timeout error
|
|
if (strpos($errorMessage, 'timeout') !== false || strpos($errorMessage, 'Timeout') !== false) {
|
|
$errorMessage = 'Proses melebihi batas waktu 10 menit. Silakan coba lagi atau hubungi administrator.';
|
|
}
|
|
|
|
cache()->put("rekap_thr_progress_{$this->sessionId}", [
|
|
'progress' => 0,
|
|
'current' => 0,
|
|
'total' => 0,
|
|
'status' => 'error',
|
|
'message' => 'Job gagal: ' . $errorMessage,
|
|
'detail' => 'Proses dihentikan karena error atau timeout.'
|
|
], 3600);
|
|
}
|
|
}
|