mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Various multi-currency things in the autocomplete API.
This commit is contained in:
parent
af78d998db
commit
a1241ebedb
@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
|
|||||||
|
|
||||||
use FireflyIII\Api\V1\Controllers\Controller;
|
use FireflyIII\Api\V1\Controllers\Controller;
|
||||||
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
|
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
|
||||||
|
use FireflyIII\Enums\AccountTypeEnum;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
@ -62,7 +63,7 @@ class AccountController extends Controller
|
|||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
$this->balanceTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
$this->balanceTypes = [AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,40 +75,40 @@ class AccountController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function accounts(AutocompleteRequest $request): JsonResponse
|
public function accounts(AutocompleteRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$data = $request->getData();
|
$data = $request->getData();
|
||||||
$types = $data['types'];
|
$types = $data['types'];
|
||||||
$query = $data['query'];
|
$query = $data['query'];
|
||||||
$date = $data['date'] ?? today(config('app.timezone'));
|
$date = $data['date'] ?? today(config('app.timezone'));
|
||||||
$return = [];
|
$return = [];
|
||||||
$result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
|
$result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
|
||||||
|
|
||||||
// TODO this code is duplicated in the V2 Autocomplete controller, which means this code is due to be deprecated.
|
|
||||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
|
||||||
|
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
foreach ($result as $account) {
|
foreach ($result as $account) {
|
||||||
$nameWithBalance = $account->name;
|
$nameWithBalance = $account->name;
|
||||||
$currency = $this->repository->getAccountCurrency($account) ?? $defaultCurrency;
|
$currency = $this->repository->getAccountCurrency($account) ?? $this->defaultCurrency;
|
||||||
|
|
||||||
if (in_array($account->accountType->type, $this->balanceTypes, true)) {
|
if (in_array($account->accountType->type, $this->balanceTypes, true)) {
|
||||||
$balance = Steam::finalAccountBalance($account, $date);
|
$balance = Steam::finalAccountBalance($account, $date);
|
||||||
|
$key = $this->convertToNative && $currency->id !== $this->defaultCurrency->id ? 'native_balance' : 'balance';
|
||||||
|
$useCurrency = $this->convertToNative && $currency->id !== $this->defaultCurrency->id ? $this->defaultCurrency : $currency;
|
||||||
|
$amount = $balance[$key] ?? '0';
|
||||||
$nameWithBalance = sprintf(
|
$nameWithBalance = sprintf(
|
||||||
'%s (%s)',
|
'%s (%s)',
|
||||||
$account->name,
|
$account->name,
|
||||||
app('amount')->formatAnything($currency, $balance['balance'], false)
|
app('amount')->formatAnything($useCurrency, $amount, false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$return[] = [
|
$return[] = [
|
||||||
'id' => (string) $account->id,
|
'id' => (string) $account->id,
|
||||||
'name' => $account->name,
|
'name' => $account->name,
|
||||||
'name_with_balance' => $nameWithBalance,
|
'name_with_balance' => $nameWithBalance,
|
||||||
'type' => $account->accountType->type,
|
'type' => $account->accountType->type,
|
||||||
'currency_id' => (string) $currency->id,
|
'currency_id' => (string) $useCurrency->id,
|
||||||
'currency_name' => $currency->name,
|
'currency_name' => $useCurrency->name,
|
||||||
'currency_code' => $currency->code,
|
'currency_code' => $useCurrency->code,
|
||||||
'currency_symbol' => $currency->symbol,
|
'currency_symbol' => $useCurrency->symbol,
|
||||||
'currency_decimal_places' => $currency->decimal_places,
|
'currency_decimal_places' => $useCurrency->decimal_places,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ use Carbon\Carbon;
|
|||||||
use Carbon\Exceptions\InvalidDateException;
|
use Carbon\Exceptions\InvalidDateException;
|
||||||
use Carbon\Exceptions\InvalidFormatException;
|
use Carbon\Exceptions\InvalidFormatException;
|
||||||
use FireflyIII\Models\Preference;
|
use FireflyIII\Models\Preference;
|
||||||
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Support\Facades\Amount;
|
use FireflyIII\Support\Facades\Amount;
|
||||||
use FireflyIII\Support\Facades\Steam;
|
use FireflyIII\Support\Facades\Steam;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
@ -58,6 +59,7 @@ abstract class Controller extends BaseController
|
|||||||
protected array $allowedSort;
|
protected array $allowedSort;
|
||||||
protected ParameterBag $parameters;
|
protected ParameterBag $parameters;
|
||||||
protected bool $convertToNative = false;
|
protected bool $convertToNative = false;
|
||||||
|
protected TransactionCurrency $defaultCurrency;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller constructor.
|
* Controller constructor.
|
||||||
@ -72,6 +74,7 @@ abstract class Controller extends BaseController
|
|||||||
if (auth()->check()) {
|
if (auth()->check()) {
|
||||||
$language = Steam::getLanguage();
|
$language = Steam::getLanguage();
|
||||||
$this->convertToNative = Amount::convertToNative();
|
$this->convertToNative = Amount::convertToNative();
|
||||||
|
$this->defaultCurrency = Amount::getDefaultCurrency();
|
||||||
app()->setLocale($language);
|
app()->setLocale($language);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -91,8 +94,8 @@ abstract class Controller extends BaseController
|
|||||||
if ($page < 1) {
|
if ($page < 1) {
|
||||||
$page = 1;
|
$page = 1;
|
||||||
}
|
}
|
||||||
if ($page > 2 ** 16) {
|
if ($page > pow(2,16)) {
|
||||||
$page = 2 ** 16;
|
$page = pow(2, 16);
|
||||||
}
|
}
|
||||||
$bag->set('page', $page);
|
$bag->set('page', $page);
|
||||||
|
|
||||||
|
@ -93,8 +93,8 @@ class Controller extends BaseController
|
|||||||
if ($page < 1) {
|
if ($page < 1) {
|
||||||
$page = 1;
|
$page = 1;
|
||||||
}
|
}
|
||||||
if ($page > 2 ** 16) {
|
if ($page > pow(2,16)) {
|
||||||
$page = 2 ** 16;
|
$page = pow(2, 16);
|
||||||
}
|
}
|
||||||
$bag->set('page', $page);
|
$bag->set('page', $page);
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class PiggyBankFactory
|
|||||||
public User $user {
|
public User $user {
|
||||||
set(User $value) {
|
set(User $value) {
|
||||||
$this->user = $value;
|
$this->user = $value;
|
||||||
$this->currencyRepository->setUser($value);
|
$this->currencyRepository->setUser($value);
|
||||||
$this->accountRepository->setUser($value);
|
$this->accountRepository->setUser($value);
|
||||||
$this->piggyBankRepository->setUser($value);
|
$this->piggyBankRepository->setUser($value);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user