Files
fewo-stommer/app/Process/Tester/Scenarios/PortalBooking.php
T

86 lines
4.3 KiB
PHP

<?php
namespace App\Process\Tester\Scenarios;
use App\Actions\ContributeCaseFacts;
use App\Actions\ProcessPortalMail;
use App\Actions\RecordPortalAmounts;
use App\Channels\Mail\PortalMail;
use App\Enums\FactSource;
use App\Enums\IntakeKind;
use App\Models\Booking;
use App\Process\Tester\SandboxContext;
use Carbon\CarbonImmutable;
/**
* S07/S08/S13: a booking binding at the portal arrives by e-mail through the same entry as the mailbox (twice:
* duplicate), guest details and the portal's amounts follow; optionally the portal reports a cancellation; then the
* stay and after departure.
*/
final class PortalBooking extends Scenario
{
public static function key(): string
{
return 'portal_booking';
}
public static function label(): string
{
return 'Feste Buchung über das Portal';
}
public static function description(): string
{
return 'Die Buchungsmail des Portals kommt (zweimal) an, Gastdaten und Portalbeträge folgen; auf Wunsch meldet das Portal eine Stornierung.';
}
public static function intakes(): array
{
return [IntakeKind::Booking];
}
public static function inputs(): array
{
return [
...parent::inputs(),
'portal_total' => ['label' => 'Gesamtpreis laut Portal (€)', 'default' => 980, 'type' => 'int'],
'cancellation' => ['label' => 'Portal meldet eine Stornierung', 'default' => false, 'type' => 'bool'],
];
}
public function events(array $inputs): array
{
$total = max(1, (int) ($inputs['portal_total'] ?? 980)) * 100;
$events = [
new ScenarioEvent('Buchungsmail des Portals kommt an', fn (SandboxContext $context) => $this->sendPortalMail($context, 'Neue Buchung: '.$context->property->name, $this->text($context))),
new ScenarioEvent('Dieselbe Buchungsmail kommt noch einmal', fn (SandboxContext $context) => $this->sendPortalMail($context, 'Neue Buchung: '.$context->property->name, $this->text($context))),
new ScenarioEvent('Gastdaten laut Portal', fn (SandboxContext $context) => app(ContributeCaseFacts::class)->handle($context->booking->fresh(), ['guest.first_name' => 'Testgast', 'guest.last_name' => 'Portal', 'guest.email' => 'portalgast@tester.invalid', 'booking.guests' => (string) $context->guests()], FactSource::PortalMail)),
new ScenarioEvent('Portal meldet Preis, Gastzahlung und angekündigte Auszahlung', fn (SandboxContext $context) => app(RecordPortalAmounts::class)->handle($context->booking->fresh(), $total, [['kind' => 'payout_announced', 'amount' => (int) round($total * 0.9)]], [['amount' => (int) round($total * 0.4), 'paid_on' => now()->toDateString()]], $context->channel->name)),
];
if ((bool) ($inputs['cancellation'] ?? false)) {
$events[] = new ScenarioEvent('Portal meldet eine Stornierung', fn (SandboxContext $context) => $this->sendPortalMail($context, 'Buchung storniert: '.$this->reference($context), 'Die Buchung vom '.$context->arrival()->format('d.m.Y').' bis '.$context->departure()->format('d.m.Y').' wurde storniert. Buchungsnummer: '.$this->reference($context)));
}
$events[] = new ScenarioEvent('Drei Tage nach der Abreise', fn (SandboxContext $context) => $context->travelTo($context->departure()->addDays(3)->setTime(9, 0)));
return $events;
}
private function reference(SandboxContext $context): string
{
return 'TEST-'.$context->arrival()->format('md');
}
private function text(SandboxContext $context): string
{
return "Gast: Testgast Portal\nAnreise: ".$context->arrival()->format('d.m.Y')."\nAbreise: ".$context->departure()->format('d.m.Y')."\nBuchungsnummer: ".$this->reference($context);
}
private function sendPortalMail(SandboxContext $context, string $subject, string $text): void
{
$senders = array_values(array_filter(array_map('trim', explode(',', (string) ($context->channel->setting('email_senders') ?? '')))));
$sender = 'buchung@'.($senders[0] ?? 'portal.invalid');
app(ProcessPortalMail::class)->handle(new PortalMail('<'.md5($subject).'@tester.invalid>', $sender, $subject, $text, CarbonImmutable::now()));
$context->booking = Booking::query()->latest('id')->first() ?? $context->booking;
}
}