Files
fewo-stommer/app/Models/Channel.php
T
LarsandClaude Opus 5.5 799869c6a4 v0.11 U1: Kanalmanager mit eigener Webseite und neuen Prozessparametern
- Eigene Webseite als Kanal eigener Art (channels.kind = website), nur zur
  Konfiguration: Webseiten-Anfragen bleiben direkt (A1) ohne Kanalbezug;
  der Eintrag nimmt an Sync, Export, Überwachung und Sperraufgaben nicht teil
  (active()/Property::channels() nur Portale, Feed nur für Portale).
- Neue Einstellungen im bestehenden ProcessSetting: Klärungsreservierung
  automatisch/Freigabe (15/180 min, 0 = keine), Rückfragemodus,
  Erinnerungen, Folgefragen, Erinnerungsabstand, Linkgültigkeit,
  Anschrift im Webformular verlangen (Standard: nein).
- „Kanäle“ in der einfachen Ansicht: Webseite und Portale an einer Stelle,
  Verweis auf „Ihre Standards“; „Wirksame Einstellungen“ mit Spalte
  „Eigene Webseite“ und Herkunft „Webseite“.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 12:52:50 +01:00

235 lines
8.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Channels\ChannelRegistry;
use App\Channels\Contracts\ChannelConnector;
use App\Enums\CaseIntakeMode;
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 of a property. A portal (kind "portal"): our system leads, occupations are imported from the
* channel and every channel gets its own export feed without its own occupations. The own website (kind "website",
* v0.11 channel manager) only carries the settings for requests from the website: it is never synchronised,
* exported, monitored or given blocking tasks, and cases from the website stay direct (A1) without a channel.
*/
#[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', 'case_intake', 'kind'])]
#[Hidden(['export_token', 'previous_export_token', 'export_snapshot', 'settings', 'ical_analysis'])]
class Channel extends Model
{
/** @use HasFactory<ChannelFactory> */
use BelongsToTenant, HasFactory;
public const KIND_PORTAL = 'portal';
public const KIND_WEBSITE = 'website';
protected $attributes = [
'status' => 'setup', 'booking_model' => 'mixed', 'import_enabled' => false,
'export_enabled' => false, 'export_mode' => 'test',
'echo_behavior' => 'unverified', 'case_intake' => 'off', 'kind' => self::KIND_PORTAL,
];
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',
'case_intake' => CaseIntakeMode::class,
'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)->where('kind', self::KIND_PORTAL);
}
/**
* Portals only (active or not) – everything except the own website's settings entry.
*
* @param Builder<Channel> $query
* @return Builder<Channel>
*/
#[Scope]
protected function portals(Builder $query): Builder
{
return $query->where('kind', self::KIND_PORTAL);
}
public function isWebsite(): bool
{
return $this->kind === self::KIND_WEBSITE;
}
public function isPortal(): bool
{
return ! $this->isWebsite();
}
/**
* The settings entry of the property's own website, created on first use.
*/
public static function websiteOf(Property $property): self
{
return self::withoutGlobalScopes()->firstOrCreate(
['property_id' => $property->id, 'kind' => self::KIND_WEBSITE],
['tenant_id' => $property->tenant_id, 'name' => 'Eigene Webseite', 'connector' => self::KIND_WEBSITE, 'status' => ChannelStatus::Active, 'booking_model' => ChannelBookingModel::Inquiry],
);
}
/**
* The settings entry of the property's own website if there is one (no write, for resolving settings).
*/
public static function existingWebsiteOf(Property $property): ?self
{
return self::withoutGlobalScopes()->where('property_id', $property->id)->where('kind', self::KIND_WEBSITE)->first();
}
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'))));
}
}