query($sql); $products = []; if ($resql) { $mapper = new dmProduct(); while ($obj = $db->fetch_object($resql)) { $product = new \Product($db); $product->fetch($obj->rowid); $products[] = $mapper->exportMappedData($product); } } return [['products' => $products], 200]; } /** * Détail d'un produit */ public function show($payload = null) { global $db; $id = $payload['id'] ?? null; if (!$id) { return ['ID required', 400]; } require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $product = new \Product($db); $res = $product->fetch($id); if ($res <= 0) { return ['Product not found', 404]; } // Charger les extrafields $product->fetch_optionals(); $mapper = new dmProduct(); $data = $mapper->exportMappedData($product); return [$data, 200]; } /** * Créer un produit */ public function create($payload = null) { global $db, $user; // Validation if (empty($payload['label'])) { return ['Label required', 400]; } require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $product = new \Product($db); $product->ref = $payload['ref'] ?? ''; $product->label = $payload['label']; $product->description = $payload['description'] ?? ''; $product->price = $payload['price'] ?? 0; $product->status = $payload['status'] ?? 1; // Créer en base $res = $product->create($user); if ($res < 0) { return ['Error creating product: ' . $product->error, 500]; } // Sauvegarder les extrafields if (!empty($payload['extrafields'])) { foreach ($payload['extrafields'] as $key => $value) { $product->array_options['options_' . $key] = $value; } $product->insertExtraFields(); } return [['id' => $res], 201]; } /** * Mettre à jour un produit */ public function update($payload = null) { global $db, $user; $id = $payload['id'] ?? null; if (!$id) { return ['ID required', 400]; } require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $product = new \Product($db); $res = $product->fetch($id); if ($res <= 0) { return ['Product not found', 404]; } // Mettre à jour les champs fournis if (isset($payload['label'])) { $product->label = $payload['label']; } if (isset($payload['description'])) { $product->description = $payload['description']; } if (isset($payload['price'])) { $product->price = $payload['price']; } if (isset($payload['status'])) { $product->status = $payload['status']; } $res = $product->update($product->id, $user); if ($res < 0) { return ['Error updating product: ' . $product->error, 500]; } // Mettre à jour les extrafields if (!empty($payload['extrafields'])) { $product->fetch_optionals(); foreach ($payload['extrafields'] as $key => $value) { $product->array_options['options_' . $key] = $value; } $product->updateExtraFields(); } return ['Updated', 200]; } /** * Supprimer un produit */ public function delete($payload = null) { global $db, $user; $id = $payload['id'] ?? null; if (!$id) { return ['ID required', 400]; } require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $product = new \Product($db); $res = $product->fetch($id); if ($res <= 0) { return ['Product not found', 404]; } // Vérifier les droits if (!$user->hasRight('produit', 'supprimer')) { return ['Permission denied', 403]; } $res = $product->delete($user); if ($res < 0) { return ['Error deleting product: ' . $product->error, 500]; } return ['Deleted', 200]; } }