Files
firefly-iii/app/Api/V1/Controllers/Autocomplete/AccountController.php

129 lines
4.7 KiB
PHP
Raw Normal View History

2020-07-19 13:06:59 +02:00
<?php
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;
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\Models\AccountType;
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;
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);
}
);
2023-12-20 19:35:52 +01:00
$this->balanceTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
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
{
$data = $request->getData();
$types = $data['types'];
$query = $data['query'];
$date = $data['date'] ?? today(config('app.timezone'));
$return = [];
2024-12-22 08:43:12 +01:00
$result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
2023-06-04 06:30:22 +02:00
// TODO this code is duplicated in the V2 Autocomplete controller, which means this code is due to be deprecated.
2020-07-19 13:06:59 +02:00
$defaultCurrency = app('amount')->getDefaultCurrency();
/** @var Account $account */
foreach ($result as $account) {
$nameWithBalance = $account->name;
2020-10-13 06:35:33 +02:00
$currency = $this->repository->getAccountCurrency($account) ?? $defaultCurrency;
2020-07-19 13:06:59 +02:00
if (in_array($account->accountType->type, $this->balanceTypes, true)) {
$balance = Steam::finalAccountBalance($account, $date);
2023-05-29 13:56:55 +02:00
$nameWithBalance = sprintf(
'%s (%s)',
$account->name,
app('amount')->formatAnything($currency, $balance['balance'], false)
2023-05-29 13:56:55 +02:00
);
2020-07-19 13:06:59 +02: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-22 08:43:12 +01:00
'currency_id' => (string) $currency->id,
2020-07-19 13:06:59 +02:00
'currency_name' => $currency->name,
'currency_code' => $currency->code,
2021-01-03 07:09:36 +01:00
'currency_symbol' => $currency->symbol,
2020-07-19 13:06:59 +02:00
'currency_decimal_places' => $currency->decimal_places,
];
}
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) {
2023-06-04 06:30:22 +02:00
$order = [AccountType::ASSET, AccountType::REVENUE, AccountType::EXPENSE];
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
2020-07-19 13:06:59 +02:00
return response()->json($return);
}
}