| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/admin/orders.php
- require_once 'admin_auth.php';
- require_once '../config/database.php';
- require_once __DIR__ . '/../config/functions.php';
- // --- Μηχανισμός Αλλαγής Γλώσσας ---
- if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
- $_SESSION['lang'] = $_GET['lang'];
- }
- $current_lang = $_SESSION['lang'] ?? 'el';
- $translations = [
- 'el' => [
- 'page_title' => 'Παραγγελίες - Hockey CMS',
- 'header_title' => 'Διαχείριση Παραγγελιών',
- 'th_id' => 'ID',
- 'th_customer' => 'Πελάτης',
- 'th_products' => 'Προϊόντα',
- 'th_total' => 'Σύνολο',
- 'th_actions' => 'Κατάσταση & Διαχείριση',
- 'status_pending' => 'Εκκρεμεί',
- 'status_completed' => 'Ολοκληρώθηκε',
- 'status_cancelled' => 'Ακύρωση & Επιστροφή Αποθ.',
- 'btn_update' => 'Ενημέρωση',
- 'btn_delete' => 'Διαγραφή',
- 'no_orders' => 'Καμία παραγγελία.',
- 'msg_ok' => 'Επιτυχής ενημέρωση της παραγγελίας και του αποθέματος!',
- 'msg_del' => 'Η παραγγελία διαγράφηκε οριστικά από το σύστημα.',
- 'confirm_del' => 'Είστε απόλυτα σίγουρος ότι θέλετε να ΔΙΑΓΡΑΨΕΤΕ αυτή την παραγγελία;'
- ],
- 'en' => [
- 'page_title' => 'Orders - Hockey CMS',
- 'header_title' => 'Orders Management',
- 'th_id' => 'ID',
- 'th_customer' => 'Customer',
- 'th_products' => 'Products',
- 'th_total' => 'Total',
- 'th_actions' => 'Status & Management',
- 'status_pending' => 'Pending',
- 'status_completed' => 'Completed',
- 'status_cancelled' => 'Cancelled & Restock',
- 'btn_update' => 'Update',
- 'btn_delete' => 'Delete',
- 'no_orders' => 'No orders found.',
- 'msg_ok' => 'Order and inventory updated successfully!',
- 'msg_del' => 'Order deleted permanently.',
- 'confirm_del' => 'Are you sure you want to DELETE this order?'
- ]
- ];
- $t = $translations[$current_lang];
- header('Content-Type: text/html; charset=utf-8');
- $message = '';
- if (isset($_POST['update_status']) && isset($_POST['order_id'])) {
- $order_id = (int)$_POST['order_id'];
- $new_status = $_POST['status'];
- try {
- $pdo->beginTransaction();
- $oldStatusStmt = $pdo->prepare("SELECT status FROM orders WHERE id = ?");
- $oldStatusStmt->execute([$order_id]);
- $old_status = $oldStatusStmt->fetchColumn();
- $stmt = $pdo->prepare("UPDATE orders SET status = ? WHERE id = ?");
- $stmt->execute([$new_status, $order_id]);
- if ($new_status === 'cancelled' && $old_status !== 'cancelled') {
- $itemsStmt = $pdo->prepare("SELECT product_id, quantity FROM order_items WHERE order_id = ?");
- $itemsStmt->execute([$order_id]);
- $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
- $restockStmt = $pdo->prepare("UPDATE products SET quantity = quantity + ?, status = 'available' WHERE id = ?");
- foreach ($items as $item) {
- $restockStmt->execute([$item['quantity'], $item['product_id']]);
- }
- }
-
- if ($old_status === 'cancelled' && $new_status !== 'cancelled') {
- $itemsStmt = $pdo->prepare("SELECT product_id, quantity FROM order_items WHERE order_id = ?");
- $itemsStmt->execute([$order_id]);
- $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
- $deductStmt = $pdo->prepare("UPDATE products SET status = CASE WHEN (quantity - ?) <= 0 THEN 'sold' ELSE status END, quantity = quantity - ? WHERE id = ?");
- foreach ($items as $item) {
- $deductStmt->execute([$item['quantity'], $item['quantity'], $item['product_id']]);
- }
- }
- $pdo->commit();
- $message = "<div class='alert success'>{$t['msg_ok']}</div>";
- } catch (Exception $e) {
- $pdo->rollBack();
- $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
- }
- }
- if (isset($_GET['delete_order'])) {
- $delete_id = (int)$_GET['delete_order'];
- try {
- $pdo->beginTransaction();
- $pdo->prepare("DELETE FROM order_items WHERE order_id = ?")->execute([$delete_id]);
- $pdo->prepare("DELETE FROM orders WHERE id = ?")->execute([$delete_id]);
- $pdo->commit();
- $message = "<div class='alert success'>{$t['msg_del']}</div>";
- } catch (Exception $e) {
- $pdo->rollBack();
- $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
- }
- }
- $orders = $pdo->query("SELECT * FROM orders ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
- $order_items = [];
- $itemsStmt = $pdo->query("SELECT oi.order_id, oi.quantity, oi.price, p.title FROM order_items oi JOIN products p ON oi.product_id = p.id");
- while ($row = $itemsStmt->fetch(PDO::FETCH_ASSOC)) {
- $order_items[$row['order_id']][] = $row;
- }
- ?>
- <!DOCTYPE html>
- <html lang="<?php echo $current_lang; ?>">
- <head>
- <meta charset="UTF-8">
- <title><?php echo $t['page_title']; ?></title>
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
- <style>
- :root {
- --primary: #f97316;
- --primary-hover: #ea580c;
- --bg-color: #0b1120;
- --panel-bg: #1e293b;
- --border-color: #334155;
- --text-main: #f8fafc;
- --text-muted: #94a3b8;
- }
- body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
- .content { margin-left: 250px; padding: 30px; }
- .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
-
- .card { background: var(--panel-bg); padding: 25px; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3); border: 1px solid var(--border-color); margin-bottom: 25px; }
- table { width: 100%; border-collapse: collapse; margin-top: 10px; }
- table th, table td { padding: 15px; border-bottom: 1px solid rgba(255,255,255,0.05); text-align: left; vertical-align: middle;}
- table th { background: rgba(0,0,0,0.2); color: var(--text-muted); text-transform: uppercase; font-size: 13px; font-weight: 700; }
- table td { color: var(--text-main); font-size: 14px; }
-
- .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
- .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
- .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
-
- .btn-sm { padding: 8px 15px; background: rgba(59, 130, 246, 0.1); border: 1px solid rgba(59, 130, 246, 0.2); color: #3b82f6; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 700; text-transform: uppercase; transition: 0.2s;}
- .btn-sm:hover { background: #3b82f6; color: white; }
-
- .btn-del { background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.2); color: #ef4444; padding: 8px 15px; text-decoration: none; border-radius: 6px; font-size: 13px; font-weight: 700; text-transform: uppercase; transition: 0.2s;}
- .btn-del:hover { background: #ef4444; color: white; }
-
- .actions-wrap { display: flex; gap: 10px; align-items: center; }
- select { padding: 8px; background: #0f172a; border: 1px solid var(--border-color); color: var(--text-main); border-radius: 6px; outline: none; font-family: 'Inter', sans-serif;}
- </style>
- </head>
- <body>
- <?php include 'sidebar.php'; ?>
- <div class="content">
- <h2><?php echo $t['header_title']; ?></h2>
- <?php echo $message; ?>
- <div class="card">
- <?php if (count($orders) > 0): ?>
- <table>
- <thead>
- <tr>
- <th><?php echo $t['th_id']; ?></th>
- <th><?php echo $t['th_customer']; ?></th>
- <th><?php echo $t['th_products']; ?></th>
- <th><?php echo $t['th_total']; ?></th>
- <th><?php echo $t['th_actions']; ?></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($orders as $order): ?>
- <tr>
- <td><strong>#<?php echo $order['id']; ?></strong></td>
- <td>
- <strong><?php echo htmlspecialchars($order['customer_name']); ?></strong><br>
- <small style="color: var(--text-muted);"><?php echo htmlspecialchars($order['customer_email']); ?></small>
- </td>
- <td>
- <ul style="margin: 0; padding-left: 20px; font-size: 14px;">
- <?php if (isset($order_items[$order['id']])): ?>
- <?php foreach ($order_items[$order['id']] as $item): ?>
- <li><?php echo htmlspecialchars($item['title']); ?> <strong style="color: #3b82f6;">(x<?php echo $item['quantity']; ?>)</strong></li>
- <?php endforeach; ?>
- <?php endif; ?>
- </ul>
- </td>
- <td><strong style="color: #10b981; font-size: 15px;"><?php echo number_format($order['total_amount'], 2, ',', '.'); ?> €</strong></td>
- <td>
- <div class="actions-wrap">
- <form method="POST" style="margin: 0; display: flex; gap: 8px;">
- <input type="hidden" name="order_id" value="<?php echo $order['id']; ?>">
- <select name="status">
- <option value="pending" <?php echo $order['status'] == 'pending' ? 'selected' : ''; ?>><?php echo $t['status_pending']; ?></option>
- <option value="completed" <?php echo $order['status'] == 'completed' ? 'selected' : ''; ?>><?php echo $t['status_completed']; ?></option>
- <option value="cancelled" <?php echo $order['status'] == 'cancelled' ? 'selected' : ''; ?>><?php echo $t['status_cancelled']; ?></option>
- </select>
- <button type="submit" name="update_status" class="btn-sm"><?php echo $t['btn_update']; ?></button>
- </form>
-
- <a href="orders.php?delete_order=<?php echo $order['id']; ?>"
- class="btn-del"
- onclick="return confirm('<?php echo $t['confirm_del']; ?>');">
- <?php echo $t['btn_delete']; ?>
- </a>
- </div>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php else: ?>
- <div style="text-align: center; padding: 40px; color: var(--text-muted);">
- <?php echo $t['no_orders']; ?>
- </div>
- <?php endif; ?>
- </div>
- </div>
- </body>
- </html>
|