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; } }