From 77b053c57322979c995c82792f0bee2e475cd159 Mon Sep 17 00:00:00 2001 From: morgane Date: Wed, 17 Jun 2026 21:13:07 +0000 Subject: [PATCH] Actualiser api/index.php --- api/index.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/api/index.php b/api/index.php index e19767a..43e69b5 100644 --- a/api/index.php +++ b/api/index.php @@ -2703,6 +2703,20 @@ function saveProduct(PDO $db): void { ? $input['shopping_name'] : computeShoppingName($input['name'], $input['category'] ?? '', $input['brand'] ?? ''); + // Sous-catégorie obligatoire uniquement pour les boissons + $category = $input['category'] ?? ''; + $subcategory = trim($input['subcategory'] ?? ''); + $validSubcategories = ['vin', 'biere', 'spiritueux', 'soda', 'jus', 'eau', 'autre']; + if ($category === 'bevande') { + if ($subcategory === '' || !in_array($subcategory, $validSubcategories, true)) { + http_response_code(400); + echo json_encode(['error' => 'subcategory_required', 'message' => 'Sous-catégorie requise pour les boissons']); + return; + } + } else { + $subcategory = null; + } + $barcode = normalizeProductBarcode($input['barcode'] ?? null); $id = !empty($input['id']) ? (int)$input['id'] : 0; @@ -2737,7 +2751,7 @@ function saveProduct(PDO $db): void { $nutriJson = isset($input['nutriments']) ? json_encode($input['nutriments']) : null; $params = [ - $input['name'], $input['brand'] ?? '', $input['category'] ?? '', + $input['name'], $input['brand'] ?? '', $category, $subcategory, $input['image_url'] ?? '', $input['unit'] ?? 'pz', $input['default_quantity'] ?? 1, $input['notes'] ?? '', $barcode, $input['package_unit'] ?? '', @@ -2758,8 +2772,8 @@ function saveProduct(PDO $db): void { } $stmt = $db->prepare(" - INSERT INTO products (name, brand, category, image_url, unit, default_quantity, notes, barcode, package_unit, shopping_name, nutriments_json) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO products (name, brand, category, subcategory, image_url, unit, default_quantity, notes, barcode, package_unit, shopping_name, nutriments_json) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute($params); echo json_encode(['success' => true, 'id' => (int)$db->lastInsertId(), 'merged' => false]); @@ -3008,7 +3022,7 @@ function listInventory(PDO $db): void { EverLog::debug('listInventory'); $location = $_GET['location'] ?? ''; $query = " - SELECT i.*, p.name, p.brand, p.category, p.image_url, p.unit, p.barcode, p.default_quantity, p.package_unit, + SELECT i.*, p.name, p.brand, p.category, p.subcategory, p.image_url, p.unit, p.barcode, p.default_quantity, p.package_unit, COALESCE(i.vacuum_sealed, 0) as vacuum_sealed, i.opened_at, p.shopping_name FROM inventory i JOIN products p ON i.product_id = p.id