| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | use FireflyIII\Support\Facades\Preferences; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2022-09-23 06:05:22 +02:00
										 |  |  | use Illuminate\Support\Facades\Notification; | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:56:10 +02:00
										 |  |  | 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 07:59:38 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |         Log::debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							|  |  |  |         // make sure user does not get the warning twice.
 | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $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 */ | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |             $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:38:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $toBeWarned[] = $item; | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |         unset($bill); | 
					
						
							|  |  |  |         Log::debug(sprintf('Now %d bills to warn about.', count($toBeWarned))); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         /** @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:38:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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:38:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |             Preferences::setForUser($bill->user, $key, true); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         Log::warning('should hit this ONCE'); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 07:59:38 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											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:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |             Log::error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |     public function warnAboutBill(WarnUserAboutBill $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-23 11:14:52 +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) { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |             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)); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 $message = $e->getMessage(); | 
					
						
							|  |  |  |                 if (str_contains($message, 'Bcc')) { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |                     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')) { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |                     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; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |                 Log::error($e->getMessage()); | 
					
						
							|  |  |  |                 Log::error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2022-09-23 06:05:22 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +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
										 |  |  | } |