804618bb54
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
3.3 KiB
PHP
87 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ActivityLog;
|
|
use App\Models\PermohonanTransfer;
|
|
use App\Models\RumahIbadah;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TransferController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = auth()->user();
|
|
$query = PermohonanTransfer::with([
|
|
'dariIbadah:id,nama', 'keIbadah:id,nama',
|
|
'rumahMiskin:id_rumah,nama_kk', 'pengaju:id,name',
|
|
])->orderByDesc('created_at');
|
|
|
|
if ($user->hasRole('pengurus_ibadah')) {
|
|
$ibadahIds = RumahIbadah::where('user_id', $user->id)->pluck('id');
|
|
$query->where(function ($q) use ($ibadahIds) {
|
|
$q->whereIn('dari_ibadah_id', $ibadahIds)
|
|
->orWhereIn('ke_ibadah_id', $ibadahIds);
|
|
});
|
|
}
|
|
|
|
return response()->json($query->limit(100)->get()->map(fn($p) => [
|
|
'id' => $p->id,
|
|
'dari_ibadah' => $p->dariIbadah?->nama,
|
|
'ke_ibadah' => $p->keIbadah?->nama,
|
|
'ke_ibadah_id' => $p->ke_ibadah_id,
|
|
'id_rumah' => $p->id_rumah,
|
|
'nama_kk' => $p->rumahMiskin?->nama_kk,
|
|
'alasan' => $p->alasan,
|
|
'status' => $p->status,
|
|
'catatan_respons' => $p->catatan_respons,
|
|
'diajukan_oleh' => $p->pengaju?->name,
|
|
'created_at' => $p->created_at?->format('d/m/Y H:i'),
|
|
]));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'dari_ibadah_id' => 'required|integer|exists:rumah_ibadah,id',
|
|
'ke_ibadah_id' => 'required|integer|exists:rumah_ibadah,id|different:dari_ibadah_id',
|
|
'id_rumah' => 'required|string|exists:rumah_miskin,id_rumah',
|
|
'alasan' => 'required|string|max:1000',
|
|
]);
|
|
$permohonan = PermohonanTransfer::create([
|
|
...$validated,
|
|
'diajukan_oleh' => auth()->id(),
|
|
'status' => 'pending',
|
|
]);
|
|
ActivityLog::record('create', "Ajukan transfer: {$permohonan->id_rumah} → ibadah #{$permohonan->ke_ibadah_id}", 'transfer', $permohonan->id);
|
|
return response()->json(['success' => true, 'data' => $permohonan]);
|
|
}
|
|
|
|
public function respond(Request $request, int $id)
|
|
{
|
|
$permohonan = PermohonanTransfer::findOrFail($id);
|
|
if ($permohonan->status !== 'pending') {
|
|
return response()->json(['message' => 'Permohonan sudah diproses'], 422);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'status' => 'required|in:disetujui,ditolak',
|
|
'catatan_respons' => 'nullable|string|max:500',
|
|
]);
|
|
|
|
$user = auth()->user();
|
|
if (!$user->hasRole('administrator')) {
|
|
$ibadahIds = RumahIbadah::where('user_id', $user->id)->pluck('id');
|
|
if (!$ibadahIds->contains($permohonan->ke_ibadah_id)) {
|
|
return response()->json(['message' => 'Tidak berhak memproses'], 403);
|
|
}
|
|
}
|
|
|
|
$permohonan->update([...$validated, 'diproses_oleh' => $user->id]);
|
|
|
|
$label = $validated['status'] === 'disetujui' ? 'disetujui' : 'ditolak';
|
|
ActivityLog::record('update', "Transfer {$label}: permohonan #{$id}", 'transfer', $id);
|
|
return response()->json(['success' => true]);
|
|
}
|
|
}
|