38526-vm/admin/pesanan.php
2026-02-17 17:13:31 +00:00

200 lines
9.2 KiB
PHP

<?php
require_once 'header.php';
$action = $_GET['action'] ?? 'tampil';
$id = $_GET['id'] ?? null;
// Handle Status Change
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
$new_status = $_POST['status'];
$stmt = db()->prepare("UPDATE pesanan SET status = ? WHERE id = ?");
$stmt->execute([$new_status, $id]);
header("Location: pesanan.php?action=detail&id=$id&msg=Status pesanan berhasil diperbarui");
exit;
}
// Handle Delete
if ($action === 'hapus' && $id) {
// Delete items first
$stmt = db()->prepare("DELETE FROM pesanan_item WHERE pesanan_id = ?");
$stmt->execute([$id]);
// Delete order
$stmt = db()->prepare("DELETE FROM pesanan WHERE id = ?");
$stmt->execute([$id]);
header('Location: pesanan.php?msg=Pesanan berhasil dihapus');
exit;
}
// Fetch Data
if ($action === 'detail' && $id) {
$order = fetch_one("SELECT * FROM pesanan WHERE id = ?", [$id]);
$items = fetch_all("SELECT pi.*, m.nama_menu FROM pesanan_item pi JOIN menu m ON pi.menu_id = m.id WHERE pi.pesanan_id = ?", [$id]);
}
$pesanan_list = fetch_all("SELECT * FROM pesanan ORDER BY id DESC");
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Kelola Pesanan</h1>
</div>
<?php if (isset($_GET['msg'])): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($_GET['msg']); ?></div>
<?php endif; ?>
<?php if ($action === 'detail' && $order): ?>
<div class="row">
<div class="col-md-8">
<div class="card shadow-sm mb-4">
<div class="card-header bg-white">
<h5 class="mb-0">Detail Pesanan #<?php echo $order['id']; ?></h5>
</div>
<div class="card-body">
<table class="table table-bordered">
<tr>
<th width="30%">Nama Pelanggan</th>
<td><?php echo htmlspecialchars($order['nama_pelanggan']); ?></td>
</tr>
<tr>
<th>WhatsApp</th>
<td>
<a href="https://wa.me/<?php echo $order['whatsapp']; ?>" target="_blank" class="text-decoration-none">
<?php echo htmlspecialchars($order['whatsapp']); ?> <i class="fa fa-external-link small"></i>
</a>
</td>
</tr>
<tr>
<th>Alamat Pengiriman</th>
<td><?php echo nl2br(htmlspecialchars($order['alamat_pengiriman'])); ?></td>
</tr>
<tr>
<th>Tanggal Kirim</th>
<td><?php echo date('d F Y', strtotime($order['tanggal_kirim'])); ?></td>
</tr>
<tr>
<th>Catatan</th>
<td><?php echo nl2br(htmlspecialchars($order['catatan'] ?? '-')); ?></td>
</tr>
<tr>
<th>Waktu Pesan</th>
<td><?php echo date('d/m/Y H:i', strtotime($order['created_at'])); ?></td>
</tr>
</table>
<h6 class="mt-4 mb-3">Item Pesanan</h6>
<div class="table-responsive">
<table class="table table-sm">
<thead class="table-light">
<tr>
<th>Menu</th>
<th class="text-center">Qty</th>
<th class="text-end">Harga Satuan</th>
<th class="text-end">Subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo htmlspecialchars($item['nama_menu']); ?></td>
<td class="text-center"><?php echo $item['qty']; ?></td>
<td class="text-end"><?php echo rupiah($item['harga_saat_pesan']); ?></td>
<td class="text-end"><?php echo rupiah($item['qty'] * $item['harga_saat_pesan']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th colspan="3" class="text-end">Total</th>
<th class="text-end h5"><?php echo rupiah($order['total_harga']); ?></th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="card-footer bg-white">
<a href="pesanan.php" class="btn btn-outline-secondary">Kembali</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow-sm">
<div class="card-header bg-white">
<h5 class="mb-0">Update Status</h5>
</div>
<div class="card-body">
<form method="POST">
<input type="hidden" name="update_status" value="1">
<div class="mb-3">
<label class="form-label">Status Saat Ini</label>
<select name="status" class="form-select">
<option value="Baru" <?php echo $order['status'] == 'Baru' ? 'selected' : ''; ?>>Baru</option>
<option value="Proses" <?php echo $order['status'] == 'Proses' ? 'selected' : ''; ?>>Proses</option>
<option value="Selesai" <?php echo $order['status'] == 'Selesai' ? 'selected' : ''; ?>>Selesai</option>
<option value="Batal" <?php echo $order['status'] == 'Batal' ? 'selected' : ''; ?>>Batal</option>
</select>
</div>
<button type="submit" class="btn btn-primary w-100">Update Status</button>
</form>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="card shadow-sm">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Pelanggan</th>
<th>WA</th>
<th>Total</th>
<th>Status</th>
<th>Waktu</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php foreach ($pesanan_list as $row): ?>
<tr>
<td>#<?php echo $row['id']; ?></td>
<td><?php echo htmlspecialchars($row['nama_pelanggan']); ?></td>
<td><?php echo htmlspecialchars($row['whatsapp']); ?></td>
<td><?php echo rupiah($row['total_harga']); ?></td>
<td>
<?php
$badge = [
'Baru' => 'bg-info',
'Proses' => 'bg-warning',
'Selesai' => 'bg-success',
'Batal' => 'bg-danger'
];
?>
<span class="badge <?php echo $badge[$row['status']] ?? 'bg-secondary'; ?>">
<?php echo $row['status']; ?>
</span>
</td>
<td class="small"><?php echo date('d/m/y H:i', strtotime($row['created_at'])); ?></td>
<td>
<a href="pesanan.php?action=detail&id=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-info">Detail</a>
<a href="pesanan.php?action=hapus&id=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus pesanan ini?')">Hapus</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($pesanan_list)): ?>
<tr>
<td colspan="7" class="text-center py-4 text-muted">Belum ada pesanan.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<?php require_once 'footer.php'; ?>