Fix: Syntax error in add_packing_list and added recursive weight calculation for containers

This commit is contained in:
Gemini Agent
2025-12-05 19:34:13 +00:00
parent 77959374a9
commit 9c62e719e3
2 changed files with 32 additions and 6 deletions

View File

@@ -161,7 +161,7 @@ require_once 'header.php';
<div class="mb-3 pb-3 border-bottom">
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" name="participate[<?php echo $user['id']; ?>]" value="1" checked id="part_<?php echo $user['id']; ?>">
<label class="form-check-label fw-bold" for="part_<?php echo $user['id']; ">
<label class="form-check-label fw-bold" for="part_<?php echo $user['id']; ?>">
<?php echo htmlspecialchars($user['username']); ?>
</label>
</div>

View File

@@ -58,11 +58,12 @@ $stmt_list_owner->close();
$page_title = "Packliste: " . htmlspecialchars($packing_list['name']);
// Robust SQL: Fetches names from Backpacks/Compartments if article is missing
// FIX: Include backpack own weight in weight_grams calculation
$sql = "SELECT
pli.id, pli.quantity, pli.parent_packing_list_item_id, pli.carrier_user_id,
pli.backpack_id, pli.backpack_compartment_id,
COALESCE(a.name, pli.name, bp.name, bpc.name, 'Unbekanntes Item') AS article_name,
COALESCE(a.weight_grams, 0) as weight_grams,
COALESCE(a.weight_grams, bp.weight_grams, 0) as weight_grams,
a.image_url, a.product_designation, a.consumable,
c.name AS category_name,
m.name AS manufacturer_name,
@@ -132,6 +133,20 @@ function get_recursive_quantity($parent_id, $items_by_parent) {
return $count;
}
// Helper for recursive weight calculation
function get_recursive_weight($parent_id, $items_by_parent) {
$weight = 0;
if (isset($items_by_parent[$parent_id])) {
foreach ($items_by_parent[$parent_id] as $child) {
// Child weight
$weight += ($child['quantity'] * $child['weight_grams']);
// Plus its descendants
$weight += get_recursive_weight($child['id'], $items_by_parent);
}
}
return $weight;
}
// Recursive Rendering
function render_item_row($item, $level, $items_by_parent) {
$has_children = isset($items_by_parent[$item['id']]);
@@ -159,11 +174,22 @@ function render_item_row($item, $level, $items_by_parent) {
$indent_px = $level * 25;
$weight_display = $item['weight_grams'] > 0 ? number_format($item['weight_grams'], 0, ',', '.') . ' g' : '-';
$total_weight_display = $item['weight_grams'] > 0 ? number_format($item['weight_grams'] * $item['quantity'], 0, ',', '.') . ' g' : '-';
// Ensure children rows are initially shown or hidden based on some logic? Default shown.
// But the toggle button should reflect state. Default expanded -> chevron-down.
// Calculate Total Weight display logic
$total_weight_val = 0;
if ($is_backpack || $is_compartment) {
// For containers: Recursive weight of children
// For backpacks specifically: Add own weight + children
$children_weight = get_recursive_weight($item['id'], $items_by_parent);
$own_weight = $item['quantity'] * $item['weight_grams']; // Usually 1 * empty_weight
$total_weight_val = $own_weight + $children_weight;
} else {
// Standard items
$total_weight_val = $item['weight_grams'] * $item['quantity'];
}
$total_weight_display = ($total_weight_val > 0) ? number_format($total_weight_val, 0, ',', '.') . ' g' : '-';
echo '<tr class="' . $bg_class . '" data-id="' . $item['id'] . '" data-parent-id="' . ($item['parent_packing_list_item_id'] ?: 0) . '">';
// Name Column with Indentation