File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/database/app/Controllers/Admin/AdminOrderController.php
Back
<?php class AdminOrderController extends Controller { public function index(): void { $db = Database::getInstance(); $search = trim(Security::sanitizeString($_GET['q'] ?? '')); $paymentMethodFilter = trim(Security::sanitizeString($_GET['payment_method'] ?? '')); $paymentStatusFilter = trim(Security::sanitizeString($_GET['payment_status'] ?? '')); $orderStatusFilter = trim(Security::sanitizeString($_GET['order_status'] ?? '')); // Check columns $colsStmt = $db->query("SHOW COLUMNS FROM orders"); $existingCols = $colsStmt ? $colsStmt->fetchAll(PDO::FETCH_COLUMN) : []; $hasPaymentCols = in_array('payment_method', $existingCols); $where = ["1=1"]; $params = []; if (!empty($search)) { $where[] = "(o.order_number LIKE :search OR u.full_name LIKE :search OR u.email LIKE :search OR sp.shop_name LIKE :search OR o.tracking_number LIKE :search)"; $params['search'] = "%{$search}%"; } if ($hasPaymentCols) { if (!empty($paymentMethodFilter) && in_array($paymentMethodFilter, ['prepaid', 'cod'])) { $where[] = "o.payment_method = :pmethod"; $params['pmethod'] = $paymentMethodFilter; } if (!empty($paymentStatusFilter) && in_array($paymentStatusFilter, ['pending', 'paid', 'failed', 'refunded'])) { $where[] = "o.payment_status = :pstatus"; $params['pstatus'] = $paymentStatusFilter; } } if (!empty($orderStatusFilter)) { $where[] = "o.status = :ostatus"; $params['ostatus'] = $orderStatusFilter; } $whereSql = implode(' AND ', $where); $sql = "SELECT o.*, u.full_name AS customer_name, u.email AS customer_email, u.phone AS customer_phone, sp.shop_name, sp.slug AS shop_slug, sp.phone AS seller_phone FROM orders o JOIN users u ON o.user_id = u.id JOIN seller_profiles sp ON o.seller_id = sp.id WHERE {$whereSql} ORDER BY o.id DESC"; $stmt = $db->prepare($sql); $stmt->execute($params); $orders = $stmt->fetchAll(); // Financial & Order Metrics $totalCollected = 0; $totalCodPending = 0; $totalPrepaidCount = 0; $totalCodCount = 0; if ($hasPaymentCols) { $metricStmt = $db->query(" SELECT COALESCE(SUM(CASE WHEN payment_status = 'paid' THEN total_amount ELSE 0 END), 0) AS total_collected, COALESCE(SUM(CASE WHEN payment_method = 'cod' AND payment_status != 'paid' AND status != 'cancelled' THEN total_amount ELSE 0 END), 0) AS total_cod_pending, SUM(CASE WHEN payment_method = 'prepaid' THEN 1 ELSE 0 END) AS prepaid_cnt, SUM(CASE WHEN payment_method = 'cod' THEN 1 ELSE 0 END) AS cod_cnt FROM orders "); $metrics = $metricStmt->fetch(); $totalCollected = (float)($metrics['total_collected'] ?? 0); $totalCodPending = (float)($metrics['total_cod_pending'] ?? 0); $totalPrepaidCount = (int)($metrics['prepaid_cnt'] ?? 0); $totalCodCount = (int)($metrics['cod_cnt'] ?? 0); } else { $totalCollected = (float)$db->query("SELECT COALESCE(SUM(total_amount), 0) FROM orders WHERE status = 'paid'")->fetchColumn(); } $this->render('admin/orders/index', [ 'title' => 'จัดการคำสั่งซื้อและการเงิน (Admin Orders & Finance)', 'orders' => $orders, 'search' => $search, 'paymentMethodFilter' => $paymentMethodFilter, 'paymentStatusFilter' => $paymentStatusFilter, 'orderStatusFilter' => $orderStatusFilter, 'totalCollected' => $totalCollected, 'totalCodPending' => $totalCodPending, 'totalPrepaidCount' => $totalPrepaidCount, 'totalCodCount' => $totalCodCount, 'hasPaymentCols' => $hasPaymentCols ], 'admin'); } /** * Mark COD order as collected/paid by Admin */ public function updatePayment(string $id): void { $this->validateCsrf(); $orderId = (int)$id; $db = Database::getInstance(); $stmt = $db->prepare("SELECT * FROM orders WHERE id = :id LIMIT 1"); $stmt->execute(['id' => $orderId]); $order = $stmt->fetch(); if (!$order) { Session::setFlash('danger', 'ไม่พบคำสั่งซื้อที่ต้องการแก้ไข'); $this->redirect('admin/orders'); } $newPaymentStatus = Security::sanitizeString($_POST['payment_status'] ?? 'paid'); if (!in_array($newPaymentStatus, ['pending', 'paid', 'failed', 'refunded'])) { $newPaymentStatus = 'paid'; } $paidAt = ($newPaymentStatus === 'paid') ? date('Y-m-d H:i:s') : null; $paymentAmount = ($newPaymentStatus === 'paid') ? (float)$order['total_amount'] : 0.00; $upStmt = $db->prepare("UPDATE orders SET payment_status = :pstatus, payment_amount = :pamount, paid_at = :paid_at WHERE id = :id"); $upStmt->execute([ 'pstatus' => $newPaymentStatus, 'pamount' => $paymentAmount, 'paid_at' => $paidAt, 'id' => $orderId ]); if (class_exists('OrderStatusHistory')) { $statusLabel = $newPaymentStatus === 'paid' ? 'ชำระเงิน/เก็บเงินสำเร็จแล้ว' : "เปลี่ยนสถานะการชำระเงินเป็น {$newPaymentStatus}"; OrderStatusHistory::add($orderId, $order['status'], $order['status'], "Admin อัปเดตสถานะการเงิน: {$statusLabel}", 'Platform Admin'); } Session::setFlash('success', "อัปเดตสถานะการชำระเงินของคำสั่งซื้อ #{$order['order_number']} เป็น '{$newPaymentStatus}' เรียบร้อยแล้ว"); $this->redirect('admin/orders'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings