|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 7 | + * |
| 8 | + * Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics) |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as published |
| 12 | + * by the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +namespace App\Helpers\Assemblies; |
| 24 | + |
| 25 | +use App\Entity\AssemblySystem\Assembly; |
| 26 | +use App\Entity\Parts\Part; |
| 27 | + |
| 28 | +class AssemblyPartAggregator |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Aggregate the required parts and their total quantities for an assembly. |
| 32 | + * |
| 33 | + * @param Assembly $assembly The assembly to process. |
| 34 | + * @param float $multiplier The quantity multiplier from the parent assembly. |
| 35 | + * @return array Array of parts with their aggregated quantities, keyed by Part ID. |
| 36 | + */ |
| 37 | + public function getAggregatedParts(Assembly $assembly, float $multiplier): array |
| 38 | + { |
| 39 | + $aggregatedParts = []; |
| 40 | + |
| 41 | + // Start processing the assembly recursively |
| 42 | + $this->processAssembly($assembly, $multiplier, $aggregatedParts); |
| 43 | + |
| 44 | + // Return the final aggregated list of parts |
| 45 | + return $aggregatedParts; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Recursive helper to process an assembly and all its BOM entries. |
| 50 | + * |
| 51 | + * @param Assembly $assembly The current assembly to process. |
| 52 | + * @param float $multiplier The quantity multiplier from the parent assembly. |
| 53 | + * @param array &$aggregatedParts The array to accumulate parts and their quantities. |
| 54 | + */ |
| 55 | + private function processAssembly(Assembly $assembly, float $multiplier, array &$aggregatedParts): void |
| 56 | + { |
| 57 | + foreach ($assembly->getBomEntries() as $bomEntry) { |
| 58 | + // If the BOM entry refers to a part, add its quantity |
| 59 | + if ($bomEntry->getPart() instanceof Part) { |
| 60 | + $part = $bomEntry->getPart(); |
| 61 | + |
| 62 | + if (!isset($aggregatedParts[$part->getId()])) { |
| 63 | + $aggregatedParts[$part->getId()] = [ |
| 64 | + 'part' => $part, |
| 65 | + 'assembly' => $assembly, |
| 66 | + 'quantity' => $bomEntry->getQuantity(), |
| 67 | + 'multiplier' => $multiplier, |
| 68 | + ]; |
| 69 | + } |
| 70 | + } elseif ($bomEntry->getReferencedAssembly() instanceof Assembly) { |
| 71 | + // If the BOM entry refers to another assembly, process it recursively |
| 72 | + $this->processAssembly($bomEntry->getReferencedAssembly(), $bomEntry->getQuantity(), $aggregatedParts); |
| 73 | + } else { |
| 74 | + $aggregatedParts[] = [ |
| 75 | + 'part' => null, |
| 76 | + 'assembly' => $assembly, |
| 77 | + 'quantity' => $bomEntry->getQuantity(), |
| 78 | + 'multiplier' => $multiplier, |
| 79 | + ]; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments