Skip to content

Firefly III: a personal finances manager

Various multi-currency things in the autocomplete API.

James Colea1241ebedb37
diff --git a/app/Api/V1/Controllers/Autocomplete/AccountController.php b/app/Api/V1/Controllers/Autocomplete/AccountController.php
index 739e99408e..fdbf680486 100644
--- a/app/Api/V1/Controllers/Autocomplete/AccountController.php
+++ b/app/Api/V1/Controllers/Autocomplete/AccountController.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
+use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
@@ -62,7 +63,7 @@ class AccountController extends Controller
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
{
- $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'));
-
- // TODO this code is duplicated in the V2 Autocomplete controller, which means this code is due to be deprecated.
- $defaultCurrency = app('amount')->getDefaultCurrency();
+ $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'));
/** @var Account $account */
foreach ($result as $account) {
$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)) {
$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(
'%s (%s)',
$account->name,
- app('amount')->formatAnything($currency, $balance['balance'], false)
+ app('amount')->formatAnything($useCurrency, $amount, false)
);
}
- $return[] = [
+ $return[] = [
'id' => (string) $account->id,
'name' => $account->name,
'name_with_balance' => $nameWithBalance,
'type' => $account->accountType->type,
- 'currency_id' => (string) $currency->id,
- 'currency_name' => $currency->name,
- 'currency_code' => $currency->code,
- 'currency_symbol' => $currency->symbol,
- 'currency_decimal_places' => $currency->decimal_places,
+ 'currency_id' => (string) $useCurrency->id,
+ 'currency_name' => $useCurrency->name,
+ 'currency_code' => $useCurrency->code,
+ 'currency_symbol' => $useCurrency->symbol,
+ 'currency_decimal_places' => $useCurrency->decimal_places,
];
}
diff --git a/app/Api/V1/Controllers/Controller.php b/app/Api/V1/Controllers/Controller.php
index 862fa08867..5f710f1220 100644
--- a/app/Api/V1/Controllers/Controller.php
+++ b/app/Api/V1/Controllers/Controller.php
@@ -28,6 +28,7 @@ use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
use FireflyIII\Models\Preference;
+use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\User;
@@ -58,6 +59,7 @@ abstract class Controller extends BaseController
protected array $allowedSort;
protected ParameterBag $parameters;
protected bool $convertToNative = false;
+ protected TransactionCurrency $defaultCurrency;
/**
* Controller constructor.
@@ -72,6 +74,7 @@ abstract class Controller extends BaseController
if (auth()->check()) {
$language = Steam::getLanguage();
$this->convertToNative = Amount::convertToNative();
+ $this->defaultCurrency = Amount::getDefaultCurrency();
app()->setLocale($language);
}
@@ -91,8 +94,8 @@ abstract class Controller extends BaseController
if ($page < 1) {
$page = 1;
}
- if ($page > 2 ** 16) {
- $page = 2 ** 16;
+ if ($page > pow(2,16)) {
+ $page = pow(2, 16);
}
$bag->set('page', $page);
diff --git a/app/Api/V2/Controllers/Controller.php b/app/Api/V2/Controllers/Controller.php
index ee8d782e48..24499ef3b5 100644
--- a/app/Api/V2/Controllers/Controller.php
+++ b/app/Api/V2/Controllers/Controller.php
@@ -93,8 +93,8 @@ class Controller extends BaseController
if ($page < 1) {
$page = 1;
}
- if ($page > 2 ** 16) {
- $page = 2 ** 16;
+ if ($page > pow(2,16)) {
+ $page = pow(2, 16);
}
$bag->set('page', $page);
diff --git a/app/Factory/PiggyBankFactory.php b/app/Factory/PiggyBankFactory.php
index e4a09ca535..fead291f9b 100644
--- a/app/Factory/PiggyBankFactory.php
+++ b/app/Factory/PiggyBankFactory.php
@@ -44,7 +44,7 @@ class PiggyBankFactory
public User $user {
set(User $value) {
$this->user = $value;
- $this->currencyRepository->setUser($value);
+ $this->currencyRepository->setUser($value);
$this->accountRepository->setUser($value);
$this->piggyBankRepository->setUser($value);
}