362 lines
12 KiB
PHP
362 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Lainnya\VerifikasiAbsen;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\PengajuanPresensi;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use App\Services\DawaiService;
|
|
use App\Services\GoogleDriveService;
|
|
use App\Services\TteService;
|
|
use App\Services\VerifikasiAbsenLinkService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\View\View;
|
|
use Throwable;
|
|
|
|
class VerifikasiAbsenController extends Controller
|
|
{
|
|
const BATAS_HARI_VERIFIKASI = 5;
|
|
|
|
private DawaiService $dawaiService;
|
|
|
|
public function __construct(
|
|
protected TteService $tteService,
|
|
protected GoogleDriveService $googleDriveService,
|
|
protected VerifikasiAbsenLinkService $linkService,
|
|
) {
|
|
$this->dawaiService = new DawaiService('http://172.16.40.128/');
|
|
}
|
|
|
|
public function show(Request $request, int $id): View|RedirectResponse|Response
|
|
{
|
|
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
|
|
|
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if ($nipAtasan === false) {
|
|
return $this->tidakBerwenang();
|
|
}
|
|
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
|
}
|
|
|
|
if ($this->isExpired($pengajuan)) {
|
|
return redirect()->route('verifikasi-absen.kadaluarsa', $id);
|
|
}
|
|
|
|
$title = 'Verifikasi Pengajuan Absen';
|
|
$nip = $nipAtasan;
|
|
$expires = $request->input('expires');
|
|
$signature = $request->input('signature');
|
|
|
|
return view('lainnya.verifikasi-absen.show', compact('title', 'pengajuan', 'nip', 'expires', 'signature'));
|
|
}
|
|
|
|
public function setujui(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'nik' => 'required|string',
|
|
'passphrase' => 'required|string',
|
|
]);
|
|
|
|
$pengajuan = PengajuanPresensi::findOrFail($id);
|
|
|
|
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if ($nipAtasan === false) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Anda tidak berwenang memverifikasi pengajuan ini.',
|
|
], 403);
|
|
}
|
|
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah diproses sebelumnya.',
|
|
], 422);
|
|
}
|
|
|
|
if ($this->isExpired($pengajuan)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah kadaluarsa dan tidak dapat diverifikasi lagi.',
|
|
], 422);
|
|
}
|
|
|
|
$dokumenTertandatangani = $this->tteService->signDokumenUser(
|
|
$request->nik,
|
|
$request->passphrase,
|
|
$pengajuan->file,
|
|
route('verifikasi-absen.show', $id)
|
|
);
|
|
|
|
if ($dokumenTertandatangani === false) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menandatangani dokumen. Periksa kembali NIK dan Passphrase Anda.',
|
|
], 422);
|
|
}
|
|
|
|
$fileName = "pengajuan-presensi-{$pengajuan->nip}-{$pengajuan->id}.pdf";
|
|
|
|
if (! $this->googleDriveService->uploadPdf($dokumenTertandatangani, $fileName)) {
|
|
Log::error("Gagal mengunggah dokumen tertandatangani ke Google Drive untuk pengajuan #$id");
|
|
}
|
|
|
|
$success = $this->proses([$id], 'Diterima', null, $nipAtasan);
|
|
|
|
if ($success) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pengajuan absen berhasil disetujui.',
|
|
'redirect' => route('verifikasi-absen.berhasil-disetujui', $id),
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menyetujui pengajuan absen. Silakan coba lagi.',
|
|
], 500);
|
|
}
|
|
|
|
public function berhasilDisetujui(int $id): View|RedirectResponse
|
|
{
|
|
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
|
|
|
if ($pengajuan->status === 'Diajukan') {
|
|
return redirect()->route('verifikasi-absen.show', $id);
|
|
}
|
|
|
|
if ($pengajuan->status !== 'Diterima') {
|
|
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
|
}
|
|
|
|
$title = 'Berhasil Menyetujui Pengajuan Absen';
|
|
|
|
return view('lainnya.verifikasi-absen.berhasil-disetujui', compact('title', 'pengajuan'));
|
|
}
|
|
|
|
public function tolak(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'catatan' => 'required|string|max:500',
|
|
]);
|
|
|
|
$pengajuan = PengajuanPresensi::findOrFail($id);
|
|
|
|
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if ($nipAtasan === false) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Anda tidak berwenang memverifikasi pengajuan ini.',
|
|
], 403);
|
|
}
|
|
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah diproses sebelumnya.',
|
|
], 422);
|
|
}
|
|
|
|
if ($this->isExpired($pengajuan)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah kadaluarsa dan tidak dapat diverifikasi lagi.',
|
|
], 422);
|
|
}
|
|
|
|
$success = $this->proses([$id], 'Ditolak', $request->catatan, $nipAtasan);
|
|
|
|
if ($success) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pengajuan absen berhasil ditolak.',
|
|
'redirect' => route('verifikasi-absen.berhasil-ditolak', $id),
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menolak pengajuan absen. Silakan coba lagi.',
|
|
], 500);
|
|
}
|
|
|
|
public function berhasilDitolak(int $id): View|RedirectResponse
|
|
{
|
|
$pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id);
|
|
|
|
if ($pengajuan->status === 'Diajukan') {
|
|
return redirect()->route('verifikasi-absen.show', $id);
|
|
}
|
|
|
|
if ($pengajuan->status !== 'Ditolak') {
|
|
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
|
}
|
|
|
|
$title = 'Berhasil Menolak Pengajuan Absen';
|
|
|
|
return view('lainnya.verifikasi-absen.berhasil-ditolak', compact('title', 'pengajuan'));
|
|
}
|
|
|
|
public function sudahDiproses(int $id): View|RedirectResponse
|
|
{
|
|
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
|
|
|
if ($pengajuan->status === 'Diajukan') {
|
|
return redirect()->route(
|
|
$this->isExpired($pengajuan) ? 'verifikasi-absen.kadaluarsa' : 'verifikasi-absen.show',
|
|
$id
|
|
);
|
|
}
|
|
|
|
$title = 'Pengajuan Sudah Diproses';
|
|
|
|
return view('lainnya.verifikasi-absen.sudah-diproses', compact('title', 'pengajuan'));
|
|
}
|
|
|
|
public function kadaluarsa(int $id): View|RedirectResponse
|
|
{
|
|
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
|
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
|
}
|
|
|
|
if (! $this->isExpired($pengajuan)) {
|
|
return redirect()->route('verifikasi-absen.show', $id);
|
|
}
|
|
|
|
$title = 'Pengajuan Kadaluarsa';
|
|
|
|
return view('lainnya.verifikasi-absen.kadaluarsa', compact('title', 'pengajuan'));
|
|
}
|
|
|
|
private function isExpired(PengajuanPresensi $pengajuan): bool
|
|
{
|
|
return $pengajuan->created_at?->copy()->addDays(self::BATAS_HARI_VERIFIKASI)->isPast() ?? false;
|
|
}
|
|
|
|
/**
|
|
* Validasi bahwa request membawa nip+expires+signature yang valid (dari tautan WhatsApp)
|
|
* DAN nip tersebut benar-benar atasan dari pegawai pemilik pengajuan.
|
|
* Return NIP atasan yang tervalidasi, atau false kalau tidak berwenang.
|
|
*/
|
|
private function otorisasiAtasan(Request $request, PengajuanPresensi $pengajuan): string|false
|
|
{
|
|
$nip = $request->input('nip');
|
|
$expires = $request->input('expires');
|
|
$signature = $request->input('signature');
|
|
|
|
if (! $this->linkService->verify($pengajuan->id, $nip, $expires, $signature)) {
|
|
return false;
|
|
}
|
|
|
|
if (! $this->dawaiService->isAtasanDari($nip, $pengajuan->nip)) {
|
|
return false;
|
|
}
|
|
|
|
return $nip;
|
|
}
|
|
|
|
private function tidakBerwenang(): Response
|
|
{
|
|
$title = 'Tidak Berwenang';
|
|
|
|
return response()->view('lainnya.verifikasi-absen.tidak-berwenang', compact('title'), 403);
|
|
}
|
|
|
|
private function proses(array $ids, string $status, ?string $catatan, ?string $nipAtasan = null): bool
|
|
{
|
|
// Diproses tanpa sesi login (diakses langsung dari tautan WhatsApp),
|
|
// sehingga identitas verifikator diambil dari NIP atasan yang tervalidasi (bukan Auth::user()).
|
|
$atasan = $nipAtasan ? Pegawai::withoutGlobalScopes()->where('nip', $nipAtasan)->first() : null;
|
|
$verifikatorRole = 'Atasan (via WhatsApp)';
|
|
|
|
try {
|
|
$pengajuans = PengajuanPresensi::whereIn('id', $ids)->get();
|
|
|
|
foreach ($pengajuans as $pengajuan) {
|
|
DB::beginTransaction();
|
|
|
|
$pengajuan->update(['status' => $status]);
|
|
|
|
$pengajuan->logs()->create([
|
|
'user_id' => null,
|
|
'method' => 'POST',
|
|
'employee_id' => $nipAtasan ?? $pengajuan->nip,
|
|
'employee_name' => $atasan?->nama ?? $pengajuan->pegawai?->nama,
|
|
'employee_role' => $verifikatorRole,
|
|
'message' => 'Pengajuan Presensi Berhasil ' . strtolower($status) . ' via WhatsApp',
|
|
]);
|
|
|
|
if ($status === 'Ditolak') {
|
|
$pengajuan->notes()->create([
|
|
'type' => 'Catatan',
|
|
'user_id' => null,
|
|
'employee_id' => $nipAtasan ?? $pengajuan->nip,
|
|
'employee_name' => $atasan?->nama ?? $pengajuan->pegawai?->nama,
|
|
'employee_role' => $verifikatorRole,
|
|
'message' => $catatan,
|
|
]);
|
|
}
|
|
|
|
// $dawai = $this->dawaiService->verifikasiUsulanPresensi(
|
|
// $pengajuan->nip,
|
|
// [$pengajuan->tanggal_mulai],
|
|
// $this->mapStatus($status),
|
|
// $pengajuan->getRawOriginal('kategori'),
|
|
// 'whatsapp',
|
|
// $verifikatorRole
|
|
// );
|
|
|
|
// if (! $dawai) {
|
|
// Log::error("Presensi DAWAI via WhatsApp: $pengajuan->nip ($status)");
|
|
// throw new Exception('Presensi: DAWAI ERROR');
|
|
// }
|
|
|
|
DB::commit();
|
|
|
|
// try {
|
|
// Http::timeout(5)->asMultipart()->post('http://172.16.50.94:8084/webhook/presensi', [
|
|
// ['name' => 'jenis', 'contents' => 'izin],
|
|
// ['name' => 'nip', 'contents' => $pengajuan->nip],
|
|
// ['name' => 'tanggal[]', 'contents' => $pengajuan->tanggal_mulai],
|
|
// ['name' => 'kategori', 'contents' => (string) $pengajuan->getRawOriginal('kategori')],
|
|
// ['name' => 'username', 'contents' => 'whatsapp'],
|
|
// ['name' => 'name', 'contents' => $verifikatorRole],
|
|
// ]);
|
|
// } catch (Throwable $webhookException) {
|
|
// Log::error("Webhook Presensi via WhatsApp error: {$pengajuan->nip} ($status) - " . $webhookException->getMessage());
|
|
// }
|
|
}
|
|
|
|
return true;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
Log::error("Verifikasi Absen via WhatsApp ($status): " . $th->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function mapStatus(string $status): ?bool
|
|
{
|
|
return match ($status) {
|
|
'Diterima' => true,
|
|
'Ditolak' => false,
|
|
default => null,
|
|
};
|
|
}
|
|
}
|