Files
fewo-stommer/app/Models/ChannelTask.php
LarsandClaude Opus 5.5 ac9d20b812 Ein Eingang fuer alle Kanaele (Paket 4, Prozessarchitektur v0.10)
Portalbuchungen und -anfragen aus Kalender und Mail werden ueber PortalIntake
zum Vorgang (nur bei sicherer Erkennung), Dubletten ueber Buchungsnummer,
gleiches Portal mit gleichen Naechten und Spiegelung; Unsicheres wird geklaert.
Portalstorno und -aenderung als Aufgabe am Vorgang ohne stille Stornierung.
Schalter je Kanal (Aus als Standard, Beobachten, Anlegen, Anlegen + weiter),
Eingangsprotokoll, Bestandsuebernahme mit Vorschau/Bericht/Ruecknahme,
Sperrnachweis in drei Stufen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 20:53:35 +01:00

123 lines
4.4 KiB
PHP
Raw Permalink 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\Availability\AvailabilityChecker;
use App\Enums\ChannelTaskAction;
use App\Enums\ChannelTaskProof;
use App\Enums\ChannelTaskStatus;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\ChannelTaskFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
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;
/**
* A change that has to be made by hand (or by an external automation) on a portal that
* cannot be kept in sync fast enough by calendar feeds, e.g. because of instant booking.
*/
#[Fillable(['channel_id', 'action', 'starts_on', 'ends_on', 'reason', 'status', 'completed_by', 'completed_at', 'reminded_at', 'automation_sent_at', 'automation_error', 'proof', 'proof_note', 'proof_at', 'proof_by'])]
class ChannelTask extends Model
{
/** @use HasFactory<ChannelTaskFactory> */
use BelongsToTenant, HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'action' => ChannelTaskAction::class,
'status' => ChannelTaskStatus::class,
'starts_on' => 'immutable_date',
'ends_on' => 'immutable_date',
'completed_at' => 'immutable_datetime',
'reminded_at' => 'immutable_datetime',
'automation_sent_at' => 'immutable_datetime',
'proof' => ChannelTaskProof::class,
'proof_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Channel, $this>
*/
public function channel(): BelongsTo
{
return $this->belongsTo(Channel::class);
}
/**
* @param Builder<ChannelTask> $query
* @return Builder<ChannelTask>
*/
#[Scope]
protected function open(Builder $query): Builder
{
return $query->where('status', ChannelTaskStatus::Open);
}
/**
* Completes the task with the proof the person can give (at least: the change was made or started).
*/
public function complete(string $completedBy, ChannelTaskProof $proof = ChannelTaskProof::Initiated, ?string $note = null): 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,
'completed_at' => now(),
'proof' => $proof,
'proof_note' => filled($note) ? trim($note) : null,
'proof_at' => now(),
'proof_by' => $completedBy,
]);
}
/**
* Raises the proof of a completed task (e.g. the block was later seen in the portal); never lowers it.
*/
public function prove(ChannelTaskProof $proof, string $by, ?string $note = null): void
{
$order = array_flip(array_map(fn (ChannelTaskProof $case): string => $case->value, ChannelTaskProof::cases()));
if ($this->status !== ChannelTaskStatus::Done || ($this->proof !== null && $order[$proof->value] <= $order[$this->proof->value])) {
return;
}
$this->update(['proof' => $proof, 'proof_note' => filled($note) ? trim($note) : $this->proof_note, 'proof_at' => now(), 'proof_by' => $by]);
}
/**
* The state of the task in plain words, including how far it is proven.
*/
public function proofLabel(): string
{
return match ($this->status) {
ChannelTaskStatus::Open => 'noch offen',
ChannelTaskStatus::Dismissed => 'verworfen',
ChannelTaskStatus::Done => ($this->proof ?? ChannelTaskProof::Initiated)->getLabel().($this->proof_note ? ' ('.$this->proof_note.')' : ''),
};
}
/**
* Human readable period, e.g. "14.06.2027 – 21.06.2027".
*/
public function period(): string
{
return $this->starts_on->format('d.m.Y').' – '.$this->ends_on->format('d.m.Y');
}
}