63 lines
3.1 KiB
PHP
63 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Channels\ICal;
|
||
|
||
use App\Models\BlockedPeriod;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
|
||
class ICalExportFields
|
||
{
|
||
public const FIELDS = ['occupation_label' => 'Belegungsbeschreibung', 'guest_first_name' => 'Gastvorname / Namensbestandteil', 'guest_full_name' => 'Vollständiger Gastname', 'booking_reference' => 'Buchungsreferenz', 'note' => 'Zusätzliche Textinformation', 'property_name' => 'Wohnungsname', 'channel_name' => 'Bekannter Ursprung / Importkanal', 'arrival' => 'Anreise', 'departure' => 'Abreise', 'nights' => 'Anzahl Nächte'];
|
||
|
||
public const ICAL_FIELDS = ['SUMMARY' => 'SUMMARY – Bezeichnung', 'DESCRIPTION' => 'DESCRIPTION – Beschreibung', 'LOCATION' => 'LOCATION – Ort / Wohnung'];
|
||
|
||
/** @return array<string, string> */
|
||
public function properties(Channel $channel, Booking|BlockedPeriod $occupation): array
|
||
{
|
||
$context = $this->context($occupation);
|
||
if (! $channel->ical_export_names) {
|
||
unset($context['guest_first_name'], $context['guest_full_name']);
|
||
}
|
||
$properties = ['SUMMARY' => 'Belegt'];
|
||
foreach ($channel->ical_export_mapping ?? [] as $mapping) {
|
||
if (! isset(self::ICAL_FIELDS[$mapping['field'] ?? ''])) {
|
||
continue;
|
||
}
|
||
$parts = [];
|
||
foreach ($mapping['items'] ?? [] as $item) {
|
||
if (isset(self::FIELDS[$item]) && filled($context[$item] ?? null)) {
|
||
$parts[] = $context[$item];
|
||
}
|
||
}
|
||
if ($parts !== []) {
|
||
$properties[$mapping['field']] = implode($mapping['separator'] ?? ' – ', $parts);
|
||
}
|
||
}
|
||
|
||
return $properties;
|
||
}
|
||
|
||
/** @return array<string, string|null> */
|
||
public function context(Booking|BlockedPeriod $occupation): array
|
||
{
|
||
$booking = $occupation instanceof Booking;
|
||
$start = $booking ? $occupation->arrival : $occupation->starts_on;
|
||
$end = $booking ? $occupation->departure : $occupation->ends_on;
|
||
$context = ['occupation_label' => $booking ? 'Buchung' : (ICalRuleEngine::TYPES[$occupation->ical_event_type] ?? 'Belegt'),
|
||
'property_name' => $occupation->property->name, 'arrival' => $start->format('d.m.Y'), 'departure' => $end->format('d.m.Y'),
|
||
'nights' => (string) (int) $start->diffInDays($end),
|
||
'channel_name' => $booking ? null : ($occupation->originChannel?->name ?? $occupation->channel?->name)];
|
||
if ($booking) {
|
||
return [...$context, 'guest_first_name' => $occupation->guest?->first_name, 'guest_full_name' => $occupation->guest?->full_name, 'booking_reference' => $occupation->reference];
|
||
}
|
||
$fields = array_intersect_key($occupation->ical_interpretation['fields'] ?? [], self::FIELDS);
|
||
if ($occupation->guest !== null) {
|
||
$fields['guest_first_name'] = $occupation->guest->anonymized_at === null ? $occupation->guest->first_name : null;
|
||
$fields['guest_full_name'] = $occupation->guest->anonymized_at === null && filled($occupation->guest->last_name) ? $occupation->guest->full_name : null;
|
||
}
|
||
|
||
return [...$context, ...$fields];
|
||
}
|
||
}
|