refactor BuketController and PesananBuketController to remove commented code and improve readability
This commit is contained in:
parent
95bc0ae157
commit
c6155e840c
|
|
@ -13,7 +13,6 @@ class BuketController extends Controller
|
|||
public function index()
|
||||
{
|
||||
$buket = Buket::latest()->get();
|
||||
|
||||
return view('admin.produk-buket.index', compact('buket'));
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +27,6 @@ public function store(Request $request)
|
|||
'deskripsi' => 'required|string|min:10',
|
||||
'foto' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
||||
], [
|
||||
// Detail Pesan Kustom menggunakan :attribute
|
||||
'required' => 'Kolom :attribute tidak boleh kosong.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'min' => ':attribute terlalu pendek, minimal harus :min karakter.',
|
||||
|
|
@ -39,7 +37,6 @@ public function store(Request $request)
|
|||
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
||||
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
||||
], [
|
||||
// Alias Atribut agar pesan lebih ramah pengguna
|
||||
'nama' => 'nama buket',
|
||||
'ukuran' => 'ukuran buket',
|
||||
'kategori' => 'kategori buket',
|
||||
|
|
@ -54,14 +51,12 @@ public function store(Request $request)
|
|||
->withInput()
|
||||
->with('error_modal', 'create');
|
||||
}
|
||||
|
||||
$path = null;
|
||||
if ($request->hasFile('foto')) {
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/buket', $filename, 'public');
|
||||
}
|
||||
|
||||
Buket::create([
|
||||
'nama' => $request->nama,
|
||||
'ukuran' => $request->ukuran,
|
||||
|
|
@ -71,14 +66,12 @@ public function store(Request $request)
|
|||
'deskripsi' => $request->deskripsi,
|
||||
'foto' => $path,
|
||||
]);
|
||||
|
||||
return redirect()->back()->with('success', 'Produk buket berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$buket = Buket::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'ukuran' => 'required|in:S,M,L',
|
||||
|
|
@ -88,7 +81,6 @@ public function update(Request $request, string $id)
|
|||
'deskripsi' => 'required|string|min:10',
|
||||
'foto' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
], [
|
||||
// Detail Pesan Kustom menggunakan :attribute
|
||||
'required' => 'Kolom :attribute tidak boleh kosong.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'min' => ':attribute terlalu pendek, minimal harus :min karakter.',
|
||||
|
|
@ -99,7 +91,6 @@ public function update(Request $request, string $id)
|
|||
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
||||
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
||||
], [
|
||||
// Alias Atribut agar pesan lebih ramah pengguna
|
||||
'nama' => 'nama buket',
|
||||
'ukuran' => 'ukuran buket',
|
||||
'kategori' => 'kategori buket',
|
||||
|
|
@ -114,23 +105,17 @@ public function update(Request $request, string $id)
|
|||
->withInput()
|
||||
->with('error_id', $id);
|
||||
}
|
||||
|
||||
$data = $request->only(['nama', 'ukuran', 'kategori', 'harga', 'request_khusus', 'deskripsi']);
|
||||
|
||||
if ($request->hasFile('foto')) {
|
||||
if ($buket->foto) {
|
||||
Storage::disk('public')->delete($buket->foto);
|
||||
}
|
||||
|
||||
$file = $request->file('foto');
|
||||
$filename = time() . '_' . $file->getClientOriginalName();
|
||||
$path = $file->storeAs('img/buket', $filename, 'public');
|
||||
|
||||
$data['foto'] = $path;
|
||||
}
|
||||
|
||||
$buket->update($data);
|
||||
|
||||
return redirect()->back()->with('success', 'Produk buket berhasil diperbarui!');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,44 +13,33 @@ public function index()
|
|||
$pesanan = TransaksiBuket::whereIn('status_transaksi', ['menunggu_verifikasi', 'diterima'])
|
||||
->orderBy('created_at', 'ASC')
|
||||
->get();
|
||||
|
||||
return view('admin.pesanan.buket', compact('pesanan'));
|
||||
}
|
||||
|
||||
public function updateStatus(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
// 1. Ambil data dengan relasi
|
||||
$pesanan = \App\Models\TransaksiBuket::with(['pelanggan', 'buket'])->findOrFail($id);
|
||||
|
||||
// 2. Tentukan status & session flash sekaligus agar tidak dobel
|
||||
if ($request->jenis === 'terima') {
|
||||
$status = 'diterima';
|
||||
session()->flash('success', "Pesanan {$pesanan->no_invoice} telah diterima!"); // Alert Hijau
|
||||
session()->flash('success', "Pesanan {$pesanan->no_invoice} telah diterima!");
|
||||
} elseif ($request->jenis === 'selesai') {
|
||||
$status = 'selesai';
|
||||
session()->flash('success', "Pesanan {$pesanan->no_invoice} berhasil diselesaikan!");
|
||||
} else {
|
||||
$status = 'ditolak';
|
||||
session()->flash('error', "Pesanan {$pesanan->no_invoice} telah ditolak!"); // Alert Merah
|
||||
session()->flash('error', "Pesanan {$pesanan->no_invoice} telah ditolak!");
|
||||
}
|
||||
|
||||
// 3. Update database
|
||||
$pesanan->update(['status_transaksi' => $status]);
|
||||
|
||||
// 4. Siapkan Data untuk Pesan WA
|
||||
$nama = $pesanan->pelanggan->nama; // Pastikan kolomnya 'nama', bukan 'nama_lengkap'
|
||||
$nama = $pesanan->pelanggan->nama;
|
||||
$produk = $pesanan->buket->nama;
|
||||
$tgl_obj = \Carbon\Carbon::parse($pesanan->tgl_ambil)->locale('id');
|
||||
$tanggal = $tgl_obj->translatedFormat('l, d F Y');
|
||||
$waktu = $tgl_obj->format('H:i');
|
||||
|
||||
$total = number_format($pesanan->total_bayar, 0, ',', '.');
|
||||
$req = $pesanan->request ?? '-';
|
||||
$ucapan = $pesanan->ucapan ?? '-';
|
||||
$invoice = $pesanan->no_invoice;
|
||||
|
||||
// 5. Susun Pesan berdasarkan kondisi
|
||||
$msg = null;
|
||||
if ($status === 'diterima') {
|
||||
$msg = "Halo Kak *{$nama}*,\n\n" .
|
||||
|
|
@ -72,23 +61,16 @@ public function updateStatus(Request $request, $id)
|
|||
"- *Waktu Pengambilan:* {$tanggal}\n\n" .
|
||||
"Admin kami akan segera menghubungi Kakak terkait proses pengembalian dana. Mohon maaf atas ketidaknyamanannya.";
|
||||
}
|
||||
|
||||
// 6. Format Nomor WhatsApp
|
||||
$wa_url = null;
|
||||
|
||||
if ($msg) {
|
||||
$no_wa = preg_replace('/[^0-9]/', '', $pesanan->pelanggan->no_wa);
|
||||
|
||||
if (str_starts_with($no_wa, '0')) {
|
||||
$no_wa = '62' . substr($no_wa, 1);
|
||||
} elseif (str_starts_with($no_wa, '8')) {
|
||||
$no_wa = '62' . $no_wa;
|
||||
}
|
||||
|
||||
$wa_url = "https://wa.me/{$no_wa}?text=" . urlencode($msg);
|
||||
}
|
||||
|
||||
// 7. Kembalikan Response JSON
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'wa_url' => $wa_url
|
||||
|
|
|
|||
Loading…
Reference in New Issue