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;
|
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
use Carbon\Carbon;
|
2022-06-25 14:24:14 +02:00
|
|
|
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;
|
2023-03-25 11:32:33 +01:00
|
|
|
use FireflyIII\Models\AccountType;
|
2024-05-12 13:31:33 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2023-09-21 15:50:49 +02:00
|
|
|
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
|
2024-05-12 13:31:33 +02:00
|
|
|
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
2023-03-25 11:32:33 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-06-25 14:24:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountController
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2023-03-25 11:32:33 +01:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
// use AccountFilter;
|
2023-03-25 11:32:33 +01:00
|
|
|
private AdminAccountRepositoryInterface $adminRepository;
|
2024-05-12 13:31:33 +02:00
|
|
|
private TransactionCurrency $default;
|
|
|
|
private ExchangeRateConverter $converter;
|
|
|
|
|
|
|
|
// private array $balanceTypes;
|
|
|
|
// 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-12 13:31:33 +02:00
|
|
|
// new way of user group validation
|
|
|
|
$userGroup = $this->validateUserGroup($request);
|
2023-03-25 11:32:33 +01:00
|
|
|
$this->adminRepository = app(AdminAccountRepositoryInterface::class);
|
2024-05-12 13:31:33 +02:00
|
|
|
$this->adminRepository->setUserGroup($userGroup);
|
|
|
|
$this->default = app('amount')->getDefaultCurrency();
|
|
|
|
$this->converter = app(ExchangeRateConverter::class);
|
|
|
|
|
|
|
|
// $this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
// $this->adminRepository->setUserGroup($this->validateUserGroup($request));
|
2023-09-24 03:22:18 +02:00
|
|
|
|
2023-03-25 11:32:33 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2024-05-12 13:31:33 +02:00
|
|
|
// $this->balanceTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Documentation for this endpoint:
|
2023-08-09 14:14:33 +02:00
|
|
|
* TODO list of checks
|
|
|
|
* 1. use dates from ParameterBag
|
|
|
|
* 2. Request validates dates
|
2023-09-23 07:15:41 +02:00
|
|
|
* 3. Request includes user_group_id
|
2023-08-09 14:14:33 +02:00
|
|
|
* 4. Endpoint is documented.
|
2023-09-23 07:15:41 +02:00
|
|
|
* 5. Collector uses user_group_id
|
2023-03-25 11:32:33 +01:00
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function accounts(AutocompleteRequest $request): JsonResponse
|
|
|
|
{
|
2024-05-12 13:31:33 +02:00
|
|
|
$queryParameters = $request->getParameters();
|
|
|
|
$result = $this->adminRepository->searchAccount((string) $queryParameters['query'], $queryParameters['account_types'], $queryParameters['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-12 17:50:54 +02:00
|
|
|
$currency = $this->adminRepository->getAccountCurrency($account);
|
2024-05-12 13:31:33 +02:00
|
|
|
return [
|
|
|
|
'id' => (string) $account->id,
|
|
|
|
'title' => $account->name,
|
|
|
|
'meta' => [
|
|
|
|
'type' => $account->accountType->type,
|
2024-05-12 17:50:54 +02:00
|
|
|
'currency_id' => null === $currency ? null : (string) $currency->id,
|
|
|
|
'currency_code' => $currency?->code,
|
|
|
|
'currency_symbol' => $currency?->symbol,
|
|
|
|
'currency_decimal' => $currency?->decimal_places,
|
2024-05-12 13:31:33 +02:00
|
|
|
'account_balances' => $this->getAccountBalances($account),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAccountBalances(Account $account): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
$balances = $this->adminRepository->getAccountBalances($account);
|
|
|
|
/** @var AccountBalance $balance */
|
|
|
|
foreach ($balances as $balance) {
|
|
|
|
$return[] = $this->parseAccountBalance($balance);
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|
2024-05-12 13:31:33 +02:00
|
|
|
return $return;
|
|
|
|
}
|
2023-03-25 11:32:33 +01:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
/**
|
|
|
|
* @param AccountBalance $balance
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function parseAccountBalance(AccountBalance $balance): array
|
|
|
|
{
|
|
|
|
$currency = $balance->transactionCurrency;
|
|
|
|
return [
|
|
|
|
'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' => $this->default->decimal_places,
|
2023-03-25 11:32:33 +01:00
|
|
|
|
2024-05-12 13:31:33 +02:00
|
|
|
];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-03-25 11:32:33 +01:00
|
|
|
}
|
2022-07-16 09:25:10 +02:00
|
|
|
}
|