126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Supply;
|
|
use App\Models\Category;
|
|
use App\Traits\LogsActivity;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class SupplyController extends Controller
|
|
{
|
|
use LogsActivity;
|
|
|
|
/**
|
|
* Tampilkan daftar perlengkapan.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$query = Supply::with('category');
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where('nama_perlengkapan', 'like', '%' . $request->search . '%');
|
|
}
|
|
|
|
$supplies = $query->latest()->paginate(10);
|
|
return view('pages.inventory.supplies.index', compact('supplies'));
|
|
}
|
|
|
|
/**
|
|
* Tampilkan form tambah.
|
|
*/
|
|
public function create(): View
|
|
{
|
|
// Category ditentukan otomatis oleh Model Supply
|
|
return view('pages.inventory.supplies.create');
|
|
}
|
|
|
|
/**
|
|
* Simpan data perlengkapan baru.
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'nama_perlengkapan' => 'required|string|max:255',
|
|
'qty' => 'required|numeric|min:0',
|
|
'unit' => 'required|string|max:50',
|
|
'category_id' => 'nullable|exists:categories,id',
|
|
'foto_produk' => 'nullable|image|max:1024',
|
|
]);
|
|
|
|
if ($request->hasFile('foto_produk')) {
|
|
$validated['foto_produk'] = $request->file('foto_produk')->store('supplies', 'public');
|
|
}
|
|
|
|
$supply = Supply::create($validated);
|
|
|
|
$this->logAction('CREATED_SUPPLY', $supply, "Menambah perlengkapan: {$supply->nama_perlengkapan}");
|
|
|
|
// FIX: Menggunakan .index agar tidak RouteNotFound
|
|
return redirect()->route('inventory.supplies.index')->with('success', 'Perlengkapan berhasil ditambahkan.');
|
|
}
|
|
|
|
/**
|
|
* Tampilkan form edit.
|
|
*/
|
|
public function edit(Supply $supply): View
|
|
{
|
|
$categories = Category::all();
|
|
return view('pages.inventory.supplies.edit', compact('supply', 'categories'));
|
|
}
|
|
|
|
/**
|
|
* Update data perlengkapan.
|
|
*/
|
|
public function update(Request $request, Supply $supply): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'nama_perlengkapan' => 'required|string|max:255',
|
|
'qty' => 'required|numeric|min:0',
|
|
'unit' => 'required|string|max:50',
|
|
'category_id' => 'nullable|exists:categories,id',
|
|
'foto_produk' => 'nullable|image|max:1024',
|
|
]);
|
|
|
|
if ($request->hasFile('foto_produk')) {
|
|
if ($supply->foto_produk) {
|
|
Storage::disk('public')->delete($supply->foto_produk);
|
|
}
|
|
$validated['foto_produk'] = $request->file('foto_produk')->store('supplies', 'public');
|
|
}
|
|
|
|
$supply->update($validated);
|
|
|
|
$this->logAction('UPDATED_SUPPLY', $supply, "Memperbarui data perlengkapan: {$supply->nama_perlengkapan}");
|
|
|
|
// FIX: Menggunakan .index
|
|
return redirect()->route('inventory.supplies.index')->with('success', 'Data perlengkapan berhasil diperbarui.');
|
|
}
|
|
|
|
/**
|
|
* Hapus data perlengkapan.
|
|
*/
|
|
public function destroy(Supply $supply): RedirectResponse
|
|
{
|
|
if (auth()->user()->role !== 'admin') {
|
|
abort(403, 'Akses Ditolak.');
|
|
}
|
|
|
|
$supplyName = $supply->nama_perlengkapan;
|
|
|
|
if ($supply->foto_produk) {
|
|
Storage::disk('public')->delete($supply->foto_produk);
|
|
}
|
|
|
|
// PENTING: Log dilakukan SEBELUM delete agar ID tidak null
|
|
$this->logAction('DELETED_SUPPLY', $supply, "Menghapus perlengkapan: {$supplyName}");
|
|
|
|
$supply->delete();
|
|
|
|
// FIX: Menggunakan .index
|
|
return redirect()->route('inventory.supplies.index')->with('success', 'Data perlengkapan berhasil dihapus.');
|
|
}
|
|
} |