backorders.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. expirePendingOrders();
  9. $pageTitle = "Nachbestellungen";
  10. $message = "";
  11. $messageType = "";
  12. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['mark_ordered'])) {
  13. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  14. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  15. $messageType = "error";
  16. } else {
  17. $result = markBackorderItemsOrdered(
  18. $_POST['product_id'] ?? 0,
  19. $_POST['size'] ?? "",
  20. $_POST['quantity'] ?? 0,
  21. );
  22. $message = $result["success"]
  23. ? ($result["updated"] ?? 0) .
  24. " Position(en) als bestellt markiert."
  25. : $result["message"];
  26. $messageType = $result["success"] ? "success" : "error";
  27. if ($result["success"]) {
  28. logAccess("Admin marked backorder items ordered", [
  29. "admin" => $_SESSION['admin_username'] ?? "unknown",
  30. "product_id" => $_POST['product_id'] ?? 0,
  31. "size" => $_POST['size'] ?? "",
  32. "quantity" => $_POST['quantity'] ?? 0,
  33. ]);
  34. }
  35. }
  36. }
  37. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['mark_delivered'])) {
  38. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  39. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  40. $messageType = "error";
  41. } else {
  42. $result = markBackorderItemsDelivered(
  43. $_POST['product_id'] ?? 0,
  44. $_POST['size'] ?? "",
  45. $_POST['quantity'] ?? 0,
  46. );
  47. $message = $result["success"]
  48. ? ($result["updated"] ?? 0) .
  49. " Position(en) als geliefert markiert."
  50. : $result["message"];
  51. $messageType = $result["success"] ? "success" : "error";
  52. if ($result["success"]) {
  53. logAccess("Admin marked backorder items delivered", [
  54. "admin" => $_SESSION['admin_username'] ?? "unknown",
  55. "product_id" => $_POST['product_id'] ?? 0,
  56. "size" => $_POST['size'] ?? "",
  57. "quantity" => $_POST['quantity'] ?? 0,
  58. ]);
  59. }
  60. }
  61. }
  62. $groups = getBackorderGroups();
  63. $bodyClass = "admin-page";
  64. include __DIR__ . "/../includes/header.php";
  65. ?>
  66. <div class="admin-header">
  67. <h2>Nachbestellungen</h2>
  68. <div>
  69. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  70. <a href="orders.php" class="btn btn-secondary">Bestellungen</a>
  71. </div>
  72. </div>
  73. <?php if ($message !== ""): ?>
  74. <div class="alert alert-<?php echo escape($messageType); ?>">
  75. <?php echo escape($message); ?>
  76. </div>
  77. <?php endif; ?>
  78. <p class="text-muted">
  79. Artikel werden nach Produkt und Größe zusammengefasst. Aktionen bearbeiten die ältesten Bestellungen zuerst (FIFO).
  80. </p>
  81. <?php if (empty($groups)): ?>
  82. <div class="alert alert-info">
  83. <p>Keine Nachbestellungen vorhanden.</p>
  84. </div>
  85. <?php else: ?>
  86. <div class="table-responsive">
  87. <table class="responsive-table">
  88. <thead>
  89. <tr>
  90. <th>Artikel</th>
  91. <th>Größe</th>
  92. <th>Nachzubestellen</th>
  93. <th>Wartet auf Lieferung</th>
  94. <th>Als bestellt markieren</th>
  95. <th>Lieferung eingetroffen</th>
  96. </tr>
  97. </thead>
  98. <tbody>
  99. <?php foreach ($groups as $group): ?>
  100. <tr>
  101. <td data-label="Artikel"><?php echo escape(
  102. $group["product_name"],
  103. ); ?></td>
  104. <td data-label="Größe"><?php echo $group["size"] !== ""
  105. ? escape($group["size"])
  106. : "-"; ?></td>
  107. <td data-label="Nachzubestellen">
  108. <strong><?php echo (int) $group[
  109. "to_be_backordered"
  110. ]; ?></strong>
  111. </td>
  112. <td data-label="Wartet auf Lieferung">
  113. <strong><?php echo (int) $group["ordered"]; ?></strong>
  114. </td>
  115. <td data-label="Als bestellt markieren">
  116. <?php if ($group["to_be_backordered"] > 0): ?>
  117. <form method="POST" class="backorder-action-form">
  118. <?php echo csrfField(); ?>
  119. <input type="hidden" name="product_id" value="<?php echo (int) $group[
  120. "product_id"
  121. ]; ?>">
  122. <input type="hidden" name="size" value="<?php echo escape(
  123. $group["size"],
  124. ); ?>">
  125. <label class="sr-only" for="qty_ordered_<?php echo (int) $group[
  126. "product_id"
  127. ]; ?>_<?php echo escape(
  128. preg_replace("/[^a-z0-9]/i", "_", $group["size"]),
  129. ); ?>">Menge</label>
  130. <input
  131. type="number"
  132. id="qty_ordered_<?php echo (int) $group[
  133. "product_id"
  134. ]; ?>_<?php echo escape(
  135. preg_replace("/[^a-z0-9]/i", "_", $group["size"]),
  136. ); ?>"
  137. name="quantity"
  138. min="1"
  139. max="<?php echo (int) $group[
  140. "to_be_backordered"
  141. ]; ?>"
  142. value="1"
  143. class="backorder-qty-input"
  144. >
  145. <button type="submit" name="mark_ordered" class="btn btn-small">
  146. Als bestellt markieren
  147. </button>
  148. </form>
  149. <?php else: ?>
  150. -
  151. <?php endif; ?>
  152. </td>
  153. <td data-label="Lieferung eingetroffen">
  154. <?php if ($group["ordered"] > 0): ?>
  155. <form method="POST" class="backorder-action-form">
  156. <?php echo csrfField(); ?>
  157. <input type="hidden" name="product_id" value="<?php echo (int) $group[
  158. "product_id"
  159. ]; ?>">
  160. <input type="hidden" name="size" value="<?php echo escape(
  161. $group["size"],
  162. ); ?>">
  163. <label class="sr-only" for="qty_delivered_<?php echo (int) $group[
  164. "product_id"
  165. ]; ?>_<?php echo escape(
  166. preg_replace("/[^a-z0-9]/i", "_", $group["size"]),
  167. ); ?>">Menge</label>
  168. <input
  169. type="number"
  170. id="qty_delivered_<?php echo (int) $group[
  171. "product_id"
  172. ]; ?>_<?php echo escape(
  173. preg_replace("/[^a-z0-9]/i", "_", $group["size"]),
  174. ); ?>"
  175. name="quantity"
  176. min="1"
  177. max="<?php echo (int) $group["ordered"]; ?>"
  178. value="1"
  179. class="backorder-qty-input"
  180. >
  181. <button type="submit" name="mark_delivered" class="btn btn-small btn-secondary">
  182. Lieferung eingetroffen
  183. </button>
  184. </form>
  185. <?php else: ?>
  186. -
  187. <?php endif; ?>
  188. </td>
  189. </tr>
  190. <?php endforeach; ?>
  191. </tbody>
  192. </table>
  193. </div>
  194. <?php endif; ?>
  195. <?php include __DIR__ . "/../includes/footer.php"; ?>