whereKey($block->property_id)->lockForUpdate()->firstOrFail(); $block = BlockedPeriod::query()->whereKey($block->id)->lockForUpdate()->firstOrFail(); if (! in_array($block->source, ['channel', 'email'], true) || $block->channel_id === null) { throw new ReservationException('Eine Akte gibt es nur für Reservierungen aus einem Portalkalender.'); } if ($block->occupationKind() === OccupationKind::Block) { throw new ReservationException('Eine Sperrzeit ist keine Reservierung. Bitte zuerst „Einordnen“ und als Portalreservierung kennzeichnen.'); } if ($block->is_representation && $block->linked_booking_id !== null) { return Booking::query()->findOrFail($block->linked_booking_id); } if ($block->is_representation) { throw new ReservationException('Dieser Eintrag ist die Spiegelung einer anderen Belegung. Die Akte gehört an die führende Belegung.'); } $property = $block->property; $channel = $block->channel; $fields = (array) ($block->ical_interpretation['fields'] ?? []); $guest = $block->guest ?? Guest::query()->create([ 'first_name' => filled($fields['guest_first_name'] ?? null) ? (string) $fields['guest_first_name'] : 'Gast', 'last_name' => '('.$channel->name.')', 'email' => '', 'tenant_id' => app(TenantContext::class)->id(), ]); $stay = new StayRequest(CarbonImmutable::parse($block->starts_on), CarbonImmutable::parse($block->ends_on), min(2, $property->max_guests)); $booking = $property->bookings()->create([ 'guest_id' => $guest->id, 'status' => BookingStatus::Reserved, 'source' => 'portal', 'origin_channel_id' => $block->origin_channel_id ?? $channel->id, 'portal_reference' => $fields['booking_reference'] ?? null, 'arrival' => $stay->arrival->toDateString(), 'departure' => $stay->departure->toDateString(), 'guests' => $stay->guests, 'hold_expires_at' => null, 'internal_notes' => 'Akte aus dem Portalkalender „'.$channel->name.'“ angelegt. Personenzahl und Preis sind nicht bekannt – bitte den Portalpreis erfassen.', ...$this->price->unknownColumns($property, $stay), ]); $block->forceFill([ 'is_representation' => true, 'representation_automatic' => false, 'canonical_block_id' => null, 'linked_booking_id' => $booking->id, 'export_approved' => false, 'needs_review' => false, 'reviewed_at' => now(), 'occupation_kind' => OccupationKind::Reservation, 'guest_id' => $guest->id, 'review_note' => 'Akte angelegt; die Buchung führt diese Belegung.', ])->save(); app(ConfirmBooking::class)->bind($booking); app(ReconcileChannelRepresentations::class)->handle($property); return $booking->refresh(); }); } }