File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/app/Controllers/OrderController.php
Back
<?php class OrderController extends Controller { // Status groups for tab filtering (matches Shopee/Lazada UX) private static array $tabGroups = [ 'all' => [], // all statuses 'to_pay' => ['pending', 'waiting_payment'], 'to_receive' => ['order_accepted', 'paid', 'preparing', 'shipping'], 'delivered' => ['delivered', 'completed'], 'cancelled' => ['cancelled', 'refunded'], ]; public function index(): void { $user = Auth::user(); $db = Database::getInstance(); // Active tab from query string $tab = $_GET['tab'] ?? 'all'; if (!array_key_exists($tab, self::$tabGroups)) { $tab = 'all'; } // Build WHERE clause for the active tab $tabStatuses = self::$tabGroups[$tab]; $whereExtra = ''; $params = ['uid' => $user['id']]; if (!empty($tabStatuses)) { $placeholders = implode(',', array_map(fn($s) => "'$s'", $tabStatuses)); $whereExtra = " AND o.status IN ($placeholders)"; } $sql = "SELECT o.*, sp.shop_name, sp.slug as shop_slug, sp.logo as shop_logo FROM orders o JOIN seller_profiles sp ON o.seller_id = sp.id WHERE o.user_id = :uid{$whereExtra} ORDER BY o.id DESC"; $stmt = $db->prepare($sql); $stmt->execute($params); $orders = $stmt->fetchAll(); // Count per tab for badge numbers $allOrders = $db->prepare("SELECT status FROM orders WHERE user_id = :uid"); $allOrders->execute(['uid' => $user['id']]); $allStatuses = $allOrders->fetchAll(PDO::FETCH_COLUMN); $tabCounts = ['all' => count($allStatuses)]; foreach (array_keys(self::$tabGroups) as $t) { if ($t === 'all') continue; $tabCounts[$t] = count(array_filter($allStatuses, fn($s) => in_array($s, self::$tabGroups[$t]))); } $reviewModel = new Review(); foreach ($orders as &$ord) { $itemSql = "SELECT oi.*, p.title, p.slug, p.sku, (SELECT image_path FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) AS primary_image FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.order_id = :oid"; $itemStmt = $db->prepare($itemSql); $itemStmt->execute(['oid' => $ord['id']]); $ord['items'] = $itemStmt->fetchAll(); foreach ($ord['items'] as &$item) { $item['review'] = $reviewModel->getByOrderItemId((int)$item['id']); } } $this->render('orders/index', [ 'title' => 'ติดตามคำสั่งซื้อของฉัน', 'orders' => $orders, 'activeTab' => $tab, 'tabCounts' => $tabCounts, ]); } public function detail(string $id): void { $user = Auth::user(); $orderId = (int)$id; $db = Database::getInstance(); $sql = "SELECT o.*, sp.shop_name, sp.slug as shop_slug, sp.phone AS seller_phone FROM orders o JOIN seller_profiles sp ON o.seller_id = sp.id WHERE o.id = :oid AND o.user_id = :uid LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute(['oid' => $orderId, 'uid' => $user['id']]); $order = $stmt->fetch(); if (!$order) { Session::setFlash('danger', 'ไม่พบคำสั่งซื้อที่ต้องการ'); $this->redirect('orders'); } // Fetch Order Items with product image $itemSql = "SELECT oi.*, p.title, p.slug, p.sku, (SELECT image_path FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) AS primary_image FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.order_id = :oid"; $itemStmt = $db->prepare($itemSql); $itemStmt->execute(['oid' => $orderId]); $items = $itemStmt->fetchAll(); $reviewModel = new Review(); foreach ($items as &$item) { $item['review'] = $reviewModel->getByOrderItemId((int)$item['id']); } $this->render('orders/detail', [ 'title' => 'รายละเอียดคำสั่งซื้อ #' . $order['order_number'], 'order' => $order, 'items' => $items ]); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.23 |
proxy
|
phpinfo
|
Settings