System > Betrieb > Benachrichtigungen: offene Meldungen (Doppelbuchungen im Postfach, Portal-Aufgaben) nur einmal oder alle 1/2/3/7 Tage erneut senden; Tages- und/oder Wochenuebersicht mit waehlbarer Uhrzeit, Wochentag und Inhalt (Postfach, Anfragen, Neues, An-/Abreisen, Zahlungen, Portale, Bewertungen). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\DigestSection;
|
||
use App\Mail\Digest;
|
||
use App\Models\Tenant;
|
||
use App\Support\DigestReport;
|
||
use App\Support\Settings;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Support\Facades\Mail;
|
||
|
||
/**
|
||
* Sends the landlord's daily and/or weekly summary once the chosen hour has come (System › Betrieb ›
|
||
* Benachrichtigungen). On the weekly day only the weekly summary goes out, never both. Runs per landlord
|
||
* from the scheduler; the sent dates on the landlord make every summary go out at most once a day.
|
||
*/
|
||
class SendDigests
|
||
{
|
||
public function __construct(private DigestReport $report) {}
|
||
|
||
/**
|
||
* @return 'daily'|'weekly'|null which summary was due (also when it was skipped for being empty)
|
||
*/
|
||
public function handle(Tenant $tenant): ?string
|
||
{
|
||
$now = CarbonImmutable::now();
|
||
$today = $now->startOfDay();
|
||
if ($now->hour < $tenant->digest_hour) {
|
||
return null;
|
||
}
|
||
|
||
$weekly = $tenant->digest_weekly && $today->isoWeekday() === $tenant->digest_weekday && ! $tenant->digest_weekly_sent_on?->isSameDay($today);
|
||
$daily = $tenant->digest_daily && ! $tenant->digest_daily_sent_on?->isSameDay($today);
|
||
if (! $weekly && ! $daily) {
|
||
return null;
|
||
}
|
||
|
||
$this->send($tenant, $weekly);
|
||
$tenant->forceFill([
|
||
'digest_daily_sent_on' => $today->toDateString(),
|
||
...($weekly ? ['digest_weekly_sent_on' => $today->toDateString()] : []),
|
||
])->save();
|
||
|
||
return $weekly ? 'weekly' : 'daily';
|
||
}
|
||
|
||
/**
|
||
* Builds and mails one summary. Without anything to report it is skipped unless the landlord wants it
|
||
* anyway or $force is set (button „Zusammenfassung jetzt senden“).
|
||
*
|
||
* @return bool whether a mail was sent
|
||
*/
|
||
public function send(Tenant $tenant, bool $weekly, bool $force = false): bool
|
||
{
|
||
$since = CarbonImmutable::now()->sub($weekly ? '7 days' : '1 day');
|
||
$sections = $this->report->build(DigestSection::chosen($tenant->digest_sections), $weekly, $since);
|
||
|
||
if ($sections === [] && ! $tenant->digest_when_empty && ! $force) {
|
||
return false;
|
||
}
|
||
|
||
Mail::to(Settings::notificationRecipients())->send(new Digest($weekly, $sections));
|
||
|
||
return true;
|
||
}
|
||
}
|