Files
verifikator/app/Services/DawaiService.php
T
2026-09-30 22:33:05 +07:00

475 lines
15 KiB
PHP

<?php
namespace App\Services;
use App\Models\Verifikator\Dosen;
use App\Models\Verifikator\PengajuanPresensi;
use App\Models\Verifikator\Tendik;
use Carbon\Carbon;
use Error;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Throwable;
class DawaiService
{
private $client;
public function __construct($default = 'http://172.16.40.128/')
{
$this->client = new Client([
'base_uri' => $default,
'timeout' => 0,
]);
}
public function getJabatan(string $nip, int $periode): array|bool
{
try {
$response = $this->client->get("api/v1/jabatan/$nip/$periode");
} catch (Throwable $th) {
try {
Log::error('DAWAI : '.$th->getMessage());
Log::error("DAWAI : $nip ($periode)");
} catch (Throwable) {
// Log failure should not crash the process
}
return false;
}
$data = json_decode($response->getBody()->getContents(), true);
return $data;
}
public function getListBawahan(string $nip): array|bool
{
try {
$response = $this->client->get("api/v1/listBawahanAtasanv2/$nip");
} catch (Throwable $th) {
Log::error('DAWAI Bawahan: '.$th->getMessage());
Log::error("DAWAI Bawahan: $nip");
return [];
}
$data = json_decode($response->getBody()->getContents(), true);
return $data;
}
public function isAtasanDari(string $nipAtasan, string $nipBawahan): bool
{
$bawahan = $this->getListBawahan($nipAtasan)['data']['bawahan'] ?? [];
return in_array($nipBawahan, array_column($bawahan, 'nip'), true);
}
public function isVerifikatorTriDharma(string $nip)
{
try {
if (count($this->getListBawahan($nip)['data']['bawahan']) === 0) {
return false;
}
return true;
} catch (Throwable $th) {
return false;
}
}
public function isVerifikatorTendik(string $nip): bool
{
try {
$bawahan = $this->getListBawahan($nip)['data']['bawahan'];
foreach ($bawahan as $bawah) {
if ($bawah['jenis_pegawai'] === 'tendik') {
return true;
}
}
return false;
} catch (Throwable $th) {
return false;
}
}
public function listPegawai(string $nip = '', string $jenisPegawai = '', string $email = ''): array
{
try {
$response = $this->client->get("api/v1/pegawai?jenis_pegawai=$jenisPegawai&nip=$nip&email=$email&asn=true&pensiun=0&wafat=0&mutkel=0&nonaktif=0&privasi=true");
$data = json_decode($response->getBody()->getContents(), true);
return $data;
} catch (Throwable $th) {
Log::error('SIDIA: '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function updateBawahan(string $atasan, array $bawahan): array
{
$listBawahan = [];
foreach ($bawahan as $nip) {
$listBawahan[] = [
'name' => 'site_nip_bawahan[]',
'contents' => $nip,
];
}
try {
Log::info('Update Bawahan Request', [
'atasan' => $atasan,
'jumlah_bawahan' => count($bawahan),
'bawahan' => $bawahan,
]);
$response = $this->client->post('api/v1/updateBawahan', [
'multipart' => [
[
'name' => 'site_nip_atasan',
'contents' => $atasan,
],
...$listBawahan,
],
]);
$data = json_decode($response->getBody()->getContents(), true);
Log::info('Update Bawahan Response', ['response' => $data]);
if ($data['status']) {
return ['success' => true, 'message' => $data['message'] ?? 'Berhasil mengubah daftar bawahan'];
}
return ['success' => false, 'message' => $data['message'] ?? 'Gagal mengubah daftar bawahan'];
} catch (Throwable $th) {
Log::error('Update Bawahan Error: '.$th->getMessage());
return ['success' => false, 'message' => $th->getMessage()];
}
}
public function verifikasiUsulanPresensi(string $nip, array $tanggals, ?bool $status, null|string|int $kategori, ?string $username = null, ?string $name = null): bool
{
$tanggal = [];
try {
foreach ($tanggals as $t) {
$tanggal[] = [
'name' => 'tanggal[]',
'contents' => $t,
];
}
$options = [
'multipart' => [
[
'name' => 'nip',
'contents' => $nip,
],
...$tanggal,
[
'name' => 'status_usulan',
'contents' => $status,
],
[
'name' => 'kategori',
'contents' => $kategori,
],
[
'name' => 'username',
'contents' => Auth::user()?->username ?? $username,
],
[
'name' => 'name',
'contents' => Auth::user()?->name ?? $name,
],
],
];
$response = $this->client->post('api/v1/verifikasiUsulanPresensi', $options);
$data = json_decode($response->getBody()->getContents(), true);
if ($data['status']) {
return true;
}
throw new Error($data['message']);
} catch (Throwable $th) {
Log::error('Presensi DAWAI : '.$th->getMessage());
return false;
}
return false;
}
public function kirimVerifikasiPresensi(PengajuanPresensi $pengajuan, string $status, ?string $catatan, string $nipVerifikator, ?string $linkTte = null): bool
{
$noHp = preg_replace('/\D+/', '', (string) (Dosen::where('nip', $nipVerifikator)->value('no_hp') ?: Tendik::where('nip', $nipVerifikator)->value('no_hp')));
$noHp = str_starts_with($noHp, '0') ? '62'.substr($noHp, 1) : $noHp;
$data = [
'nip' => $pengajuan->nip,
'tanggal_mulai' => Carbon::parse($pengajuan->tanggal_mulai)->format('Y-m-d'),
'tanggal_selesai' => Carbon::parse($pengajuan->tanggal_selesai ?? $pengajuan->tanggal_mulai)->format('Y-m-d'),
'keterangan' => $pengajuan->keterangan,
'status_pengajuan_id' => $pengajuan->getRawOriginal('kategori'),
'link_file' => ($status === 'Diterima' ? $linkTte : null) ?: $pengajuan->file,
'status_verifikasi' => $status,
'lokasi' => 'Indonesia',
'no_np' => $noHp,
];
if ($status === 'Ditolak') {
$data['alasan_tolak'] = $catatan;
}
if (! $this->kirimPengajuanPresensi($data)) {
return false;
}
Log::info("DAWAI siskaPengajuanPresensi berhasil: {$pengajuan->nip} ($status)");
return true;
}
public function kirimPengajuanPresensi(array $data): bool
{
try {
$multipart = [];
foreach ($data as $name => $contents) {
$multipart[] = ['name' => $name, 'contents' => (string) ($contents ?? '')];
}
$response = $this->client->post('api/v1/siskaPengajuanPresensi', [
'multipart' => $multipart,
'timeout' => 15,
]);
$result = json_decode($response->getBody()->getContents(), true);
if (is_array($result) && array_key_exists('status', $result) && ! $result['status']) {
throw new Error($result['message'] ?? 'DAWAI menolak pengajuan presensi');
}
return true;
} catch (ClientException $e) {
$pesan = json_decode((string) $e->getResponse()->getBody(), true)['meta']['message'] ?? '';
if ($e->getResponse()->getStatusCode() === 400 && str_contains(strtolower($pesan), 'sudah ada')) {
Log::warning("DAWAI siskaPengajuanPresensi: data sudah ada di DAWAI ({$data['nip']} {$data['tanggal_mulai']}): $pesan");
return true;
}
Log::error('DAWAI siskaPengajuanPresensi: '.$e->getMessage());
return false;
} catch (Throwable $th) {
Log::error('DAWAI siskaPengajuanPresensi: '.$th->getMessage());
return false;
}
}
public function listJabatanFungsional(?int $id)
{
try {
$response = $this->client->get("api/v1/list-jafung?id=$id");
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function listJabatanTambahan(?int $id)
{
try {
$response = $this->client->get("api/v1/list-dtt?id=$id");
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function listStatusPresensi(?string $kategori = null)
{
try {
$response = $this->client->get("api/v1/listStatusPresensi?kategori=$kategori");
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getDetailPresensi(string $nip, int $tahun, int $bulan, ?int $tanggal)
{
try {
$response = $this->client->get("api/v1/getDetailPresensi/$tahun/$bulan/$nip?hari=$tanggal");
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function cutiExists(string $nip, string $tanggal): bool
{
try {
$response = $this->client->post('api/v1/cekPengajuanCuti', [
'form_params' => [
'nip' => $nip,
'tanggal' => $tanggal,
],
]);
$data = json_decode($response->getBody()->getContents(), true);
return $data['status'];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
return true;
}
}
public function listRekening()
{
try {
$response = $this->client->get('api/v1/rekening');
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getRekapP1(int $tahun, int $bulan, int $unit): array
{
try {
$response = $this->client->get("api/v1/psatu/$tahun/$bulan/?id_unit=$unit");
return json_decode($response->getBody()->getContents(), true) ?? [];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getDataP1(?string $jenisPegawai, int $tahun, int $bulan, ?int $unit)
{
$response = $this->getDataP1Response($jenisPegawai, $tahun, $bulan, $unit);
return $response['data'] ?? [];
}
public function getDataP1Response(?string $jenisPegawai, int $tahun, int $bulan, ?int $unit): array
{
try {
$response = $this->client->get("api/v1/psatu/$tahun/$bulan/?id_unit=$unit&jenis_pegawai=$jenisPegawai");
$data = json_decode($response->getBody()->getContents(), true);
return is_array($data) ? $data : [];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getDataP1Selisih(?string $jenisPegawai, int $tahun, int $bulan, ?int $unit)
{
$response = $this->getDataP1SelisihResponse($jenisPegawai, $tahun, $bulan, $unit);
return $response['data'] ?? [];
}
public function getDataP1SelisihResponse(?string $jenisPegawai, int $tahun, int $bulan, ?int $unit): array
{
try {
$response = $this->client->get("api/v1/psatu/$tahun/$bulan/?id_unit=$unit&jenis_pegawai=$jenisPegawai&selisih=true");
$data = json_decode($response->getBody()->getContents(), true);
return is_array($data) ? $data : [];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getRekapThr(int $tahun, int $unit): array
{
try {
$response = $this->client->get("api/v1/thrApi/$tahun/?id_unit=$unit");
return json_decode($response->getBody()->getContents(), true) ?? [];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getDataThr(?string $jenisPegawai, int $tahun, ?int $unit, bool $selisih = false)
{
try {
$url = "api/v1/thrApi/$tahun/?id_unit=$unit&jenis_pegawai=$jenisPegawai";
if ($selisih) {
$url .= '&selisih=true';
}
$response = $this->client->get($url);
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getRekapR13(int $tahun, int $unit): array
{
try {
$response = $this->client->get("api/v1/r13Api/$tahun/6?id_unit=$unit");
return json_decode($response->getBody()->getContents(), true) ?? [];
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
public function getDataR13(?string $jenisPegawai, int $tahun, ?int $unit, bool $selisih = false)
{
try {
$url = "api/v1/r13Api/$tahun/6?id_unit=$unit&jenis_pegawai=$jenisPegawai";
if ($selisih) {
$url .= '&selisih=true';
}
$response = $this->client->get($url);
$data = json_decode($response->getBody()->getContents(), true)['data'];
return $data;
} catch (Throwable $th) {
Log::error('DAWAI : '.$th->getMessage());
throw new Error($th->getMessage(), 500);
}
}
}