209 lines
7.5 KiB
PHP
209 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Publikasi;
|
|
|
|
use App\Models\Verifikator\Periode;
|
|
use App\Models\Verifikator\Publikasi;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use App\Services\InsentifService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class GetDataPublikasiCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'publikasi:get-data';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Mengambil dan menyimpan data publikasi jurnal dari API Insentif Jurnal untuk pegawai yang memiliki jabatan fungsional (P1_JF) pada periode aktif';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
$this->info('Memulai proses pengambilan data publikasi dari API Insentif Jurnal...');
|
|
$this->newLine();
|
|
|
|
DB::beginTransaction();
|
|
|
|
// Inisialisasi service
|
|
$publikasiService = new InsentifService;
|
|
|
|
// Ambil periode aktif
|
|
$periodeModel = Periode::where('status', true)->first();
|
|
if (! $periodeModel) {
|
|
$this->error('Tidak ada periode aktif yang ditemukan!');
|
|
|
|
return 1;
|
|
}
|
|
|
|
$periode = $periodeModel->id_periode;
|
|
$this->info("Periode aktif: {$periodeModel->nama} (ID: {$periode})");
|
|
$this->newLine();
|
|
|
|
// Ambil data pegawai yang memiliki jabatan fungsional (P1_JF)
|
|
$this->info('Mengambil data pegawai dengan jabatan fungsional (P1_JF)...');
|
|
$pegawais = Pegawai::withoutGlobalScope('aktif')->with([
|
|
'jabatan' => function ($query) use ($periode) {
|
|
$query
|
|
->where('active', true)
|
|
->where('periode', $periode)
|
|
->with('detail', function ($subquery) {
|
|
$subquery->where('p1_jf', true);
|
|
});
|
|
},
|
|
])
|
|
->whereHas('jabatan', function ($query) use ($periode) {
|
|
$query
|
|
->where('active', true)
|
|
->where('periode', $periode)
|
|
->whereHas('detail', function ($subquery) {
|
|
$subquery->where('p1_jf', true);
|
|
});
|
|
})
|
|
->get();
|
|
|
|
$totalPegawai = $pegawais->count();
|
|
$this->info("Total pegawai dengan P1_JF: {$totalPegawai}");
|
|
$this->newLine();
|
|
|
|
if ($totalPegawai === 0) {
|
|
$this->warn('Tidak ada pegawai yang memenuhi kriteria.');
|
|
DB::commit();
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Proses setiap pegawai
|
|
$progressBar = $this->output->createProgressBar($totalPegawai);
|
|
$progressBar->start();
|
|
|
|
$successCount = 0;
|
|
$errorCount = 0;
|
|
$noDataCount = 0;
|
|
|
|
foreach ($pegawais as $pegawai) {
|
|
try {
|
|
// Ambil data jurnal dari API Insentif
|
|
$jurnals = $publikasiService->jurnal($pegawai->nip, $periode);
|
|
|
|
// Jika tidak ada data dengan NIP, coba dengan NIP SIAKAD
|
|
if (count($jurnals) == 0 && ! empty($pegawai->nip_siakad)) {
|
|
$jurnals = $publikasiService->jurnal($pegawai->nip_siakad, $periode);
|
|
}
|
|
|
|
// Jika tidak ada data jurnal, skip
|
|
if (count($jurnals) == 0) {
|
|
$noDataCount++;
|
|
$progressBar->advance();
|
|
|
|
continue;
|
|
}
|
|
|
|
// Ambil jabatan terbaru
|
|
$jabatan = $pegawai->jabatan->detail->sortByDesc('bulan')->first();
|
|
|
|
if ($jabatan->golongan === null) {
|
|
$this->newLine(2);
|
|
$this->warn("Pegawai {$pegawai->nip} ({$pegawai->nama}) tidak memiliki data golongan!");
|
|
$this->newLine();
|
|
$progressBar->display();
|
|
$errorCount++;
|
|
|
|
continue;
|
|
}
|
|
|
|
// Hitung pajak berdasarkan golongan
|
|
$pajak = 0;
|
|
if (str_contains($jabatan->golongan, 'III') || $jabatan->golongan === 'X') {
|
|
$pajak = 0.05;
|
|
} elseif (str_contains($jabatan->golongan, 'IV')) {
|
|
$pajak = 0.15;
|
|
}
|
|
|
|
// Hitung poin dan biaya
|
|
$poinTotal = $jurnals->sum('point');
|
|
$biayaTotal = $poinTotal * 660000;
|
|
$poinRemunerasi = $poinTotal > 42 ? 42 : $poinTotal;
|
|
$biayaRemunerasi = $poinRemunerasi * 660000;
|
|
|
|
// Simpan data publikasi
|
|
Publikasi::updateOrCreate(
|
|
[
|
|
'nip' => $pegawai->nip,
|
|
'periode' => $periode,
|
|
],
|
|
[
|
|
'jabatan_pegawai_detail_id' => $jabatan->id,
|
|
'jurnal' => $jurnals->sum('point'),
|
|
'prosiding' => 0,
|
|
'book_chapter' => 0,
|
|
'buku_lainnya' => 0,
|
|
'buku_referensi' => 0,
|
|
'monograf' => 0,
|
|
'hak_cipta' => 0,
|
|
'paten' => 0,
|
|
'poin_total' => $poinTotal,
|
|
'poin_remunerasi' => $poinRemunerasi,
|
|
'biaya_total' => round($biayaTotal),
|
|
'biaya_remunerasi' => round($biayaRemunerasi),
|
|
'pajak' => round($biayaRemunerasi * $pajak),
|
|
'netto' => round($biayaRemunerasi - $biayaRemunerasi * $pajak),
|
|
]
|
|
);
|
|
|
|
$successCount++;
|
|
} catch (Throwable $th) {
|
|
$errorCount++;
|
|
Log::error('Error saat proses publikasi', [
|
|
'nip' => $pegawai->nip ?? null,
|
|
'nama' => $pegawai->nama ?? null,
|
|
'error' => $th->getMessage(),
|
|
'trace' => $th->getTraceAsString(),
|
|
]);
|
|
}
|
|
|
|
$progressBar->advance();
|
|
}
|
|
|
|
$progressBar->finish();
|
|
$this->newLine(2);
|
|
|
|
DB::commit();
|
|
|
|
// Tampilkan ringkasan
|
|
$this->info('========== RINGKASAN ==========');
|
|
$this->info("Total pegawai diproses: {$totalPegawai}");
|
|
$this->info("Berhasil disimpan: {$successCount}");
|
|
$this->info("Tidak ada data jurnal: {$noDataCount}");
|
|
if ($errorCount > 0) {
|
|
$this->warn("Error: {$errorCount}");
|
|
}
|
|
$this->info('Proses selesai!');
|
|
|
|
return 0;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
$this->error('Terjadi error: '.$th->getMessage());
|
|
Log::error('Error saat get data publikasi', [
|
|
'error' => $th->getMessage(),
|
|
'trace' => $th->getTraceAsString(),
|
|
]);
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
}
|