Skip to content

Firefly III: a personal finances manager

Fix download exchange rates.

James Colea68a251f2ddf
diff --git a/app/Http/Controllers/DebugController.php b/app/Http/Controllers/DebugController.php
index 9c07d36c31..11aee3c1b3 100644
--- a/app/Http/Controllers/DebugController.php
+++ b/app/Http/Controllers/DebugController.php
@@ -313,11 +313,10 @@ final class DebugController extends Controller
if ($time > 0) {
$carbon = Carbon::createFromTimestamp($time);
$lastCronjob = $carbon->format('Y-m-d H:i:s');
- $lastCronjobAgo = $carbon->locale('en')->diffForHumans();
$cronJobs[] = [
'user' => $userId,
'last_run' => $lastCronjob,
- 'last_run_ago' => $lastCronjobAgo,
+ 'last_run_ago' => Steam::secondsAsTime((int) $carbon->diffInSeconds(now(config('app.timezone')))),
];
}
}
diff --git a/app/Jobs/DownloadExchangeRates.php b/app/Jobs/DownloadExchangeRates.php
index 60db43d82e..0a53f68c3c 100644
--- a/app/Jobs/DownloadExchangeRates.php
+++ b/app/Jobs/DownloadExchangeRates.php
@@ -146,7 +146,7 @@ class DownloadExchangeRates implements ShouldQueue
{
// if we have it already, don't bother searching for it again.
if (array_key_exists($code, $this->active)) {
- Log::debug(sprintf('Already know what the result is of searching for %s', $code));
+ // Log::debug(sprintf('Already know what the result is of searching for %s', $code));
return $this->active[$code];
}
@@ -158,13 +158,7 @@ class DownloadExchangeRates implements ShouldQueue
return null;
}
- // if (false === $currency->enabled) {
- // Log::debug(sprintf('Currency %s is not enabled.', $code));
- // $this->active[$code] = null;
- //
- // return null;
- // }
- Log::debug(sprintf('Currency %s is enabled.', $code));
+ // Log::debug(sprintf('Currency %s exists.', $code));
$this->active[$code] = $currency;
return $currency;
@@ -172,8 +166,8 @@ class DownloadExchangeRates implements ShouldQueue
private function saveRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date, float $rate): void
{
- $this->repository->setUser($this->user);
- $this->repository->setUserGroup($this->user->userGroup);
+ //$this->repository->setUser($this->user);
+ //$this->repository->setUserGroup($this->user->userGroup);
if ($this->repository->isEnabled($from) && $this->repository->isEnabled($to)) {
$existing = $this->repository->getExchangeRate($from, $to, $date);
if (!$existing instanceof CurrencyExchangeRate) {
@@ -197,7 +191,6 @@ class DownloadExchangeRates implements ShouldQueue
continue;
}
- Log::debug(sprintf('Currency %s is in use.', $code));
$this->saveRate($currency, $to, $date, $rate);
}
}
diff --git a/app/Support/Cronjobs/ExchangeRatesCronjob.php b/app/Support/Cronjobs/ExchangeRatesCronjob.php
index 2bb19e8c60..c9b051872b 100644
--- a/app/Support/Cronjobs/ExchangeRatesCronjob.php
+++ b/app/Support/Cronjobs/ExchangeRatesCronjob.php
@@ -29,6 +29,7 @@ use FireflyIII\Jobs\DownloadExchangeRates;
use FireflyIII\Models\Configuration;
use FireflyIII\Support\Facades\AppConfiguration;
use FireflyIII\Support\Facades\Preferences;
+use FireflyIII\Support\Facades\Steam;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
@@ -47,7 +48,7 @@ class ExchangeRatesCronjob extends AbstractCronjob
$config = AppConfiguration::get($key, 0);
$lastTime = (int) $config->data;
$diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
- $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
+ $diffForHumans = Steam::secondsAsTime((int) now(config('app.timezone'))->diffInSeconds(Carbon::createFromTimestamp($lastTime)));
if (0 === $lastTime) {
Log::info(sprintf('Exchange rates cron-job has never fired before for user #%d.', $user->id));
}
diff --git a/app/Support/Steam.php b/app/Support/Steam.php
index 3d8c0fb25c..3eab3b862c 100644
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@ -43,7 +43,6 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Safe\Exceptions\UrlException;
use ValueError;
-
use function Safe\parse_url;
use function Safe\preg_replace;
@@ -57,56 +56,76 @@ class Steam
* properly gets the balance of a range.
*/
public function accountsBalancesInRange(
- Collection $accounts,
- Carbon $start,
- Carbon $end,
+ Collection $accounts,
+ Carbon $start,
+ Carbon $end,
?TransactionCurrency $primary = null,
- ?bool $convertToPrimary = null
- ): array {
+ ?bool $convertToPrimary = null
+ ): array
+ {
return [
$this->accountsBalancesOptimized($accounts, $start, $primary, $convertToPrimary, inclusive: false),
$this->accountsBalancesOptimized($accounts, $end, $primary, $convertToPrimary),
];
}
+ public function secondsAsTime(int $seconds): string
+ {
+ $seconds = $seconds < 0 ? abs($seconds) : $seconds;
+ $weeks = floor($seconds / 604800);
+ $days = floor($seconds / 86400);
+ $hours = floor($seconds / 3600);
+ $minutes = floor(($seconds % 3600) / 60);
+ $remainingSeconds = $seconds % 60;
+ $return = '';
+ if($weeks > 0) {
+ $return .= sprintf('%dw', $weeks);
+ }
+ if($days > 0) {
+ $return .= sprintf('%dd', $days);
+ }
+ $return .= sprintf('%dh%dm%ds', $hours, $minutes, $remainingSeconds);
+ return $return;
+ }
+
public function accountsBalancesOptimized(
- Collection $accounts,
- Carbon $date,
+ Collection $accounts,
+ Carbon $date,
?TransactionCurrency $primary = null,
- ?bool $convertToPrimary = null,
- bool $inclusive = true
- ): array {
+ ?bool $convertToPrimary = null,
+ bool $inclusive = true
+ ): array
+ {
// Log::debug(sprintf('accountsBalancesOptimized: Called for %d account(s) with date/time "%s" (inclusive: %s)', $accounts->count(), $date->toIso8601String(), var_export($inclusive, true)));
- $result = [];
+ $result = [];
$convertToPrimary ??= Amount::convertToPrimary();
$primary ??= Amount::getPrimaryCurrency();
- $currencies = $this->getCurrencies($accounts);
+ $currencies = $this->getCurrencies($accounts);
// balance(s) in all currencies for ALL accounts.
$arrayOfSums = Transaction::query()
- ->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', $inclusive ? '<=' : '<', $date->format('Y-m-d H:i:s'))
- ->whereNull('transaction_journals.deleted_at')
- ->groupBy(['transactions.account_id', 'transaction_currencies.code'])
- ->get(['transactions.account_id', 'transaction_currencies.code', DB::raw('SUM(transactions.amount) as sum_of_amount')])
- ->toArray()
- ;
+ ->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', $inclusive ? '<=' : '<', $date->format('Y-m-d H:i:s'))
+ ->whereNull('transaction_journals.deleted_at')
+ ->groupBy(['transactions.account_id', 'transaction_currencies.code'])
+ ->get(['transactions.account_id', 'transaction_currencies.code', DB::raw('SUM(transactions.amount) as sum_of_amount')])
+ ->toArray();
// Log::debug('Array of sums: ', $arrayOfSums);
/** @var Account $account */
foreach ($accounts as $account) {
// this key is overwritten right away, but I must remember it is always created.
- $currency = $currencies[$account->id];
- $return = [
+ $currency = $currencies[$account->id];
+ $return = [
'pc_balance' => '0',
'balance' => '0',
];
// FIRST, add virtual balance to this.
- $virtualBalance = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
- $pcVirtualBalance = '0';
+ $virtualBalance = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
+ $pcVirtualBalance = '0';
if (!$convertToPrimary) {
$return['balance'] = bcadd($return['balance'], $virtualBalance);
@@ -114,24 +133,24 @@ class Steam
}
if ($convertToPrimary) {
// the primary currency balance is combined with a converted virtual_balance:
- $converter = new ExchangeRateConverter();
- $pcVirtualBalance = $converter->convert($currency, $primary, $date, $virtualBalance);
+ $converter = new ExchangeRateConverter();
+ $pcVirtualBalance = $converter->convert($currency, $primary, $date, $virtualBalance);
// Log::debug(sprintf('ADD converted virtual balance (%s) to "pc_balance"', $pcVirtualBalance));
$return['pc_balance'] = bcadd($pcVirtualBalance, $return['pc_balance']);
}
// second array
- $accountSums = array_filter($arrayOfSums, static fn (array $entry): bool => $entry['account_id'] === $account->id);
+ $accountSums = array_filter($arrayOfSums, static fn(array $entry): bool => $entry['account_id'] === $account->id);
if (0 === count($accountSums)) {
// Log::debug('No sums in transactions, so return whatever we have now.', $return);
$result[$account->id] = $return;
continue;
}
- $sumsByCode = [];
+ $sumsByCode = [];
foreach ($accountSums as $accountSum) {
// $accountSum = array_values($accountSum)[0];
- $sumOfAmount = (string) $accountSum['sum_of_amount'];
+ $sumOfAmount = (string)$accountSum['sum_of_amount'];
$sumOfAmount = $this->floatalize('' === $sumOfAmount ? '0' : $sumOfAmount);
$sumsByCode[$accountSum['code']] = $sumOfAmount;
}
@@ -156,7 +175,7 @@ class Steam
if ($convertToPrimary) {
// the primary currency balance is combined with a converted virtual_balance:
- $converter = new ExchangeRateConverter();
+ $converter = new ExchangeRateConverter();
// $return['pc_balance'] = bcadd($pcVirtualBalance, $return['pc_balance']);
$sumsByCode[$primary->code] = bcadd($pcVirtualBalance, $sumsByCode[$primary->code] ?? '0');
@@ -210,10 +229,10 @@ class Steam
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
if (str_contains($number, '.')) {
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;
@@ -355,15 +374,17 @@ class Steam
"balance": balance in the account's currency OR user's primary currency if the account has no currency
--> "pc_balance": balance in the user's primary currency, with all amounts converted to the primary currency.
"EUR": balance in EUR (or whatever currencies the account has balance in)
- TXT)]
+ TXT
+ )]
public function finalAccountBalance(
- Account $account,
- Carbon $date,
+ Account $account,
+ Carbon $date,
?TransactionCurrency $primary = null,
- ?bool $convertToPrimary = null,
- bool $inclusive = true
- ): array {
- $cache = new CacheProperties();
+ ?bool $convertToPrimary = null,
+ bool $inclusive = true
+ ): array
+ {
+ $cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty($date);
if ($cache->has()) {
@@ -379,26 +400,25 @@ class Steam
$primary = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
}
// account balance thing.
- $currencyPresent = property_exists($account, 'meta') && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
+ $currencyPresent = property_exists($account, 'meta') && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
if ($currencyPresent) {
$accountCurrency = $account->meta['currency'];
}
if (!$currencyPresent) {
$accountCurrency = $this->getAccountCurrency($account);
}
- $hasCurrency = null !== $accountCurrency;
- $currency = $hasCurrency ? $accountCurrency : $primary;
- $return = ['pc_balance' => '0', 'balance' => '0']; // this key is overwritten right away, but I must remember it is always created.
+ $hasCurrency = null !== $accountCurrency;
+ $currency = $hasCurrency ? $accountCurrency : $primary;
+ $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.
- $array = $account
+ $array = $account
->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
->where('transaction_journals.date', $inclusive ? '<=' : '<', $date->format('Y-m-d H:i:s'))
->get(['transaction_currencies.code', 'transactions.amount'])
- ->toArray()
- ;
- $others = $this->groupAndSumTransactions($array, 'code', 'amount');
+ ->toArray();
+ $others = $this->groupAndSumTransactions($array, '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';
@@ -415,7 +435,7 @@ class Steam
}
// 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) {
// the primary currency balance is combined with a converted virtual_balance:
@@ -431,7 +451,7 @@ class Steam
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
}
- $final = array_merge($return, $others);
+ $final = array_merge($return, $others);
// Log::debug('Final balance is', $final);
$cache->store($final);
@@ -449,7 +469,7 @@ class Steam
// Log::debug(sprintf('called finalAccountBalanceInRange(#%d, %s, %s)', $account->id, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
// set up cache
- $cache = new CacheProperties();
+ $cache = new CacheProperties();
$cache->addProperty($account->id);
$cache->addProperty('final-balance-in-range');
$cache->addProperty($start);
@@ -461,25 +481,25 @@ class Steam
return $cache->get();
}
- $balances = [];
- $formatted = $start->format('Y-m-d');
+ $balances = [];
+ $formatted = $start->format('Y-m-d');
// Log::debug('Get first balance to start.');
// 2025-10-08 replaced finalAccountBalance with accountsBalancesOptimized:
- $primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
- $startBalance = $this->accountsBalancesOptimized(new Collection()->push($account), $start, $primaryCurrency, $convertToPrimary, false)[$account->id];
- $accountCurrency = $this->getAccountCurrency($account);
- $hasCurrency = $accountCurrency instanceof TransactionCurrency;
- $currency = $accountCurrency ?? $primaryCurrency;
+ $primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
+ $startBalance = $this->accountsBalancesOptimized(new Collection()->push($account), $start, $primaryCurrency, $convertToPrimary, false)[$account->id];
+ $accountCurrency = $this->getAccountCurrency($account);
+ $hasCurrency = $accountCurrency instanceof TransactionCurrency;
+ $currency = $accountCurrency ?? $primaryCurrency;
// Log::debug(sprintf('Currency is %s', $currency->code));
// set start balances:
if (!array_key_exists($currency->code, $startBalance)) {
// set first balance to 0 or the virtual balance of the account.
- $startBalance[$currency->code] = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
+ $startBalance[$currency->code] = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
Log::debug(sprintf('[a] Start balance in %s is %s', $currency->code, $startBalance[$currency->code]));
}
if ($hasCurrency && !array_key_exists($accountCurrency->code, $startBalance)) {
- $startBalance[$currency->code] = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
+ $startBalance[$currency->code] = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
Log::debug(sprintf('[b] Start balance in %s is %s', $currency->code, $startBalance[$currency->code]));
}
if ($hasCurrency && !array_key_exists($primaryCurrency->code, $startBalance)) {
@@ -488,13 +508,13 @@ class Steam
$converted = $converter->convert($accountCurrency, $primaryCurrency, $start, $startBalance[$accountCurrency->code]);
$startBalance[$primaryCurrency->code] = $converted;
Log::debug(sprintf(
- '[c] Also set start balance in %s to %s because convertToPrimary = true, and this account is not in that currency.',
- $primaryCurrency->code,
- $converted
- ));
+ '[c] Also set start balance in %s to %s because convertToPrimary = true, and this account is not in that currency.',
+ $primaryCurrency->code,
+ $converted
+ ));
}
if ($accountCurrency->id !== $primaryCurrency->id && !$convertToPrimary) {
- $startBalance[$primaryCurrency->code] = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
+ $startBalance[$primaryCurrency->code] = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
// Log::debug(sprintf('[d] Also set start balance in %s to %s', $primaryCurrency->code, $startBalance[$primaryCurrency->code]));
}
@@ -502,16 +522,16 @@ class Steam
if (!$hasCurrency) {
// Log::debug(sprintf('[e] Also set start balance in %s', $primaryCurrency->code));
- $startBalance[$primaryCurrency->code] = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
+ $startBalance[$primaryCurrency->code] = (string)('' === (string)$account->virtual_balance ? '0' : $account->virtual_balance);
}
- $currencies = [$currency->id => $currency, $primaryCurrency->id => $primaryCurrency];
+ $currencies = [$currency->id => $currency, $primaryCurrency->id => $primaryCurrency];
$balances[$formatted] = $startBalance;
Log::debug('Final start balance: ', $startBalance);
// sums up the balance changes per day.
// Log::debug(sprintf('Date >= %s and <= %s', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
- $set = $account
+ $set = $account
->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
@@ -520,52 +540,51 @@ class Steam
->groupBy('transactions.transaction_currency_id')
->orderBy('transaction_journals.date', 'ASC')
->whereNull('transaction_journals.deleted_at')
- ->get(['transaction_journals.date', 'transactions.transaction_currency_id', DB::raw('SUM(transactions.amount) AS sum_of_day')])
- ;
+ ->get(['transaction_journals.date', 'transactions.transaction_currency_id', DB::raw('SUM(transactions.amount) AS sum_of_day')]);
- $currentBalance = $startBalance;
- $converter = new ExchangeRateConverter();
+ $currentBalance = $startBalance;
+ $converter = new ExchangeRateConverter();
/** @var Transaction $entry */
foreach ($set as $entry) {
// get date object
- $carbon = new Carbon($entry->date, $entry->date_tz);
- $carbonKey = $carbon->format('Y-m-d');
+ $carbon = new Carbon($entry->date, $entry->date_tz);
+ $carbonKey = $carbon->format('Y-m-d');
// 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.
- $sumOfDay = $this->floatalize($sumOfDay);
+ $sumOfDay = $this->floatalize($sumOfDay);
// find currency of this entry, does not have to exist.
$currencies[$entry->transaction_currency_id] ??= Amount::getTransactionCurrencyById($entry->transaction_currency_id);
// make sure this $entry has its own $entryCurrency
/** @var TransactionCurrency $entryCurrency */
- $entryCurrency = $currencies[$entry->transaction_currency_id];
+ $entryCurrency = $currencies[$entry->transaction_currency_id];
// Log::debug(sprintf('Processing transaction(s) on moment %s', $carbon->format('Y-m-d H:i:s')));
// add amount to current balance in currency code.
- $currentBalance[$entryCurrency->code] ??= '0';
- $currentBalance[$entryCurrency->code] = bcadd($sumOfDay, (string) $currentBalance[$entryCurrency->code]);
+ $currentBalance[$entryCurrency->code] ??= '0';
+ $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 (!$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 there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
if ($convertToPrimary) {
$pcSumOfDay = $converter->convert($entryCurrency, $primaryCurrency, $carbon, $sumOfDay);
- $currentBalance['pc_balance'] = bcadd((string) ($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay);
- $currentBalance[$primaryCurrency->code] = bcadd((string) ($currentBalance[$primaryCurrency->code] ?? '0'), $pcSumOfDay);
+ $currentBalance['pc_balance'] = bcadd((string)($currentBalance['pc_balance'] ?? '0'), $pcSumOfDay);
+ $currentBalance[$primaryCurrency->code] = bcadd((string)($currentBalance[$primaryCurrency->code] ?? '0'), $pcSumOfDay);
// if it's the same currency as the entry, also add to balance (see other code).
if ($currency->id === $entryCurrency->id) {
- $currentBalance['balance'] = bcadd((string) $currentBalance['balance'], $sumOfDay);
+ $currentBalance['balance'] = bcadd((string)$currentBalance['balance'], $sumOfDay);
}
}
// add to final array.
- $balances[$carbonKey] = $currentBalance;
+ $balances[$carbonKey] = $currentBalance;
// Log::debug(sprintf('Updated entry [%s]', $carbonKey), $currentBalance);
}
@@ -583,26 +602,26 @@ class Steam
*/
public function floatalize(string $value): string
{
- $value = strtoupper($value);
+ $value = strtoupper($value);
if (!str_contains($value, 'E')) {
return $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, '.')) {
- $post = strlen(substr($number, (int) strpos($number, '.') + 1));
- $mantis = substr($value, (int) strpos($value, 'E') + 1);
+ $post = strlen(substr($number, (int)strpos($number, '.') + 1));
+ $mantis = substr($value, (int)strpos($value, 'E') + 1);
if ($mantis < 0) {
- $post += abs((int) $mantis);
+ $post += abs((int)$mantis);
}
// 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.
- return number_format((float) $value, 0, '.', '');
+ return number_format((float)$value, 0, '.', '');
}
public function formatAmountByCode(string $amount, string $code, ?bool $coloured = null): string
@@ -626,7 +645,7 @@ class Steam
public function formatAmountBySymbol(string $amount, ?string $symbol = null, ?int $decimalPlaces = null, ?bool $coloured = null): string
{
if (null === $symbol) {
- $message = sprintf(
+ $message = sprintf(
'formatAmountBySymbol("%s", %s, %d, %s) was called without a symbol. Please browse to /flush to clear your cache.',
$amount,
var_export($symbol, true),
@@ -637,8 +656,8 @@ class Steam
$currency = Amount::getPrimaryCurrency();
}
if (null !== $symbol) {
- $decimalPlaces ??= 2;
- $coloured ??= true;
+ $decimalPlaces ??= 2;
+ $coloured ??= true;
$currency = new TransactionCurrency();
$currency->symbol = $symbol;
$currency->decimal_places = $decimalPlaces;
@@ -649,8 +668,8 @@ class Steam
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
- $type = $account->accountType->type;
- $list = config('firefly.valid_currency_account_types');
+ $type = $account->accountType->type;
+ $list = config('firefly.valid_currency_account_types');
// return null if not in this list.
if (!in_array($type, $list, true)) {
@@ -661,7 +680,7 @@ class Steam
return null;
}
- return Amount::getTransactionCurrencyById((int) $result->data);
+ return Amount::getTransactionCurrencyById((int)$result->data);
}
public function getHostName(string $ipAddress): string
@@ -675,11 +694,11 @@ class Steam
$hostName = $ipAddress;
}
- if ('' !== (string) $hostName && $hostName !== $ipAddress) {
+ if ('' !== (string)$hostName && $hostName !== $ipAddress) {
$host = $hostName;
}
- return (string) $host;
+ return (string)$host;
}
/**
@@ -703,19 +722,18 @@ class Steam
{
$list = [];
- $set = auth()
+ $set = auth()
->user()
->transactions()
->whereIn('transactions.account_id', $accounts)
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
- ->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')])
- ;
+ ->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')]);
/** @var Transaction $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'));
- $list[(int) $entry->account_id] = $date;
+ $list[(int)$entry->account_id] = $date;
}
return $list;
@@ -731,14 +749,14 @@ class Steam
if (null !== $cached) {
return $cached;
}
- $locale = Preferences::get('locale', config('firefly.default_locale', 'equal'))->data;
+ $locale = Preferences::get('locale', config('firefly.default_locale', 'equal'))->data;
if (is_array($locale)) {
$locale = 'equal';
}
if ('equal' === $locale) {
$locale = $this->getLanguage();
}
- $locale = (string) $locale;
+ $locale = (string)$locale;
// Check for Windows to replace the locale correctly.
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
@@ -777,7 +795,7 @@ class Steam
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
{
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
- $returnUrl = $safeUrl;
+ $returnUrl = $safeUrl;
// die('in get safe url');
try {
@@ -840,24 +858,24 @@ class Steam
// has a K in it, remove the K and multiply by 1024.
$bytes = bcmul(rtrim($string, 'k'), '1024');
- return (int) $bytes;
+ return (int)$bytes;
}
if (false !== stripos($string, 'm')) {
// has a M in it, remove the M and multiply by 1048576.
$bytes = bcmul(rtrim($string, 'm'), '1048576');
- return (int) $bytes;
+ return (int)$bytes;
}
if (false !== stripos($string, 'g')) {
// has a G in it, remove the G and multiply by (1024)^3.
$bytes = bcmul(rtrim($string, 'g'), '1073741824');
- return (int) $bytes;
+ return (int)$bytes;
}
- return (int) $string;
+ return (int)$string;
}
public function positive(string $amount): string
@@ -896,13 +914,13 @@ class Steam
if (null === $preference) {
$singleton->setPreference($key, $currency);
}
- $current = $amount;
+ $current = $amount;
if ($currency->id !== $primary->id) {
$current = $converter->convert($currency, $primary, $date, $amount);
// Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $primary->code, $current));
}
- $total = bcadd((string) $current, $total);
+ $total = bcadd((string)$current, $total);
}
return $total;
@@ -916,18 +934,18 @@ class Steam
$primary = Amount::getPrimaryCurrency();
$currencies[$primary->id] = $primary;
- $ids = $accounts->pluck('id')->toArray();
- $result = AccountMeta::query()->whereIn('account_id', $ids)->where('name', 'currency_id')->get();
+ $ids = $accounts->pluck('id')->toArray();
+ $result = AccountMeta::query()->whereIn('account_id', $ids)->where('name', 'currency_id')->get();
/** @var AccountMeta $item */
foreach ($result as $item) {
- $integer = (int) $item->data;
+ $integer = (int)$item->data;
if (0 !== $integer) {
- $accountPreferences[(int) $item->account_id] = $integer;
+ $accountPreferences[(int)$item->account_id] = $integer;
}
}
// collect those currencies, skip primary because we already have it.
- $set = TransactionCurrency::query()->whereIn('id', $accountPreferences)->where('id', '!=', $primary->id)->get();
+ $set = TransactionCurrency::query()->whereIn('id', $accountPreferences)->where('id', '!=', $primary->id)->get();
foreach ($set as $item) {
$currencies[$item->id] = $item;
}
@@ -938,7 +956,7 @@ class Steam
$currencyPresent = property_exists($account, 'meta') && array_key_exists('currency', $account->meta) && null !== $account->meta['currency'];
if ($currencyPresent) {
$currencyId = $account->meta['currency']->id;
- $currencies[$currencyId] ??= $account->meta['currency'];
+ $currencies[$currencyId] ??= $account->meta['currency'];
$accountCurrencies[$accountId] = $account->meta['currency'];
}
if (!$currencyPresent && !array_key_exists($accountId, $accountPreferences)) {
@@ -958,7 +976,7 @@ class Steam
foreach ($array as $item) {
$groupKey = $item[$group] ?? 'unknown';
- $return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string) $item[$field]);
+ $return[$groupKey] = bcadd($return[$groupKey] ?? '0', (string)$item[$field]);
}
return $return;