2022-06-25 14:24:14 +02:00
|
|
|
<?php
|
2022-10-16 19:22:26 +02:00
|
|
|
|
2022-06-25 14:24:14 +02:00
|
|
|
/*
|
|
|
|
* AccountController.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:22:26 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-06-25 14:24:14 +02:00
|
|
|
namespace FireflyIII\Api\V2\Controllers\Autocomplete;
|
|
|
|
|
|
|
|
use FireflyIII\Api\V2\Controllers\Controller;
|
2023-03-25 11:32:33 +01:00
|
|
|
use FireflyIII\Api\V2\Request\Autocomplete\AutocompleteRequest;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Models\Account;
|
2024-05-12 13:31:33 +02:00
|
|
|
use FireflyIII\Models\AccountBalance;
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2024-05-18 05:51:02 +02:00
|
|
|
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
|
2024-05-12 13:31:33 +02:00
|
|
|
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
2025-02-09 09:30:44 +01:00
|
|
|
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
|
2023-03-25 11:32:33 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2024-05-18 05:51:02 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-06-25 14:24:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountController
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2024-05-18 05:51:02 +02:00
|
|
|
private ExchangeRateConverter $converter;
|
2024-12-22 08:43:12 +01:00
|
|
|
private TransactionCurrency $default;
|
|
|
|
private AccountRepositoryInterface $repository;
|
2023-03-25 11:32:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2024-05-18 05:51:02 +02:00
|
|
|
$userGroup = $this->validateUserGroup($request);
|
|
|
|
$this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
$this->repository->setUserGroup($userGroup);
|
2025-01-19 19:07:19 +01:00
|
|
|
$this->default = app('amount')->getNativeCurrency();
|
2024-05-19 10:26:25 +02:00
|
|
|
$this->converter = app(ExchangeRateConverter::class);
|
2024-05-18 06:49:29 +02:00
|
|
|
|
2023-03-25 11:32:33 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-19 06:36:31 +02:00
|
|
|
* Documentation: https://api-docs.firefly-iii.org/?urls.primaryName=2.1.0%20(v2)#/autocomplete/getAccountsAC
|
2023-03-25 11:32:33 +01:00
|
|
|
*/
|
|
|
|
public function accounts(AutocompleteRequest $request): JsonResponse
|
|
|
|
{
|
2024-10-20 10:16:54 +02:00
|
|
|
$params = $request->getParameters();
|
2024-10-21 05:15:16 +02:00
|
|
|
$result = $this->repository->searchAccount($params['query'], $params['account_types'], $params['page'], $params['size']);
|
|
|
|
$return = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-03-25 11:32:33 +01:00
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($result as $account) {
|
2024-05-12 13:31:33 +02:00
|
|
|
$return[] = $this->parseAccount($account);
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
return response()->json($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseAccount(Account $account): array
|
|
|
|
{
|
2024-05-18 05:51:02 +02:00
|
|
|
$currency = $this->repository->getAccountCurrency($account);
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
return [
|
|
|
|
'id' => (string) $account->id,
|
|
|
|
'title' => $account->name,
|
|
|
|
'meta' => [
|
2024-05-19 06:36:31 +02:00
|
|
|
'type' => $account->accountType->type,
|
2024-10-20 10:16:54 +02:00
|
|
|
// TODO is multi currency property.
|
2024-05-19 06:36:31 +02:00
|
|
|
'currency_id' => null === $currency ? null : (string) $currency->id,
|
|
|
|
'currency_code' => $currency?->code,
|
|
|
|
'currency_symbol' => $currency?->symbol,
|
|
|
|
'currency_decimal_places' => $currency?->decimal_places,
|
|
|
|
'account_balances' => $this->getAccountBalances($account),
|
2024-05-12 13:31:33 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAccountBalances(Account $account): array
|
|
|
|
{
|
|
|
|
$return = [];
|
2024-05-18 05:51:02 +02:00
|
|
|
$balances = $this->repository->getAccountBalances($account);
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
/** @var AccountBalance $balance */
|
|
|
|
foreach ($balances as $balance) {
|
2024-05-18 05:51:02 +02:00
|
|
|
try {
|
|
|
|
$return[] = $this->parseAccountBalance($balance);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
Log::error(sprintf('Could not parse convert account balance: %s', $e->getMessage()));
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
return $return;
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
|
2024-05-18 05:51:02 +02:00
|
|
|
/**
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2024-05-12 13:31:33 +02:00
|
|
|
private function parseAccountBalance(AccountBalance $balance): array
|
|
|
|
{
|
|
|
|
$currency = $balance->transactionCurrency;
|
2024-05-13 05:10:16 +02:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
return [
|
2024-05-19 06:36:31 +02:00
|
|
|
'title' => $balance->title,
|
|
|
|
'native_amount' => $this->converter->convert($currency, $this->default, today(), $balance->balance),
|
|
|
|
'amount' => app('steam')->bcround($balance->balance, $currency->decimal_places),
|
|
|
|
'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,
|
2024-05-12 13:31:33 +02:00
|
|
|
];
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|
2022-07-16 09:25:10 +02:00
|
|
|
}
|