| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountController.php | 
					
						
							|  |  |  |  * Copyright (c) 2020 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Controllers\Autocomplete; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; | 
					
						
							| 
									
										
										
										
											2024-12-28 18:38:19 +01:00
										 |  |  | use FireflyIII\Enums\AccountTypeEnum; | 
					
						
							| 
									
										
										
										
											2025-08-15 19:50:12 +02:00
										 |  |  | use FireflyIII\Enums\UserRoleEnum; | 
					
						
							| 
									
										
										
										
											2021-09-18 05:26:31 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-03-21 05:51:24 +01:00
										 |  |  | use FireflyIII\Support\Debug\Timer; | 
					
						
							| 
									
										
										
										
											2025-08-09 16:38:32 +02:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											2024-12-23 08:26:35 +01:00
										 |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | use FireflyIII\Support\Http\Api\AccountFilter; | 
					
						
							|  |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2025-02-10 16:47:59 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use AccountFilter; | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-01 12:01:50 +01:00
										 |  |  |     // this array only exists to test if the constructor will use it properly.
 | 
					
						
							| 
									
										
										
										
											2025-08-16 06:13:15 +02:00
										 |  |  |     protected array $accepts       = ['application/json', 'application/vnd.api+json']; | 
					
						
							| 
									
										
										
										
											2025-08-15 19:50:12 +02:00
										 |  |  |     protected array $acceptedRoles = [UserRoleEnum::READ_ONLY]; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 06:32:41 +01:00
										 |  |  |     /** @var array<int, string> */ | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |     private array                      $balanceTypes; | 
					
						
							|  |  |  |     private AccountRepositoryInterface $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-07-21 06:14:47 +02:00
										 |  |  |      * AccountController constructor. | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2025-08-15 19:50:12 +02:00
										 |  |  |                 $this->validateUserGroup($request); | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |                 $this->repository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2025-08-15 19:50:12 +02:00
										 |  |  |                 $this->repository->setUser($this->user); | 
					
						
							|  |  |  |                 $this->repository->setUserGroup($this->userGroup); | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-12-28 18:38:19 +01:00
										 |  |  |         $this->balanceTypes = [AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value]; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 05:26:31 +02:00
										 |  |  |      * Documentation for this endpoint: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/autocomplete/getAccountsAC
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:26:31 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-03-30 20:09:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function accounts(AutocompleteRequest $request): JsonResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 16:44:06 +02:00
										 |  |  |         $data        = $request->getData(); | 
					
						
							|  |  |  |         $types       = $data['types']; | 
					
						
							|  |  |  |         $query       = $data['query']; | 
					
						
							|  |  |  |         $date        = $data['date'] ?? today(config('app.timezone')); | 
					
						
							|  |  |  |         $return      = []; | 
					
						
							| 
									
										
										
										
											2025-08-10 06:42:59 +02:00
										 |  |  |         $timer       = Timer::getInstance(); | 
					
						
							| 
									
										
										
										
											2025-08-10 06:38:23 +02:00
										 |  |  |         $timer->start(sprintf('AC accounts "%s"', $query)); | 
					
						
							| 
									
										
										
										
											2025-08-09 16:44:06 +02:00
										 |  |  |         $result      = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit')); | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 16:47:59 +01:00
										 |  |  |         // set date to subday + end-of-day for account balance. so it is at $date 23:59:59
 | 
					
						
							| 
									
										
										
										
											2025-03-05 15:02:40 +01:00
										 |  |  |         $date->endOfDay(); | 
					
						
							| 
									
										
										
										
											2025-02-02 05:38:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 16:38:32 +02:00
										 |  |  |         $allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($result as $account) { | 
					
						
							|  |  |  |             $nameWithBalance = $account->name; | 
					
						
							| 
									
										
										
										
											2025-07-31 20:38:57 +02:00
										 |  |  |             $currency        = $this->repository->getAccountCurrency($account) ?? $this->primaryCurrency; | 
					
						
							| 
									
										
										
										
											2024-12-30 07:30:23 +01:00
										 |  |  |             $useCurrency     = $currency; | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |             if (in_array($account->accountType->type, $this->balanceTypes, true)) { | 
					
						
							| 
									
										
										
										
											2025-02-11 16:10:16 +01:00
										 |  |  |                 // this one is correct.
 | 
					
						
							| 
									
										
										
										
											2025-02-10 16:47:59 +01:00
										 |  |  |                 Log::debug(sprintf('accounts: Call finalAccountBalance with date/time "%s"', $date->toIso8601String())); | 
					
						
							| 
									
										
										
										
											2025-08-09 16:44:06 +02:00
										 |  |  |                 $balance         = $allBalances[$account->id] ?? []; | 
					
						
							| 
									
										
										
										
											2025-07-31 20:47:20 +02:00
										 |  |  |                 $key             = $this->convertToPrimary && $currency->id !== $this->primaryCurrency->id ? 'pc_balance' : 'balance'; | 
					
						
							| 
									
										
										
										
											2025-07-31 20:38:57 +02:00
										 |  |  |                 $useCurrency     = $this->convertToPrimary && $currency->id !== $this->primaryCurrency->id ? $this->primaryCurrency : $currency; | 
					
						
							| 
									
										
										
										
											2024-12-28 18:38:19 +01:00
										 |  |  |                 $amount          = $balance[$key] ?? '0'; | 
					
						
							| 
									
										
										
										
											2025-08-09 16:38:32 +02:00
										 |  |  |                 $nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false)); | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-30 04:12:18 +01:00
										 |  |  |             $return[]        = [ | 
					
						
							| 
									
										
										
										
											2025-02-09 06:08:12 +01:00
										 |  |  |                 'id'                              => (string) $account->id, | 
					
						
							|  |  |  |                 'name'                            => $account->name, | 
					
						
							|  |  |  |                 'name_with_balance'               => $nameWithBalance, | 
					
						
							|  |  |  |                 'type'                            => $account->accountType->type, | 
					
						
							|  |  |  |                 'currency_id'                     => (string) $useCurrency->id, | 
					
						
							|  |  |  |                 'currency_name'                   => $useCurrency->name, | 
					
						
							|  |  |  |                 'currency_code'                   => $useCurrency->code, | 
					
						
							|  |  |  |                 'currency_symbol'                 => $useCurrency->symbol, | 
					
						
							|  |  |  |                 'currency_decimal_places'         => $useCurrency->decimal_places, | 
					
						
							| 
									
										
										
										
											2025-02-09 05:26:37 +01:00
										 |  |  |                 'account_currency_id'             => (string) $currency->id, | 
					
						
							|  |  |  |                 'account_currency_name'           => $currency->name, | 
					
						
							|  |  |  |                 'account_currency_code'           => $currency->code, | 
					
						
							|  |  |  |                 'account_currency_symbol'         => $currency->symbol, | 
					
						
							|  |  |  |                 'account_currency_decimal_places' => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 18:25:09 +02:00
										 |  |  |         // custom order.
 | 
					
						
							| 
									
										
										
										
											2021-01-03 07:09:36 +01:00
										 |  |  |         usort( | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |             $return, | 
					
						
							| 
									
										
										
										
											2023-11-04 17:26:38 +01:00
										 |  |  |             static function (array $left, array $right) { | 
					
						
							| 
									
										
										
										
											2024-12-30 07:30:23 +01:00
										 |  |  |                 $order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value]; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $posA  = (int) array_search($left['type'], $order, true); | 
					
						
							|  |  |  |                 $posB  = (int) array_search($right['type'], $order, true); | 
					
						
							| 
									
										
										
										
											2021-01-03 07:09:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 return $posA - $posB; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:23:00 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-01-03 07:09:36 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2025-08-10 06:38:23 +02:00
										 |  |  |         $timer->stop(sprintf('AC accounts "%s"', $query)); | 
					
						
							| 
									
										
										
										
											2025-03-21 20:20:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 04:48:51 +01:00
										 |  |  |         return response()->api($return); | 
					
						
							| 
									
										
										
										
											2020-07-19 13:06:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |