Collect account balances, optimized.

This commit is contained in:
James Cole
2025-08-06 20:15:02 +02:00
parent 0ad6beb66c
commit 70071767ab
7 changed files with 276 additions and 144 deletions

View File

@@ -157,6 +157,10 @@ class DebugController extends Controller
return view('debug', compact('table', 'now', 'logContent')); return view('debug', compact('table', 'now', 'logContent'));
} }
public function apiTest() {
return view('test.api-test');
}
private function generateTable(): string private function generateTable(): string
{ {
// system information: // system information:

View File

@@ -51,7 +51,7 @@ use Override;
*/ */
class AccountEnrichment implements EnrichmentInterface class AccountEnrichment implements EnrichmentInterface
{ {
private array $accountIds; private array $ids;
private array $accountTypeIds; private array $accountTypeIds;
private array $accountTypes; private array $accountTypes;
private Collection $collection; private Collection $collection;
@@ -66,13 +66,14 @@ class AccountEnrichment implements EnrichmentInterface
private array $lastActivities; private array $lastActivities;
private ?Carbon $date = null; private ?Carbon $date = null;
private bool $convertToPrimary = false; private bool $convertToPrimary = false;
private array $balances = [];
/** /**
* TODO The account enricher must do conversion from and to the primary currency. * TODO The account enricher must do conversion from and to the primary currency.
*/ */
public function __construct() public function __construct()
{ {
$this->accountIds = []; $this->ids = [];
$this->openingBalances = []; $this->openingBalances = [];
$this->currencies = []; $this->currencies = [];
$this->accountTypeIds = []; $this->accountTypeIds = [];
@@ -105,7 +106,7 @@ class AccountEnrichment implements EnrichmentInterface
// prep local fields // prep local fields
$this->collection = $collection; $this->collection = $collection;
$this->collectAccountIds(); $this->collectIds();
$this->getAccountTypes(); $this->getAccountTypes();
$this->collectMetaData(); $this->collectMetaData();
$this->collectNotes(); $this->collectNotes();
@@ -118,14 +119,14 @@ class AccountEnrichment implements EnrichmentInterface
return $this->collection; return $this->collection;
} }
private function collectAccountIds(): void private function collectIds(): void
{ {
/** @var Account $account */ /** @var Account $account */
foreach ($this->collection as $account) { foreach ($this->collection as $account) {
$this->accountIds[] = (int) $account->id; $this->ids[] = (int) $account->id;
$this->accountTypeIds[] = (int) $account->account_type_id; $this->accountTypeIds[] = (int) $account->account_type_id;
} }
$this->accountIds = array_unique($this->accountIds); $this->ids = array_unique($this->ids);
$this->accountTypeIds = array_unique($this->accountTypeIds); $this->accountTypeIds = array_unique($this->accountTypeIds);
} }
@@ -142,7 +143,7 @@ class AccountEnrichment implements EnrichmentInterface
private function collectMetaData(): void private function collectMetaData(): void
{ {
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt']) $set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
->whereIn('account_id', $this->accountIds) ->whereIn('account_id', $this->ids)
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray() ->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray()
; ;
@@ -167,7 +168,7 @@ class AccountEnrichment implements EnrichmentInterface
private function collectNotes(): void private function collectNotes(): void
{ {
$notes = Note::query()->whereIn('noteable_id', $this->accountIds) $notes = Note::query()->whereIn('noteable_id', $this->ids)
->whereNotNull('notes.text') ->whereNotNull('notes.text')
->where('notes.text', '!=', '') ->where('notes.text', '!=', '')
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray() ->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
@@ -180,7 +181,7 @@ class AccountEnrichment implements EnrichmentInterface
private function collectLocations(): void private function collectLocations(): void
{ {
$locations = Location::query()->whereIn('locatable_id', $this->accountIds) $locations = Location::query()->whereIn('locatable_id', $this->ids)
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray() ->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray()
; ;
foreach ($locations as $location) { foreach ($locations as $location) {
@@ -234,13 +235,10 @@ class AccountEnrichment implements EnrichmentInterface
private function appendCollectedData(): void private function appendCollectedData(): void
{ {
$notes = $this->notes; $this->collection = $this->collection->map(function (Account $item) {
$openingBalances = $this->openingBalances; $id = (int) $item->id;
$locations = $this->locations;
$lastActivities = $this->lastActivities;
$this->collection = $this->collection->map(function (Account $item) use ($notes, $openingBalances, $locations, $lastActivities) {
$item->full_account_type = $this->accountTypes[(int) $item->account_type_id] ?? null; $item->full_account_type = $this->accountTypes[(int) $item->account_type_id] ?? null;
$accountMeta = [ $meta = [
'currency' => null, 'currency' => null,
'location' => [ 'location' => [
'latitude' => null, 'latitude' => null,
@@ -248,45 +246,50 @@ class AccountEnrichment implements EnrichmentInterface
'zoom_level' => null, 'zoom_level' => null,
], ],
'opening_balance_date' => null, 'opening_balance_date' => null,
'opening_balance_amount' => null,
'account_number'=> null,
'notes' => $notes[$id] ?? null,
'last_activity' => $this->lastActivities[$id] ?? null,
]; ];
if (array_key_exists((int) $item->id, $this->meta)) {
foreach ($this->meta[(int) $item->id] as $name => $value) { // if location, add location:
$accountMeta[$name] = $value; if (array_key_exists($id, $this->locations)) {
$meta['location'] = $this->locations[$id];
}
if (array_key_exists($id, $this->meta)) {
foreach ($this->meta[$id] as $name => $value) {
$meta[$name] = $value;
} }
} }
// also add currency, if present. // also add currency, if present.
if (array_key_exists('currency_id', $accountMeta)) { if (array_key_exists('currency_id', $meta)) {
$currencyId = (int) $accountMeta['currency_id']; $currencyId = (int) $meta['currency_id'];
$accountMeta['currency'] = $this->currencies[$currencyId]; $meta['currency'] = $this->currencies[$currencyId];
} }
// if notes, add notes. if (array_key_exists($id, $this->openingBalances)) {
if (array_key_exists($item->id, $notes)) { $meta['opening_balance_date'] = $this->openingBalances[$id]['date'];
$accountMeta['notes'] = $notes[$item->id]; $meta['opening_balance_amount'] = $this->openingBalances[$id]['amount'];
}
// if opening balance, add opening balance
if (array_key_exists($item->id, $openingBalances)) {
$accountMeta['opening_balance_date'] = $openingBalances[$item->id]['date'];
$accountMeta['opening_balance_amount'] = $openingBalances[$item->id]['amount'];
} }
// add balances // add balances
// get currencies: // get currencies:
$currency = $this->primaryCurrency; // assume primary currency $currency = $this->primaryCurrency; // assume primary currency
if (null !== $accountMeta['currency']) { if (null !== $meta['currency']) {
$currency = $accountMeta['currency']; $currency = $meta['currency'];
} }
// get the current balance: // get the current balance:
$date = $this->getDate(); $date = $this->getDate();
$finalBalance = Steam::finalAccountBalance($item, $date, $this->primaryCurrency, $this->convertToPrimary); //$finalBalance = Steam::finalAccountBalance($item, $date, $this->primaryCurrency, $this->convertToPrimary);
$finalBalance = $this->balances[$id];
Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance); Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance);
// collect current balances: // collect current balances:
$currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places); $currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places);
$openingBalance = Steam::bcround($accountMeta['opening_balance_amount'] ?? '0', $currency->decimal_places); $openingBalance = Steam::bcround($meta['opening_balance_amount'] ?? '0', $currency->decimal_places);
$virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places); $virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places);
$debtAmount = $accountMeta['current_debt'] ?? null; $debtAmount = $meta['current_debt'] ?? null;
// set some pc_ default values to NULL: // set some pc_ default values to NULL:
$pcCurrentBalance = null; $pcCurrentBalance = null;
@@ -311,11 +314,11 @@ class AccountEnrichment implements EnrichmentInterface
} }
// set opening balance(s) to NULL if the date is null // set opening balance(s) to NULL if the date is null
if (null === $accountMeta['opening_balance_date']) { if (null === $meta['opening_balance_date']) {
$openingBalance = null; $openingBalance = null;
$pcOpeningBalance = null; $pcOpeningBalance = null;
} }
$accountMeta['balances'] = [ $meta['balances'] = [
'current_balance' => $currentBalance, 'current_balance' => $currentBalance,
'pc_current_balance' => $pcCurrentBalance, 'pc_current_balance' => $pcCurrentBalance,
'opening_balance' => $openingBalance, 'opening_balance' => $openingBalance,
@@ -326,16 +329,7 @@ class AccountEnrichment implements EnrichmentInterface
'pc_debt_amount' => $pcDebtAmount, 'pc_debt_amount' => $pcDebtAmount,
]; ];
// end add balances // end add balances
$item->meta = $meta;
// if location, add location:
if (array_key_exists($item->id, $locations)) {
$accountMeta['location'] = $locations[$item->id];
}
if (array_key_exists($item->id, $lastActivities)) {
$accountMeta['last_activity'] = $lastActivities[$item->id];
}
$item->meta = $accountMeta;
return $item; return $item;
}); });
@@ -343,10 +337,12 @@ class AccountEnrichment implements EnrichmentInterface
private function collectLastActivities(): void private function collectLastActivities(): void
{ {
$this->lastActivities = Steam::getLastActivities($this->accountIds); $this->lastActivities = Steam::getLastActivities($this->ids);
} }
private function collectBalances(): void {} private function collectBalances(): void {
$this->balances = Steam::finalAccountsBalanceOptimized($this->collection, $this->getDate(), $this->primaryCurrency, $this->convertToPrimary);
}
public function setDate(?Carbon $date): void public function setDate(?Carbon $date): void
{ {

View File

@@ -24,8 +24,10 @@ declare(strict_types=1);
namespace FireflyIII\Support; namespace FireflyIII\Support;
use Carbon\Carbon; use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Facades\Amount;
@@ -34,11 +36,9 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Exception;
use ValueError; use ValueError;
use function Safe\preg_replace;
use function Safe\parse_url; use function Safe\parse_url;
use function Safe\preg_replace;
/** /**
* Class Steam. * Class Steam.
@@ -64,10 +64,10 @@ class Steam
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision)); // Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
if (str_contains($number, '.')) { if (str_contains($number, '.')) {
if ('-' !== $number[0]) { if ('-' !== $number[0]) {
return bcadd($number, '0.'.str_repeat('0', $precision).'5', $precision); return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
} }
return bcsub($number, '0.'.str_repeat('0', $precision).'5', $precision); return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
} }
return $number; return $number;
@@ -264,8 +264,7 @@ class Steam
'transactions.transaction_currency_id', 'transactions.transaction_currency_id',
DB::raw('SUM(transactions.amount) AS sum_of_day'), DB::raw('SUM(transactions.amount) AS sum_of_day'),
] ]
) );
;
$currentBalance = $startBalance; $currentBalance = $startBalance;
$converter = new ExchangeRateConverter(); $converter = new ExchangeRateConverter();
@@ -277,7 +276,7 @@ class Steam
$carbon = new Carbon($entry->date, $entry->date_tz); $carbon = new Carbon($entry->date, $entry->date_tz);
$carbonKey = $carbon->format('Y-m-d'); $carbonKey = $carbon->format('Y-m-d');
// make sure sum is a string: // make sure sum is a string:
$sumOfDay = (string) ($entry->sum_of_day ?? '0'); $sumOfDay = (string)($entry->sum_of_day ?? '0');
// #10426 make sure sum is not in scientific notation. // #10426 make sure sum is not in scientific notation.
$sumOfDay = $this->floatalize($sumOfDay); $sumOfDay = $this->floatalize($sumOfDay);
@@ -292,20 +291,20 @@ class Steam
// add amount to current balance in currency code. // add amount to current balance in currency code.
$currentBalance[$entryCurrency->code] ??= '0'; $currentBalance[$entryCurrency->code] ??= '0';
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, (string) $currentBalance[$entryCurrency->code]); $currentBalance[$entryCurrency->code] = bcadd($sumOfDay, (string)$currentBalance[$entryCurrency->code]);
// if not requested to convert to primary currency, add the amount to "balance", do nothing else. // if not requested to convert to primary currency, add the amount to "balance", do nothing else.
if (!$convertToPrimary) { if (!$convertToPrimary) {
$currentBalance['balance'] = bcadd((string) $currentBalance['balance'], $sumOfDay); $currentBalance['balance'] = bcadd((string)$currentBalance['balance'], $sumOfDay);
} }
// if convert to primary currency add the converted amount to "pc_balance". // if convert to primary currency add the converted amount to "pc_balance".
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency. // if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
if ($convertToPrimary) { if ($convertToPrimary) {
$pcSumOfDay = $converter->convert($entryCurrency, $primaryCurrency, $carbon, $sumOfDay); $pcSumOfDay = $converter->convert($entryCurrency, $primaryCurrency, $carbon, $sumOfDay);
$currentBalance['pc_balance'] = bcadd((string) ($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay); $currentBalance['pc_balance'] = bcadd((string)($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay);
// if it's the same currency as the entry, also add to balance (see other code). // if it's the same currency as the entry, also add to balance (see other code).
if ($currency->id === $entryCurrency->id) { if ($currency->id === $entryCurrency->id) {
$currentBalance['balance'] = bcadd((string) $currentBalance['balance'], $sumOfDay); $currentBalance['balance'] = bcadd((string)$currentBalance['balance'], $sumOfDay);
} }
} }
// add to final array. // add to final array.
@@ -318,6 +317,70 @@ class Steam
return $balances; return $balances;
} }
public function finalAccountsBalanceOptimized(Collection $accounts, Carbon $date, ?TransactionCurrency $primary = null, ?bool $convertToPrimary = null): array
{
$result = [];
$convertToPrimary = $convertToPrimary ?? Amount::convertToPrimary();
$primary = $primary ?? Amount::getPrimaryCurrency();
$currencies = $this->getCurrencies($accounts);
// balance(s) in all currencies for ALL accounts.
$array = Transaction::whereIn('account_id', $accounts->pluck('id')->toArray())
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
->get(['transactions.account_id', 'transaction_currencies.code', 'transactions.amount'])->toArray();
/** @var Account $account */
foreach ($accounts as $account) {
// filter array back to this account:
$filtered = array_filter($array, function ($item) use ($account) {
return (int)$item['account_id'] === $account->id;
});
$currency = $currencies[$account->id];
// this array is PER account, so we wait a bit before we change code here.
$return = [
'pc_balance' => '0',
'balance' => '0', // this key is overwritten right away, but I must remember it is always created.
];
// balance(s) in all currencies.
$others = $this->groupAndSumTransactions($filtered, 'code', 'amount');
// Log::debug('All balances are (joined)', $others);
// if there is no request to convert, take this as "balance" and "pc_balance".
$return['balance'] = $others[$currency->code] ?? '0';
if (!$convertToPrimary) {
unset($return['pc_balance']);
// Log::debug(sprintf('Set balance to %s, unset pc_balance', $return['balance']));
}
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
if ($convertToPrimary) {
$return['pc_balance'] = $this->convertAllBalances($others, $primary, $date); // todo sum all and convert.
// Log::debug(sprintf('Set pc_balance to %s', $return['pc_balance']));
}
// either way, the balance is always combined with the virtual balance:
$virtualBalance = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
if ($convertToPrimary) {
// the primary currency balance is combined with a converted virtual_balance:
$converter = new ExchangeRateConverter();
$pcVirtualBalance = $converter->convert($currency, $primary, $date, $virtualBalance);
$return['pc_balance'] = bcadd($pcVirtualBalance, $return['pc_balance']);
// Log::debug(sprintf('Primary virtual balance makes the primary total %s', $return['pc_balance']));
}
if (!$convertToPrimary) {
// if not, also increase the balance + primary balance for consistency.
$return['balance'] = bcadd($return['balance'], $virtualBalance);
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
}
$final = array_merge($return, $others);
$result[$account->id] = $final;
// Log::debug('Final balance is', $final);
}
return $result;
}
/** /**
* Returns smaller than or equal to, so be careful with END OF DAY. * Returns smaller than or equal to, so be careful with END OF DAY.
* *
@@ -340,7 +403,7 @@ class Steam
if ($cache->has()) { if ($cache->has()) {
Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s'))); Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
return $cache->get(); //return $cache->get();
} }
// Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s'))); // Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
if (null === $convertToPrimary) { if (null === $convertToPrimary) {
@@ -369,8 +432,7 @@ class Steam
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id') ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s')) ->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
->get(['transaction_currencies.code', 'transactions.amount'])->toArray() ->get(['transaction_currencies.code', 'transactions.amount'])->toArray();
;
$others = $this->groupAndSumTransactions($array, 'code', 'amount'); $others = $this->groupAndSumTransactions($array, 'code', 'amount');
// Log::debug('All balances are (joined)', $others); // Log::debug('All balances are (joined)', $others);
// if there is no request to convert, take this as "balance" and "pc_balance". // if there is no request to convert, take this as "balance" and "pc_balance".
@@ -386,7 +448,7 @@ class Steam
} }
// either way, the balance is always combined with the virtual balance: // either way, the balance is always combined with the virtual balance:
$virtualBalance = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance); $virtualBalance = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
if ($convertToPrimary) { if ($convertToPrimary) {
// the primary currency balance is combined with a converted virtual_balance: // the primary currency balance is combined with a converted virtual_balance:
@@ -421,7 +483,7 @@ class Steam
return null; return null;
} }
return TransactionCurrency::find((int) $result->data); return TransactionCurrency::find((int)$result->data);
} }
private function groupAndSumTransactions(array $array, string $group, string $field): array private function groupAndSumTransactions(array $array, string $group, string $field): array
@@ -430,7 +492,7 @@ class Steam
foreach ($array as $item) { foreach ($array as $item) {
$groupKey = $item[$group] ?? 'unknown'; $groupKey = $item[$group] ?? 'unknown';
$return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string) $item[$field]); $return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string)$item[$field]);
} }
return $return; return $return;
@@ -478,11 +540,11 @@ class Steam
$hostName = $ipAddress; $hostName = $ipAddress;
} }
if ('' !== (string) $hostName && $hostName !== $ipAddress) { if ('' !== (string)$hostName && $hostName !== $ipAddress) {
$host = $hostName; $host = $hostName;
} }
return (string) $host; return (string)$host;
} }
public function getLastActivities(array $accounts): array public function getLastActivities(array $accounts): array
@@ -499,7 +561,7 @@ class Steam
foreach ($set as $entry) { foreach ($set as $entry) {
$date = new Carbon($entry->max_date, config('app.timezone')); $date = new Carbon($entry->max_date, config('app.timezone'));
$date->setTimezone(config('app.timezone')); $date->setTimezone(config('app.timezone'));
$list[(int) $entry->account_id] = $date; $list[(int)$entry->account_id] = $date;
} }
return $list; return $list;
@@ -517,7 +579,7 @@ class Steam
if ('equal' === $locale) { if ('equal' === $locale) {
$locale = $this->getLanguage(); $locale = $this->getLanguage();
} }
$locale = (string) $locale; $locale = (string)$locale;
// Check for Windows to replace the locale correctly. // Check for Windows to replace the locale correctly.
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) { if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
@@ -617,20 +679,20 @@ class Steam
} }
Log::debug(sprintf('Floatalizing %s', $value)); Log::debug(sprintf('Floatalizing %s', $value));
$number = substr($value, 0, (int) strpos($value, 'E')); $number = substr($value, 0, (int)strpos($value, 'E'));
if (str_contains($number, '.')) { if (str_contains($number, '.')) {
$post = strlen(substr($number, (int) strpos($number, '.') + 1)); $post = strlen(substr($number, (int)strpos($number, '.') + 1));
$mantis = substr($value, (int) strpos($value, 'E') + 1); $mantis = substr($value, (int)strpos($value, 'E') + 1);
if ($mantis < 0) { if ($mantis < 0) {
$post += abs((int) $mantis); $post += abs((int)$mantis);
} }
// TODO careless float could break financial math. // TODO careless float could break financial math.
return number_format((float) $value, $post, '.', ''); return number_format((float)$value, $post, '.', '');
} }
// TODO careless float could break financial math. // TODO careless float could break financial math.
return number_format((float) $value, 0, '.', ''); return number_format((float)$value, 0, '.', '');
} }
public function opposite(?string $amount = null): ?string public function opposite(?string $amount = null): ?string
@@ -650,24 +712,24 @@ class Steam
// has a K in it, remove the K and multiply by 1024. // has a K in it, remove the K and multiply by 1024.
$bytes = bcmul(rtrim($string, 'k'), '1024'); $bytes = bcmul(rtrim($string, 'k'), '1024');
return (int) $bytes; return (int)$bytes;
} }
if (false !== stripos($string, 'm')) { if (false !== stripos($string, 'm')) {
// has a M in it, remove the M and multiply by 1048576. // has a M in it, remove the M and multiply by 1048576.
$bytes = bcmul(rtrim($string, 'm'), '1048576'); $bytes = bcmul(rtrim($string, 'm'), '1048576');
return (int) $bytes; return (int)$bytes;
} }
if (false !== stripos($string, 'g')) { if (false !== stripos($string, 'g')) {
// has a G in it, remove the G and multiply by (1024)^3. // has a G in it, remove the G and multiply by (1024)^3.
$bytes = bcmul(rtrim($string, 'g'), '1073741824'); $bytes = bcmul(rtrim($string, 'g'), '1073741824');
return (int) $bytes; return (int)$bytes;
} }
return (int) $string; return (int)$string;
} }
public function positive(string $amount): string public function positive(string $amount): string
@@ -689,4 +751,42 @@ class Steam
return $amount; return $amount;
} }
private function getCurrencies(Collection $accounts): array
{
$currencies = [];
$accountCurrencies = [];
$accountPreferences = [];
$primary = Amount::getPrimaryCurrency();
$ids = $accounts->pluck('id')->toArray();
$result = AccountMeta::whereIn('account_id', $ids)->where('name', 'currency_id')->get();
/** @var AccountMeta $item */
foreach ($result as $item) {
$accountPreferences[(int)$item->account_id] = (int)$item->data;
}
// collect those currencies.
$set = TransactionCurrency::whereIn('id', $accountPreferences)->get();
foreach ($set as $item) {
$currencies[$item->id] = $item;
}
/** @var Account $account */
foreach ($accounts as $account) {
$accountId = $account->id;
$currencyPresent = isset($account->meta) && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
if ($currencyPresent) {
$currencyId = $account->meta['currency']->id;
$currencies[$currencyId] ??= $account->meta['currency'];
$accountCurrencies[$accountId] = $account->meta['currency'];
}
if (!$currencyPresent && !array_key_exists($account->id, $accountPreferences)) {
$accountCurrencies[$accountId] = $primary;
}
if (!$currencyPresent && array_key_exists($account->id, $accountPreferences)) {
$accountCurrencies[$account->id] = $currencies[$accountPreferences[$account->id]];
}
}
return $accountCurrencies;
}
} }

View File

@@ -143,7 +143,7 @@ class AccountTransformer extends AbstractTransformer
'notes' => $account->meta['notes'] ?? null, 'notes' => $account->meta['notes'] ?? null,
'monthly_payment_date' => $monthlyPaymentDate, 'monthly_payment_date' => $monthlyPaymentDate,
'credit_card_type' => $creditCardType, 'credit_card_type' => $creditCardType,
'account_number' => $account->meta['account_number'] ?? null, 'account_number' => $account->meta['account_number'],
'iban' => '' === $account->iban ? null : $account->iban, 'iban' => '' === $account->iban ? null : $account->iban,
'bic' => $account->meta['BIC'] ?? null, 'bic' => $account->meta['BIC'] ?? null,
'opening_balance_date' => $openingBalanceDate, 'opening_balance_date' => $openingBalanceDate,
@@ -155,7 +155,7 @@ class AccountTransformer extends AbstractTransformer
'longitude' => $longitude, 'longitude' => $longitude,
'latitude' => $latitude, 'latitude' => $latitude,
'zoom_level' => $zoomLevel, 'zoom_level' => $zoomLevel,
'last_activity' => array_key_exists('last_activity', $account->meta) ? $account->meta['last_activity']->toAtomString() : null, 'last_activity' => $account->meta['last_activity']?->toAtomString(),
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',

View File

@@ -0,0 +1,33 @@
<html>
<head>
<title>Firefly III API test</title>
<base href="{{ route('index', null, true) }}/">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<p>
Hi there,
</p>
<p>
This page is created to do some basic API testing. It's not very exciting, is it?
</p>
<script src="v1/js/app.js?v={{ FF_VERSION }}" type="text/javascript" nonce="{{ JS_NONCE }}"></script>
<script type="text/javascript" nonce="{{ JS_NONCE }}">
$(function () {
"use strict";
console.log('Hello from the API test page!');
$.ajax({
url: 'api/v1/accounts?size=50&date=2025-08-06',
type: 'GET',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
},
});
});
</script>
</body>
</html>

View File

@@ -1,2 +0,0 @@
list-length: {{ listLength }}
user-email: {{ Auth.user.email }}

View File

@@ -120,6 +120,7 @@ Route::group(
Route::get('flush', ['uses' => 'DebugController@flush', 'as' => 'flush']); Route::get('flush', ['uses' => 'DebugController@flush', 'as' => 'flush']);
Route::get('routes', ['uses' => 'DebugController@routes', 'as' => 'routes']); Route::get('routes', ['uses' => 'DebugController@routes', 'as' => 'routes']);
Route::get('debug', 'DebugController@index')->name('debug'); Route::get('debug', 'DebugController@index')->name('debug');
Route::get('debug/api-test', 'DebugController@apiTest')->name('api-test');
} }
); );