| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * RenderPartialViews.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +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-08-10 18:19:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +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-08-10 18:19:51 +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-08-10 18:19:51 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Http\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  | use FireflyIII\Enums\AccountTypeEnum; | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | use FireflyIII\Helpers\Report\PopupReportInterface; | 
					
						
							| 
									
										
										
										
											2019-09-03 17:30:45 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2018-09-06 19:47:29 +02:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  | use FireflyIII\Models\Rule; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleAction; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleTrigger; | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Tag\TagRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-08-27 06:19:16 +02:00
										 |  |  | use FireflyIII\Support\Search\OperatorQuerySearch; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use Throwable; | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait RenderPartialViews | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait RenderPartialViews | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * View for transactions in a budget for an account. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function budgetEntry(array $attributes): string // generate view for report.
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var PopupReportInterface $popupHelper */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $popupHelper      = app(PopupReportInterface::class); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var BudgetRepositoryInterface $budgetRepository */ | 
					
						
							|  |  |  |         $budgetRepository = app(BudgetRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $budget           = $budgetRepository->find((int) $attributes['budgetId']); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $accountRepos     = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $account          = $accountRepos->find((int) $attributes['accountId']); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |         if (null === $budget || null === $account) { | 
					
						
							| 
									
										
										
										
											2023-11-29 06:30:35 +01:00
										 |  |  |             throw new FireflyException('Could not render popup.report.balance-amount because budget or account is null.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $journals         = $popupHelper->balanceForBudget($budget, $account, $attributes); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |             $view = 'Firefly III could not render the view. Please see the log files.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($view, 0, $e); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         return $view; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get options for budget report. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function budgetReportOptions(): string // render a view
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var BudgetRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(BudgetRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2024-11-08 21:03:33 +01:00
										 |  |  |         $budgets    = $repository->getActiveBudgets(); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.options.budget', compact('budgets'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * View for spent in a single budget. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function budgetSpentAmount(array $attributes): string // generate view for report.
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var BudgetRepositoryInterface $budgetRepository */ | 
					
						
							|  |  |  |         $budgetRepository = app(BudgetRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var PopupReportInterface $popupHelper */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $popupHelper      = app(PopupReportInterface::class); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $budget           = $budgetRepository->find((int) $attributes['budgetId']); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         if (null === $budget) { | 
					
						
							| 
									
										
										
										
											2024-09-29 16:04:54 +02:00
										 |  |  |             // transactions without a budget.
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             $budget = new Budget(); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $journals         = $popupHelper->byBudget($budget, $attributes); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $view = 'Firefly III could not render the view. Please see the log files.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($view, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $view; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * View for transactions in a category. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function categoryEntry(array $attributes): string // generate view for report.
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var PopupReportInterface $popupHelper */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $popupHelper        = app(PopupReportInterface::class); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var CategoryRepositoryInterface $categoryRepository */ | 
					
						
							|  |  |  |         $categoryRepository = app(CategoryRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $category           = $categoryRepository->find((int) $attributes['categoryId']); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         $journals           = $popupHelper->byCategory($category, $attributes); | 
					
						
							| 
									
										
										
										
											2020-10-01 07:31:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $view = view('popup.report.category-entry', compact('journals', 'category'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $view = 'Firefly III could not render the view. Please see the log files.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($view, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $view; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get options for category report. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function categoryReportOptions(): string // render a view
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var CategoryRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(CategoryRepositoryInterface::class); | 
					
						
							|  |  |  |         $categories = $repository->getCategories(); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.options.category', compact('categories'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Cannot render reports.options.category: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get options for double report. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function doubleReportOptions(): string // render a view
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  |         $expense    = $repository->getActiveAccountsByType([AccountTypeEnum::EXPENSE->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value]); | 
					
						
							|  |  |  |         $revenue    = $repository->getActiveAccountsByType([AccountTypeEnum::REVENUE->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value]); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $set        = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($expense as $account) { | 
					
						
							|  |  |  |             // loop revenue, find same account:
 | 
					
						
							|  |  |  |             /** @var Account $otherAccount */ | 
					
						
							|  |  |  |             foreach ($revenue as $otherAccount) { | 
					
						
							|  |  |  |                 if ( | 
					
						
							|  |  |  |                     ( | 
					
						
							|  |  |  |                         ($otherAccount->name === $account->name) | 
					
						
							|  |  |  |                         || (null !== $account->iban && null !== $otherAccount->iban && $otherAccount->iban === $account->iban) | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                     && $otherAccount->id !== $account->id | 
					
						
							|  |  |  |                 ) { | 
					
						
							|  |  |  |                     $set[] = $account; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.options.double', compact('set'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns all the expenses that went to the given expense account. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function expenseEntry(array $attributes): string // generate view for report.
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var PopupReportInterface $popupHelper */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $popupHelper       = app(PopupReportInterface::class); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $account           = $accountRepository->find((int) $attributes['accountId']); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $account) { | 
					
						
							|  |  |  |             return 'This is an unknown account. Apologies.'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $journals          = $popupHelper->byExpenses($account, $attributes); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $view = view('popup.report.expense-entry', compact('journals', 'account'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $view = 'Firefly III could not render the view. Please see the log files.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($view, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $view; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get current (from system) rule actions. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function getCurrentActions(Rule $rule): array // get info from object and present.
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $index          = 0; | 
					
						
							|  |  |  |         $actions        = []; | 
					
						
							| 
									
										
										
										
											2021-06-12 08:40:35 +02:00
										 |  |  |         // must be repos
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |         $currentActions = $rule->ruleActions()->orderBy('order', 'ASC')->get(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |         /** @var RuleAction $entry */ | 
					
						
							|  |  |  |         foreach ($currentActions as $entry) { | 
					
						
							|  |  |  |             $count = ($index + 1); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |                 $actions[] = view( | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                     'rules.partials.action', | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         'oldAction'  => $entry->action_type, | 
					
						
							|  |  |  |                         'oldValue'   => $entry->action_value, | 
					
						
							|  |  |  |                         'oldChecked' => $entry->stop_processing, | 
					
						
							|  |  |  |                         'count'      => $count, | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 )->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |             } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Throwable was thrown in getCurrentActions(): %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 06:40:12 +02:00
										 |  |  |                 throw new FireflyException(sprintf('Could not render: %s', $e->getMessage()), 0, $e); | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |             ++$index; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $actions; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get current (from DB) rule triggers. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function getCurrentTriggers(Rule $rule): array // get info from object and present.
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 11:43:17 +01:00
										 |  |  |         // TODO duplicated code.
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $operators       = config('search.operators'); | 
					
						
							|  |  |  |         $triggers        = []; | 
					
						
							| 
									
										
										
										
											2020-08-27 06:19:16 +02:00
										 |  |  |         foreach ($operators as $key => $operator) { | 
					
						
							|  |  |  |             if ('user_action' !== $key && false === $operator['alias']) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key)); | 
					
						
							| 
									
										
										
										
											2020-08-27 06:19:16 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asort($triggers); | 
					
						
							|  |  |  |         $index           = 0; | 
					
						
							|  |  |  |         $renderedEntries = []; | 
					
						
							| 
									
										
										
										
											2021-06-12 08:40:35 +02:00
										 |  |  |         // must be repos
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |         $currentTriggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |         /** @var RuleTrigger $entry */ | 
					
						
							|  |  |  |         foreach ($currentTriggers as $entry) { | 
					
						
							|  |  |  |             if ('user_action' !== $entry->trigger_type) { | 
					
						
							|  |  |  |                 $count = ($index + 1); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                 try { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                     $rootOperator      = OperatorQuerySearch::getRootOperator((string) $entry->trigger_type); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |                     if (str_starts_with($rootOperator, '-')) { | 
					
						
							| 
									
										
										
										
											2022-10-01 07:34:30 +02:00
										 |  |  |                         $rootOperator = substr($rootOperator, 1); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |                     $renderedEntries[] = view( | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                         'rules.partials.trigger', | 
					
						
							|  |  |  |                         [ | 
					
						
							| 
									
										
										
										
											2022-10-01 07:34:30 +02:00
										 |  |  |                             'oldTrigger'    => $rootOperator, | 
					
						
							|  |  |  |                             'oldValue'      => $entry->trigger_value, | 
					
						
							|  |  |  |                             'oldChecked'    => $entry->stop_processing, | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                             'oldProhibited' => str_starts_with((string) $entry->trigger_type, '-'), | 
					
						
							| 
									
										
										
										
											2022-10-01 07:34:30 +02:00
										 |  |  |                             'count'         => $count, | 
					
						
							|  |  |  |                             'triggers'      => $triggers, | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                         ] | 
					
						
							|  |  |  |                     )->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |                 } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                     app('log')->debug(sprintf('Throwable was thrown in getCurrentTriggers(): %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                     app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 06:40:12 +02:00
										 |  |  |                     throw new FireflyException(sprintf('Could not render: %s', $e->getMessage()), 0, $e); | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |                 ++$index; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-27 06:19:16 +02:00
										 |  |  |         return $renderedEntries; | 
					
						
							| 
									
										
										
										
											2018-12-31 08:11:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns all the incomes that went to the given asset account. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function incomeEntry(array $attributes): string // generate view for report.
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var PopupReportInterface $popupHelper */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $popupHelper       = app(PopupReportInterface::class); | 
					
						
							|  |  |  |         $account           = $accountRepository->find((int) $attributes['accountId']); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $account) { | 
					
						
							|  |  |  |             return 'This is an unknown category. Apologies.'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $journals          = $popupHelper->byIncome($account, $attributes); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $view = view('popup.report.income-entry', compact('journals', 'account'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Could not render: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $view = 'Firefly III could not render the view. Please see the log files.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($view, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $view; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get options for default report. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function noReportOptions(): string // render a view
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.options.no-options')->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get options for tag report. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function tagReportOptions(): string // render a view
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TagRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(TagRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-05-04 20:58:43 +02:00
										 |  |  |         $tags       = $repository->get(); | 
					
						
							| 
									
										
										
										
											2019-07-26 17:48:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |             $result = view('reports.options.tag', compact('tags'))->render(); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |             $result = 'Could not render view.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($result, 0, $e); | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 18:19:51 +02:00
										 |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  | } |