94 lines
3.6 KiB
PHP
94 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pemohon;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Permohonan;
|
|
use App\Models\Vot;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PermohonanController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$permohonan = Permohonan::with(['vot', 'jabatan'])->where('user_id', auth()->id())->latest()->paginate(10);
|
|
return view('permohonan.index', compact('permohonan'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('permohonan.form', ['permohonan' => new Permohonan(), 'vot' => Vot::with('jabatan')->where('status', true)->get()]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $this->validated($request);
|
|
|
|
DB::transaction(function () use ($request, $validated) {
|
|
$vot = Vot::findOrFail($validated['vot_id']);
|
|
$permohonan = Permohonan::create([
|
|
'no_rujukan' => 'MBIP/BJT/'.now()->format('YmdHis').'/'.auth()->id(),
|
|
'user_id' => auth()->id(),
|
|
'jabatan_id' => auth()->user()->jabatan_id ?: $vot->jabatan_id,
|
|
'vot_id' => $vot->id,
|
|
'kategori' => $validated['kategori'],
|
|
'tujuan' => $validated['tujuan'],
|
|
'status' => $request->input('action') === 'submit' ? 'Submitted' : 'Draft',
|
|
'submitted_at' => $request->input('action') === 'submit' ? now() : null,
|
|
'gambar_1' => $this->storeImage($request, 'gambar_1'),
|
|
'gambar_2' => $this->storeImage($request, 'gambar_2'),
|
|
]);
|
|
|
|
$total = 0;
|
|
foreach ($validated['items'] as $row) {
|
|
$jumlah = $row['kuantiti'] * $row['harga_anggaran'];
|
|
$total += $jumlah;
|
|
$permohonan->items()->create($row + ['jumlah' => $jumlah]);
|
|
}
|
|
$permohonan->update(['jumlah_keseluruhan' => $total]);
|
|
});
|
|
|
|
return redirect()->route('permohonan.index')->with('success', 'Permohonan berjaya disimpan.');
|
|
}
|
|
|
|
public function show(Permohonan $permohonan)
|
|
{
|
|
$this->authorizeOwner($permohonan);
|
|
return view('permohonan.show', ['permohonan' => $permohonan->load(['items', 'vot', 'jabatan', 'user'])]);
|
|
}
|
|
|
|
public function submit(Permohonan $permohonan)
|
|
{
|
|
$this->authorizeOwner($permohonan);
|
|
$permohonan->update(['status' => 'Submitted', 'submitted_at' => now()]);
|
|
return back()->with('success', 'Permohonan dihantar kepada Pelaksana.');
|
|
}
|
|
|
|
private function validated(Request $request): array
|
|
{
|
|
return $request->validate([
|
|
'vot_id' => ['required', 'exists:vot,id'],
|
|
'kategori' => ['required', 'max:255'],
|
|
'tujuan' => ['required'],
|
|
'gambar_1' => ['nullable', 'image', 'mimes:jpg,jpeg,png', 'max:5120'],
|
|
'gambar_2' => ['nullable', 'image', 'mimes:jpg,jpeg,png', 'max:5120'],
|
|
'items' => ['required', 'array', 'min:1'],
|
|
'items.*.item' => ['required', 'max:255'],
|
|
'items.*.kuantiti' => ['required', 'integer', 'min:1'],
|
|
'items.*.harga_anggaran' => ['required', 'numeric', 'min:0'],
|
|
]);
|
|
}
|
|
|
|
private function storeImage(Request $request, string $field): ?string
|
|
{
|
|
return $request->hasFile($field) ? $request->file($field)->store('permohonan', 'public') : null;
|
|
}
|
|
|
|
private function authorizeOwner(Permohonan $permohonan): void
|
|
{
|
|
abort_unless($permohonan->user_id === auth()->id() || auth()->user()->hasAnyRole(['Admin', 'Pelaksana']), 403);
|
|
}
|
|
}
|