Loheide-eu-mitarbeiterplatt.../plugins-core/qm/src/Reviews.php
Friederich Loheide 4d855c8a74
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m38s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m7s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m24s
QM: Wiedervorlage und Lesebestätigung, Platzhalter für Profilbilder (0.21.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:36:47 +00:00

119 lines
4.2 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 Inhaus\Plugins\Qm;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Support\Audit;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Inhaus\Plugins\Qm\Models\QmEntry;
/**
* Wiedervorlage: Dokumente, die zu einem Termin geprüft werden müssen.
* Die verantwortliche Person (sonst wer den Eintrag angelegt hat) wird
* LEAD_DAYS Tage vorher im Portal benachrichtigt.
*/
class Reviews
{
public const LEAD_DAYS = 14;
public const INTERVALS = [6 => 'Alle 6 Monate', 12 => 'Jährlich', 24 => 'Alle 2 Jahre', 36 => 'Alle 3 Jahre'];
public function __construct(private readonly QmAccess $access) {}
/** Status eines Termins: overdue | due (innerhalb der Vorlaufzeit) | planned | null */
public static function state(QmEntry $entry): ?string
{
if (! $entry->review_due_at) {
return null;
}
return match (true) {
$entry->review_due_at->isBefore(today()) => 'overdue',
$entry->review_due_at->isBefore(today()->addDays(self::LEAD_DAYS + 1)) => 'due',
default => 'planned',
};
}
public function canSee(?User $user): bool
{
return $user && ($this->access->canManage($user) || QmEntry::query()->where('responsible_id', $user->id)->whereNotNull('review_due_at')->exists());
}
public function canComplete(QmEntry $entry, ?User $user): bool
{
return $user && ($this->access->canManage($user) || $entry->responsible_id === $user->id);
}
/**
* Alle Termine (QM-Team) bzw. die eigenen (Verantwortliche), fällige zuerst.
*
* @return Collection<int, QmEntry>
*/
public function list(User $user): Collection
{
return QmEntry::query()
->whereNotNull('review_due_at')
->when(! $this->access->canManage($user), fn (Builder $q) => $q->where('responsible_id', $user->id))
->with(['responsible', 'reviewer', 'readingFile', 'editableFile'])
->orderBy('review_due_at')
->get();
}
/** Anzahl überfälliger und bald fälliger Termine für die Person */
public function openCount(User $user): int
{
return QmEntry::query()
->whereNotNull('review_due_at')
->where('review_due_at', '<=', today()->addDays(self::LEAD_DAYS))
->when(! $this->access->canManage($user), fn (Builder $q) => $q->where('responsible_id', $user->id))
->count();
}
/**
* Prüfung erledigt: nächster Termin nach Intervall (ohne Intervall: keiner).
*/
public function complete(QmEntry $entry, User $user): void
{
$entry->forceFill([
'reviewed_at' => now(),
'reviewed_by' => $user->id,
'review_due_at' => $entry->review_interval_months ? today()->addMonthsNoOverflow($entry->review_interval_months) : null,
'review_notified_at' => null,
])->save();
Audit::log('qm.entry_reviewed', ['entry' => $entry->id, 'title' => $entry->title, 'next' => $entry->review_due_at?->toDateString()]);
}
/**
* Täglich: fällige Termine melden (einmal je Termin).
*/
public function notifyDue(): int
{
$entries = QmEntry::query()
->whereNotNull('review_due_at')
->whereNull('review_notified_at')
->where('review_due_at', '<=', today()->addDays(self::LEAD_DAYS))
->with(['responsible', 'folder'])
->get();
foreach ($entries as $entry) {
$recipient = $entry->responsible ?? User::query()->find($entry->created_by);
if ($recipient?->is_active) {
$overdue = $entry->review_due_at->isBefore(today());
$recipient->notify(new PortalNotification(
$overdue ? 'QM-Prüfung überfällig' : 'QM-Prüfung steht an',
'„'.$entry->title.'“ – fällig am '.$entry->review_due_at->format('d.m.Y').'.',
route('qm.reviews'),
'calendar-clock',
));
}
$entry->forceFill(['review_notified_at' => now()])->save();
}
return $entries->count();
}
}