order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. require_once __DIR__ . "/../config.php";
  3. require_once __DIR__ . "/../includes/functions.php";
  4. if (empty($_SESSION['admin_logged_in'])) {
  5. header("Location: login.php");
  6. exit();
  7. }
  8. $message = "";
  9. $messageType = "";
  10. if (
  11. $_SERVER['REQUEST_METHOD'] === "POST" &&
  12. isset($_POST['toggle_item_backorder'])
  13. ) {
  14. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  15. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  16. $messageType = "error";
  17. } else {
  18. $result = toggleOrderItemBackorder(
  19. $_POST['order_id'] ?? "",
  20. (int) ($_POST['item_index'] ?? -1),
  21. );
  22. $message = $result["success"]
  23. ? "Nachbestellstatus wurde aktualisiert."
  24. : $result["message"];
  25. $messageType = $result["success"] ? "success" : "error";
  26. if ($result["success"]) {
  27. logAccess("Admin toggled order item backorder", [
  28. "admin" => $_SESSION['admin_username'] ?? "unknown",
  29. "order_id" => $_POST['order_id'] ?? "",
  30. "item_index" => $_POST['item_index'] ?? -1,
  31. ]);
  32. }
  33. }
  34. }
  35. if (
  36. $_SERVER['REQUEST_METHOD'] === "POST" &&
  37. isset($_POST['toggle_item_processed'])
  38. ) {
  39. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  40. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  41. $messageType = "error";
  42. } else {
  43. $result = toggleOrderItemProcessed(
  44. $_POST['order_id'] ?? "",
  45. (int) ($_POST['item_index'] ?? -1),
  46. );
  47. $message = $result["success"]
  48. ? "Position wurde aktualisiert."
  49. : $result["message"];
  50. $messageType = $result["success"] ? "success" : "error";
  51. if ($result["success"]) {
  52. logAccess("Admin toggled order item", [
  53. "admin" => $_SESSION['admin_username'] ?? "unknown",
  54. "order_id" => $_POST['order_id'] ?? "",
  55. "item_index" => $_POST['item_index'] ?? -1,
  56. ]);
  57. }
  58. }
  59. }
  60. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['cancel_order'])) {
  61. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  62. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  63. $messageType = "error";
  64. } else {
  65. $adminUsername = $_SESSION['admin_username'] ?? "";
  66. $result = cancelOrder(
  67. $_POST['order_id'] ?? "",
  68. $adminUsername,
  69. $_POST['cancellation_reason'] ?? "",
  70. );
  71. $message = $result["success"]
  72. ? "Bestellung wurde storniert."
  73. : $result["message"];
  74. $messageType = $result["success"] ? "success" : "error";
  75. if ($result["success"]) {
  76. logAccess("Admin cancelled order", [
  77. "admin" => $adminUsername,
  78. "order_id" => $_POST['order_id'] ?? "",
  79. ]);
  80. }
  81. }
  82. }
  83. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['uncancel_order'])) {
  84. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  85. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  86. $messageType = "error";
  87. } else {
  88. $adminUsername = $_SESSION['admin_username'] ?? "";
  89. $result = uncancelOrder($_POST['order_id'] ?? "");
  90. $message = $result["success"]
  91. ? "Stornierung wurde aufgehoben."
  92. : $result["message"];
  93. $messageType = $result["success"] ? "success" : "error";
  94. if ($result["success"]) {
  95. logAccess("Admin uncancelled order", [
  96. "admin" => $adminUsername,
  97. "order_id" => $_POST['order_id'] ?? "",
  98. ]);
  99. }
  100. }
  101. }
  102. $orderId = trim((string) ($_GET['id'] ?? $_POST['order_id'] ?? ""));
  103. $order = $orderId !== "" ? getOrderById($orderId) : null;
  104. $pageTitle =
  105. $order !== null
  106. ? "Bestellung " . $order["id"]
  107. : ($orderId !== ""
  108. ? "Bestellung nicht gefunden"
  109. : "Bestellung");
  110. $bodyClass = "admin-page";
  111. include __DIR__ . "/../includes/header.php";
  112. ?>
  113. <div class="admin-header">
  114. <h2><?php echo $order !== null
  115. ? "Bestellung " . escape($order["id"])
  116. : "Bestellung"; ?></h2>
  117. <div class="admin-dashboard-actions">
  118. <?php if ($order !== null): ?>
  119. <a
  120. href="order-pdf.php?id=<?php echo urlencode($order["id"]); ?>"
  121. class="btn btn-secondary"
  122. target="_blank"
  123. rel="noopener noreferrer"
  124. >Bestellung drucken</a>
  125. <?php endif; ?>
  126. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  127. <a href="orders.php" class="btn">Zurück zur Bestellliste</a>
  128. </div>
  129. </div>
  130. <?php if ($message !== ""): ?>
  131. <div class="alert alert-<?php echo escape($messageType); ?>">
  132. <?php echo escape($message); ?>
  133. </div>
  134. <?php endif; ?>
  135. <?php if ($order === null): ?>
  136. <div class="alert alert-info">
  137. <p><?php echo $orderId !== ""
  138. ? "Die Bestellung wurde nicht gefunden."
  139. : "Keine Bestellnummer angegeben."; ?></p>
  140. </div>
  141. <?php else: ?>
  142. <div class="panel">
  143. <p><strong>Status:</strong> <span class="status <?php echo escape(
  144. getOrderStatusClass($order),
  145. ); ?>"><?php echo escape(
  146. getOrderStatusLabel($order),
  147. ); ?></span>
  148. <?php if (orderHasBackorder($order)): ?>
  149. <span class="status status-backorder">Nachbestellung</span>
  150. <?php endif; ?>
  151. </p>
  152. <p><strong>Name:</strong> <?php echo escape(
  153. $order["customer_name"],
  154. ); ?></p>
  155. <p><strong>E-Mail:</strong> <?php echo escape(
  156. $order["customer_email"],
  157. ); ?></p>
  158. <p><strong>Organisation:</strong> <?php echo escape(
  159. $order["organization_label"],
  160. ); ?></p>
  161. <p><strong>Erstellt:</strong> <?php echo escape(
  162. formatDate($order["created_at"]),
  163. ); ?></p>
  164. <?php if ($order["admin_notified_at"] !== ""): ?>
  165. <p><strong>Intern weitergeleitet:</strong> <?php echo escape(
  166. formatDate($order["admin_notified_at"]),
  167. ); ?></p>
  168. <?php endif; ?>
  169. <p><strong>Kommentar:</strong><br><?php echo $order[
  170. "comment"
  171. ] !== ""
  172. ? nl2br(escape($order["comment"]))
  173. : "Kein Kommentar"; ?></p>
  174. <?php if ($order["status"] === "cancelled"): ?>
  175. <div class="alert alert-warning">
  176. <p><strong>Storniert am:</strong> <?php echo escape(
  177. formatDate($order["cancelled_at"]),
  178. ); ?></p>
  179. <p><strong>Storniert durch:</strong> <?php echo escape(
  180. $order["cancelled_by"],
  181. ); ?></p>
  182. <p><strong>Stornogrund:</strong><br><?php echo $order[
  183. "cancellation_reason"
  184. ] !== ""
  185. ? nl2br(escape($order["cancellation_reason"]))
  186. : "Kein Grund angegeben"; ?></p>
  187. </div>
  188. <form
  189. method="POST"
  190. class="inline-form"
  191. onsubmit="return confirm('Stornierung wirklich aufheben? Die Bestellung kann danach wieder bearbeitet werden.');"
  192. >
  193. <?php echo csrfField(); ?>
  194. <input type="hidden" name="order_id" value="<?php echo escape(
  195. $order["id"],
  196. ); ?>">
  197. <button type="submit" name="uncancel_order" class="btn btn-small">
  198. Stornierung aufheben
  199. </button>
  200. </form>
  201. <?php endif; ?>
  202. <h4>Positionen</h4>
  203. <div class="table-responsive">
  204. <table class="responsive-table table-compact">
  205. <thead>
  206. <tr>
  207. <th>Artikel</th>
  208. <th>Größe</th>
  209. <th>Lieferhinweis</th>
  210. <th>Bearbeitet</th>
  211. <th>Nachbestellung</th>
  212. <th>Aktion</th>
  213. </tr>
  214. </thead>
  215. <tbody>
  216. <?php foreach ($order["items"] as $index => $item): ?>
  217. <tr>
  218. <td data-label="Artikel"><?php echo escape(
  219. $item["product_name"],
  220. ); ?></td>
  221. <td data-label="Größe"><?php echo $item["size"] !==
  222. ""
  223. ? escape($item["size"])
  224. : "-"; ?></td>
  225. <td data-label="Lieferhinweis"><?php echo $item[
  226. "availability_label"
  227. ] !== ""
  228. ? escape($item["availability_label"])
  229. : "-"; ?></td>
  230. <td data-label="Bearbeitet">
  231. <span class="status <?php echo !empty(
  232. $item["is_processed"]
  233. )
  234. ? "status-processed"
  235. : "status-open"; ?>">
  236. <?php echo !empty($item["is_processed"])
  237. ? "Ja"
  238. : "Nein"; ?>
  239. </span>
  240. </td>
  241. <td data-label="Nachbestellung">
  242. <?php
  243. $backorderStatus = (string) ($item["backorder_status"] ?? "");
  244. if ($backorderStatus !== ""): ?>
  245. <span class="status <?php echo escape(
  246. getBackorderStatusClass($backorderStatus),
  247. ); ?>"><?php echo escape(
  248. getBackorderStatusLabel($backorderStatus),
  249. ); ?></span>
  250. <?php else: ?>
  251. -
  252. <?php endif; ?>
  253. </td>
  254. <td data-label="Aktionen">
  255. <?php if ($order["status"] !== "cancelled"): ?>
  256. <form method="POST" class="inline-form">
  257. <?php echo csrfField(); ?>
  258. <input type="hidden" name="order_id" value="<?php echo escape(
  259. $order["id"],
  260. ); ?>">
  261. <input type="hidden" name="item_index" value="<?php echo (int) $index; ?>">
  262. <button type="submit" name="toggle_item_processed" class="btn btn-small">
  263. <?php echo !empty(
  264. $item["is_processed"]
  265. )
  266. ? "Als offen markieren"
  267. : "Als bearbeitet markieren"; ?>
  268. </button>
  269. </form>
  270. <?php
  271. $canToggleBackorder =
  272. $backorderStatus === "to_be_backordered" ||
  273. ($backorderStatus === "" &&
  274. empty($item["is_processed"]));
  275. if ($canToggleBackorder): ?>
  276. <form method="POST" class="inline-form">
  277. <?php echo csrfField(); ?>
  278. <input type="hidden" name="order_id" value="<?php echo escape(
  279. $order["id"],
  280. ); ?>">
  281. <input type="hidden" name="item_index" value="<?php echo (int) $index; ?>">
  282. <button type="submit" name="toggle_item_backorder" class="btn btn-small btn-secondary">
  283. <?php echo $backorderStatus === "to_be_backordered"
  284. ? "Nachbestellung aufheben"
  285. : "Als Nachbestellung markieren"; ?>
  286. </button>
  287. </form>
  288. <?php endif; ?>
  289. <?php else: ?>
  290. -
  291. <?php endif; ?>
  292. </td>
  293. </tr>
  294. <?php endforeach; ?>
  295. </tbody>
  296. </table>
  297. </div>
  298. <?php if (
  299. $order["status"] !== "cancelled" &&
  300. $order["status"] !== "processed"
  301. ): ?>
  302. <button
  303. type="button"
  304. class="btn btn-secondary btn-small"
  305. id="cancel-order-open"
  306. >
  307. Bestellung stornieren
  308. </button>
  309. <div
  310. id="cancel-order-modal"
  311. class="modal"
  312. role="dialog"
  313. aria-labelledby="cancel-order-title"
  314. aria-hidden="true"
  315. >
  316. <div class="modal-content modal-content-compact">
  317. <button
  318. type="button"
  319. class="modal-close btn btn-secondary btn-small"
  320. id="cancel-order-close"
  321. aria-label="Schließen"
  322. >
  323. &times;
  324. </button>
  325. <h4 id="cancel-order-title">Bestellung stornieren</h4>
  326. <form method="POST" id="cancel-order-form">
  327. <?php echo csrfField(); ?>
  328. <input type="hidden" name="order_id" value="<?php echo escape(
  329. $order["id"],
  330. ); ?>">
  331. <div class="form-group">
  332. <label for="cancellation_reason">Stornogrund</label>
  333. <textarea
  334. id="cancellation_reason"
  335. name="cancellation_reason"
  336. rows="3"
  337. placeholder="Optionaler Grund"
  338. ></textarea>
  339. </div>
  340. <button type="submit" name="cancel_order" class="btn">
  341. Stornierung bestätigen
  342. </button>
  343. </form>
  344. </div>
  345. </div>
  346. <script>
  347. (function () {
  348. const modal = document.getElementById("cancel-order-modal");
  349. const openBtn = document.getElementById("cancel-order-open");
  350. const closeBtn = document.getElementById("cancel-order-close");
  351. if (!modal || !openBtn || !closeBtn) {
  352. return;
  353. }
  354. function openModal() {
  355. modal.classList.add("is-open");
  356. modal.setAttribute("aria-hidden", "false");
  357. const reason = document.getElementById("cancellation_reason");
  358. if (reason) {
  359. reason.focus();
  360. }
  361. }
  362. function closeModal() {
  363. modal.classList.remove("is-open");
  364. modal.setAttribute("aria-hidden", "true");
  365. }
  366. openBtn.addEventListener("click", openModal);
  367. closeBtn.addEventListener("click", closeModal);
  368. modal.addEventListener("click", function (event) {
  369. if (event.target === modal) {
  370. closeModal();
  371. }
  372. });
  373. document.addEventListener("keydown", function (event) {
  374. if (event.key === "Escape" && modal.classList.contains("is-open")) {
  375. closeModal();
  376. }
  377. });
  378. })();
  379. </script>
  380. <?php endif; ?>
  381. </div>
  382. <?php endif; ?>
  383. <?php include __DIR__ . "/../includes/footer.php"; ?>