[ 'label' => 'Kunde godkendte ekstra arbejde', 'requires_comment' => true, 'active' => true, ], 'vehicle_condition_extra_work' => [ 'label' => 'Køretøjets tilstand krævede ekstra tid', 'requires_comment' => true, 'active' => true, ], 'quality_rework' => [ 'label' => 'Kvalitetsopfølgning eller omvask', 'requires_comment' => true, 'active' => true, ], 'legacy_note_only' => [ 'label' => 'Legacy note only', 'requires_comment' => true, 'active' => false, ], ]; } public static function productRequiresReason(int $productId): bool { return in_array($productId, self::AFFECTED_PRODUCT_IDS, true); } public static function validateForProduct(int $productId, array $data): array { if (!self::productRequiresReason($productId)) { return ['reason_code' => null, 'reason_label_snapshot' => null, 'reason_comment' => null]; } $code = trim((string)($data['reason_code'] ?? $data['order_item_reason_code'] ?? '')); if ($code === '') { throw new InvalidArgumentException('Reason code is required for this product'); } $reasons = self::reasons(); if (!array_key_exists($code, $reasons)) { throw new InvalidArgumentException('Reason code is invalid for this product'); } $reason = $reasons[$code]; if (!$reason['active']) { throw new InvalidArgumentException('Reason code is deprecated for this product'); } $comment = trim((string)($data['reason_comment'] ?? $data['comment'] ?? $data['notes'] ?? '')); if ($reason['requires_comment'] && $comment === '') { throw new InvalidArgumentException('Reason comment is required for this product'); } $snapshot = $reason['label']; return ['reason_code' => $code, 'reason_label_snapshot' => $snapshot, 'reason_comment' => $comment]; } }