diff --git a/src/Command/UpdateDestinationCostUnitsCommand.php b/src/Command/UpdateDestinationCostUnitsCommand.php new file mode 100644 index 0000000..18f3412 --- /dev/null +++ b/src/Command/UpdateDestinationCostUnitsCommand.php @@ -0,0 +1,134 @@ +addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + 'Run without persisting changes to the database' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $isDryRun = true === $input->getOption('dry-run'); + + if ($isDryRun) { + $io->note('Running in dry-run mode. No changes will be persisted.'); + } + + $qb = $this->destinationRepository->createQueryBuilder('d'); + $destinations = $qb + ->where($qb->expr()->isNotNull('d.hotelBusProId')) + ->andWhere($qb->expr()->orX( + $qb->expr()->isNull('d.costUnit'), + $qb->expr()->eq('d.costUnit', ':emptyString') + )) + ->andWhere($qb->expr()->isNull('d.deletedAt')) + ->setParameter('emptyString', '') + ->getQuery() + ->getResult() + ; + + $totalCount = count($destinations); + + if (0 === $totalCount) { + $io->success('No destinations found that need cost unit updates.'); + + return Command::SUCCESS; + } + + $io->info(sprintf('Found %d destination(s) with assigned hotels and empty cost units.', $totalCount)); + + $updatedCount = 0; + $skippedCount = 0; + + foreach ($destinations as $destination) { + $hotelBusProId = $destination->getHotelBusProId(); + $hotel = $this->hotelDataProvider->get($hotelBusProId); + + if (null === $hotel) { + $io->warning(sprintf( + 'Hotel with BusProId %d not found for destination #%d', + $hotelBusProId, + $destination->getId() + )); + ++$skippedCount; + continue; + } + + $costUnit = $hotel->getCostUnit(); + + if (null === $costUnit || '' === $costUnit) { + $io->warning(sprintf( + 'Hotel "%s" (BusProId: %d) has no cost unit. Skipping destination #%d', + $hotel->getName(), + $hotelBusProId, + $destination->getId() + )); + ++$skippedCount; + continue; + } + + $destination->setCostUnit($costUnit); + ++$updatedCount; + + $io->writeln(sprintf( + 'Updated destination #%d (%s) with cost unit "%s" from hotel "%s"', + $destination->getId(), + $destination->getProduct(), + $costUnit, + $hotel->getName() + )); + } + + if (false === $isDryRun && $updatedCount > 0) { + $this->entityManager->flush(); + $io->success(sprintf( + 'Successfully updated %d destination(s). Skipped %d destination(s).', + $updatedCount, + $skippedCount + )); + } elseif ($isDryRun && $updatedCount > 0) { + $io->success(sprintf( + 'Dry-run complete. Would update %d destination(s). Would skip %d destination(s).', + $updatedCount, + $skippedCount + )); + } else { + $io->info(sprintf('No destinations updated. Skipped %d destination(s).', $skippedCount)); + } + + return Command::SUCCESS; + } +} diff --git a/src/Controller/Administrative/System/Destination/DestinationTrait.php b/src/Controller/Administrative/System/Destination/DestinationTrait.php index 5579ebc..74a4aa4 100644 --- a/src/Controller/Administrative/System/Destination/DestinationTrait.php +++ b/src/Controller/Administrative/System/Destination/DestinationTrait.php @@ -38,7 +38,7 @@ trait DestinationTrait ->setHotelBusProId($hotel->getBusProId()) ->setCountry($destinationDto->getCountry()) ->setPickups($pickups) - ->setCostUnit($destinationDto->getCostUnit()) + ->setCostUnit($hotel->getCostUnit()) ; } } diff --git a/src/Form/DestinationType.php b/src/Form/DestinationType.php index 3c22e45..287c560 100644 --- a/src/Form/DestinationType.php +++ b/src/Form/DestinationType.php @@ -38,9 +38,6 @@ class DestinationType extends AbstractType 'allow_add' => true, 'allow_delete' => true, ]) - ->add('costUnit', TextType::class, [ - 'label' => 'Kostenstelle', - ]) ->add('country', ChoiceType::class, [ 'label' => 'Land', 'choices' => [ diff --git a/src/Model/DestinationDto.php b/src/Model/DestinationDto.php index 742a565..c2a9179 100644 --- a/src/Model/DestinationDto.php +++ b/src/Model/DestinationDto.php @@ -24,8 +24,6 @@ class DestinationDto private array $pickups = []; - private ?string $costUnit = null; - public static function fromEntity(Destination $destination): static { $instance = new static(); @@ -41,7 +39,6 @@ class DestinationDto ->setHotelBusProId($destination->getHotelBusProId()) ->setCountry($destination->getCountry()) ->setPickups($pickupIds) - ->setCostUnit($destination->getCostUnit()) ; return $instance; @@ -118,16 +115,4 @@ class DestinationDto return $this; } - - public function getCostUnit(): ?string - { - return $this->costUnit; - } - - public function setCostUnit(?string $costUnit): static - { - $this->costUnit = $costUnit; - - return $this; - } } diff --git a/templates/administrative/system/destination/_form.html.twig b/templates/administrative/system/destination/_form.html.twig index 1afcb50..80e3ea3 100644 --- a/templates/administrative/system/destination/_form.html.twig +++ b/templates/administrative/system/destination/_form.html.twig @@ -19,7 +19,6 @@
{{ form_row(form.dateFrom) }} {{ form_row(form.dateTo) }} - {{ form_row(form.costUnit) }}
{{ form_row(form.country) }}