| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * AccountTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2022 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | namespace FireflyIII\Transformers\V2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:35:13 +01:00
										 |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2022-07-17 08:51:53 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountTransformer | 
					
						
							| 
									
										
										
										
											2025-03-14 19:28:44 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-03-14 19:18:17 +01:00
										 |  |  |  * @deprecated | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class AccountTransformer extends AbstractTransformer | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |     private array               $accountMeta; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |     private array               $accountTypes; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |     private array               $balanceDifferences; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |     private array               $convertedBalances; | 
					
						
							|  |  |  |     private array               $currencies; | 
					
						
							|  |  |  |     private TransactionCurrency $default; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     private array               $fullTypes; | 
					
						
							| 
									
										
										
										
											2024-03-16 23:06:16 +01:00
										 |  |  |     private array               $lastActivity; | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  |     private array               $objectGroups; | 
					
						
							| 
									
										
										
										
											2025-01-03 14:56:06 +01:00
										 |  |  |     private array $balances; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |      * This method collects meta-data for one or all accounts in the transformer's collection. | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-09 19:31:27 +01:00
										 |  |  |     public function collectMetaData(Collection $objects): Collection | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $this->currencies         = []; | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         $this->balances           = []; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $this->accountMeta        = []; | 
					
						
							|  |  |  |         $this->accountTypes       = []; | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  |         $this->fullTypes          = []; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $this->lastActivity       = []; | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  |         $this->objectGroups       = []; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $this->convertedBalances  = []; | 
					
						
							|  |  |  |         $this->balanceDifferences = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 06:33:12 +02:00
										 |  |  |         Log::debug(sprintf('collectMetaData on %d object(s)', $objects->count())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 20:20:11 +02:00
										 |  |  |         // first collect all the "heavy" stuff that relies on ALL data to be present.
 | 
					
						
							|  |  |  |         // get last activity:
 | 
					
						
							|  |  |  |         $this->getLastActivity($objects); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         // get balances of all accounts
 | 
					
						
							|  |  |  |         $this->getMetaBalances($objects); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get default currency:
 | 
					
						
							|  |  |  |         $this->getDefaultCurrency(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // collect currency and other meta-data:
 | 
					
						
							|  |  |  |         $this->collectAccountMetaData($objects); | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         // get account types:
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $this->collectAccountTypes($objects); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  |         // add balance difference
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { | 
					
						
							|  |  |  |             $this->getBalanceDifference($objects, $this->parameters->get('start'), $this->parameters->get('end')); | 
					
						
							| 
									
										
										
										
											2024-03-13 06:50:08 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 20:20:11 +02:00
										 |  |  |         // get object groups
 | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  |         $this->getObjectGroups($objects); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 20:22:31 +02:00
										 |  |  |         // sort:
 | 
					
						
							| 
									
										
										
										
											2024-04-30 20:33:00 +02:00
										 |  |  |         $objects                  = $this->sortAccounts($objects); | 
					
						
							| 
									
										
										
										
											2024-04-30 20:22:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 20:20:11 +02:00
										 |  |  |         // if pagination is disabled, do it now:
 | 
					
						
							| 
									
										
										
										
											2024-04-30 20:22:31 +02:00
										 |  |  |         if (true === $this->parameters->get('disablePagination')) { | 
					
						
							|  |  |  |             $page    = (int) $this->parameters->get('page'); | 
					
						
							|  |  |  |             $size    = (int) $this->parameters->get('pageSize'); | 
					
						
							|  |  |  |             $objects = $objects->slice(($page - 1) * $size, $size); | 
					
						
							| 
									
										
										
										
											2024-04-29 20:20:11 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 20:22:31 +02:00
										 |  |  |         return $objects; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     private function getLastActivity(Collection $accounts): void | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $lastActivity      = $accountRepository->getLastActivity($accounts); | 
					
						
							|  |  |  |         foreach ($lastActivity as $row) { | 
					
						
							|  |  |  |             $this->lastActivity[(int) $row['account_id']] = Carbon::parse($row['date_max'], config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function getMetaBalances(Collection $accounts): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:32:58 +01:00
										 |  |  |             $this->convertedBalances = app('steam')->finalAccountsBalance($accounts, $this->getDate()); | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         } catch (FireflyException $e) { | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     private function getDate(): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $date = today(config('app.timezone')); | 
					
						
							|  |  |  |         if (null !== $this->parameters->get('date')) { | 
					
						
							|  |  |  |             $date = $this->parameters->get('date'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |     private function getDefaultCurrency(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $this->default = app('amount')->getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAccountMetaData(Collection $accounts): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var CurrencyRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2024-04-30 20:33:00 +02:00
										 |  |  |         $repository        = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  |         $metaFields        = $accountRepository->getMetaValues($accounts, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']); | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $currencyIds       = $metaFields->where('name', 'currency_id')->pluck('data')->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 20:33:00 +02:00
										 |  |  |         $currencies        = $repository->getByIds($currencyIds); | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         foreach ($currencies as $currency) { | 
					
						
							|  |  |  |             $id                    = $currency->id; | 
					
						
							|  |  |  |             $this->currencies[$id] = $currency; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         foreach ($metaFields as $entry) { | 
					
						
							|  |  |  |             $id                                   = $entry->account_id; | 
					
						
							|  |  |  |             $this->accountMeta[$id][$entry->name] = $entry->data; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAccountTypes(Collection $accounts): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $accountTypes      = $accountRepository->getAccountTypes($accounts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountType $row */ | 
					
						
							|  |  |  |         foreach ($accountTypes as $row) { | 
					
						
							|  |  |  |             $this->accountTypes[$row->id] = (string) config(sprintf('firefly.shortNamesByFullName.%s', $row->type)); | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  |             $this->fullTypes[$row->id]    = $row->type; | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     private function getBalanceDifference(Collection $accounts, Carbon $start, Carbon $end): void | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  |         if ('en_US' === config('app.fallback_locale')) { | 
					
						
							| 
									
										
										
										
											2025-01-04 09:01:44 +01:00
										 |  |  |             throw new FireflyException('Used deprecated method, rethink this.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         // collect balances, start and end for both native and converted.
 | 
					
						
							|  |  |  |         // yes the b is usually used for boolean by idiots but here it's for balance.
 | 
					
						
							|  |  |  |         $bStart = []; | 
					
						
							|  |  |  |         $bEnd   = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-02-11 06:13:28 +01:00
										 |  |  |             Log::debug(sprintf('v2 transformer: finalAccountsBalance("%s")', $start->format('Y-m-d H:i:s'))); | 
					
						
							|  |  |  |             Log::debug(sprintf('v2 transformer: finalAccountsBalance("%s")', $end->format('Y-m-d H:i:s'))); | 
					
						
							| 
									
										
										
										
											2024-12-22 20:32:58 +01:00
										 |  |  |             $bStart = app('steam')->finalAccountsBalance($accounts, $start); | 
					
						
							|  |  |  |             $bEnd   = app('steam')->finalAccountsBalance($accounts, $end); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         } catch (FireflyException $e) { | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							|  |  |  |             $id = $account->id; | 
					
						
							|  |  |  |             if (array_key_exists($id, $bStart) && array_key_exists($id, $bEnd)) { | 
					
						
							|  |  |  |                 $this->balanceDifferences[$id] = [ | 
					
						
							|  |  |  |                     'balance'        => bcsub($bEnd[$id]['balance'], $bStart[$id]['balance']), | 
					
						
							|  |  |  |                     'native_balance' => bcsub($bEnd[$id]['native_balance'], $bStart[$id]['native_balance']), | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     private function getObjectGroups(Collection $accounts): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository  = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->objectGroups = $accountRepository->getObjectGroups($accounts); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |     private function sortAccounts(Collection $accounts): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var null|array $sort */ | 
					
						
							|  |  |  |         $sort = $this->parameters->get('sort'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null === $sort || 0 === count($sort)) { | 
					
						
							|  |  |  |             return $accounts; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * @var string $column | 
					
						
							|  |  |  |          * @var string $direction | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         foreach ($sort as $column => $direction) { | 
					
						
							|  |  |  |             // account_number + iban
 | 
					
						
							|  |  |  |             if ('iban' === $column) { | 
					
						
							|  |  |  |                 $accounts = $this->sortByIban($accounts, $direction); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ('balance' === $column) { | 
					
						
							|  |  |  |                 $accounts = $this->sortByBalance($accounts, $direction); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ('last_activity' === $column) { | 
					
						
							|  |  |  |                 $accounts = $this->sortByLastActivity($accounts, $direction); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ('balance_difference' === $column) { | 
					
						
							|  |  |  |                 $accounts = $this->sortByBalanceDifference($accounts, $direction); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  |             if ('current_debt' === $column) { | 
					
						
							|  |  |  |                 $accounts = $this->sortByCurrentDebt($accounts, $direction); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         return $accounts; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function sortByIban(Collection $accounts, string $direction): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $meta = $this->accountMeta; | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         return $accounts->sort(function (Account $left, Account $right) use ($meta, $direction) { | 
					
						
							|  |  |  |             $leftIban  = trim(sprintf('%s%s', $left->iban, $meta[$left->id]['account_number'] ?? '')); | 
					
						
							|  |  |  |             $rightIban = trim(sprintf('%s%s', $right->iban, $meta[$right->id]['account_number'] ?? '')); | 
					
						
							|  |  |  |             if ('asc' === $direction) { | 
					
						
							|  |  |  |                 return strcasecmp($leftIban, $rightIban); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return strcasecmp($rightIban, $leftIban); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function sortByBalance(Collection $accounts, string $direction): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $balances = $this->convertedBalances; | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         return $accounts->sort(function (Account $left, Account $right) use ($balances, $direction) { | 
					
						
							|  |  |  |             $leftBalance  = (float) ($balances[$left->id]['native_balance'] ?? 0); | 
					
						
							|  |  |  |             $rightBalance = (float) ($balances[$right->id]['native_balance'] ?? 0); | 
					
						
							|  |  |  |             if ('asc' === $direction) { | 
					
						
							|  |  |  |                 return $leftBalance <=> $rightBalance; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $rightBalance <=> $leftBalance; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function sortByLastActivity(Collection $accounts, string $direction): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $dates = $this->lastActivity; | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         return $accounts->sort(function (Account $left, Account $right) use ($dates, $direction) { | 
					
						
							|  |  |  |             $leftDate  = $dates[$left->id] ?? Carbon::create(1900, 1, 1, 0, 0, 0); | 
					
						
							|  |  |  |             $rightDate = $dates[$right->id] ?? Carbon::create(1900, 1, 1, 0, 0, 0); | 
					
						
							|  |  |  |             if ('asc' === $direction) { | 
					
						
							|  |  |  |                 return $leftDate->gt($rightDate) ? 1 : -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $rightDate->gt($leftDate) ? 1 : -1; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  |     private function sortByBalanceDifference(Collection $accounts, string $direction): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         $balances = $this->balanceDifferences; | 
					
						
							| 
									
										
										
										
											2024-04-21 17:23:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-20 16:18:41 +02:00
										 |  |  |         return $accounts->sort(function (Account $left, Account $right) use ($balances, $direction) { | 
					
						
							|  |  |  |             $leftBalance  = (float) ($balances[$left->id]['native_balance'] ?? 0); | 
					
						
							|  |  |  |             $rightBalance = (float) ($balances[$right->id]['native_balance'] ?? 0); | 
					
						
							|  |  |  |             if ('asc' === $direction) { | 
					
						
							|  |  |  |                 return $leftBalance <=> $rightBalance; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $rightBalance <=> $leftBalance; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-28 13:30:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function sortByCurrentDebt(Collection $accounts, string $direction): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $amounts = $this->accountMeta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $accounts->sort(function (Account $left, Account $right) use ($amounts, $direction) { | 
					
						
							|  |  |  |             $leftCurrent  = (float) ($amounts[$left->id]['current_debt'] ?? 0); | 
					
						
							|  |  |  |             $rightCurrent = (float) ($amounts[$right->id]['current_debt'] ?? 0); | 
					
						
							|  |  |  |             if ('asc' === $direction) { | 
					
						
							|  |  |  |                 return $leftCurrent <=> $rightCurrent; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $rightCurrent <=> $leftCurrent; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Transform the account. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(Account $account): array | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $id                 = $account->id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // various meta
 | 
					
						
							|  |  |  |         $accountRole        = $this->accountMeta[$id]['account_role'] ?? null; | 
					
						
							|  |  |  |         $accountType        = $this->accountTypes[$id]; | 
					
						
							|  |  |  |         $order              = $account->order; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // liability type
 | 
					
						
							|  |  |  |         $liabilityType      = 'liabilities' === $accountType ? $this->fullTypes[$id] : null; | 
					
						
							|  |  |  |         $liabilityDirection = $this->accountMeta[$id]['liability_direction'] ?? null; | 
					
						
							|  |  |  |         $interest           = $this->accountMeta[$id]['interest'] ?? null; | 
					
						
							|  |  |  |         $interestPeriod     = $this->accountMeta[$id]['interest_period'] ?? null; | 
					
						
							|  |  |  |         $currentDebt        = $this->accountMeta[$id]['current_debt'] ?? null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // no currency? use default
 | 
					
						
							|  |  |  |         $currency           = $this->default; | 
					
						
							|  |  |  |         if (array_key_exists($id, $this->accountMeta) && 0 !== (int) ($this->accountMeta[$id]['currency_id'] ?? 0)) { | 
					
						
							|  |  |  |             $currency = $this->currencies[(int) $this->accountMeta[$id]['currency_id']]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // amounts and calculation.
 | 
					
						
							|  |  |  |         $balance            = $this->balances[$id]['balance'] ?? null; | 
					
						
							|  |  |  |         $nativeBalance      = $this->convertedBalances[$id]['native_balance'] ?? null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // no order for some accounts:
 | 
					
						
							|  |  |  |         if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) { | 
					
						
							|  |  |  |             $order = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // object group
 | 
					
						
							|  |  |  |         $objectGroupId      = $this->objectGroups[$id]['id'] ?? null; | 
					
						
							|  |  |  |         $objectGroupOrder   = $this->objectGroups[$id]['order'] ?? null; | 
					
						
							|  |  |  |         $objectGroupTitle   = $this->objectGroups[$id]['title'] ?? null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // balance difference
 | 
					
						
							|  |  |  |         $diffStart          = null; | 
					
						
							|  |  |  |         $diffEnd            = null; | 
					
						
							|  |  |  |         $balanceDiff        = null; | 
					
						
							|  |  |  |         $nativeBalanceDiff  = null; | 
					
						
							|  |  |  |         if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { | 
					
						
							|  |  |  |             $diffStart         = $this->parameters->get('start')->toAtomString(); | 
					
						
							|  |  |  |             $diffEnd           = $this->parameters->get('end')->toAtomString(); | 
					
						
							|  |  |  |             $balanceDiff       = $this->balanceDifferences[$id]['balance'] ?? null; | 
					
						
							|  |  |  |             $nativeBalanceDiff = $this->balanceDifferences[$id]['native_balance'] ?? null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'id'                             => (string) $account->id, | 
					
						
							|  |  |  |             'created_at'                     => $account->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'                     => $account->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'active'                         => $account->active, | 
					
						
							|  |  |  |             'order'                          => $order, | 
					
						
							|  |  |  |             'name'                           => $account->name, | 
					
						
							|  |  |  |             'iban'                           => '' === (string) $account->iban ? null : $account->iban, | 
					
						
							|  |  |  |             'account_number'                 => $this->accountMeta[$id]['account_number'] ?? null, | 
					
						
							|  |  |  |             'type'                           => strtolower($accountType), | 
					
						
							|  |  |  |             'account_role'                   => $accountRole, | 
					
						
							|  |  |  |             'currency_id'                    => (string) $currency->id, | 
					
						
							|  |  |  |             'currency_code'                  => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'                => $currency->symbol, | 
					
						
							|  |  |  |             'currency_decimal_places'        => $currency->decimal_places, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             'native_currency_id'             => (string) $this->default->id, | 
					
						
							|  |  |  |             'native_currency_code'           => $this->default->code, | 
					
						
							|  |  |  |             'native_currency_symbol'         => $this->default->symbol, | 
					
						
							|  |  |  |             'native_currency_decimal_places' => $this->default->decimal_places, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // balance:
 | 
					
						
							|  |  |  |             'current_balance'                => $balance, | 
					
						
							|  |  |  |             'native_current_balance'         => $nativeBalance, | 
					
						
							|  |  |  |             'current_balance_date'           => $this->getDate()->endOfDay()->toAtomString(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // balance difference
 | 
					
						
							|  |  |  |             'balance_difference'             => $balanceDiff, | 
					
						
							|  |  |  |             'native_balance_difference'      => $nativeBalanceDiff, | 
					
						
							|  |  |  |             'balance_difference_start'       => $diffStart, | 
					
						
							|  |  |  |             'balance_difference_end'         => $diffEnd, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // more meta
 | 
					
						
							|  |  |  |             'last_activity'                  => array_key_exists($id, $this->lastActivity) ? $this->lastActivity[$id]->toAtomString() : null, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // liability stuff
 | 
					
						
							|  |  |  |             'liability_type'                 => $liabilityType, | 
					
						
							|  |  |  |             'liability_direction'            => $liabilityDirection, | 
					
						
							|  |  |  |             'interest'                       => $interest, | 
					
						
							|  |  |  |             'interest_period'                => $interestPeriod, | 
					
						
							|  |  |  |             'current_debt'                   => $currentDebt, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // object group
 | 
					
						
							|  |  |  |             'object_group_id'                => null !== $objectGroupId ? (string) $objectGroupId : null, | 
					
						
							|  |  |  |             'object_group_order'             => $objectGroupOrder, | 
					
						
							|  |  |  |             'object_group_title'             => $objectGroupTitle, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //            'notes'                   => $this->repository->getNoteText($account),
 | 
					
						
							|  |  |  |             //            'monthly_payment_date'    => $monthlyPaymentDate,
 | 
					
						
							|  |  |  |             //            'credit_card_type'        => $creditCardType,
 | 
					
						
							|  |  |  |             //            'bic'                     => $this->repository->getMetaValue($account, 'BIC'),
 | 
					
						
							|  |  |  |             //            'virtual_balance'         => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''),
 | 
					
						
							|  |  |  |             //            'opening_balance'         => $openingBalance,
 | 
					
						
							|  |  |  |             //            'opening_balance_date'    => $openingBalanceDate,
 | 
					
						
							|  |  |  |             //            'include_net_worth'       => $includeNetWorth,
 | 
					
						
							|  |  |  |             //            'longitude'               => $longitude,
 | 
					
						
							|  |  |  |             //            'latitude'                => $latitude,
 | 
					
						
							|  |  |  |             //            'zoom_level'              => $zoomLevel,
 | 
					
						
							|  |  |  |             'links'                          => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							|  |  |  |                     'uri' => '/accounts/'.$account->id, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2024-04-28 14:40:22 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | } |