120 lines
3.5 KiB
PHP
120 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\BusProNet\Model\Booking;
|
|
use App\Repository\BookingEditDraftRepository;
|
|
use App\Service\BookingEditDataLoaderService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
#[AsCommand(
|
|
name: 'app:draft:backfill-booking-number',
|
|
description: 'Backfill booking numbers (vorgang) for existing drafts from API',
|
|
)]
|
|
class DraftBackfillBookingNumberCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly BookingEditDraftRepository $draftRepository,
|
|
private readonly BookingEditDataLoaderService $dataLoader,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show what would be updated without actually updating');
|
|
}
|
|
|
|
/**
|
|
* @see Command::execute()
|
|
*/
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
$dryRun = $input->getOption('dry-run');
|
|
|
|
$drafts = $this->draftRepository->findBy(['bookingNumber' => null]);
|
|
|
|
if (0 === \count($drafts)) {
|
|
$io->success('No drafts without booking number found.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$io->title(sprintf('Processing %d draft(s)', \count($drafts)));
|
|
|
|
$updated = 0;
|
|
$failed = 0;
|
|
$skipped = 0;
|
|
|
|
foreach ($drafts as $draft) {
|
|
$bookingId = $draft->getBookingId();
|
|
$user = $draft->getUser();
|
|
|
|
$bookingData = $this->dataLoader->fetchBookingData($bookingId, $user);
|
|
|
|
if (!$bookingData instanceof Booking) {
|
|
$io->warning(sprintf('Could not fetch booking %d for user %s', $bookingId, $user->getEmail()));
|
|
++$failed;
|
|
|
|
continue;
|
|
}
|
|
|
|
// bookingNumber maps to BPN XML vorgang
|
|
$bookingNumber = $bookingData->bookingNumber;
|
|
|
|
if (null === $bookingNumber) {
|
|
$io->warning(sprintf('No booking number (vorgang) found for booking %d', $bookingId));
|
|
++$skipped;
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($dryRun) {
|
|
$io->text(sprintf(
|
|
'[DRY RUN] Would set booking number %d for draft %d (bookingId %d)',
|
|
$bookingNumber,
|
|
$draft->getId(),
|
|
$bookingId
|
|
));
|
|
} else {
|
|
$draft->setBookingNumber($bookingNumber);
|
|
}
|
|
|
|
++$updated;
|
|
}
|
|
|
|
if (false === $dryRun) {
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
$io->newLine();
|
|
$io->table(
|
|
['Status', 'Count'],
|
|
[
|
|
['Updated', $updated],
|
|
['Failed (API error)', $failed],
|
|
['Skipped (no booking number)', $skipped],
|
|
]
|
|
);
|
|
|
|
if ($dryRun) {
|
|
$io->note('Dry run - no changes were made.');
|
|
} else {
|
|
$io->success(sprintf('Updated %d draft(s).', $updated));
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|