124 lines
3.6 KiB
PHP
124 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Pelanggan;
|
|
use App\Models\Template;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PelangganApiController extends Controller
|
|
{
|
|
// 🔹 Simpan pesanan pelanggan via API
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'nama_pemesan' => 'required|string|max:255',
|
|
'email' => 'required|email',
|
|
'no_tlpn' => 'required|string|max:20',
|
|
'template_id' => 'required|exists:templates,id',
|
|
'form' => 'required|array',
|
|
]);
|
|
|
|
$template = Template::findOrFail($request->input('template_id'));
|
|
|
|
// 🔸 Generate kode undangan unik
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
while (Pelanggan::where('invitation_code', $invitationCode)->exists()) {
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
}
|
|
|
|
// 🔸 Simpan data pelanggan baru
|
|
$pelanggan = Pelanggan::create([
|
|
'nama_pemesan' => $request->nama_pemesan,
|
|
'email' => $request->email,
|
|
'no_tlpn' => $request->no_tlpn,
|
|
'template_id' => $template->id,
|
|
'form' => $request->input('form'),
|
|
'harga' => $template->harga,
|
|
'status' => 'menunggu',
|
|
'invitation_code' => $invitationCode,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pesanan berhasil dibuat',
|
|
'data' => $pelanggan,
|
|
], 201);
|
|
}
|
|
|
|
// 🔹 Ambil semua data pelanggan
|
|
public function index()
|
|
{
|
|
$pelanggan = Pelanggan::with('template')->latest()->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $pelanggan,
|
|
]);
|
|
}
|
|
|
|
// 🔹 Ambil pesanan berdasarkan ID
|
|
public function show($id)
|
|
{
|
|
$pelanggan = Pelanggan::with('template')->find($id);
|
|
|
|
if (!$pelanggan) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data pelanggan tidak ditemukan.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $pelanggan,
|
|
]);
|
|
}
|
|
|
|
// 🔹 Ambil pesanan berdasarkan invitation code
|
|
public function getByInvitationCode($code)
|
|
{
|
|
$pelanggan = Pelanggan::with('template')
|
|
->where('invitation_code', $code)
|
|
->first();
|
|
|
|
if (!$pelanggan) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data pelanggan dengan kode undangan tidak ditemukan.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $pelanggan,
|
|
]);
|
|
}
|
|
|
|
// 🔹 Update status pesanan (opsional untuk admin mobile)
|
|
public function updateStatus(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'status' => 'required|in:menunggu,diterima,ditolak',
|
|
]);
|
|
|
|
$pelanggan = Pelanggan::find($id);
|
|
if (!$pelanggan) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pesanan tidak ditemukan.',
|
|
], 404);
|
|
}
|
|
|
|
$pelanggan->update(['status' => $request->status]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Status pesanan berhasil diperbarui.',
|
|
'data' => $pelanggan,
|
|
]);
|
|
}
|
|
}
|