Files
fewo-stommer/app/Channels/ICal/ICalFeed.php
T
LarsandClaude Opus 5.5 3bcfc09417
Deploy FeWo mit PipelineManager / deploy (push) Successful in 4m44s
Pakete 8, 5 und 6: Beträge und Zahlungen, Ändern und Stornieren, A6 im Vorgang
- Paket 8: Zahlungsarten (Gast an uns, laut Portal, Auszahlung, Erstattung),
  Portalbeträge als Angaben mit Quelle, Übersicht nach Zuständigkeit (R09).
- Paket 5: Ändern mit Folgenvorschau, vereinbart oder dem Gast vorgeschlagen
  (signierter Link), ein Stornoweg mit Grund und Erstattung, Portalstorno erst
  nach Bestätigung.
- Paket 6: eigene Nutzung, Besuch und Sperre als Vorgang mit Kurabgabe und
  Aufenthaltsbestätigung; Bestandsübernahme der Sperren mit identischem Export.
- Wirksame Einstellungen (Expertenansicht), Abnahmekatalog T01–T14, Ergebnis-,
  Abnahme- und Konfigurationsdokument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 22:16:47 +01:00

101 lines
4.2 KiB
PHP

<?php
namespace App\Channels\ICal;
use App\Enums\ChannelExportMode;
use App\Models\Booking;
use App\Models\Channel;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
/**
* The calendar a portal imports from us: every occupation of the property except the ones that
* came from this very portal (otherwise the portal would block its own bookings twice).
* Events carry no guest data.
*/
class ICalFeed
{
public function build(Channel $channel): string
{
$property = $channel->property;
$from = CarbonImmutable::today()->subDays(30);
$until = CarbonImmutable::today()->addYears(2);
$suffix = config('channels.uid_suffix');
$events = [];
$bookings = $property->bookings()->occupying()->overlapping($from, $until);
if ($channel->export_mode === ChannelExportMode::Test) {
$bookings->whereNull('import_channel_id')->whereNull('origin_channel_id')->whereIn('source', Booking::DIRECT_SOURCES);
} else {
$bookings->where(fn ($query) => $query->whereNull('import_channel_id')->orWhere('import_channel_id', '!=', $channel->id))
->where(fn ($query) => $query->whereNull('origin_channel_id')->orWhere('origin_channel_id', '!=', $channel->id));
}
foreach ($bookings->with(['guest', 'property'])->orderBy('id')->get() as $booking) {
/** @var Booking $booking */
// A block taken over as a case keeps the identity the portals already know.
$events[] = $this->event(($booking->export_uid ?? "booking-{$booking->id}").$suffix, $booking->arrival, $booking->departure, $booking->updated_at, (new ICalExportFields)->properties($channel, $booking));
}
$blocks = $property->blockedPeriods()
->occupying()->where('export_approved', true)
->overlapping($from, $until)
->where(fn ($query) => $query->whereNull('channel_id')->orWhere('channel_id', '!=', $channel->id))
->where(fn ($query) => $query->whereNull('origin_channel_id')->orWhere('origin_channel_id', '!=', $channel->id));
if ($channel->export_mode === ChannelExportMode::Test) {
$blocks->whereNull('channel_id')->whereNull('origin_channel_id')->where('source', 'manual');
}
foreach ($blocks->with(['property', 'channel', 'originChannel', 'guest'])->orderBy('id')->get() as $block) {
$events[] = $this->event("block-{$block->id}{$suffix}", $block->starts_on, $block->ends_on, $block->updated_at, (new ICalExportFields)->properties($channel, $block));
}
$lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//Ferienwohnung Stommer//Belegung//DE',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'X-WR-CALNAME:'.$this->escape($property->name),
...$events,
'END:VCALENDAR',
];
return implode("\r\n", array_map(fn (string $line): string => $this->fold($line), explode("\r\n", implode("\r\n", $lines))))."\r\n";
}
private function event(string $uid, CarbonInterface $start, CarbonInterface $end, ?CarbonInterface $modified, array $properties): string
{
return implode("\r\n", [
'BEGIN:VEVENT',
"UID:{$uid}",
'DTSTAMP:'.($modified ?? now())->utc()->format('Ymd\THis\Z'),
'DTSTART;VALUE=DATE:'.$start->format('Ymd'),
'DTEND;VALUE=DATE:'.$end->format('Ymd'),
...array_map(fn (string $field, string $value): string => $field.':'.$this->escape($value), array_keys($properties), array_values($properties)),
'TRANSP:OPAQUE',
'END:VEVENT',
]);
}
private function escape(string $text): string
{
return str_replace(['\\', ';', ',', "\n"], ['\\\\', '\\;', '\\,', '\\n'], str_replace(["\r\n", "\r"], "\n", $text));
}
private function fold(string $line): string
{
$parts = [];
$limit = 75;
while (strlen($line) > $limit) {
$part = mb_strcut($line, 0, $limit, 'UTF-8');
$parts[] = $part;
$line = substr($line, strlen($part));
$limit = 74;
}
$parts[] = $line;
return implode("\r\n ", $parts);
}
}