diff --git a/src/Controller/MachineStructureController.php b/src/Controller/MachineStructureController.php index ed0a909..70d9250 100644 --- a/src/Controller/MachineStructureController.php +++ b/src/Controller/MachineStructureController.php @@ -656,26 +656,15 @@ class MachineStructureController extends AbstractController } $composant = $parentLink->getComposant(); - $structure = $composant->getStructure(); + $piece = $link->getPiece(); - if (!is_array($structure) || !isset($structure['pieces']) || !is_array($structure['pieces'])) { - return 1; - } - - $piece = $link->getPiece(); - $typePiece = $piece->getTypePiece(); - $typePieceId = $typePiece?->getId(); - - foreach ($structure['pieces'] as $pieceDef) { - if (!is_array($pieceDef)) { - continue; - } - if (isset($pieceDef['typePieceId']) && $pieceDef['typePieceId'] === $typePieceId) { - return (int) ($pieceDef['quantity'] ?? 1); + foreach ($composant->getPieceSlots() as $slot) { + if ($slot->getSelectedPiece()?->getId() === $piece->getId()) { + return $slot->getQuantity(); } } - return 1; + return $link->getQuantity(); } private function normalizeProductLinks(array $links): array @@ -709,7 +698,7 @@ class MachineStructureController extends AbstractController 'typeComposant' => $this->normalizeModelType($type), 'productId' => $composant->getProduct()?->getId(), 'product' => $composant->getProduct() ? $this->normalizeProduct($composant->getProduct()) : null, - 'structure' => $this->enrichStructureWithPieceData($composant->getStructure()), + 'structure' => $this->buildStructureFromSlots($composant), 'constructeurs' => $this->normalizeConstructeurs($composant->getConstructeurs()), 'documents' => [], 'customFields' => $type ? $this->normalizeCustomFieldDefinitions($type->getComponentCustomFields()) : [], @@ -717,26 +706,45 @@ class MachineStructureController extends AbstractController ]; } - private function enrichStructureWithPieceData(?array $structure): ?array + private function buildStructureFromSlots(Composant $composant): array { - if (!is_array($structure) || !isset($structure['pieces']) || !is_array($structure['pieces'])) { - return $structure; + $pieces = []; + foreach ($composant->getPieceSlots() as $slot) { + $pieceData = [ + 'typePieceId' => $slot->getTypePiece()?->getId(), + 'quantity' => $slot->getQuantity(), + 'selectedPieceId' => $slot->getSelectedPiece()?->getId(), + ]; + if ($slot->getSelectedPiece()) { + $pieceData['resolvedPiece'] = $this->normalizePiece($slot->getSelectedPiece()); + } + $pieces[] = $pieceData; } - foreach ($structure['pieces'] as &$entry) { - if (!is_array($entry)) { - continue; - } - $selectedId = $entry['selectedPieceId'] ?? null; - if ($selectedId) { - $piece = $this->pieceRepository->find($selectedId); - if ($piece instanceof Piece) { - $entry['resolvedPiece'] = $this->normalizePiece($piece); - } - } + $subcomponents = []; + foreach ($composant->getSubcomponentSlots() as $slot) { + $subcomponents[] = [ + 'alias' => $slot->getAlias(), + 'familyCode' => $slot->getFamilyCode(), + 'typeComposantId' => $slot->getTypeComposant()?->getId(), + 'selectedComponentId' => $slot->getSelectedComposant()?->getId(), + ]; } - return $structure; + $products = []; + foreach ($composant->getProductSlots() as $slot) { + $products[] = [ + 'typeProductId' => $slot->getTypeProduct()?->getId(), + 'familyCode' => $slot->getFamilyCode(), + 'selectedProductId' => $slot->getSelectedProduct()?->getId(), + ]; + } + + return [ + 'pieces' => $pieces, + 'subcomponents' => $subcomponents, + 'products' => $products, + ]; } private function normalizePiece(Piece $piece): array diff --git a/src/Entity/Piece.php b/src/Entity/Piece.php index d73017d..81684c7 100644 --- a/src/Entity/Piece.php +++ b/src/Entity/Piece.php @@ -212,9 +212,13 @@ class Piece { $this->product = $product; - if ($product && empty($this->productIds)) { - $productId = $product->getId(); - $this->productIds = $productId ? [$productId] : null; + if (null !== $product) { + $this->addProduct($product); + + if (empty($this->productIds)) { + $productId = $product->getId(); + $this->productIds = $productId ? [$productId] : null; + } } if (!$product && empty($this->productIds)) { @@ -229,19 +233,7 @@ class Piece */ public function getProductIds(): array { - if (!is_array($this->productIds)) { - return []; - } - - return array_values( - array_filter( - array_map( - static fn ($value) => is_string($value) ? trim($value) : '', - $this->productIds, - ), - static fn (string $value) => '' !== $value, - ), - ); + return $this->products->map(fn (Product $p) => $p->getId())->toArray(); } public function setProductIds(?array $productIds): static