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

134 lines
5.1 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;
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;
// 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);
}
);
$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
{
$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
// 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;
$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)) {
$balance = Steam::finalAccountBalance($account, $date);
$key = $this->convertToNative && $currency->id !== $this->nativeCurrency->id ? 'native_balance' : 'balance';
$useCurrency = $this->convertToNative && $currency->id !== $this->nativeCurrency->id ? $this->nativeCurrency : $currency;
$amount = $balance[$key] ?? '0';
2023-05-29 13:56:55 +02:00
$nameWithBalance = sprintf(
'%s (%s)',
$account->name,
app('amount')->formatAnything($useCurrency, $amount, 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,
'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
}
}