- Buchung ist der einheitliche Vorgang: Eingangsart, Vorgangsart A1–A6, eingefrorenes Abwicklungsprofil, gespeicherter Verlauf (booking_events); Gast, Personen und Beträge dürfen unbekannt sein, Platzhalter-Gäste werden migriert und nicht mehr angelegt - Einstellungen in Ebenen: Vermieterstandard (So arbeite ich), Abweichung der Wohnung (Experte), Abweichung je Portal und Eingangsart; ersetzt booking_model als Steuergröße - Ein Weg für extern verbindliche Buchungen (TakeOverExternalBooking) statt RecordPortalBooking und "Akte anlegen"; Portalpreis als belegte Gesamtposition - Startseite "Zu erledigen", neue Vorgangsseite mit nächstem Schritt, Automatik-Schalter, "Beenden" mit Storno statt Statusumschaltung; Status von Hand nur im Expertenmodus - Migration übernimmt bisheriges Verhalten unverändert (abweichende Wohnungswerte bleiben als Abweichung), Bericht: php artisan process:settings-report Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
6.5 KiB
PHP
186 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Channels\ChannelRegistry;
|
|
use App\Channels\Contracts\ChannelConnector;
|
|
use App\Enums\ChannelBookingModel;
|
|
use App\Enums\ChannelEchoBehavior;
|
|
use App\Enums\ChannelExportMode;
|
|
use App\Enums\ChannelStatus;
|
|
use App\Enums\TenantServiceKey;
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use App\Tenancy\TenantFeatures;
|
|
use Database\Factories\ChannelFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* A sales channel (portal) of a property. Our system leads: occupations are imported from the
|
|
* channel and every channel gets its own export feed without its own occupations.
|
|
*/
|
|
#[Fillable(['property_id', 'name', 'connector', 'settings', 'is_active', 'requires_manual_blocking', 'manage_url', 'status', 'booking_model', 'import_enabled', 'export_enabled', 'export_mode', 'external_poll_minutes', 'echo_behavior', 'echo_notes', 'operating_notes', 'portal_verified_at', 'payment_tasks_inquiry', 'payment_tasks_reservation', 'contract_template_id', 'contract_template_reservation_id', 'process_overrides'])]
|
|
#[Hidden(['export_token', 'previous_export_token', 'export_snapshot', 'settings', 'ical_analysis'])]
|
|
class Channel extends Model
|
|
{
|
|
/** @use HasFactory<ChannelFactory> */
|
|
use BelongsToTenant, HasFactory;
|
|
|
|
protected $attributes = [
|
|
'status' => 'setup', 'booking_model' => 'mixed', 'import_enabled' => false,
|
|
'export_enabled' => false, 'export_mode' => 'test',
|
|
'echo_behavior' => 'unverified',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Channel $channel): void {
|
|
$channel->export_token ??= Str::random(48);
|
|
});
|
|
static::saving(function (Channel $channel): void {
|
|
if ($channel->isDirty('status')) {
|
|
$channel->is_active = $channel->status === ChannelStatus::Active;
|
|
} elseif ($channel->isDirty('is_active')) {
|
|
$channel->status = $channel->is_active ? ChannelStatus::Active : ChannelStatus::Paused;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'settings' => 'array',
|
|
'process_overrides' => 'array',
|
|
'ical_analysis' => 'encrypted:array',
|
|
'ical_analyzed_at' => 'immutable_datetime',
|
|
'ical_rules' => 'array',
|
|
'ical_active_rules' => 'array',
|
|
'ical_export_mapping' => 'array',
|
|
'ical_export_names' => 'boolean',
|
|
'status' => ChannelStatus::class,
|
|
'booking_model' => ChannelBookingModel::class,
|
|
'echo_behavior' => ChannelEchoBehavior::class,
|
|
'portal_verified_at' => 'immutable_datetime',
|
|
'payment_tasks_inquiry' => 'boolean',
|
|
'payment_tasks_reservation' => 'boolean',
|
|
'export_mode' => ChannelExportMode::class,
|
|
'import_enabled' => 'boolean',
|
|
'export_enabled' => 'boolean',
|
|
'sync_warnings' => 'array',
|
|
'last_error_at' => 'immutable_datetime',
|
|
'export_snapshot_at' => 'immutable_datetime',
|
|
'last_export_requested_at' => 'immutable_datetime',
|
|
'previous_export_token_expires_at' => 'immutable_datetime',
|
|
'is_active' => 'boolean',
|
|
'requires_manual_blocking' => 'boolean',
|
|
'last_synced_at' => 'immutable_datetime',
|
|
'last_success_at' => 'immutable_datetime',
|
|
'alerted_at' => 'immutable_datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Property, $this>
|
|
*/
|
|
public function property(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Property::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<BlockedPeriod, $this>
|
|
*/
|
|
public function blockedPeriods(): HasMany
|
|
{
|
|
return $this->hasMany(BlockedPeriod::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<ChannelSyncLog, $this>
|
|
*/
|
|
public function syncLogs(): HasMany
|
|
{
|
|
return $this->hasMany(ChannelSyncLog::class)->latest('id');
|
|
}
|
|
|
|
/** @return HasMany<ChannelExportRequest, $this> */
|
|
public function exportRequests(): HasMany
|
|
{
|
|
return $this->hasMany(ChannelExportRequest::class)->latest('requested_at')->latest('id');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<ChannelTask, $this>
|
|
*/
|
|
public function tasks(): HasMany
|
|
{
|
|
return $this->hasMany(ChannelTask::class);
|
|
}
|
|
|
|
/**
|
|
* @param Builder<Channel> $query
|
|
* @return Builder<Channel>
|
|
*/
|
|
#[Scope]
|
|
protected function active(Builder $query): Builder
|
|
{
|
|
return $query->where('is_active', true)->where('status', ChannelStatus::Active);
|
|
}
|
|
|
|
public function connector(): ChannelConnector
|
|
{
|
|
return app(ChannelRegistry::class)->resolve($this->connector);
|
|
}
|
|
|
|
public function importsOccupations(): bool
|
|
{
|
|
return $this->is_active && $this->status === ChannelStatus::Active && $this->import_enabled
|
|
&& $this->servesItsLandlord() && $this->connector()->canImport($this);
|
|
}
|
|
|
|
public function exportsOccupations(): bool
|
|
{
|
|
return $this->is_active && $this->status === ChannelStatus::Active && $this->export_enabled && $this->servesItsLandlord();
|
|
}
|
|
|
|
/**
|
|
* Whether the landlord this channel belongs to has the portal synchronisation unlocked at all.
|
|
* Asked here, so that neither an import nor the export feed can run around the switch.
|
|
*/
|
|
public function servesItsLandlord(): bool
|
|
{
|
|
return app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $this->tenant);
|
|
}
|
|
|
|
public function exportUrl(): string
|
|
{
|
|
return route('channels.feed', ['token' => $this->export_token]);
|
|
}
|
|
|
|
public function setting(string $key): mixed
|
|
{
|
|
return $this->settings[$key] ?? null;
|
|
}
|
|
|
|
/**
|
|
* The import is considered stale when no successful sync happened within the threshold.
|
|
*/
|
|
public function isStale(): bool
|
|
{
|
|
return $this->importsOccupations()
|
|
&& ($this->last_success_at === null || $this->last_success_at->lessThan(now()->subMinutes(config('fewo.channels.stale_after_minutes'))));
|
|
}
|
|
}
|