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 */ protected function casts(): array { return [ 'kind' => BookingTaskKind::class, 'status' => BookingTaskStatus::class, 'due_on' => 'immutable_date', 'completed_at' => 'immutable_datetime', ]; } /** * @return BelongsTo */ public function booking(): BelongsTo { return $this->belongsTo(Booking::class); } /** * @param Builder $query * @return Builder */ #[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 $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; } }