fix(api): order order_items so primary precedes addons in getOrderItems

The SELECT in orders_o::getOrderItems had no explicit ORDER BY clause, so
MySQL returned rows in undefined order. On the POS Fuldfør click and the
superuser invoice tree, addons (related_item_id != NULL) were sometimes
returned before their primary item, which broke the FE tree-builder and
the OrderContentTable render.

Add a stable ordering: primary items first (related_item_id IS NULL DESC),
addons grouped by their parent (related_item_id ASC), and insertion order
as the final tiebreaker (id ASC).
This commit is contained in:
Truck Wash Agent
2026-08-10 20:04:57 +02:00
parent 43df3e4dca
commit 7ec64ec84d
+6 -1
View File
@@ -615,7 +615,12 @@ class orders_o extends db
public function getOrderItems(int $order_id): array public function getOrderItems(int $order_id): array
{ {
global $db; global $db;
$sql = "SELECT * FROM order_items WHERE order_id = $order_id"; // Order primary items first (related_item_id IS NULL), then addons grouped by
// their parent (related_item_id ASC), and finally fall back to insertion order
// (id ASC). Without an explicit ORDER BY, MySQL is free to return rows in any
// order, which causes the FE tree-builder to render addons before their
// primary on the invoice and POS displays.
$sql = "SELECT * FROM order_items WHERE order_id = $order_id ORDER BY (related_item_id IS NULL) DESC, related_item_id ASC, id ASC";
$result = $db->query($sql); $result = $db->query($sql);
$order_items = []; $order_items = [];
if ($result->num_rows > 0 && $result) { if ($result->num_rows > 0 && $result) {