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

125 lines
4.3 KiB
PHP
Raw Normal View History

2020-07-19 13:06:59 +02:00
<?php
/**
* 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;
use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
2021-09-18 05:26:31 +02:00
use JsonException;
2020-07-19 13:06:59 +02:00
/**
* Class AccountController
*/
class AccountController extends Controller
{
use AccountFilter;
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);
}
);
2020-08-02 18:25:09 +02: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:
* https://api-docs.firefly-iii.org/#/autocomplete/getAccountsAC
*
2020-07-19 13:06:59 +02:00
* @param AutocompleteRequest $request
*
* @return JsonResponse
2021-09-18 05:26:31 +02:00
* @throws JsonException
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 = [];
2022-03-29 14:56:27 +02:00
$result = $this->repository->searchAccount((string) $query, $types, $data['limit']);
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 = app('steam')->balance($account, $date);
$nameWithBalance = sprintf('%s (%s)', $account->name, app('amount')->formatAnything($currency, $balance, false));
}
$return[] = [
2022-03-29 14:56:27 +02:00
'id' => (string) $account->id,
2020-07-19 13:06:59 +02:00
'name' => $account->name,
'name_with_balance' => $nameWithBalance,
'type' => $account->accountType->type,
'currency_id' => $currency->id,
'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.
$order = [AccountType::ASSET, AccountType::REVENUE, AccountType::EXPENSE];
2021-01-03 07:09:36 +01:00
usort(
$return, function ($a, $b) use ($order) {
2020-08-02 18:25:09 +02:00
$pos_a = array_search($a['type'], $order);
$pos_b = array_search($b['type'], $order);
2021-01-03 07:09:36 +01:00
2020-08-02 18:25:09 +02:00
return $pos_a - $pos_b;
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);
}
}