Files
fewo-stommer/app/Models/BookingTask.php
T
LarsandClaude Opus 5.5 40d20cb7d1 Pakete 7 und 9: Auskunft und Buchungswunsch, Aufenthalt und Abschluss (Zwischenstand)
- Haltefrist 0: unverbindliche Auskunft mit signiertem Link "Buchung anfragen",
  erneute Prüfung unter Sperre, automatische oder manuelle Bestätigung, Absage
  bei vergebenem Zeitraum, Ablauf unbeantworteter Auskünfte
- Aufenthalt: Anreisedaten, Aufgaben je Wohnung, gebuchte Leistungen,
  Abweichungen und Folgeaufgaben als Aufgaben am Vorgang
- Abschluss getrennt: Aufenthalt abgeschlossen / kaufmännisch abgeschlossen,
  Nachläufer (Forderung, Auszahlung) bleiben sichtbar
- Zahlungsstatus nur aus Geld auf unserem Konto; Altbestand per Vorschau-Befehl
- Verlauf weist Testzustellungen als solche aus
- Abnahmetests T16-T25, T23 über isolierten SMTP-Empfänger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:29:19 +01:00

107 lines
3.9 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\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Models\Concerns\BelongsToTenant;
use App\Process\CaseClosure;
use App\Process\CaseLog;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Something the landlord has to do about a booking. One task per booking and kind; a finished or
* dismissed task is never created again.
*/
#[Fillable(['booking_id', 'kind', 'subject', 'title', 'responsible', 'status', 'due_on', 'note', 'completed_by', 'completed_at'])]
class BookingTask extends Model
{
use BelongsToTenant;
protected static function booted(): void
{
static::created(function (BookingTask $task): void {
if ($task->booking !== null) {
CaseLog::record($task->booking, 'task', 'Aufgabe: '.$task->label().($task->responsible ? ' (zuständig: '.$task->responsible.')' : ''));
if ($task->kind->isCommercial() && $task->status === BookingTaskStatus::Open) {
CaseClosure::refresh($task->booking);
}
}
});
static::updated(function (BookingTask $task): void {
if ($task->wasChanged('status') && $task->status !== BookingTaskStatus::Open && $task->booking !== null) {
CaseLog::record($task->booking, 'task', 'Aufgabe '.($task->status === BookingTaskStatus::Done ? 'erledigt' : 'verworfen').': '.$task->label().($task->wasChanged('note') && filled($task->note) ? ' – '.$task->note : ''), actor: $task->completed_by);
}
// Money tasks decide the commercial closing of the case (package 9).
if ($task->wasChanged('status') && $task->booking !== null && $task->kind->isCommercial()) {
CaseClosure::refresh($task->booking);
}
});
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'kind' => BookingTaskKind::class,
'status' => BookingTaskStatus::class,
'due_on' => 'immutable_date',
'completed_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Booking, $this>
*/
public function booking(): BelongsTo
{
return $this->belongsTo(Booking::class);
}
/**
* @param Builder<BookingTask> $query
* @return Builder<BookingTask>
*/
#[Scope]
protected function open(Builder $query): Builder
{
return $query->where('status', BookingTaskStatus::Open);
}
public function complete(string $by, ?string $note = null): void
{
$this->update(['status' => BookingTaskStatus::Done, 'completed_by' => $by, 'completed_at' => now(), ...($note !== null && $note !== '' ? ['note' => mb_substr($note, 0, 1000)] : [])]);
}
/**
* What the task is about in plain words: its own title (services, issues, follow-ups) or the label of its kind.
*/
public function label(): string
{
return filled($this->title) ? (string) $this->title : $this->kind->getLabel();
}
/**
* Opens the task of this kind and subject for the case once; a task that was done or dismissed is not opened again.
*
* @param array<string, mixed> $attributes
*/
public static function openOnce(Booking $booking, BookingTaskKind $kind, string $subject = '', array $attributes = []): self
{
$task = static::withoutGlobalScope('tenant')->firstOrNew(['booking_id' => $booking->id, 'kind' => $kind, 'subject' => $subject]);
if (! $task->exists) {
$task->forceFill(['tenant_id' => $booking->tenant_id, 'status' => BookingTaskStatus::Open, ...$attributes])->save();
}
return $task;
}
}