106 lines
3.4 KiB
PHP
106 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Pelanggan;
|
|
use App\Models\Template;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PelangganController extends Controller
|
|
{
|
|
// 🔹 Tampilkan daftar pesanan (admin)
|
|
public function index()
|
|
{
|
|
$pelanggans = Pelanggan::with('template')->latest()->paginate(10);
|
|
return view('admin.pelanggans.index', compact('pelanggans'));
|
|
}
|
|
|
|
// 🔹 Tampilkan detail pesanan (admin)
|
|
public function show($id)
|
|
{
|
|
$pelanggan = Pelanggan::with('template')->findOrFail($id);
|
|
return view('admin.pelanggans.show', compact('pelanggan'));
|
|
}
|
|
|
|
// 🔹 Tampilkan detail pesanan berdasarkan invitation code
|
|
public function showByCode($code)
|
|
{
|
|
$pelanggan = Pelanggan::with('template')
|
|
->where('invitation_code', $code)
|
|
->firstOrFail();
|
|
|
|
return view('admin.pelanggans.show', compact('pelanggan'));
|
|
}
|
|
|
|
// 🔹 Pelanggan membuat pesanan (frontend user)
|
|
public function store(Request $request)
|
|
{
|
|
$template = Template::findOrFail($request->input('template_id'));
|
|
|
|
$rules = [
|
|
'nama_pemesan' => 'required|string|max:255',
|
|
'email' => 'required|email',
|
|
'no_tlpn' => 'required|string|max:30',
|
|
'template_id' => 'required|exists:templates,id',
|
|
];
|
|
|
|
// 🔸 Validasi dinamis dari JSON form template
|
|
if ($template->form) {
|
|
foreach ($template->form as $field => $options) {
|
|
$rule = [];
|
|
$rule[] = !empty($options['required']) ? 'required' : 'nullable';
|
|
$rule[] = 'string';
|
|
$rules["form.$field"] = implode('|', $rule);
|
|
}
|
|
}
|
|
|
|
$validated = $request->validate($rules);
|
|
|
|
// 🔸 Generate kode undangan unik
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
while (Pelanggan::where('invitation_code', $invitationCode)->exists()) {
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
}
|
|
|
|
// 🔸 Simpan pesanan baru
|
|
$pelanggan = Pelanggan::create([
|
|
'nama_pemesan' => $validated['nama_pemesan'],
|
|
'email' => $validated['email'],
|
|
'no_tlpn' => $validated['no_tlpn'],
|
|
'template_id' => $template->id,
|
|
'form' => $request->input('form'),
|
|
'harga' => $template->harga,
|
|
'status' => 'menunggu',
|
|
'invitation_code' => $invitationCode,
|
|
]);
|
|
|
|
return redirect()->route('pelanggans.show', $pelanggan->id)
|
|
->with('success', 'Pesanan berhasil dikirim, status menunggu konfirmasi admin.');
|
|
}
|
|
|
|
// 🔹 Update status pesanan (admin)
|
|
public function update(Request $request, $id)
|
|
{
|
|
$pelanggan = Pelanggan::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'status' => 'required|in:menunggu,diterima,ditolak',
|
|
]);
|
|
|
|
$pelanggan->update($validated);
|
|
|
|
return redirect()->back()->with('success', 'Status pesanan berhasil diperbarui.');
|
|
}
|
|
|
|
// 🔹 Hapus pesanan (admin)
|
|
public function destroy($id)
|
|
{
|
|
$pelanggan = Pelanggan::findOrFail($id);
|
|
$pelanggan->delete();
|
|
|
|
return redirect()->route('admin.pelanggans.index')
|
|
->with('success', 'Pesanan berhasil dihapus.');
|
|
}
|
|
}
|