first commit
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Lainnya\VerifikasiAbsen;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Verifikator\PengajuanPresensi;
|
||||
use App\Services\DawaiService;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
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
|
||||
{
|
||||
public function __construct(protected DawaiService $dawaiService) {}
|
||||
|
||||
public function showSetujui(int $id): View|RedirectResponse
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
||||
|
||||
if ($pengajuan->status !== 'Diajukan') {
|
||||
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
||||
}
|
||||
|
||||
$title = 'Setujui Pengajuan Absen';
|
||||
|
||||
return view('lainnya.verifikasi-absen.setujui', compact('title', 'pengajuan'));
|
||||
}
|
||||
|
||||
public function setujui(Request $request, int $id): RedirectResponse
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::findOrFail($id);
|
||||
|
||||
if ($pengajuan->status !== 'Diajukan') {
|
||||
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
||||
}
|
||||
|
||||
$success = $this->proses([$id], 'Diterima', null);
|
||||
|
||||
if ($success) {
|
||||
return redirect()->route('verifikasi-absen.berhasil-disetujui', $id);
|
||||
}
|
||||
|
||||
return back()->with('error', 'Gagal menyetujui pengajuan absen. Silakan coba lagi.');
|
||||
}
|
||||
|
||||
public function berhasilDisetujui(int $id): View
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
||||
$title = 'Berhasil Menyetujui Pengajuan Absen';
|
||||
|
||||
return view('lainnya.verifikasi-absen.berhasil-disetujui', compact('title', 'pengajuan'));
|
||||
}
|
||||
|
||||
public function showTolak(int $id): View|RedirectResponse
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
||||
|
||||
if ($pengajuan->status !== 'Diajukan') {
|
||||
return redirect()->route('verifikasi-absen.sudah-diproses', $id);
|
||||
}
|
||||
|
||||
$title = 'Tolak Pengajuan Absen';
|
||||
|
||||
return view('lainnya.verifikasi-absen.tolak', compact('title', 'pengajuan'));
|
||||
}
|
||||
|
||||
public function tolak(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'catatan' => 'required|string|max:500',
|
||||
]);
|
||||
|
||||
$pengajuan = PengajuanPresensi::findOrFail($id);
|
||||
|
||||
if ($pengajuan->status !== 'Diajukan') {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Pengajuan ini sudah diproses sebelumnya.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$success = $this->proses([$id], 'Ditolak', $request->catatan);
|
||||
|
||||
if ($success) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Pengajuan absen berhasil ditolak.',
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menolak pengajuan absen. Silakan coba lagi.',
|
||||
], 500);
|
||||
}
|
||||
|
||||
public function sudahDiproses(int $id): View
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::with('pegawai')->findOrFail($id);
|
||||
$title = 'Pengajuan Sudah Diproses';
|
||||
|
||||
return view('lainnya.verifikasi-absen.sudah-diproses', compact('title', 'pengajuan'));
|
||||
}
|
||||
|
||||
private function proses(array $ids, string $status, ?string $catatan): bool
|
||||
{
|
||||
try {
|
||||
$pengajuans = PengajuanPresensi::whereIn('id', $ids)->get();
|
||||
|
||||
foreach ($pengajuans as $pengajuan) {
|
||||
DB::beginTransaction();
|
||||
|
||||
$pengajuan->update(['status' => $status]);
|
||||
|
||||
$pengajuan->logs()->create([
|
||||
'user_id' => Auth::id(),
|
||||
'method' => 'POST',
|
||||
'employee_id' => Auth::user()->nip,
|
||||
'employee_name' => Auth::user()->name,
|
||||
'employee_role' => 'Verifikator',
|
||||
'message' => 'Pengajuan Presensi Berhasil ' . strtolower($status) . ' via WhatsApp',
|
||||
]);
|
||||
|
||||
if ($status === 'Ditolak') {
|
||||
$pengajuan->notes()->create([
|
||||
'type' => 'Catatan',
|
||||
'user_id' => Auth::id(),
|
||||
'employee_id' => Auth::user()->nip,
|
||||
'employee_name' => Auth::user()->name,
|
||||
'employee_role' => 'Verifikator',
|
||||
'message' => $catatan,
|
||||
]);
|
||||
}
|
||||
|
||||
$dawai = $this->dawaiService->verifikasiUsulanPresensi(
|
||||
$pengajuan->nip,
|
||||
[$pengajuan->tanggal_mulai],
|
||||
$this->mapStatus($status),
|
||||
$pengajuan->getRawOriginal('kategori')
|
||||
);
|
||||
|
||||
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' => 'nip', 'contents' => $pengajuan->nip],
|
||||
['name' => 'tanggal[]', 'contents' => $pengajuan->tanggal_mulai],
|
||||
['name' => 'kategori', 'contents' => (string) $pengajuan->getRawOriginal('kategori')],
|
||||
['name' => 'username', 'contents' => Auth::user()->nip],
|
||||
['name' => 'name', 'contents' => Auth::user()->name],
|
||||
]);
|
||||
} 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user