| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  | use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2022-07-17 08:51:53 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 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; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |     private array               $balances; | 
					
						
							|  |  |  |     private array               $convertedBalances; | 
					
						
							|  |  |  |     private array               $currencies; | 
					
						
							|  |  |  |     private TransactionCurrency $default; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function collectMetaData(Collection $objects): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |         $this->currencies        = []; | 
					
						
							|  |  |  |         $this->accountMeta       = []; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         $this->accountTypes      = []; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |         $this->balances          = app('steam')->balancesByAccounts($objects, $this->getDate()); | 
					
						
							|  |  |  |         $this->convertedBalances = app('steam')->balancesByAccountsConverted($objects, $this->getDate()); | 
					
						
							| 
									
										
										
										
											2023-10-28 14:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var CurrencyRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2023-10-28 15:03:33 +02:00
										 |  |  |         $repository    = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->default = app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get currencies:
 | 
					
						
							|  |  |  |         $accountIds  = $objects->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $meta        = AccountMeta::whereIn('account_id', $accountIds) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->where('name', 'currency_id') | 
					
						
							|  |  |  |             ->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         $currencyIds = $meta->pluck('data')->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $currencies = $repository->getByIds($currencyIds); | 
					
						
							|  |  |  |         foreach ($currencies as $currency) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |             $id                    = $currency->id; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->currencies[$id] = $currency; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         foreach ($meta as $entry) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             $id                                   = $entry->account_id; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->accountMeta[$id][$entry->name] = $entry->data; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         // get account types:
 | 
					
						
							|  |  |  |         // select accounts.id, account_types.type from account_types left join accounts on accounts.account_type_id = account_types.id;
 | 
					
						
							|  |  |  |         $accountTypes = AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id') | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->whereIn('accounts.id', $accountIds) | 
					
						
							|  |  |  |             ->get(['accounts.id', 'account_types.type']) | 
					
						
							|  |  |  |         ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         /** @var AccountType $row */ | 
					
						
							|  |  |  |         foreach ($accountTypes as $row) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             $this->accountTypes[$row->id] = (string)config(sprintf('firefly.shortNamesByFullName.%s', $row->type)); | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Transform the account. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(Account $account): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |         $id = $account->id; | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         // various meta
 | 
					
						
							|  |  |  |         $accountRole = $this->accountMeta[$id]['account_role'] ?? null; | 
					
						
							|  |  |  |         $accountType = $this->accountTypes[$id]; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |         $order       = $account->order; | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  |         // no currency? use default
 | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |         $currency = $this->default; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         if (0 !== (int)$this->accountMeta[$id]['currency_id']) { | 
					
						
							|  |  |  |             $currency = $this->currencies[(int)$this->accountMeta[$id]['currency_id']]; | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |         // amounts and calculation.
 | 
					
						
							|  |  |  |         $balance       = $this->balances[$id] ?? null; | 
					
						
							|  |  |  |         $nativeBalance = $this->convertedBalances[$id]['native_balance'] ?? null; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |         // no order for some accounts:
 | 
					
						
							|  |  |  |         if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) { | 
					
						
							|  |  |  |             $order = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             'id'                      => (string)$account->id, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'created_at'              => $account->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'              => $account->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'active'                  => $account->active, | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |             'order'                   => $order, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'name'                    => $account->name, | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |             'iban'                    => '' === $account->iban ? null : $account->iban, | 
					
						
							| 
									
										
										
										
											2023-08-27 07:45:09 +02:00
										 |  |  |             'type'                    => strtolower($accountType), | 
					
						
							|  |  |  |             'account_role'            => $accountRole, | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |             'currency_id'             => (string)$currency->id, | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  |             'currency_code'           => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'         => $currency->symbol, | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |             'currency_decimal_places' => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |             'native_currency_id'             => (string)$this->default->id, | 
					
						
							|  |  |  |             'native_currency_code'           => $this->default->code, | 
					
						
							|  |  |  |             'native_currency_symbol'         => $this->default->symbol, | 
					
						
							| 
									
										
										
										
											2023-11-26 12:24:37 +01:00
										 |  |  |             'native_currency_decimal_places' => $this->default->decimal_places, | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // balance:
 | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |             'current_balance'                => $balance, | 
					
						
							|  |  |  |             'native_current_balance'         => $nativeBalance, | 
					
						
							|  |  |  |             'current_balance_date'           => $this->getDate(), | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // more meta
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 16:41:28 +02:00
										 |  |  |             //            'notes'                   => $this->repository->getNoteText($account),
 | 
					
						
							|  |  |  |             //            'monthly_payment_date'    => $monthlyPaymentDate,
 | 
					
						
							|  |  |  |             //            'credit_card_type'        => $creditCardType,
 | 
					
						
							|  |  |  |             //            'account_number'          => $this->repository->getMetaValue($account, 'account_number'),
 | 
					
						
							|  |  |  |             //            'bic'                     => $this->repository->getMetaValue($account, 'BIC'),
 | 
					
						
							|  |  |  |             //            'virtual_balance'         => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''),
 | 
					
						
							|  |  |  |             //            'opening_balance'         => $openingBalance,
 | 
					
						
							|  |  |  |             //            'opening_balance_date'    => $openingBalanceDate,
 | 
					
						
							|  |  |  |             //            'liability_type'          => $liabilityType,
 | 
					
						
							|  |  |  |             //            'liability_direction'     => $liabilityDirection,
 | 
					
						
							|  |  |  |             //            'interest'                => $interest,
 | 
					
						
							|  |  |  |             //            'interest_period'         => $interestPeriod,
 | 
					
						
							|  |  |  |             //            'current_debt'            => $this->repository->getMetaValue($account, 'current_debt'),
 | 
					
						
							|  |  |  |             //            'include_net_worth'       => $includeNetWorth,
 | 
					
						
							|  |  |  |             //            'longitude'               => $longitude,
 | 
					
						
							|  |  |  |             //            'latitude'                => $latitude,
 | 
					
						
							|  |  |  |             //            'zoom_level'              => $zoomLevel,
 | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |             'links'                          => [ | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                     'uri' => '/accounts/'.$account->id, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function getDate(): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $date = today(config('app.timezone')); | 
					
						
							|  |  |  |         if (null !== $this->parameters->get('date')) { | 
					
						
							|  |  |  |             $date = $this->parameters->get('date'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | } |