Merge branch 'develop' into 5.8-dev

This commit is contained in:
James Cole
2022-12-27 21:13:42 +01:00
19 changed files with 58 additions and 53 deletions

View File

@@ -88,7 +88,7 @@ class UpdateRequest extends FormRequest
'notes',
];
$this->floatFields = [
$this->floatFields = [ // not really floats, for validation.
'amount',
'foreign_amount',
];
@@ -408,8 +408,7 @@ class UpdateRequest extends FormRequest
if (array_key_exists($fieldName, $transaction)) {
$value = $transaction[$fieldName];
if (is_float($value)) {
// TODO this effectively limits the max number of decimals in currencies to 14.
$current[$fieldName] = sprintf('%.14f', $value);
$current[$fieldName] = sprintf('%.24f', $value);
}
if (!is_float($value)) {
$current[$fieldName] = (string) $value;

View File

@@ -47,9 +47,8 @@ class PreferenceStoreRequest extends FormRequest
if ('false' === $array['data']) {
$array['data'] = false;
}
// TODO remove float
if (is_numeric($array['data'])) {
$array['data'] = (float) $array['data'];
$array['data'] = (float) $array['data']; // intentional float.
}
return $array;

View File

@@ -48,9 +48,8 @@ class PreferenceUpdateRequest extends FormRequest
if ('false' === $array['data']) {
$array['data'] = false;
}
// TODO remove float
if (is_numeric($array['data'])) {
$array['data'] = (float) $array['data'];
$array['data'] = (float) $array['data']; // intentional float.
}
return $array;

View File

@@ -543,7 +543,7 @@ class TagController extends Controller
$result[] = [
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
'amount_float' => (float) $journal['amount'],
'amount_float' => (float) $journal['amount'], // intentional float.
'amount' => $journal['amount'],
'date' => $journal['date']->isoFormat($this->monthAndDayFormat),
'date_sort' => $journal['date']->format('Y-m-d'),

View File

@@ -100,7 +100,7 @@ class RecurrenceFormRequest extends FormRequest
];
// fill in foreign currency data
if (null !== $this->convertFloat('foreign_amount')) {
if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
$return['transactions'][0]['foreign_amount'] = $this->convertString('foreign_amount');
$return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id');
}
@@ -229,7 +229,7 @@ class RecurrenceFormRequest extends FormRequest
$rules['repetitions'] = 'required|numeric|between:0,254';
}
// if foreign amount, currency must be different.
if (null !== $this->convertFloat('foreign_amount')) {
if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id';
}

View File

@@ -71,20 +71,20 @@ class Amount
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
{
$locale = app('steam')->getLocale();
$rounded = app('steam')->bcround($amount, $decimalPlaces);
$coloured = $coloured ?? true;
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = $fmt->format((float)app('steam')->bcround($amount, $decimalPlaces)); // intentional float
$result = $fmt->format((float)$rounded); // intentional float
if (true === $coloured) {
if (1 === bccomp($amount, '0')) {
if (1 === bccomp($rounded, '0')) {
return sprintf('<span class="text-success">%s</span>', $result);
}
if (-1 === bccomp($amount, '0')) {
if (-1 === bccomp($rounded, '0')) {
return sprintf('<span class="text-danger">%s</span>', $result);
}

View File

@@ -151,7 +151,7 @@ class FrontpageChartGenerator
$tempData[] = [
'name' => trans('firefly.no_category'),
'sum' => $currency['sum'],
'sum_float' => round((float) $currency['sum'], $currency['currency_decimal_places'] ?? 2),
'sum_float' => round((float) $currency['sum'], $currency['currency_decimal_places'] ?? 2), // intentional float
'currency_id' => (int) $currency['currency_id'],
];
}

View File

@@ -130,8 +130,8 @@ trait ModelInformation
$billTriggers = ['currency_is', 'amount_more', 'amount_less', 'description_contains'];
$values = [
$bill->transactionCurrency()->first()->name,
round((float) $bill->amount_min, 24),
round((float) $bill->amount_max, 24),
$bill->amount_min,
$bill->amount_max,
$bill->name,
];
foreach ($billTriggers as $index => $trigger) {

View File

@@ -104,13 +104,19 @@ class Steam
*/
public function bcround(?string $number, int $precision = 0): string
{
if(null === $number) {
return '0';
}
if('' === trim($number)) {
return '0';
}
Log::debug(sprintf('Trying bcround("%",%d)', $number, $precision));
// if the number contains "E", it's in scientific notation, so we need to convert it to a normal number first.
if(false !== stripos($number,'e')) {
$number = sprintf('%.24f',$number);
}
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);
@@ -584,8 +590,10 @@ class Steam
if ($mantis < 0) {
$post += abs((int)$mantis);
}
// TODO careless float could break financial math.
return number_format((float)$value, $post, '.', '');
}
// TODO careless float could break financial math.
return number_format((float)$value, 0, '.', '');
}

View File

@@ -78,7 +78,7 @@ class AccountTransformer extends AbstractTransformer
[$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType);
$openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', '');
$openingBalance = app('steam')->bcround($openingBalance, $decimalPlaces);
$includeNetWorth = '0' !== $this->repository->getMetaValue($account, 'include_net_worth');
$longitude = null;
$latitude = null;
@@ -109,7 +109,7 @@ class AccountTransformer extends AbstractTransformer
'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol,
'currency_decimal_places' => $decimalPlaces,
'current_balance' => number_format((float) app('steam')->balance($account, $date), $decimalPlaces, '.', ''),
'current_balance' => app('steam')->bcround(app('steam')->balance($account, $date), $decimalPlaces),
'current_balance_date' => $date->toAtomString(),
'notes' => $this->repository->getNoteText($account),
'monthly_payment_date' => $monthlyPaymentDate,
@@ -117,7 +117,7 @@ class AccountTransformer extends AbstractTransformer
'account_number' => $this->repository->getMetaValue($account, 'account_number'),
'iban' => '' === $account->iban ? null : $account->iban,
'bic' => $this->repository->getMetaValue($account, 'BIC'),
'virtual_balance' => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''),
'virtual_balance' => app('steam')->bcround($account->virtual_balance, $decimalPlaces),
'opening_balance' => $openingBalance,
'opening_balance_date' => $openingBalanceDate,
'liability_type' => $liabilityType,

View File

@@ -69,7 +69,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places,
'amount' => number_format((float) $availableBudget->amount, $currency->decimal_places, '.', ''),
'amount' => app('steam')->bcround($availableBudget->amount, $currency->decimal_places),
'start' => $availableBudget->start_date->toAtomString(),
'end' => $availableBudget->end_date->endOfDay()->toAtomString(),
'spent_in_budgets' => [],

View File

@@ -115,8 +115,8 @@ class BillTransformer extends AbstractTransformer
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places,
'name' => $bill->name,
'amount_min' => number_format((float) $bill->amount_min, $currency->decimal_places, '.', ''),
'amount_max' => number_format((float) $bill->amount_max, $currency->decimal_places, '.', ''),
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),

View File

@@ -84,7 +84,7 @@ class BudgetLimitTransformer extends AbstractTransformer
$currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places;
}
$amount = number_format((float) $amount, $currencyDecimalPlaces, '.', '');
$amount = app('steam')->bcround($amount, $currencyDecimalPlaces);
return [
'id' => (string) $budgetLimit->id,

View File

@@ -84,7 +84,7 @@ class BudgetTransformer extends AbstractTransformer
$abCurrencyId = (string) $autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type];
$abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', '');
$abAmount = app('steam')->bcround($autoBudget->amount, $autoBudget->transactionCurrency->decimal_places);
$abPeriod = $autoBudget->period;
}
@@ -120,7 +120,7 @@ class BudgetTransformer extends AbstractTransformer
{
$return = [];
foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
$data['sum'] = app('steam')->bcround($data['sum'], (int) $data['currency_decimal_places']);
$return[] = $data;
}

View File

@@ -95,7 +95,7 @@ class CategoryTransformer extends AbstractTransformer
{
$return = [];
foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
$data['sum'] = app('steam')->bcround($data['sum'], (int) $data['currency_decimal_places']);
$return[] = $data;
}

View File

@@ -83,7 +83,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
'id' => (string) $event->id,
'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(),
'amount' => number_format((float) $event->amount, $currency->decimal_places, '.', ''),
'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,

View File

@@ -88,21 +88,21 @@ class PiggyBankTransformer extends AbstractTransformer
// get currently saved amount:
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
$currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', '');
$currentAmount = app('steam')->bcround($currentAmountStr, $currency->decimal_places);
// Amounts, depending on 0.0 state of target amount
$percentage = null;
$targetAmountString = null;
$leftToSaveString = null;
$savePerMonth = null;
if (0.000 !== (float) $piggyBank->targetamount) {
if (0 !== bccomp(app('steam')->bcround($currentAmountStr, $currency->decimal_places), '0')) {
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
$targetAmount = (string)$piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', $targetAmount) ? '0.01' : $targetAmount;
$percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
$targetAmountString = number_format((float) $targetAmount, $currency->decimal_places, '.', '');
$leftToSaveString = number_format((float) $leftToSave, $currency->decimal_places, '.', '');
$savePerMonth = number_format((float) $this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', '');
$targetAmountString = app('steam')->bcround($targetAmount, $currency->decimal_places);
$leftToSaveString = app('steam')->bcround($leftToSave, $currency->decimal_places);
$savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
}
$startDate = $piggyBank->startdate?->toAtomString();
$targetDate = $piggyBank->targetdate?->toAtomString();

View File

@@ -196,10 +196,10 @@ class RecurrenceTransformer extends AbstractTransformer
$destinationType = $destinationAccount->accountType->type;
$destinationIban = $destinationAccount->iban;
}
$amount = number_format((float) $transaction->amount, $transaction->transactionCurrency->decimal_places, '.', '');
$amount = app('steam')->bcround($transaction->amount, $transaction->transactionCurrency->decimal_places);
$foreignAmount = null;
if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) {
$foreignAmount = number_format((float) $transaction->foreign_amount, $foreignCurrencyDp, '.', '');
$foreignAmount = app('steam')->bcround($transaction->foreign_amount, $foreignCurrencyDp);
}
$transactionArray = [
'currency_id' => (string) $transaction->transaction_currency_id,

View File

@@ -364,7 +364,7 @@ class TransactionGroupTransformer extends AbstractTransformer
$bill = $this->getBill($journal->bill);
if (null !== $foreignAmount && null !== $foreignCurrency) {
$foreignAmount = number_format((float) $foreignAmount, $foreignCurrency['decimal_places'] ?? 0, '.', '');
$foreignAmount = app('steam')->bcround($foreignAmount, $foreignCurrency['decimal_places'] ?? 0);
}
$longitude = null;
@@ -394,7 +394,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'foreign_currency_symbol' => $foreignCurrency['symbol'],
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
'amount' => number_format((float) $amount, $currency->decimal_places, '.', ''),
'amount' => app('steam')->bcround($amount, $currency->decimal_places),
'foreign_amount' => $foreignAmount,
'description' => $journal->description,
@@ -462,7 +462,7 @@ class TransactionGroupTransformer extends AbstractTransformer
{
$result = $journal->transactions->first(
static function (Transaction $transaction) {
return (float) $transaction->amount < 0;
return (float) $transaction->amount < 0; // lame but it works.
}
);
if (null === $result) {
@@ -482,7 +482,7 @@ class TransactionGroupTransformer extends AbstractTransformer
{
$result = $journal->transactions->first(
static function (Transaction $transaction) {
return (float) $transaction->amount > 0;
return (float) $transaction->amount > 0; // lame but it works
}
);
if (null === $result) {