Generische iCal-Kanäle mit Bestandsklärung und sicheren Exporten bereitstellen
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
This commit is contained in:
1 parent
6fe5bbc6e3
commit
783068501c
42 files changed
+2646
-248
No files matched your search
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use App\Models\Concerns\HasChannelProvenance;
|
||||
use App\Observers\BlockedPeriodObserver;
|
||||
use Carbon\CarbonInterface;
|
||||
use Database\Factories\BlockedPeriodFactory;
|
||||
@@ -13,17 +14,20 @@
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* Dates that are not bookable (own use, maintenance, bookings from portals).
|
||||
* Like bookings, ends_on is the first free day again.
|
||||
*/
|
||||
#[ObservedBy(BlockedPeriodObserver::class)]
|
||||
#[Fillable(['property_id', 'channel_id', 'starts_on', 'ends_on', 'reason', 'source', 'external_uid'])]
|
||||
#[Fillable(['property_id', 'channel_id', 'origin_channel_id', 'starts_on', 'ends_on', 'reason', 'source', 'external_uid', 'needs_review', 'review_note', 'missing_imports', 'is_representation', 'canonical_block_id', 'linked_booking_id', 'export_approved', 'reviewed_at'])]
|
||||
class BlockedPeriod extends Model
|
||||
{
|
||||
/** @use HasFactory<BlockedPeriodFactory> */
|
||||
use BelongsToTenant, HasFactory;
|
||||
use BelongsToTenant, HasChannelProvenance, HasFactory, SoftDeletes;
|
||||
|
||||
protected $attributes = ['is_representation' => false, 'export_approved' => true];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
@@ -35,6 +39,11 @@ protected function casts(): array
|
||||
return [
|
||||
'starts_on' => 'immutable_date',
|
||||
'ends_on' => 'immutable_date',
|
||||
'needs_review' => 'boolean',
|
||||
'missing_imports' => 'integer',
|
||||
'is_representation' => 'boolean',
|
||||
'export_approved' => 'boolean',
|
||||
'reviewed_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -54,6 +63,33 @@ public function channel(): BelongsTo
|
||||
return $this->belongsTo(Channel::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Channel, $this> */
|
||||
public function originChannel(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Channel::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<BlockedPeriod, $this> */
|
||||
public function canonicalBlock(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'canonical_block_id')->withTrashed();
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Booking, $this> */
|
||||
public function linkedBooking(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Booking::class, 'linked_booking_id');
|
||||
}
|
||||
|
||||
/** @param Builder<BlockedPeriod> $query
|
||||
* @return Builder<BlockedPeriod>
|
||||
*/
|
||||
#[Scope]
|
||||
protected function occupying(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_representation', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<BlockedPeriod> $query
|
||||
* @return Builder<BlockedPeriod>
|
||||
|
||||
+11
-1
@@ -5,6 +5,7 @@
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\ReferralSource;
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use App\Models\Concerns\HasChannelProvenance;
|
||||
use App\Observers\BookingObserver;
|
||||
use Carbon\CarbonInterface;
|
||||
use Database\Factories\BookingFactory;
|
||||
@@ -28,6 +29,8 @@
|
||||
'guest_id',
|
||||
'status',
|
||||
'source',
|
||||
'import_channel_id',
|
||||
'origin_channel_id',
|
||||
'referral_source',
|
||||
'utm_source',
|
||||
'utm_medium',
|
||||
@@ -66,7 +69,14 @@
|
||||
class Booking extends Model
|
||||
{
|
||||
/** @use HasFactory<BookingFactory> */
|
||||
use BelongsToTenant, HasFactory;
|
||||
use BelongsToTenant, HasChannelProvenance, HasFactory;
|
||||
|
||||
public const DIRECT_SOURCES = ['website', 'manual', 'phone', 'direct', 'email'];
|
||||
|
||||
public function isDirect(): bool
|
||||
{
|
||||
return $this->import_channel_id === null && $this->origin_channel_id === null && in_array($this->source, self::DIRECT_SOURCES, true);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
|
||||
+39
-4
@@ -4,6 +4,10 @@
|
||||
|
||||
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;
|
||||
@@ -22,18 +26,31 @@
|
||||
* 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'])]
|
||||
#[Hidden(['export_token'])]
|
||||
#[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'])]
|
||||
#[Hidden(['export_token', 'previous_export_token', 'export_snapshot', 'settings'])]
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,6 +62,18 @@ protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'settings' => 'array',
|
||||
'status' => ChannelStatus::class,
|
||||
'booking_model' => ChannelBookingModel::class,
|
||||
'echo_behavior' => ChannelEchoBehavior::class,
|
||||
'portal_verified_at' => 'immutable_datetime',
|
||||
'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',
|
||||
@@ -92,7 +121,7 @@ public function tasks(): HasMany
|
||||
#[Scope]
|
||||
protected function active(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
return $query->where('is_active', true)->where('status', ChannelStatus::Active);
|
||||
}
|
||||
|
||||
public function connector(): ChannelConnector
|
||||
@@ -102,7 +131,13 @@ public function connector(): ChannelConnector
|
||||
|
||||
public function importsOccupations(): bool
|
||||
{
|
||||
return $this->is_active && $this->servesItsLandlord() && $this->connector()->canImport($this);
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['channel_id', 'successful', 'events', 'added', 'changed', 'removed', 'conflicts', 'message', 'duration_ms'])]
|
||||
#[Fillable(['channel_id', 'successful', 'events', 'added', 'changed', 'removed', 'conflicts', 'message', 'duration_ms', 'warnings'])]
|
||||
class ChannelSyncLog extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
@@ -23,6 +23,7 @@ protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'successful' => 'boolean',
|
||||
'warnings' => 'array',
|
||||
'created_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Availability\AvailabilityChecker;
|
||||
use App\Enums\ChannelTaskAction;
|
||||
use App\Enums\ChannelTaskStatus;
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
@@ -61,6 +62,14 @@ protected function open(Builder $query): Builder
|
||||
|
||||
public function complete(string $completedBy): void
|
||||
{
|
||||
if ($this->status !== ChannelTaskStatus::Open) {
|
||||
return;
|
||||
}
|
||||
if ($this->action === ChannelTaskAction::Unblock && ! app(AvailabilityChecker::class)->isAvailable($this->channel->property, $this->starts_on, $this->ends_on)) {
|
||||
$this->update(['status' => ChannelTaskStatus::Dismissed, 'automation_error' => 'Aktuell belegt – Entsperraufgabe nicht mehr gültig.']);
|
||||
|
||||
return;
|
||||
}
|
||||
$this->update([
|
||||
'status' => ChannelTaskStatus::Done,
|
||||
'completed_by' => $completedBy,
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
use App\Models\Channel;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
trait HasChannelProvenance
|
||||
{
|
||||
public static function bootHasChannelProvenance(): void
|
||||
{
|
||||
static::saving(function (Model $model): void {
|
||||
foreach (['channel_id', 'import_channel_id', 'origin_channel_id'] as $field) {
|
||||
$id = $model->getAttribute($field);
|
||||
if ($id !== null && ! Channel::withoutGlobalScope('tenant')->whereKey($id)->where('property_id', $model->property_id)->exists()) {
|
||||
throw ValidationException::withMessages([$field => 'Der Kanal muss zur selben Wohnung gehören.']);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user