orders.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/admin/orders.php
  3. require_once 'admin_auth.php';
  4. require_once '../config/database.php';
  5. require_once __DIR__ . '/../config/functions.php';
  6. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  7. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  8. $_SESSION['lang'] = $_GET['lang'];
  9. }
  10. $current_lang = $_SESSION['lang'] ?? 'el';
  11. $translations = [
  12. 'el' => [
  13. 'page_title' => 'Παραγγελίες - Hockey CMS',
  14. 'header_title' => 'Διαχείριση Παραγγελιών',
  15. 'th_id' => 'ID',
  16. 'th_customer' => 'Πελάτης',
  17. 'th_products' => 'Προϊόντα',
  18. 'th_total' => 'Σύνολο',
  19. 'th_actions' => 'Κατάσταση & Διαχείριση',
  20. 'status_pending' => 'Εκκρεμεί',
  21. 'status_completed' => 'Ολοκληρώθηκε',
  22. 'status_cancelled' => 'Ακύρωση & Επιστροφή Αποθ.',
  23. 'btn_update' => 'Ενημέρωση',
  24. 'btn_delete' => 'Διαγραφή',
  25. 'no_orders' => 'Καμία παραγγελία.',
  26. 'msg_ok' => 'Επιτυχής ενημέρωση της παραγγελίας και του αποθέματος!',
  27. 'msg_del' => 'Η παραγγελία διαγράφηκε οριστικά από το σύστημα.',
  28. 'confirm_del' => 'Είστε απόλυτα σίγουρος ότι θέλετε να ΔΙΑΓΡΑΨΕΤΕ αυτή την παραγγελία;'
  29. ],
  30. 'en' => [
  31. 'page_title' => 'Orders - Hockey CMS',
  32. 'header_title' => 'Orders Management',
  33. 'th_id' => 'ID',
  34. 'th_customer' => 'Customer',
  35. 'th_products' => 'Products',
  36. 'th_total' => 'Total',
  37. 'th_actions' => 'Status & Management',
  38. 'status_pending' => 'Pending',
  39. 'status_completed' => 'Completed',
  40. 'status_cancelled' => 'Cancelled & Restock',
  41. 'btn_update' => 'Update',
  42. 'btn_delete' => 'Delete',
  43. 'no_orders' => 'No orders found.',
  44. 'msg_ok' => 'Order and inventory updated successfully!',
  45. 'msg_del' => 'Order deleted permanently.',
  46. 'confirm_del' => 'Are you sure you want to DELETE this order?'
  47. ]
  48. ];
  49. $t = $translations[$current_lang];
  50. header('Content-Type: text/html; charset=utf-8');
  51. $message = '';
  52. if (isset($_POST['update_status']) && isset($_POST['order_id'])) {
  53. $order_id = (int)$_POST['order_id'];
  54. $new_status = $_POST['status'];
  55. try {
  56. $pdo->beginTransaction();
  57. $oldStatusStmt = $pdo->prepare("SELECT status FROM orders WHERE id = ?");
  58. $oldStatusStmt->execute([$order_id]);
  59. $old_status = $oldStatusStmt->fetchColumn();
  60. $stmt = $pdo->prepare("UPDATE orders SET status = ? WHERE id = ?");
  61. $stmt->execute([$new_status, $order_id]);
  62. if ($new_status === 'cancelled' && $old_status !== 'cancelled') {
  63. $itemsStmt = $pdo->prepare("SELECT product_id, quantity FROM order_items WHERE order_id = ?");
  64. $itemsStmt->execute([$order_id]);
  65. $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
  66. $restockStmt = $pdo->prepare("UPDATE products SET quantity = quantity + ?, status = 'available' WHERE id = ?");
  67. foreach ($items as $item) {
  68. $restockStmt->execute([$item['quantity'], $item['product_id']]);
  69. }
  70. }
  71. if ($old_status === 'cancelled' && $new_status !== 'cancelled') {
  72. $itemsStmt = $pdo->prepare("SELECT product_id, quantity FROM order_items WHERE order_id = ?");
  73. $itemsStmt->execute([$order_id]);
  74. $items = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
  75. $deductStmt = $pdo->prepare("UPDATE products SET status = CASE WHEN (quantity - ?) <= 0 THEN 'sold' ELSE status END, quantity = quantity - ? WHERE id = ?");
  76. foreach ($items as $item) {
  77. $deductStmt->execute([$item['quantity'], $item['quantity'], $item['product_id']]);
  78. }
  79. }
  80. $pdo->commit();
  81. $message = "<div class='alert success'>{$t['msg_ok']}</div>";
  82. } catch (Exception $e) {
  83. $pdo->rollBack();
  84. $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  85. }
  86. }
  87. if (isset($_GET['delete_order'])) {
  88. $delete_id = (int)$_GET['delete_order'];
  89. try {
  90. $pdo->beginTransaction();
  91. $pdo->prepare("DELETE FROM order_items WHERE order_id = ?")->execute([$delete_id]);
  92. $pdo->prepare("DELETE FROM orders WHERE id = ?")->execute([$delete_id]);
  93. $pdo->commit();
  94. $message = "<div class='alert success'>{$t['msg_del']}</div>";
  95. } catch (Exception $e) {
  96. $pdo->rollBack();
  97. $message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  98. }
  99. }
  100. $orders = $pdo->query("SELECT * FROM orders ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
  101. $order_items = [];
  102. $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");
  103. while ($row = $itemsStmt->fetch(PDO::FETCH_ASSOC)) {
  104. $order_items[$row['order_id']][] = $row;
  105. }
  106. ?>
  107. <!DOCTYPE html>
  108. <html lang="<?php echo $current_lang; ?>">
  109. <head>
  110. <meta charset="UTF-8">
  111. <title><?php echo $t['page_title']; ?></title>
  112. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  113. <style>
  114. :root {
  115. --primary: #f97316;
  116. --primary-hover: #ea580c;
  117. --bg-color: #0b1120;
  118. --panel-bg: #1e293b;
  119. --border-color: #334155;
  120. --text-main: #f8fafc;
  121. --text-muted: #94a3b8;
  122. }
  123. body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
  124. .content { margin-left: 250px; padding: 30px; }
  125. .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
  126. .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; }
  127. table { width: 100%; border-collapse: collapse; margin-top: 10px; }
  128. table th, table td { padding: 15px; border-bottom: 1px solid rgba(255,255,255,0.05); text-align: left; vertical-align: middle;}
  129. table th { background: rgba(0,0,0,0.2); color: var(--text-muted); text-transform: uppercase; font-size: 13px; font-weight: 700; }
  130. table td { color: var(--text-main); font-size: 14px; }
  131. .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
  132. .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  133. .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
  134. .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;}
  135. .btn-sm:hover { background: #3b82f6; color: white; }
  136. .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;}
  137. .btn-del:hover { background: #ef4444; color: white; }
  138. .actions-wrap { display: flex; gap: 10px; align-items: center; }
  139. 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;}
  140. </style>
  141. </head>
  142. <body>
  143. <?php include 'sidebar.php'; ?>
  144. <div class="content">
  145. <h2><?php echo $t['header_title']; ?></h2>
  146. <?php echo $message; ?>
  147. <div class="card">
  148. <?php if (count($orders) > 0): ?>
  149. <table>
  150. <thead>
  151. <tr>
  152. <th><?php echo $t['th_id']; ?></th>
  153. <th><?php echo $t['th_customer']; ?></th>
  154. <th><?php echo $t['th_products']; ?></th>
  155. <th><?php echo $t['th_total']; ?></th>
  156. <th><?php echo $t['th_actions']; ?></th>
  157. </tr>
  158. </thead>
  159. <tbody>
  160. <?php foreach ($orders as $order): ?>
  161. <tr>
  162. <td><strong>#<?php echo $order['id']; ?></strong></td>
  163. <td>
  164. <strong><?php echo htmlspecialchars($order['customer_name']); ?></strong><br>
  165. <small style="color: var(--text-muted);"><?php echo htmlspecialchars($order['customer_email']); ?></small>
  166. </td>
  167. <td>
  168. <ul style="margin: 0; padding-left: 20px; font-size: 14px;">
  169. <?php if (isset($order_items[$order['id']])): ?>
  170. <?php foreach ($order_items[$order['id']] as $item): ?>
  171. <li><?php echo htmlspecialchars($item['title']); ?> <strong style="color: #3b82f6;">(x<?php echo $item['quantity']; ?>)</strong></li>
  172. <?php endforeach; ?>
  173. <?php endif; ?>
  174. </ul>
  175. </td>
  176. <td><strong style="color: #10b981; font-size: 15px;"><?php echo number_format($order['total_amount'], 2, ',', '.'); ?> €</strong></td>
  177. <td>
  178. <div class="actions-wrap">
  179. <form method="POST" style="margin: 0; display: flex; gap: 8px;">
  180. <input type="hidden" name="order_id" value="<?php echo $order['id']; ?>">
  181. <select name="status">
  182. <option value="pending" <?php echo $order['status'] == 'pending' ? 'selected' : ''; ?>><?php echo $t['status_pending']; ?></option>
  183. <option value="completed" <?php echo $order['status'] == 'completed' ? 'selected' : ''; ?>><?php echo $t['status_completed']; ?></option>
  184. <option value="cancelled" <?php echo $order['status'] == 'cancelled' ? 'selected' : ''; ?>><?php echo $t['status_cancelled']; ?></option>
  185. </select>
  186. <button type="submit" name="update_status" class="btn-sm"><?php echo $t['btn_update']; ?></button>
  187. </form>
  188. <a href="orders.php?delete_order=<?php echo $order['id']; ?>"
  189. class="btn-del"
  190. onclick="return confirm('<?php echo $t['confirm_del']; ?>');">
  191. <?php echo $t['btn_delete']; ?>
  192. </a>
  193. </div>
  194. </td>
  195. </tr>
  196. <?php endforeach; ?>
  197. </tbody>
  198. </table>
  199. <?php else: ?>
  200. <div style="text-align: center; padding: 40px; color: var(--text-muted);">
  201. <?php echo $t['no_orders']; ?>
  202. </div>
  203. <?php endif; ?>
  204. </div>
  205. </div>
  206. </body>
  207. </html>