136 lines
4.4 KiB
PHP
136 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Guest;
|
|
use App\Models\Pelanggan;
|
|
use App\Models\Template;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PelangganApiController extends Controller
|
|
{
|
|
// 🔹 Simpan pesanan pelanggan via API
|
|
public function store(Request $request)
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'nama_pemesan' => 'required|string|max:255',
|
|
'email' => 'required|email',
|
|
'no_tlpn' => 'required|string|max:20',
|
|
'template_slug' => 'required|exists:templates,slug',
|
|
'form' => 'required|array',
|
|
'foto.*' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
]);
|
|
|
|
$template = Template::where('slug', $request->input('template_slug'))->firstOrFail();
|
|
|
|
// Generate unique customer code
|
|
$customerCode = 'CUST-' . strtoupper(Str::random(6));
|
|
while (Pelanggan::where('kode_pelanggan', $customerCode)->exists()) {
|
|
$customerCode = 'CUST-' . strtoupper(Str::random(6));
|
|
}
|
|
|
|
// Handle file uploads
|
|
$fotoPaths = [];
|
|
if ($request->hasFile('foto')) {
|
|
foreach ($request->file('foto') as $file) {
|
|
$path = $file->store('photos', 'public'); // Store in storage/public/photos
|
|
$fotoPaths[] = $path;
|
|
}
|
|
}
|
|
|
|
// Save customer data
|
|
$pelanggan = Pelanggan::create([
|
|
'nama_pemesan' => $validated['nama_pemesan'],
|
|
'email' => $validated['email'],
|
|
'no_tlpn' => $validated['no_tlpn'],
|
|
'template_id' => $template->id,
|
|
'form' => array_merge($validated['form'], ['foto' => $fotoPaths]),
|
|
'harga' => $template->harga,
|
|
'status' => 'menunggu',
|
|
'kode_pelanggan' => $customerCode,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pesanan berhasil dibuat',
|
|
'data' => $pelanggan,
|
|
], 201);
|
|
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Validation failed',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Template not found',
|
|
], 404);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error in store method: ' . $e->getMessage());
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'An error occurred: ' . $e->getMessage(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
// 🔹 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,
|
|
]);
|
|
}
|
|
|
|
// 🔹 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,
|
|
]);
|
|
}
|
|
}
|