Portalbuchungen und -anfragen aus Kalender und Mail werden ueber PortalIntake zum Vorgang (nur bei sicherer Erkennung), Dubletten ueber Buchungsnummer, gleiches Portal mit gleichen Naechten und Spiegelung; Unsicheres wird geklaert. Portalstorno und -aenderung als Aufgabe am Vorgang ohne stille Stornierung. Schalter je Kanal (Aus als Standard, Beobachten, Anlegen, Anlegen + weiter), Eingangsprotokoll, Bestandsuebernahme mit Vorschau/Bericht/Ruecknahme, Sperrnachweis in drei Stufen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
368 lines
20 KiB
PHP
368 lines
20 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Actions\ProcessPortalMail;
|
||
use App\Actions\ProvideGuestContact;
|
||
use App\Actions\SyncChannel;
|
||
use App\Actions\TakeOverExternalBooking;
|
||
use App\Channels\Mail\PortalMail;
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Enums\CaseIntakeMode;
|
||
use App\Enums\CaseType;
|
||
use App\Enums\ChannelTaskProof;
|
||
use App\Enums\IntakeKind;
|
||
use App\Enums\IntakeOutcome;
|
||
use App\Enums\ProcessingMode;
|
||
use App\Filament\Resources\ChannelTasks\Pages\ListChannelTasks;
|
||
use App\Filament\Resources\PortalOccupations\Pages\ViewPortalOccupation;
|
||
use App\Mail\BookingConfirmed;
|
||
use App\Mail\BookingDocumentation;
|
||
use App\Mail\OfferMade;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Models\Channel;
|
||
use App\Models\ChannelTask;
|
||
use App\Models\IntakeObservation;
|
||
use App\Models\Property;
|
||
use App\Models\User;
|
||
use App\Process\CaseActivities;
|
||
use App\Process\CaseAutomation;
|
||
use App\Process\WorkList;
|
||
use App\Support\Settings;
|
||
use App\Tenancy\TenantContext;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\PropertySeeder;
|
||
use Database\Seeders\SiteSeeder;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Livewire\Livewire;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Package 4 of UMSETZUNGSPLAN_v0.10: one intake for all channels – bookings and requests from portals become cases
|
||
* without a manual "file", the same booking arriving by mail and calendar stays one case, uncertain entries are
|
||
* clarified, portal reports reach the case as tasks, portal blocks state their proof, and nothing happens on a channel
|
||
* that was not switched on.
|
||
*/
|
||
class CaseIntakeTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private const BOOKING_MAIL = "Neue Buchung für Ferienwohnung Stommer\nAnreise: 07.12.2026\nAbreise: 14.12.2026\nBuchungsnummer: HA-7X3K9Q";
|
||
|
||
private Property $property;
|
||
|
||
private Channel $fewo;
|
||
|
||
/** What the portal calendars deliver right now. */
|
||
private string $currentFeed = '';
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-11-02 10:00'));
|
||
$this->seed([PropertySeeder::class, SiteSeeder::class]);
|
||
$this->property = Property::sole();
|
||
app(TenantContext::class)->set($this->property->tenant);
|
||
$this->fewo = Channel::factory()->for($this->property)->create([
|
||
'name' => 'FeWo-direkt',
|
||
'booking_model' => 'portal_booking',
|
||
'case_intake' => CaseIntakeMode::Create,
|
||
'settings' => ['import_url' => 'https://portal.example/fewo.ics', 'email_senders' => 'fewo-direkt.de'],
|
||
'process_overrides' => ['booking' => ['payments' => 'portal']],
|
||
]);
|
||
$this->fewo->forceFill(['ical_active_rules' => [self::reservationRule()]])->save();
|
||
Mail::fake();
|
||
Storage::fake('local');
|
||
Http::fake(fn () => Http::response($this->currentFeed));
|
||
$this->withoutVite();
|
||
}
|
||
|
||
/** 4.2 */
|
||
public function test_a_booking_mail_becomes_a_case_and_the_later_calendar_entry_joins_it(): void
|
||
{
|
||
$this->mail('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
$case = Booking::query()->sole();
|
||
$this->assertSame([BookingStatus::Confirmed, IntakeKind::Booking, CaseType::A4, 'HA-7X3K9Q', $this->fewo->id], [$case->status, $case->intake, $case->case_type, $case->portal_reference, $case->origin_channel_id]);
|
||
$this->assertNull($case->guest_id);
|
||
$this->assertNull($case->total);
|
||
$this->assertNull($case->guests);
|
||
$this->assertSame(ProcessingMode::Manual, $case->processing_mode);
|
||
$this->assertSame(0, $case->offer_version);
|
||
$this->assertSame('Automatisch aus der Buchungsmail von FeWo-direkt angelegt', $case->events()->where('type', 'intake')->value('summary'));
|
||
|
||
$this->sync($this->fewo, self::feed('evt-1', 'Reservation: Nora Beispiel', 'Buchung HA-7X3K9Q', '20261207', '20261214'));
|
||
|
||
$this->assertSame(1, Booking::query()->count());
|
||
$entry = $this->fewo->blockedPeriods()->where('source', 'channel')->sole();
|
||
$this->assertTrue($entry->is_representation);
|
||
$this->assertSame($case->id, $entry->linked_booking_id);
|
||
$this->assertStringContainsString('Weitere Quelle zugeordnet', (string) $case->events()->where('type', 'intake')->reorder()->orderByDesc('id')->value('summary'));
|
||
|
||
// The portal collects the money: no payment tasks of our own; nothing went to the guest.
|
||
$this->assertFalse($case->fresh()->wantsPaymentTasks());
|
||
$this->assertSame('läuft über das Portal', collect(CaseActivities::for($case->fresh()))->firstWhere('key', 'payment')?->detail);
|
||
Mail::assertNotSent(BookingDocumentation::class);
|
||
Mail::assertNotSent(BookingConfirmed::class);
|
||
}
|
||
|
||
public function test_a_calendar_reservation_becomes_one_case_however_often_the_calendar_is_read(): void
|
||
{
|
||
$feed = self::feed('evt-2', 'Reservation: Nora Beispiel', 'Buchung HA-AAAAA1', '20261207', '20261214');
|
||
|
||
$this->sync($this->fewo, $feed);
|
||
$this->sync($this->fewo, $feed);
|
||
$this->sync($this->fewo, $feed);
|
||
|
||
$case = Booking::query()->sole();
|
||
$this->assertSame('HA-AAAAA1', $case->portal_reference);
|
||
$this->assertStringContainsString('Name laut Portal: Nora Beispiel', (string) $case->internal_notes);
|
||
$this->assertNull($case->guest_id, 'a name in a calendar is no guest identity');
|
||
$this->assertSame(1, IntakeObservation::query()->where('outcome', IntakeOutcome::Create)->count());
|
||
}
|
||
|
||
public function test_the_same_portal_with_the_same_nights_joins_the_existing_case(): void
|
||
{
|
||
$case = app(TakeOverExternalBooking::class)->handle($this->property, $this->fewo, CarbonImmutable::parse('2026-12-07'), CarbonImmutable::parse('2026-12-14'), ['portal_total' => 90000]);
|
||
|
||
$this->sync($this->fewo, self::feed('evt-3', 'Reservation: Nora Beispiel', '', '20261207', '20261214'));
|
||
|
||
$this->assertSame(1, Booking::query()->count());
|
||
$this->assertSame($case->id, $this->fewo->blockedPeriods()->sole()->linked_booking_id);
|
||
}
|
||
|
||
/** 4.3 */
|
||
public function test_an_unrecognised_calendar_entry_is_clarified_and_the_answer_booking_creates_the_case(): void
|
||
{
|
||
$this->sync($this->fewo, self::feed('evt-4', 'Blocked', '', '20261207', '20261214'));
|
||
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertContains('Belegung von FeWo-direkt klären', array_column(WorkList::items(), 'title'));
|
||
$observation = IntakeObservation::query()->sole();
|
||
$this->assertSame(IntakeOutcome::Clarify, $observation->outcome);
|
||
$this->assertStringContainsString('Belegt-Meldung ist kein Nachweis', implode(' ', $observation->reasons));
|
||
|
||
$this->actingAs(User::factory()->create());
|
||
Livewire::test(ViewPortalOccupation::class, ['record' => $this->fewo->blockedPeriods()->sole()->id])
|
||
->callAction('clarify', ['answer' => 'booking'])
|
||
->assertRedirect();
|
||
|
||
$this->assertSame(IntakeKind::Booking, Booking::query()->sole()->intake);
|
||
}
|
||
|
||
public function test_entries_that_are_no_booking_of_this_portal_never_become_cases(): void
|
||
{
|
||
$other = Channel::factory()->for($this->property)->create(['name' => 'Ostseeklar', 'case_intake' => CaseIntakeMode::Create, 'settings' => ['import_url' => 'https://portal.example/other.ics']]);
|
||
$other->forceFill(['ical_active_rules' => [[...self::reservationRule(), 'origin' => 'channel:'.$this->fewo->id]]])->save();
|
||
|
||
// Reservation booked at another portal, shown in this calendar.
|
||
$this->sync($other, self::feed('evt-5', 'Reservation: Nora Beispiel', '', '20261207', '20261214'));
|
||
// A reservation overlapping a case without being the same booking.
|
||
app(TakeOverExternalBooking::class)->handle($this->property, $this->fewo, CarbonImmutable::parse('2027-01-10'), CarbonImmutable::parse('2027-01-17'));
|
||
$this->sync($this->fewo, self::feed('evt-6', 'Reservation: Max Muster', 'Buchung HA-BBBBB2', '20270112', '20270119'));
|
||
|
||
$this->assertSame(1, Booking::query()->count());
|
||
$this->assertTrue($this->fewo->blockedPeriods()->where('external_uid', 'evt-6')->sole()->needs_review);
|
||
$this->assertSame(0, IntakeObservation::query()->where('outcome', IntakeOutcome::Create)->count());
|
||
}
|
||
|
||
/** 4.4 */
|
||
public function test_a_cancellation_mail_reaches_the_case_as_a_task_and_cancels_nothing(): void
|
||
{
|
||
$this->mail('Neue Buchung', self::BOOKING_MAIL);
|
||
$case = Booking::query()->sole();
|
||
|
||
$this->mail('Buchung storniert: HA-7X3K9Q', 'Die Buchung vom 07.12.2026 bis 14.12.2026 wurde storniert.', 'msg-2');
|
||
$this->mail('Buchung storniert: HA-7X3K9Q', 'Die Buchung vom 07.12.2026 bis 14.12.2026 wurde storniert.', 'msg-3');
|
||
|
||
$case->refresh();
|
||
$this->assertSame(BookingStatus::Confirmed, $case->status);
|
||
$task = BookingTask::query()->where('booking_id', $case->id)->sole();
|
||
$this->assertSame(BookingTaskKind::PortalCancellationReported, $task->kind);
|
||
$this->assertStringContainsString('FeWo-direkt meldet eine Stornierung', $task->note);
|
||
$this->assertSame(2, $case->events()->where('type', 'portal_signal')->count(), 'two different mails are two reports');
|
||
$this->assertNotNull($this->fewo->blockedPeriods()->where('source', 'email')->first(), 'the mail entry of the case is not released');
|
||
$this->assertSame('Das Portal meldet eine Stornierung – bitte prüfen', CaseActivities::next($case)?->label);
|
||
}
|
||
|
||
/** 4.4 */
|
||
public function test_a_disappeared_or_changed_calendar_entry_is_reported_once_and_nothing_is_released(): void
|
||
{
|
||
$feed = self::feed('evt-7', 'Reservation: Nora Beispiel', 'Buchung HA-CCCCC3', '20261207', '20261214');
|
||
$this->sync($this->fewo, $feed);
|
||
$case = Booking::query()->sole();
|
||
|
||
$this->sync($this->fewo, self::feed('evt-7', 'Reservation: Nora Beispiel', 'Buchung HA-CCCCC3', '20261208', '20261214'));
|
||
$this->assertSame(BookingTaskKind::PortalChangeReported, BookingTask::query()->where('booking_id', $case->id)->sole()->kind);
|
||
|
||
$empty = self::feed();
|
||
$this->sync($this->fewo, $empty);
|
||
$this->assertSame(0, BookingTask::query()->where('kind', BookingTaskKind::PortalCancellationReported)->count(), 'one missing import is no report yet');
|
||
$this->sync($this->fewo, $empty);
|
||
$this->sync($this->fewo, $empty);
|
||
|
||
$this->assertSame(1, BookingTask::query()->where('kind', BookingTaskKind::PortalCancellationReported)->count());
|
||
$this->assertSame(2, $case->events()->where('type', 'portal_signal')->count());
|
||
$this->assertSame(BookingStatus::Confirmed, $case->fresh()->status);
|
||
$this->assertTrue(Booking::query()->occupying()->whereKey($case->id)->exists());
|
||
}
|
||
|
||
/** 4.5 */
|
||
public function test_portal_blocks_state_how_far_they_are_proven(): void
|
||
{
|
||
$manual = Channel::factory()->for($this->property)->create(['name' => 'Ostseeklar', 'requires_manual_blocking' => true, 'export_enabled' => true, 'export_mode' => 'regular', 'status' => 'active']);
|
||
$case = app(TakeOverExternalBooking::class)->handle($this->property, $this->fewo, CarbonImmutable::parse('2026-12-07'), CarbonImmutable::parse('2026-12-14'));
|
||
$task = ChannelTask::query()->where('channel_id', $manual->id)->sole();
|
||
|
||
$this->actingAs(User::factory()->create(['name' => 'Ingrid']));
|
||
Livewire::test(ListChannelTasks::class)
|
||
->callTableAction('done', $task, ['proof' => ChannelTaskProof::Initiated->value, 'note' => null])
|
||
->assertHasNoTableActionErrors();
|
||
$this->assertStringContainsString('veranlasst', collect(CaseActivities::for($case))->firstWhere('key', 'channel_block')->detail);
|
||
|
||
Livewire::test(ListChannelTasks::class)
|
||
->filterTable('status', null)
|
||
->callTableAction('prove', $task, ['proof' => ChannelTaskProof::Observed->value, 'note' => 'im Kalender rot'])
|
||
->assertHasNoTableActionErrors();
|
||
$task->refresh();
|
||
$this->assertSame(ChannelTaskProof::Observed, $task->proof);
|
||
$this->assertStringContainsString('im Portal gesehen (im Kalender rot)', collect(CaseActivities::for($case))->firstWhere('key', 'channel_block')->detail);
|
||
|
||
$task->prove(ChannelTaskProof::Initiated, 'Ingrid');
|
||
$this->assertSame(ChannelTaskProof::Observed, $task->fresh()->proof, 'proof is never lowered');
|
||
}
|
||
|
||
/** 4.6 */
|
||
public function test_observing_creates_nothing_and_reports_what_would_have_been_created(): void
|
||
{
|
||
$this->fewo->update(['case_intake' => CaseIntakeMode::Observe]);
|
||
|
||
$this->mail('Neue Buchung', self::BOOKING_MAIL);
|
||
$this->sync($this->fewo, self::feed('evt-8', 'Reservation: Nora Beispiel', 'Buchung HA-DDDDD4', '20270110', '20270117'));
|
||
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertSame(2, IntakeObservation::query()->where('outcome', IntakeOutcome::Create)->where('applied', false)->count());
|
||
$this->artisan('channels:intake-report')->expectsOutputToContain('Wären entstanden (Beobachten): 2')->assertSuccessful();
|
||
}
|
||
|
||
public function test_a_channel_that_was_not_switched_on_behaves_as_before(): void
|
||
{
|
||
$this->fewo->update(['case_intake' => CaseIntakeMode::Off]);
|
||
|
||
$this->mail('Neue Buchung', self::BOOKING_MAIL);
|
||
$this->sync($this->fewo, self::feed('evt-9', 'Reservation: Nora Beispiel', 'Buchung HA-EEEEE5', '20270110', '20270117'));
|
||
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertSame(0, IntakeObservation::query()->count());
|
||
$this->assertSame(CaseIntakeMode::Off, Channel::factory()->for($this->property)->create()->fresh()->case_intake, 'new channels start switched off');
|
||
}
|
||
|
||
/** 4.7 */
|
||
public function test_a_portal_request_becomes_a_request_that_asks_for_the_missing_data(): void
|
||
{
|
||
$this->mail('Neue Buchungsanfrage von Max', "Anreise: 07.12.2026\nAbreise: 14.12.2026\nBuchungsnummer: HA-FFFFF6");
|
||
|
||
$case = Booking::query()->sole();
|
||
$this->assertSame([IntakeKind::Inquiry, CaseType::A2, BookingStatus::Reserved, 'HA-FFFFF6'], [$case->intake, $case->case_type, $case->status, $case->portal_reference]);
|
||
$this->assertNull($case->guest_id);
|
||
$this->assertNull($case->guests);
|
||
$this->assertNull($case->total);
|
||
$this->assertSame('provideContact', CaseActivities::next($case)?->action);
|
||
Mail::assertNotSent(OfferMade::class);
|
||
}
|
||
|
||
public function test_cases_created_with_automatic_handling_still_send_no_unreleased_documents(): void
|
||
{
|
||
$this->fewo->update(['case_intake' => CaseIntakeMode::Continue]);
|
||
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
|
||
|
||
$this->mail('Neue Buchung', self::BOOKING_MAIL);
|
||
$case = Booking::query()->sole();
|
||
app(ProvideGuestContact::class)->handle($case, ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.org'], ProvideGuestContact::MANUAL);
|
||
$case->update(['total' => 90000]);
|
||
app(CaseAutomation::class)->advance($case->fresh());
|
||
|
||
$this->assertNull($case->fresh()->processing_mode);
|
||
Mail::assertNotSent(BookingDocumentation::class);
|
||
$this->assertSame('sendDocuments', CaseActivities::next($case->fresh())?->action);
|
||
}
|
||
|
||
/** 4.1 */
|
||
public function test_a_destination_solutions_booking_is_entered_by_hand_and_handled_by_us(): void
|
||
{
|
||
$ds = Channel::factory()->for($this->property)->create(['name' => 'Destination Solutions', 'booking_model' => 'portal_booking']);
|
||
$manual = Channel::factory()->for($this->property)->create(['name' => 'Ostseeklar', 'requires_manual_blocking' => true, 'export_enabled' => true, 'export_mode' => 'regular', 'status' => 'active']);
|
||
|
||
$case = app(TakeOverExternalBooking::class)->handle($this->property, $ds, CarbonImmutable::parse('2027-02-01'), CarbonImmutable::parse('2027-02-08'), ['portal_reference' => 'DS-1', 'portal_total' => 84000, 'guests' => 2, 'guest' => ['first_name' => 'Hans', 'last_name' => 'Gast', 'email' => 'hans@example.org']]);
|
||
|
||
$this->assertSame([CaseType::A3, BookingStatus::Confirmed, 0, 84000], [$case->case_type, $case->status, $case->offer_version, $case->total]);
|
||
$this->assertTrue($case->wantsPaymentTasks());
|
||
$keys = array_column(array_map(fn ($activity): array => ['key' => $activity->key], CaseActivities::for($case)), 'key');
|
||
$this->assertContains('channel_block', $keys);
|
||
$this->assertContains('deposit', $keys);
|
||
$this->assertSame(1, ChannelTask::query()->where('channel_id', $manual->id)->count());
|
||
$this->assertSame('sendDocuments', collect(CaseActivities::for($case))->firstWhere('key', 'documents')->action);
|
||
}
|
||
|
||
public function test_existing_portal_entries_are_adopted_by_a_command_with_preview_report_and_revert(): void
|
||
{
|
||
$this->fewo->update(['case_intake' => CaseIntakeMode::Off]);
|
||
$this->sync($this->fewo, self::feed('evt-10', 'Reservation: Nora Beispiel', 'Buchung HA-GGGGG7', '20270110', '20270117'));
|
||
$this->assertSame(0, Booking::query()->count());
|
||
|
||
$this->artisan('channels:adopt-occupations')->expectsOutputToContain('Vorschau – nichts geändert')->assertSuccessful();
|
||
$this->assertSame(0, Booking::query()->count());
|
||
|
||
$this->artisan('channels:adopt-occupations', ['--apply' => true])->assertSuccessful();
|
||
$case = Booking::query()->sole();
|
||
$this->assertSame(ProcessingMode::Manual, $case->processing_mode);
|
||
$this->assertSame($case->id, $this->fewo->blockedPeriods()->sole()->linked_booking_id);
|
||
|
||
$this->artisan('channels:adopt-occupations', ['--apply' => true])->assertSuccessful();
|
||
$this->assertSame(1, Booking::query()->count(), 'a repeated run adopts nothing twice');
|
||
|
||
$report = collect(Storage::disk('local')->files('intake-adoptions'))->first(fn (string $file): bool => ! str_contains($file, 'vorschau') && str_contains(Storage::disk('local')->get($file), '"created": true'));
|
||
$this->artisan('channels:adopt-occupations', ['--revert' => $report])->expectsOutputToContain('Zurückgenommen')->assertSuccessful();
|
||
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$entry = $this->fewo->blockedPeriods()->sole();
|
||
$this->assertFalse($entry->is_representation);
|
||
$this->assertNull($entry->linked_booking_id);
|
||
}
|
||
|
||
private function mail(string $subject, string $text, string $messageId = 'msg-1'): void
|
||
{
|
||
app(ProcessPortalMail::class)->handle(new PortalMail($messageId, 'noreply@fewo-direkt.de', $subject, $text, CarbonImmutable::now()));
|
||
}
|
||
|
||
private function sync(Channel $channel, string $feed): void
|
||
{
|
||
$this->currentFeed = $feed;
|
||
$this->assertTrue(app(SyncChannel::class)->handle($channel->fresh())->successful);
|
||
}
|
||
|
||
private static function reservationRule(): array
|
||
{
|
||
return ['name' => 'Buchung', 'priority' => 10, 'enabled' => true, 'case_sensitive' => false, 'match' => 'all',
|
||
'conditions' => [['field' => 'SUMMARY', 'operator' => 'starts_with', 'value' => 'Reservation: ']],
|
||
'event_type' => 'portal_reservation', 'origin' => 'current', 'representation' => 'leading_candidate',
|
||
'extractions' => [
|
||
['field' => 'SUMMARY', 'target' => 'guest_full_name', 'method' => 'after', 'start' => 'Reservation: '],
|
||
['field' => 'DESCRIPTION', 'target' => 'booking_reference', 'method' => 'after', 'start' => 'Buchung '],
|
||
]];
|
||
}
|
||
|
||
private static function feed(?string $uid = null, string $summary = '', string $description = '', string $start = '20261207', string $end = '20261214'): string
|
||
{
|
||
$event = $uid === null ? '' : "BEGIN:VEVENT\r\nUID:{$uid}\r\nDTSTAMP:20261102T090000Z\r\nDTSTART;VALUE=DATE:{$start}\r\nDTEND;VALUE=DATE:{$end}\r\nSUMMARY:{$summary}\r\n".($description !== '' ? "DESCRIPTION:{$description}\r\n" : '')."END:VEVENT\r\n";
|
||
|
||
return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic//DE\r\n{$event}END:VCALENDAR\r\n";
|
||
}
|
||
}
|