| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PopupReport.php | 
					
						
							| 
									
										
										
										
											2020-01-28 08:46:01 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +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/>. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers\Report; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  | use FireflyIII\Helpers\Collector\GroupCollectorInterface; | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\Category; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2021-01-07 20:31:48 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-25 19:09:05 +01:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2023-10-28 15:03:33 +02:00
										 |  |  | use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class PopupReport. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PopupReport implements PopupReportInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-29 21:56:39 +02:00
										 |  |  |      * Collect the transactions for one account and one budget. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function balanceForBudget(Budget $budget, Account $account, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2019-08-28 12:28:23 +02:00
										 |  |  |         $collector->setAccounts(new Collection([$account])) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withBudgetInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							|  |  |  |             ->setRange($attributes['startDate'], $attributes['endDate'])->setBudget($budget) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |      * Collect the transactions for one account and no budget. | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function balanceForNoBudget(Account $account, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         // filter by currency, if set.
 | 
					
						
							|  |  |  |         $currencyId = $attributes['currencyId'] ?? null; | 
					
						
							|  |  |  |         $currency   = null; | 
					
						
							|  |  |  |         if (null !== $currencyId) { | 
					
						
							|  |  |  |             /** @var CurrencyRepositoryInterface $repos */ | 
					
						
							|  |  |  |             $repos    = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $currency = $repos->find((int)$currencyId); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |         $collector | 
					
						
							|  |  |  |             ->setAccounts(new Collection([$account])) | 
					
						
							|  |  |  |             ->setTypes([TransactionType::WITHDRAWAL]) | 
					
						
							| 
									
										
										
										
											2019-08-28 12:28:23 +02:00
										 |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |             ->setRange($attributes['startDate'], $attributes['endDate']) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->withoutBudget() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |         if (null !== $currency) { | 
					
						
							|  |  |  |             $collector->setCurrency($currency); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-30 18:42:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |      * Collect the transactions for a budget. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function byBudget(Budget $budget, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         // filter by currency, if set.
 | 
					
						
							|  |  |  |         $currencyId = $attributes['currencyId'] ?? null; | 
					
						
							|  |  |  |         $currency   = null; | 
					
						
							|  |  |  |         if (null !== $currencyId) { | 
					
						
							|  |  |  |             /** @var CurrencyRepositoryInterface $repos */ | 
					
						
							|  |  |  |             $repos    = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $currency = $repos->find((int)$currencyId); | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2019-08-28 12:28:23 +02:00
										 |  |  |         $collector->setAccounts($attributes['accounts']) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withBudgetInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							|  |  |  |             ->setRange($attributes['startDate'], $attributes['endDate']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         if (null !== $currency) { | 
					
						
							|  |  |  |             $collector->setCurrency($currency); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $budget->id) { | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |             $collector->setTypes([TransactionType::WITHDRAWAL])->withoutBudget(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $budget->id) { | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |             $collector->setBudget($budget); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-07 23:14:16 +02:00
										 |  |  |      * Collect journals by a category. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |     public function byCategory(?Category $category, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         // filter by currency, if set.
 | 
					
						
							|  |  |  |         $currencyId = $attributes['currencyId'] ?? null; | 
					
						
							|  |  |  |         $currency   = null; | 
					
						
							|  |  |  |         if (null !== $currencyId) { | 
					
						
							|  |  |  |             /** @var CurrencyRepositoryInterface $repos */ | 
					
						
							|  |  |  |             $repos    = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $currency = $repos->find((int)$currencyId); | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         $collector->setAccounts($attributes['accounts']) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::DEPOSIT]) | 
					
						
							|  |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withBudgetInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							|  |  |  |             ->setRange($attributes['startDate'], $attributes['endDate'])->withAccountInformation() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         if (null !== $category) { | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |             $collector->setCategory($category); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         if (null === $category) { | 
					
						
							| 
									
										
										
										
											2019-09-01 15:23:33 +02:00
										 |  |  |             $collector->withoutCategory(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         if (null !== $currency) { | 
					
						
							|  |  |  |             $collector->setCurrency($currency); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-07 23:14:16 +02:00
										 |  |  |      * Group transactions by expense. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function byExpenses(Account $account, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         // filter by currency, if set.
 | 
					
						
							|  |  |  |         $currencyId = $attributes['currencyId'] ?? null; | 
					
						
							|  |  |  |         $currency   = null; | 
					
						
							|  |  |  |         if (null !== $currencyId) { | 
					
						
							|  |  |  |             /** @var CurrencyRepositoryInterface $repos */ | 
					
						
							|  |  |  |             $repos    = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             $currency = $repos->find((int)$currencyId); | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 21:17:46 +02:00
										 |  |  |         /** @var JournalRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(JournalRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 20:31:48 +01:00
										 |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $accountRepository->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 20:31:48 +01:00
										 |  |  |         // set report accounts + the request accounts:
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // $set = $attributes['accounts'] ?? new Collection;
 | 
					
						
							|  |  |  |         // $set->push($account);
 | 
					
						
							| 
									
										
										
										
											2021-01-07 20:31:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 11:42:35 +02:00
										 |  |  |         $collector->setDestinationAccounts(new Collection([$account])) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->setRange($attributes['startDate'], $attributes['endDate']) | 
					
						
							|  |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withBudgetInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							|  |  |  |             ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $currency) { | 
					
						
							|  |  |  |             $collector->setCurrency($currency); | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:08:09 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-07 23:14:16 +02:00
										 |  |  |      * Collect transactions by income. | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function byIncome(Account $account, array $attributes): array | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-02-25 19:09:05 +01:00
										 |  |  |         /** @var JournalRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(JournalRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($account->user); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							|  |  |  |         $collector = app(GroupCollectorInterface::class); | 
					
						
							| 
									
										
										
										
											2019-08-14 19:51:46 +02:00
										 |  |  |         $collector | 
					
						
							|  |  |  |             ->setSourceAccounts(new Collection([$account])) | 
					
						
							|  |  |  |             ->setDestinationAccounts($attributes['accounts']) | 
					
						
							|  |  |  |             ->setRange($attributes['startDate'], $attributes['endDate']) | 
					
						
							|  |  |  |             ->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]) | 
					
						
							| 
									
										
										
										
											2019-08-28 12:28:23 +02:00
										 |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |             ->withBudgetInformation() | 
					
						
							|  |  |  |             ->withCategoryInformation() | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->withAccountInformation() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-08-28 12:28:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 19:51:46 +02:00
										 |  |  |         return $collector->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2017-03-29 21:21:10 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-07-07 08:09:42 +02:00
										 |  |  | } |