mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Merge branch 'develop' into 5.8-dev
# Conflicts: # app/Api/V1/Controllers/Chart/AccountController.php # app/Api/V1/Controllers/Insight/Expense/AccountController.php # app/Api/V1/Controllers/Insight/Expense/BillController.php # app/Api/V1/Controllers/Insight/Expense/BudgetController.php # app/Api/V1/Controllers/Insight/Expense/CategoryController.php # app/Api/V1/Controllers/Insight/Expense/PeriodController.php # app/Console/Commands/Upgrade/MigrateToGroups.php # app/Http/Controllers/Account/IndexController.php # app/Http/Controllers/Budget/AvailableBudgetController.php # app/Http/Controllers/Budget/BudgetLimitController.php # app/Http/Controllers/Budget/EditController.php # app/Http/Controllers/Chart/AccountController.php # app/Http/Controllers/Json/FrontpageController.php # app/Http/Controllers/PiggyBank/EditController.php
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Amount.php
|
||||
* Copyright (c) 2019 james@firefly-iii.org
|
||||
@@ -42,26 +43,26 @@ class Amount
|
||||
* This method will properly format the given number, in color or "black and white",
|
||||
* as a currency, given two things: the currency required and the current locale.
|
||||
*
|
||||
* @param TransactionCurrency $format
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
* @param TransactionCurrency $format
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
*/
|
||||
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = null): string
|
||||
{
|
||||
return $this->formatFlat($format->symbol, (int) $format->decimal_places, $amount, $coloured);
|
||||
return $this->formatFlat($format->symbol, (int)$format->decimal_places, $amount, $coloured);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will properly format the given number, in color or "black and white",
|
||||
* as a currency, given two things: the currency required and the current locale.
|
||||
*
|
||||
* @param string $symbol
|
||||
* @param int $decimalPlaces
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
* @param string $symbol
|
||||
* @param int $decimalPlaces
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
@@ -77,13 +78,13 @@ class Amount
|
||||
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);
|
||||
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
|
||||
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
|
||||
$result = $fmt->format((float) $amount);
|
||||
$result = $fmt->format((float)app('steam')->bcround($amount, $decimalPlaces)); // intentional float
|
||||
|
||||
if (true === $coloured) {
|
||||
if ($amount > 0) {
|
||||
if (1 === bccomp($amount, '0')) {
|
||||
return sprintf('<span class="text-success">%s</span>', $result);
|
||||
}
|
||||
if ($amount < 0) {
|
||||
if (-1 === bccomp($amount, '0')) {
|
||||
return sprintf('<span class="text-danger">%s</span>', $result);
|
||||
}
|
||||
|
||||
@@ -132,7 +133,7 @@ class Amount
|
||||
}
|
||||
$cache->store(config('firefly.default_currency', 'EUR'));
|
||||
|
||||
return (string) config('firefly.default_currency', 'EUR');
|
||||
return (string)config('firefly.default_currency', 'EUR');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,7 +149,7 @@ class Amount
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param User $user
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
@@ -165,7 +166,7 @@ class Amount
|
||||
$currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR';
|
||||
|
||||
// at this point the currency preference could be encrypted, if coming from an old version.
|
||||
$currencyCode = $this->tryDecrypt((string) $currencyPrefStr);
|
||||
$currencyCode = $this->tryDecrypt((string)$currencyPrefStr);
|
||||
|
||||
// could still be json encoded:
|
||||
/** @var TransactionCurrency|null $currency */
|
||||
@@ -180,7 +181,7 @@ class Amount
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -249,8 +250,8 @@ class Amount
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $info
|
||||
* @param string $field
|
||||
* @param array $info
|
||||
* @param string $field
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -266,10 +267,10 @@ class Amount
|
||||
* string $sign = $localeconv['negative_sign']
|
||||
* bool $csPrecedes = $localeconv['n_cs_precedes'].
|
||||
*
|
||||
* @param bool $sepBySpace
|
||||
* @param int $signPosn
|
||||
* @param string $sign
|
||||
* @param bool $csPrecedes
|
||||
* @param bool $sepBySpace
|
||||
* @param int $signPosn
|
||||
* @param string $sign
|
||||
* @param bool $csPrecedes
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
@@ -324,11 +325,11 @@ class Amount
|
||||
}
|
||||
|
||||
// default is amount before currency
|
||||
$format = $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE;
|
||||
$format = $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE;
|
||||
|
||||
if ($csPrecedes) {
|
||||
// alternative is currency before amount
|
||||
$format = $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE;
|
||||
$format = $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE;
|
||||
}
|
||||
|
||||
return $format;
|
||||
|
@@ -105,8 +105,8 @@ class WholePeriodChartGenerator
|
||||
$earnedInfoKey = sprintf('earned-in-%s', $code);
|
||||
$spentAmount = $spent[$key][$currencyId]['sum'] ?? '0';
|
||||
$earnedAmount = $earned[$key][$currencyId]['sum'] ?? '0';
|
||||
$chartData[$spentInfoKey]['entries'][$label] = round((float) $spentAmount, $currency['currency_decimal_places']);
|
||||
$chartData[$earnedInfoKey]['entries'][$label] = round((float) $earnedAmount, $currency['currency_decimal_places']);
|
||||
$chartData[$spentInfoKey]['entries'][$label] = app('steam')->bcround($spentAmount, $currency['currency_decimal_places']);
|
||||
$chartData[$earnedInfoKey]['entries'][$label] = app('steam')->bcround($earnedAmount, $currency['currency_decimal_places']);
|
||||
}
|
||||
$current = app('navigation')->addPeriod($current, $step, 0);
|
||||
}
|
||||
|
@@ -93,7 +93,7 @@ class CurrencyForm
|
||||
|
||||
// make sure value is formatted nicely:
|
||||
if (null !== $value && '' !== $value) {
|
||||
$value = round((float) $value, $defaultCurrency->decimal_places);
|
||||
$value = app('steam')->bcround($value, $defaultCurrency->decimal_places);
|
||||
}
|
||||
try {
|
||||
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
|
||||
@@ -163,7 +163,7 @@ class CurrencyForm
|
||||
|
||||
// make sure value is formatted nicely:
|
||||
if (null !== $value && '' !== $value) {
|
||||
$value = round((float) $value, $defaultCurrency->decimal_places);
|
||||
$value = app('steam')->bcround($value, $defaultCurrency->decimal_places);
|
||||
}
|
||||
try {
|
||||
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Steam.php
|
||||
* Copyright (c) 2019 james@firefly-iii.org
|
||||
@@ -44,8 +45,8 @@ use ValueError;
|
||||
class Steam
|
||||
{
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -55,7 +56,7 @@ class Steam
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
|
||||
$currencyId = (int) $repository->getMetaValue($account, 'currency_id');
|
||||
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
|
||||
$transactions = $account->transactions()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d 23:59:59'))
|
||||
@@ -76,8 +77,8 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transactions
|
||||
* @param string $key
|
||||
* @param array $transactions
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -86,7 +87,7 @@ class Steam
|
||||
$sum = '0';
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$value = (string) ($transaction[$key] ?? '0');
|
||||
$value = (string)($transaction[$key] ?? '0');
|
||||
$value = '' === $value ? '0' : $value;
|
||||
$sum = bcadd($sum, $value);
|
||||
}
|
||||
@@ -94,15 +95,35 @@ class Steam
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* https://stackoverflow.com/questions/1642614/how-to-ceil-floor-and-round-bcmath-numbers
|
||||
*
|
||||
* @param string $number
|
||||
* @param int $precision
|
||||
* @return string
|
||||
*/
|
||||
public function bcround(string $number, int $precision = 0): string
|
||||
{
|
||||
if (str_contains($number, '.')) {
|
||||
if ($number[0] !== '-') {
|
||||
return bcadd($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
||||
}
|
||||
|
||||
return bcsub($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
||||
}
|
||||
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the balance for the given account during the whole range, using this format:.
|
||||
*
|
||||
* [yyyy-mm-dd] => 123,2
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param TransactionCurrency|null $currency
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -133,7 +154,7 @@ class Steam
|
||||
$repository->setUser($account->user);
|
||||
$currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
$currencyId = (int) $currency->id;
|
||||
$currencyId = (int)$currency->id;
|
||||
|
||||
$start->addDay();
|
||||
|
||||
@@ -161,14 +182,14 @@ class Steam
|
||||
/** @var Transaction $entry */
|
||||
foreach ($set as $entry) {
|
||||
// normal amount and foreign amount
|
||||
$modified = null === $entry->modified ? '0' : (string) $entry->modified;
|
||||
$foreignModified = null === $entry->modified_foreign ? '0' : (string) $entry->modified_foreign;
|
||||
$modified = null === $entry->modified ? '0' : (string)$entry->modified;
|
||||
$foreignModified = null === $entry->modified_foreign ? '0' : (string)$entry->modified_foreign;
|
||||
$amount = '0';
|
||||
if ($currencyId === (int) $entry->transaction_currency_id || 0 === $currencyId) {
|
||||
if ($currencyId === (int)$entry->transaction_currency_id || 0 === $currencyId) {
|
||||
// use normal amount:
|
||||
$amount = $modified;
|
||||
}
|
||||
if ($currencyId === (int) $entry->foreign_currency_id) {
|
||||
if ($currencyId === (int)$entry->foreign_currency_id) {
|
||||
// use foreign amount:
|
||||
$amount = $foreignModified;
|
||||
}
|
||||
@@ -187,9 +208,9 @@ class Steam
|
||||
/**
|
||||
* Gets balance at the end of current month by default
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
* @param TransactionCurrency|null $currency
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
@@ -226,7 +247,7 @@ class Steam
|
||||
->get(['transactions.foreign_amount'])->toArray();
|
||||
$foreignBalance = $this->sumTransactions($transactions, 'foreign_amount');
|
||||
$balance = bcadd($nativeBalance, $foreignBalance);
|
||||
$virtual = null === $account->virtual_balance ? '0' : (string) $account->virtual_balance;
|
||||
$virtual = null === $account->virtual_balance ? '0' : (string)$account->virtual_balance;
|
||||
$balance = bcadd($balance, $virtual);
|
||||
|
||||
$cache->store($balance);
|
||||
@@ -237,8 +258,8 @@ class Steam
|
||||
/**
|
||||
* This method always ignores the virtual balance.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $date
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -269,8 +290,8 @@ class Steam
|
||||
/**
|
||||
* Same as above, but also groups per currency.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $date
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
* @throws JsonException
|
||||
@@ -300,8 +321,8 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
* @param Account $account
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -323,7 +344,7 @@ class Steam
|
||||
$return = [];
|
||||
/** @var stdClass $entry */
|
||||
foreach ($balances as $entry) {
|
||||
$return[(int) $entry->transaction_currency_id] = (string) $entry->sum_for_currency;
|
||||
$return[(int)$entry->transaction_currency_id] = (string)$entry->sum_for_currency;
|
||||
}
|
||||
$cache->store($return);
|
||||
|
||||
@@ -331,7 +352,7 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @param string $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -393,7 +414,7 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accounts
|
||||
* @param array $accounts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -409,7 +430,7 @@ class Steam
|
||||
foreach ($set as $entry) {
|
||||
$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;
|
||||
@@ -456,7 +477,7 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
* @param string $locale
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -489,8 +510,8 @@ class Steam
|
||||
/**
|
||||
* Make sure URL is safe.
|
||||
*
|
||||
* @param string $unknownUrl
|
||||
* @param string $safeUrl
|
||||
* @param string $unknownUrl
|
||||
* @param string $safeUrl
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -515,7 +536,7 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -539,7 +560,7 @@ class Steam
|
||||
* Convert a scientific notation to float
|
||||
* Additionally fixed a problem with PHP <= 5.2.x with big integers
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function floatalize(string $value): string
|
||||
@@ -554,15 +575,15 @@ class Steam
|
||||
$post = strlen(substr($number, strpos($number, '.') + 1));
|
||||
$mantis = substr($value, strpos($value, 'E') + 1);
|
||||
if ($mantis < 0) {
|
||||
$post += abs((int) $mantis);
|
||||
$post += abs((int)$mantis);
|
||||
}
|
||||
return number_format((float) $value, $post, '.', '');
|
||||
return number_format((float)$value, $post, '.', '');
|
||||
}
|
||||
return number_format((float) $value, 0, '.', '');
|
||||
return number_format((float)$value, 0, '.', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $amount
|
||||
* @param string|null $amount
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
@@ -576,7 +597,7 @@ class Steam
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @param string $string
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@@ -588,28 +609,28 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
Reference in New Issue
Block a user