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;
|
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;
|
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 FireflyIII\User;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountController
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
|
|
|
use AccountFilter;
|
2025-02-01 12:01:50 +01:00
|
|
|
// this array only exists to test if the constructor will use it properly.
|
|
|
|
protected array $accepts = ['application/json', 'application/vnd.api+json'];
|
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) {
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
$this->repository->setUser($user);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2024-12-28 18:38:19 +01:00
|
|
|
$data = $request->getData();
|
|
|
|
$types = $data['types'];
|
|
|
|
$query = $data['query'];
|
|
|
|
$date = $data['date'] ?? today(config('app.timezone'));
|
|
|
|
$return = [];
|
|
|
|
$result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
|
2020-07-19 13:06:59 +02:00
|
|
|
|
2025-02-02 05:38:47 +01:00
|
|
|
// set date to end-of-day for account balance.
|
|
|
|
$date->endOfDay();
|
|
|
|
|
2020-07-19 13:06:59 +02:00
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($result as $account) {
|
|
|
|
$nameWithBalance = $account->name;
|
2025-01-22 05:24:12 +01:00
|
|
|
$currency = $this->repository->getAccountCurrency($account) ?? $this->nativeCurrency;
|
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)) {
|
2024-12-22 19:42:06 +01:00
|
|
|
$balance = Steam::finalAccountBalance($account, $date);
|
2025-01-22 05:24:12 +01:00
|
|
|
$key = $this->convertToNative && $currency->id !== $this->nativeCurrency->id ? 'native_balance' : 'balance';
|
|
|
|
$useCurrency = $this->convertToNative && $currency->id !== $this->nativeCurrency->id ? $this->nativeCurrency : $currency;
|
2024-12-28 18:38:19 +01:00
|
|
|
$amount = $balance[$key] ?? '0';
|
2023-05-29 13:56:55 +02:00
|
|
|
$nameWithBalance = sprintf(
|
|
|
|
'%s (%s)',
|
|
|
|
$account->name,
|
2024-12-28 18:38:19 +01:00
|
|
|
app('amount')->formatAnything($useCurrency, $amount, false)
|
2023-05-29 13:56:55 +02:00
|
|
|
);
|
2020-07-19 13:06:59 +02:00
|
|
|
}
|
|
|
|
|
2024-12-30 04:12:18 +01:00
|
|
|
$return[] = [
|
2024-12-22 08:43:12 +01:00
|
|
|
'id' => (string) $account->id,
|
2020-07-19 13:06:59 +02:00
|
|
|
'name' => $account->name,
|
|
|
|
'name_with_balance' => $nameWithBalance,
|
|
|
|
'type' => $account->accountType->type,
|
2024-12-28 18:38:19 +01:00
|
|
|
'currency_id' => (string) $useCurrency->id,
|
|
|
|
'currency_name' => $useCurrency->name,
|
|
|
|
'currency_code' => $useCurrency->code,
|
|
|
|
'currency_symbol' => $useCurrency->symbol,
|
|
|
|
'currency_decimal_places' => $useCurrency->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
|
|
|
);
|
2020-08-02 18:25:09 +02:00
|
|
|
|
2025-01-25 04:48:51 +01:00
|
|
|
return response()->api($return);
|
2020-07-19 13:06:59 +02:00
|
|
|
}
|
|
|
|
}
|