|
24 | 24 |
|
25 | 25 | use App\Entity\AssemblySystem\Assembly; |
26 | 26 | use App\Entity\Parts\Part; |
| 27 | +use Dompdf\Dompdf; |
| 28 | +use Dompdf\Options; |
| 29 | +use Twig\Environment; |
27 | 30 |
|
28 | 31 | class AssemblyPartAggregator |
29 | 32 | { |
| 33 | + public function __construct(private readonly Environment $twig) |
| 34 | + { |
| 35 | + } |
| 36 | + |
30 | 37 | /** |
31 | 38 | * Aggregate the required parts and their total quantities for an assembly. |
32 | 39 | * |
@@ -80,4 +87,181 @@ private function processAssembly(Assembly $assembly, float $multiplier, array &$ |
80 | 87 | } |
81 | 88 | } |
82 | 89 | } |
| 90 | + |
| 91 | + /** |
| 92 | + * Exports a hierarchical Bill of Materials (BOM) for assemblies and parts in a readable format, |
| 93 | + * including the multiplier for each part and assembly. |
| 94 | + * |
| 95 | + * @param Assembly $assembly The root assembly to export. |
| 96 | + * @param string $indentationSymbol The symbol used for indentation (e.g., ' '). |
| 97 | + * @param int $initialDepth The starting depth for formatting (default: 0). |
| 98 | + * @return string Human-readable hierarchical BOM list. |
| 99 | + */ |
| 100 | + public function exportReadableHierarchy(Assembly $assembly, string $indentationSymbol = ' ', int $initialDepth = 0): string |
| 101 | + { |
| 102 | + // Start building the hierarchy |
| 103 | + $output = ''; |
| 104 | + $this->processAssemblyHierarchy($assembly, $initialDepth, 1, $indentationSymbol, $output); |
| 105 | + |
| 106 | + return $output; |
| 107 | + } |
| 108 | + |
| 109 | + public function exportReadableHierarchyForPdf(array $assemblyHierarchies): string |
| 110 | + { |
| 111 | + $html = $this->twig->render('assemblies/export_bom_pdf.html.twig', [ |
| 112 | + 'assemblies' => $assemblyHierarchies, |
| 113 | + ]); |
| 114 | + |
| 115 | + $options = new Options(); |
| 116 | + $options->set('isHtml5ParserEnabled', true); |
| 117 | + $options->set('isPhpEnabled', true); |
| 118 | + |
| 119 | + $dompdf = new Dompdf($options); |
| 120 | + $dompdf->loadHtml($html); |
| 121 | + $dompdf->setPaper('A4'); |
| 122 | + $dompdf->render(); |
| 123 | + |
| 124 | + $canvas = $dompdf->getCanvas(); |
| 125 | + $font = $dompdf->getFontMetrics()->getFont('Arial', 'normal'); |
| 126 | + |
| 127 | + return $dompdf->output(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Recursive method to process assemblies and their parts. |
| 132 | + * |
| 133 | + * @param Assembly $assembly The current assembly to process. |
| 134 | + * @param int $depth The current depth in the hierarchy. |
| 135 | + * @param float $parentMultiplier The multiplier inherited from the parent (default is 1 for root). |
| 136 | + * @param string $indentationSymbol The symbol used for indentation. |
| 137 | + * @param string &$output The cumulative output string. |
| 138 | + */ |
| 139 | + private function processAssemblyHierarchy(Assembly $assembly, int $depth, float $parentMultiplier, string $indentationSymbol, string &$output): void |
| 140 | + { |
| 141 | + // Add the current assembly to the output |
| 142 | + if ($depth === 0) { |
| 143 | + $output .= sprintf( |
| 144 | + "%sAssembly: %s [IPN: %s]\n\n", |
| 145 | + str_repeat($indentationSymbol, $depth), |
| 146 | + $assembly->getName(), |
| 147 | + $assembly->getIpn(), |
| 148 | + ); |
| 149 | + } else { |
| 150 | + $output .= sprintf( |
| 151 | + "%sAssembly: %s [IPN: %s, Multiplier: %.2f]\n\n", |
| 152 | + str_repeat($indentationSymbol, $depth), |
| 153 | + $assembly->getName(), |
| 154 | + $assembly->getIpn(), |
| 155 | + $parentMultiplier |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + // Gruppiere BOM-Einträge in Kategorien |
| 160 | + $parts = []; |
| 161 | + $referencedAssemblies = []; |
| 162 | + $others = []; |
| 163 | + |
| 164 | + foreach ($assembly->getBomEntries() as $bomEntry) { |
| 165 | + if ($bomEntry->getPart() instanceof Part) { |
| 166 | + $parts[] = $bomEntry; |
| 167 | + } elseif ($bomEntry->getReferencedAssembly() instanceof Assembly) { |
| 168 | + $referencedAssemblies[] = $bomEntry; |
| 169 | + } else { |
| 170 | + $others[] = $bomEntry; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (!empty($parts)) { |
| 175 | + // Process each BOM entry for the current assembly |
| 176 | + foreach ($parts as $bomEntry) { |
| 177 | + $effectiveQuantity = $bomEntry->getQuantity() * $parentMultiplier; |
| 178 | + |
| 179 | + $output .= sprintf( |
| 180 | + "%sPart: %s [IPN: %s, MPNR: %s, Quantity: %.2f%s, EffectiveQuantity: %.2f]\n", |
| 181 | + str_repeat($indentationSymbol, $depth + 1), |
| 182 | + $bomEntry->getPart()?->getName(), |
| 183 | + $bomEntry->getPart()?->getIpn() ?? '-', |
| 184 | + $bomEntry->getPart()?->getManufacturerProductNumber() ?? '-', |
| 185 | + $bomEntry->getQuantity(), |
| 186 | + $parentMultiplier > 1 ? sprintf(", Multiplier: %.2f", $parentMultiplier) : '', |
| 187 | + $effectiveQuantity, |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + $output .= "\n"; |
| 192 | + } |
| 193 | + |
| 194 | + foreach ($referencedAssemblies as $bomEntry) { |
| 195 | + // Add referenced assembly details |
| 196 | + $referencedQuantity = $bomEntry->getQuantity() * $parentMultiplier; |
| 197 | + |
| 198 | + $output .= sprintf( |
| 199 | + "%sReferenced Assembly: %s [IPN: %s, Quantity: %.2f%s, EffectiveQuantity: %.2f]\n", |
| 200 | + str_repeat($indentationSymbol, $depth + 1), |
| 201 | + $bomEntry->getReferencedAssembly()->getName(), |
| 202 | + $bomEntry->getReferencedAssembly()->getIpn() ?? '-', |
| 203 | + $bomEntry->getQuantity(), |
| 204 | + $parentMultiplier > 1 ? sprintf(", Multiplier: %.2f", $parentMultiplier) : '', |
| 205 | + $referencedQuantity, |
| 206 | + ); |
| 207 | + |
| 208 | + // Recurse into the referenced assembly |
| 209 | + $this->processAssemblyHierarchy( |
| 210 | + $bomEntry->getReferencedAssembly(), |
| 211 | + $depth + 2, // Increase depth for nested assemblies |
| 212 | + $referencedQuantity, // Pass the calculated multiplier |
| 213 | + $indentationSymbol, |
| 214 | + $output |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + foreach ($others as $bomEntry) { |
| 219 | + $output .= sprintf( |
| 220 | + "%sOther: %s [Quantity: %.2f, Multiplier: %.2f]\n", |
| 221 | + str_repeat($indentationSymbol, $depth + 1), |
| 222 | + $bomEntry->getName(), |
| 223 | + $bomEntry->getQuantity(), |
| 224 | + $parentMultiplier, |
| 225 | + ); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + public function processAssemblyHierarchyForPdf(Assembly $assembly, int $depth, float $quantity, float $parentMultiplier): array |
| 230 | + { |
| 231 | + $result = [ |
| 232 | + 'name' => $assembly->getName(), |
| 233 | + 'ipn' => $assembly->getIpn(), |
| 234 | + 'quantity' => $quantity, |
| 235 | + 'multiplier' => $depth === 0 ? null : $parentMultiplier, |
| 236 | + 'parts' => [], |
| 237 | + 'referencedAssemblies' => [], |
| 238 | + 'others' => [], |
| 239 | + ]; |
| 240 | + |
| 241 | + foreach ($assembly->getBomEntries() as $bomEntry) { |
| 242 | + if ($bomEntry->getPart() instanceof Part) { |
| 243 | + $result['parts'][] = [ |
| 244 | + 'name' => $bomEntry->getPart()->getName(), |
| 245 | + 'ipn' => $bomEntry->getPart()->getIpn(), |
| 246 | + 'quantity' => $bomEntry->getQuantity(), |
| 247 | + 'effectiveQuantity' => $bomEntry->getQuantity() * $parentMultiplier, |
| 248 | + ]; |
| 249 | + } elseif ($bomEntry->getReferencedAssembly() instanceof Assembly) { |
| 250 | + $result['referencedAssemblies'][] = $this->processAssemblyHierarchyForPdf( |
| 251 | + $bomEntry->getReferencedAssembly(), |
| 252 | + $depth + 1, |
| 253 | + $bomEntry->getQuantity(), |
| 254 | + $parentMultiplier * $bomEntry->getQuantity() |
| 255 | + ); |
| 256 | + } else { |
| 257 | + $result['others'][] = [ |
| 258 | + 'name' => $bomEntry->getName(), |
| 259 | + 'quantity' => $bomEntry->getQuantity(), |
| 260 | + 'multiplier' => $parentMultiplier, |
| 261 | + ]; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + return $result; |
| 266 | + } |
83 | 267 | } |
0 commit comments