585 lines
20 KiB
PHP
585 lines
20 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 GuzzleHttp\Exception\ServerException;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
use Throwable;
|
|
|
|
class DawaiService
|
|
{
|
|
private $client;
|
|
|
|
private ?string $presensiError = null;
|
|
|
|
public function getPresensiError(): string
|
|
{
|
|
return $this->presensiError ?? 'Layanan DAWAI belum dapat memproses verifikasi. Silakan coba kembali.';
|
|
}
|
|
|
|
private function setPresensiError(Throwable $error): void
|
|
{
|
|
$message = null;
|
|
if ($error instanceof ClientException) {
|
|
$body = json_decode((string) $error->getResponse()->getBody(), true);
|
|
$message = $body['meta']['message'] ?? $body['message'] ?? null;
|
|
} elseif ($error instanceof Error) {
|
|
$message = $error->getMessage();
|
|
}
|
|
$this->presensiError = is_string($message) && trim($message) !== ''
|
|
? 'DAWAI : '.strip_tags($message)
|
|
: null;
|
|
}
|
|
|
|
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
|
|
{
|
|
$this->presensiError = null;
|
|
$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) {
|
|
$this->setPresensiError($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, $pengajuan)) {
|
|
return false;
|
|
}
|
|
|
|
Log::info("DAWAI siskaPengajuanPresensi berhasil: {$pengajuan->nip} ($status)");
|
|
|
|
return true;
|
|
}
|
|
|
|
public function kirimPengajuanPresensi(array $data, ?PengajuanPresensi $pengajuan = null): bool
|
|
{
|
|
$this->presensiError = null;
|
|
try {
|
|
$multipart = [];
|
|
foreach ($data as $name => $contents) {
|
|
$multipart[] = ['name' => $name, 'contents' => (string) ($contents ?? '')];
|
|
}
|
|
|
|
$response = $this->client->post('api/v1/siskaPengajuanPresensi', [
|
|
'multipart' => $multipart,
|
|
'timeout' => 90,
|
|
]);
|
|
|
|
$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');
|
|
}
|
|
|
|
$dawaiId = is_array($result['data'] ?? null) ? ($result['data']['id'] ?? null) : ($result['data'] ?? null);
|
|
if ($pengajuan && $dawaiId) {
|
|
$pengajuan->update(['dawai_id' => $dawaiId]);
|
|
} elseif ($pengajuan) {
|
|
Log::warning("DAWAI siskaPengajuanPresensi: respons tanpa id, dawai_id tidak disimpan (pengajuan #{$pengajuan->id}): ".json_encode($result));
|
|
}
|
|
|
|
return true;
|
|
} catch (ClientException $e) {
|
|
$body = (string) $e->getResponse()->getBody();
|
|
$decoded = json_decode($body, true);
|
|
$pesan = $decoded['meta']['message'] ?? '';
|
|
|
|
if ($e->getResponse()->getStatusCode() === 400 && str_contains(strtolower($pesan), 'sudah ada')) {
|
|
$dawaiId = $this->cariDawaiIdTersimpan($decoded, $data, $pengajuan);
|
|
|
|
if ($pengajuan && $dawaiId) {
|
|
$pengajuan->update(['dawai_id' => $dawaiId]);
|
|
Log::warning("DAWAI siskaPengajuanPresensi: data sudah ada di DAWAI ({$data['nip']} {$data['tanggal_mulai']}), dawai_id $dawaiId disimpan: $pesan");
|
|
} else {
|
|
Log::warning("DAWAI siskaPengajuanPresensi: data sudah ada di DAWAI ({$data['nip']} {$data['tanggal_mulai']}), dawai_id tidak ditemukan (pengajuan #".($pengajuan?->id ?? '-')."): $pesan | $body");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
$this->setPresensiError($e);
|
|
Log::warning('DAWAI siskaPengajuanPresensi: '.($pesan !== '' ? $pesan : $e->getMessage()));
|
|
|
|
return false;
|
|
} catch (Throwable $th) {
|
|
$this->setPresensiError($th);
|
|
Log::error('DAWAI siskaPengajuanPresensi: '.$th->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function cariDawaiIdTersimpan(?array $respons, array $data, ?PengajuanPresensi $pengajuan): int|string|null
|
|
{
|
|
foreach (['data.id', 'meta.data.id', 'data', 'id'] as $kunci) {
|
|
$nilai = data_get($respons, $kunci);
|
|
if (is_scalar($nilai) && is_numeric($nilai)) {
|
|
return $nilai;
|
|
}
|
|
}
|
|
|
|
if (! $pengajuan) {
|
|
return null;
|
|
}
|
|
|
|
return PengajuanPresensi::where('tipe', $pengajuan->tipe)
|
|
->where('nip', $pengajuan->nip)
|
|
->where('kategori', $pengajuan->getRawOriginal('kategori'))
|
|
->whereDate('tanggal_mulai', $data['tanggal_mulai'])
|
|
->whereNotNull('dawai_id')
|
|
->whereKeyNot($pengajuan->getKey())
|
|
->value('dawai_id');
|
|
}
|
|
|
|
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 (ServerException $th) {
|
|
$body = (string) $th->getResponse()->getBody();
|
|
$penyebab = preg_match('/\b\w*(?:Error|Exception):[^\r\n<]+/', html_entity_decode($body), $match)
|
|
? trim($match[0])
|
|
: Str::limit(trim(strip_tags($body)), 200);
|
|
|
|
Log::error('DAWAI : listStatusPresensi gagal - server DAWAI error (HTTP '.$th->getResponse()->getStatusCode().') bukan dari Verifikator, perbaiki di sisi DAWAI. Penyebab: '.$penyebab);
|
|
throw new Error($th->getMessage(), 500);
|
|
} catch (Throwable $th) {
|
|
Log::error('DAWAI : listStatusPresensi gagal - '.$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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array{berhasil: bool, message: string, data: mixed} message dan data berasal dari respons DAWAI
|
|
*/
|
|
public function deletePengajuanCutiDinas(int|string $id): array
|
|
{
|
|
try {
|
|
$response = $this->client->post('api/v2/siremun/cuti/delete', [
|
|
'json' => ['id' => (string) $id],
|
|
'timeout' => 15,
|
|
]);
|
|
|
|
$result = json_decode($response->getBody()->getContents(), true);
|
|
$message = $result['meta']['message'] ?? $result['message'] ?? 'Pengajuan presensi berhasil dihapus';
|
|
|
|
if (is_array($result) && (
|
|
(array_key_exists('status', $result) && ! $result['status'])
|
|
|| (isset($result['meta']['code']) && (int) $result['meta']['code'] !== 200)
|
|
)) {
|
|
Log::error("DAWAI deletePengajuanCutiDinas ($id): $message");
|
|
|
|
return ['berhasil' => false, 'message' => $message, 'data' => $result['data'] ?? null];
|
|
}
|
|
|
|
return ['berhasil' => true, 'message' => $message, 'data' => $result['data'] ?? null];
|
|
} catch (ClientException $e) {
|
|
$message = json_decode((string) $e->getResponse()->getBody(), true)['meta']['message'] ?? $e->getMessage();
|
|
Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$e->getMessage());
|
|
|
|
return ['berhasil' => false, 'message' => $message, 'data' => null];
|
|
} catch (Throwable $th) {
|
|
Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$th->getMessage());
|
|
|
|
return ['berhasil' => false, 'message' => $th->getMessage(), 'data' => null];
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|