173 lines
5.1 KiB
PHP
173 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Barang;
|
|
use App\Models\Pesanan;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class BarangController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth')->except(['index', 'show']);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$query = Barang::latest();
|
|
|
|
if ($request->has('kategori')) {
|
|
$query->where('kategori', $request->kategori);
|
|
}
|
|
|
|
if ($request->has('search')) {
|
|
$searchTerm = $request->search;
|
|
$query->where(function($q) use ($searchTerm) {
|
|
$q->where('nama_barang', 'LIKE', "%{$searchTerm}%")
|
|
->orWhere('deskripsi', 'LIKE', "%{$searchTerm}%")
|
|
->orWhere('kategori', 'LIKE', "%{$searchTerm}%");
|
|
});
|
|
}
|
|
|
|
$barang = $query->get();
|
|
return view('barang.index', compact('barang'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('barang.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'barang_id' => 'required|exists:barang,id',
|
|
'jumlah' => 'required|integer|min:1',
|
|
'catatan' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
$barang = Barang::findOrFail($request->barang_id);
|
|
|
|
// Cek stok
|
|
if ($barang->stok < $request->jumlah) {
|
|
return back()->with('error', 'Stok tidak mencukupi');
|
|
}
|
|
|
|
// Buat pesanan
|
|
$pesanan = Pesanan::create([
|
|
'user_id' => auth()->id(),
|
|
'barang_id' => $request->barang_id,
|
|
'jumlah' => $request->jumlah,
|
|
'total_harga' => $barang->harga * $request->jumlah,
|
|
'status' => 'pending',
|
|
'catatan' => $request->catatan,
|
|
]);
|
|
|
|
// Kurangi stok
|
|
$barang->update([
|
|
'stok' => $barang->stok - $request->jumlah
|
|
]);
|
|
|
|
return redirect()->route('pesanan.show', $pesanan)
|
|
->with('success', 'Pesanan berhasil dibuat');
|
|
}
|
|
|
|
public function show(Barang $barang)
|
|
{
|
|
return view('barang.show', compact('barang'));
|
|
}
|
|
|
|
public function edit(Barang $barang)
|
|
{
|
|
return view('barang.edit', compact('barang'));
|
|
}
|
|
|
|
public function update(Request $request, Barang $barang)
|
|
{
|
|
$request->validate([
|
|
'nama_barang' => 'required|string|max:255',
|
|
'deskripsi' => 'nullable|string',
|
|
'harga' => 'required|numeric|min:0',
|
|
'stok' => 'required|integer|min:0',
|
|
'kategori' => 'required|string|in:Lampu,Kipas,Kulkas,TV,Magic Com,Mesin Cuci,Kompor',
|
|
'gambar' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
|
|
]);
|
|
|
|
$data = $request->all();
|
|
|
|
if ($request->hasFile('gambar')) {
|
|
// Delete old image
|
|
if ($barang->gambar) {
|
|
Storage::disk('public')->delete($barang->gambar);
|
|
}
|
|
|
|
$gambar = $request->file('gambar');
|
|
// Simpan dengan nama asli
|
|
$fileName = $gambar->getClientOriginalName();
|
|
// Pastikan nama file tidak ada spasi dan karakter khusus
|
|
$fileName = preg_replace('/[^A-Za-z0-9\-\_\.]/', '', $fileName);
|
|
$path = $gambar->storeAs('products', $fileName, 'public');
|
|
$data['gambar'] = $path;
|
|
}
|
|
|
|
$barang->update($data);
|
|
return redirect()->route('barang.index')->with('success', 'Barang berhasil diperbarui');
|
|
}
|
|
|
|
public function destroy(Barang $barang)
|
|
{
|
|
if ($barang->gambar) {
|
|
Storage::disk('public')->delete($barang->gambar);
|
|
}
|
|
|
|
$barang->delete();
|
|
return redirect()->route('barang.index')->with('success', 'Barang berhasil dihapus');
|
|
}
|
|
|
|
public function checkout(Request $request)
|
|
{
|
|
// Cek role user
|
|
if (Auth::user()->role !== 'customer') {
|
|
return back()->with('error', 'Anda tidak memiliki akses untuk melakukan checkout');
|
|
}
|
|
|
|
$request->validate([
|
|
'barang_id' => 'required|exists:barang,id',
|
|
'jumlah' => 'required|integer|min:1',
|
|
'catatan' => 'nullable|string'
|
|
]);
|
|
|
|
$barang = Barang::findOrFail($request->barang_id);
|
|
|
|
// Cek stok
|
|
if ($barang->stok < $request->jumlah) {
|
|
return back()->with('error', 'Stok tidak mencukupi');
|
|
}
|
|
|
|
$total = $barang->harga * $request->jumlah;
|
|
|
|
// Buat pesanan langsung
|
|
$pesanan = Pesanan::create([
|
|
'user_id' => Auth::id(),
|
|
'barang_id' => $request->barang_id,
|
|
'jumlah' => $request->jumlah,
|
|
'total_harga' => $total,
|
|
'status' => 'pending',
|
|
'catatan' => $request->catatan
|
|
]);
|
|
|
|
// Kurangi stok
|
|
$barang->update([
|
|
'stok' => $barang->stok - $request->jumlah
|
|
]);
|
|
|
|
return redirect()->route('pesanan.show', $pesanan)
|
|
->with('success', 'Pesanan berhasil dibuat');
|
|
}
|
|
}
|