Files
firefly-iii/app/Handlers/Events/BillEventHandler.php

144 lines
5.3 KiB
PHP
Raw Normal View History

2022-03-28 12:23:46 +02:00
<?php
2022-03-29 14:55:51 +02:00
/*
* BillEventHandler.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2022-03-28 12:23:46 +02:00
namespace FireflyIII\Handlers\Events;
use Exception;
use FireflyIII\Events\Model\Bill\WarnUserAboutBill;
2025-08-09 08:34:18 +02:00
use FireflyIII\Events\Model\Bill\WarnUserAboutOverdueSubscriptions;
use FireflyIII\Models\Bill;
2022-09-23 06:05:22 +02:00
use FireflyIII\Notifications\User\BillReminder;
2025-08-09 08:34:18 +02:00
use FireflyIII\Notifications\User\SubscriptionsOverdueReminder;
use FireflyIII\Support\Facades\Preferences;
use Illuminate\Support\Facades\Log;
2022-09-23 06:05:22 +02:00
use Illuminate\Support\Facades\Notification;
use function Safe\json_encode;
2022-03-28 12:23:46 +02:00
/**
* Class BillEventHandler
*/
class BillEventHandler
{
2025-08-09 08:34:18 +02:00
public function warnAboutOverdueSubscriptions(WarnUserAboutOverdueSubscriptions $event): void
{
2025-08-09 08:34:18 +02:00
Log::debug(sprintf('Now in %s', __METHOD__));
// make sure user does not get the warning twice.
$overdue = $event->overdue;
$user = $event->user;
$toBeWarned = [];
2025-08-09 08:34:18 +02:00
Log::debug(sprintf('%d bills to warn about.', count($overdue)));
foreach ($overdue as $item) {
/** @var Bill $bill */
$bill = $item['bill'];
$key = sprintf('bill_overdue_%s_%s', $bill->id, substr(hash('sha256', json_encode($item['dates']['pay_dates'], JSON_THROW_ON_ERROR)), 0, 10));
$pref = Preferences::getForUser($bill->user, $key, false);
2025-08-09 08:34:18 +02:00
if (true === $pref->data) {
Log::debug(sprintf('User #%d has already been warned about overdue subscription #%d.', $bill->user->id, $bill->id));
2025-08-09 08:34:18 +02:00
continue;
}
$toBeWarned[] = $item;
}
2025-08-09 08:34:18 +02:00
unset($bill);
Log::debug(sprintf('Now %d bills to warn about.', count($toBeWarned)));
/** @var bool $sendNotification */
2025-08-09 08:34:18 +02:00
$sendNotification = Preferences::getForUser($user, 'notification_bill_reminder', true)->data;
if (false === $sendNotification) {
Log::debug('User has disabled bill reminders.');
2025-08-09 08:34:18 +02:00
return;
}
2025-08-09 11:13:03 +02:00
Log::debug(sprintf('Will warn about %d overdue subscription(s).', count($toBeWarned)));
2025-08-09 08:34:18 +02:00
if (0 === count($toBeWarned)) {
Log::debug('No overdue subscriptions to warn about.');
2025-08-09 08:34:18 +02:00
return;
}
foreach ($toBeWarned as $item) {
/** @var Bill $bill */
$bill = $item['bill'];
$key = sprintf('bill_overdue_%s_%s', $bill->id, substr(hash('sha256', json_encode($item['dates']['pay_dates'], JSON_THROW_ON_ERROR)), 0, 10));
Preferences::setForUser($bill->user, $key, true);
2025-08-09 08:34:18 +02:00
}
Log::warning('should hit this ONCE');
2025-08-09 08:34:18 +02:00
try {
2025-08-09 11:13:03 +02:00
Notification::send($user, new SubscriptionsOverdueReminder($toBeWarned));
2025-08-09 08:34:18 +02:00
} catch (Exception $e) {
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
2025-08-09 08:34:18 +02:00
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
2025-08-09 08:34:18 +02:00
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
}
2025-08-09 08:34:18 +02:00
}
2022-03-28 12:23:46 +02:00
public function warnAboutBill(WarnUserAboutBill $event): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
2022-03-28 12:23:46 +02:00
$bill = $event->bill;
2023-12-20 19:35:52 +01:00
2023-01-03 06:48:53 +01:00
/** @var bool $preference */
2025-08-23 08:56:25 +02:00
$preference = Preferences::getForUser($bill->user, 'notification_bill_reminder', true)->data;
2022-03-28 12:23:46 +02:00
2022-09-23 06:05:22 +02:00
if (true === $preference) {
Log::debug('Bill reminder is true!');
2023-12-20 19:35:52 +01:00
2023-06-11 16:12:13 +02:00
try {
Notification::send($bill->user, new BillReminder($bill, $event->field, $event->diff));
} catch (Exception $e) {
2023-06-11 16:12:13 +02:00
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
Log::warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
2023-12-20 19:35:52 +01:00
2023-06-11 16:12:13 +02:00
return;
}
if (str_contains($message, 'RFC 2822')) {
Log::warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
2023-12-20 19:35:52 +01:00
2023-06-11 16:12:13 +02:00
return;
}
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2023-06-11 16:12:13 +02:00
}
return;
2022-09-23 06:05:22 +02:00
}
Log::debug('User has disabled bill reminders.');
2022-03-28 12:23:46 +02:00
}
2022-03-29 14:55:51 +02:00
}