| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AutomationHandler.php | 
					
						
							| 
									
										
										
										
											2020-01-28 08:45:38 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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/>. | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Handlers\Events; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  | use FireflyIII\Events\RequestedReportOnJournals; | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							|  |  |  | use FireflyIII\Notifications\User\TransactionCreation; | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  | use FireflyIII\Transformers\TransactionGroupTransformer; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | use Illuminate\Support\Facades\Notification; | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AutomationHandler | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AutomationHandler | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |      * Respond to the creation of X journals. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param RequestedReportOnJournals $event | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |     public function reportJournals(RequestedReportOnJournals $event): void | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('In reportJournals.'); | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $user       = $repository->find($event->userId); | 
					
						
							| 
									
										
										
										
											2023-11-01 18:45:15 +01:00
										 |  |  |         /** @var bool $sendReport */ | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |         $sendReport = app('preferences')->getForUser($user, 'notification_transaction_creation', false)->data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 13:19:41 +02:00
										 |  |  |         if (false === $sendReport) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Not sending report, because config says so.'); | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-10-13 13:19:41 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |         if (null === $user || 0 === $event->groups->count()) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('No transaction groups in event, nothing to email about.'); | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Continue with message!'); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |         // transform groups into array:
 | 
					
						
							|  |  |  |         /** @var TransactionGroupTransformer $transformer */ | 
					
						
							|  |  |  |         $transformer = app(TransactionGroupTransformer::class); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |         $groups      = []; | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |         /** @var TransactionGroup $group */ | 
					
						
							|  |  |  |         foreach ($event->groups as $group) { | 
					
						
							|  |  |  |             $groups[] = $transformer->transformObject($group); | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             Notification::send($user, new TransactionCreation($groups)); | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |         } catch (Exception $e) { /** @phpstan-ignore-line */ | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |             $message = $e->getMessage(); | 
					
						
							|  |  |  |             if (str_contains($message, 'Bcc')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                 app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (str_contains($message, 'RFC 2822')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                 app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('If there is no error above this line, message was sent.'); | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |