Skip to content

Firefly III: a personal finances manager

Fix https://github.com/firefly-iii/firefly-iii/issues/6471

James Colebab4c05e5f7d
diff --git a/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php b/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php
index 54bc859473..9cbe848241 100644
--- a/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php
+++ b/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php
@@ -48,6 +48,7 @@ class UpdateRequest extends FormRequest
private array $dateFields;
private array $integerFields;
private array $stringFields;
+ private array $floatFields;
private array $textareaFields;
/**
@@ -84,12 +85,15 @@ class UpdateRequest extends FormRequest
'notes',
];
- $this->convertStringFields = [
+ $this->floatFields = [
+ 'amount',
+ 'foreign_amount',
+ ];
+
+ $this->stringFields = [
'type',
'currency_code',
'foreign_currency_code',
- 'amount',
- 'foreign_amount',
'description',
'source_name',
'source_iban',
@@ -163,6 +167,7 @@ class UpdateRequest extends FormRequest
$current = $this->getDateData($current, $transaction);
$current = $this->getBooleanData($current, $transaction);
$current = $this->getArrayData($current, $transaction);
+ $current = $this->getFloatData($current, $transaction);
$return[] = $current;
}
@@ -196,7 +201,7 @@ class UpdateRequest extends FormRequest
*/
private function getStringData(array $current, array $transaction): array
{
- foreach ($this->convertStringFields as $fieldName) {
+ foreach ($this->stringFields as $fieldName) {
if (array_key_exists($fieldName, $transaction)) {
$current[$fieldName] = $this->clearString((string) $transaction[$fieldName], false);
}
@@ -389,4 +394,27 @@ class UpdateRequest extends FormRequest
}
);
}
+
+ /**
+ * @param array $current
+ * @param array $transaction
+ * @return array
+ */
+ private function getFloatData(array $current, array $transaction): array
+ {
+ foreach ($this->floatFields as $fieldName) {
+ 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);
+ }
+ if (!is_float($value)) {
+ $current[$fieldName] = (string) $value;
+ }
+ }
+ }
+
+ return $current;
+ }
}
diff --git a/app/Http/Requests/RecurrenceFormRequest.php b/app/Http/Requests/RecurrenceFormRequest.php
index e2bf43d001..41e5c010ab 100644
--- a/app/Http/Requests/RecurrenceFormRequest.php
+++ b/app/Http/Requests/RecurrenceFormRequest.php
@@ -99,7 +99,7 @@ class RecurrenceFormRequest extends FormRequest
];
// fill in foreign currency data
- if (null !== $this->float('foreign_amount')) {
+ if (null !== $this->convertFloat('foreign_amount')) {
$return['transactions'][0]['foreign_amount'] = $this->convertString('foreign_amount');
$return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id');
}
@@ -228,7 +228,7 @@ class RecurrenceFormRequest extends FormRequest
$rules['repetitions'] = 'required|numeric|between:0,254';
}
// if foreign amount, currency must be different.
- if (null !== $this->float('foreign_amount')) {
+ if (null !== $this->convertFloat('foreign_amount')) {
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id';
}
@@ -237,7 +237,7 @@ class RecurrenceFormRequest extends FormRequest
$rules['repeat_until'] = 'required|date|after:' . $tomorrow->format('Y-m-d');
}
- // switchc on type to expand rules for source and destination accounts:
+ // switch on type to expand rules for source and destination accounts:
switch ($this->convertString('transaction_type')) {
case strtolower(TransactionType::WITHDRAWAL):
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
diff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.php
index f9907647f5..24777d11e1 100644
--- a/app/Services/Internal/Support/JournalServiceTrait.php
+++ b/app/Services/Internal/Support/JournalServiceTrait.php
@@ -293,6 +293,7 @@ trait JournalServiceTrait
if ('' === $amount) {
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
}
+ Log::debug(sprintf('Now in getAmount("%s")', $amount));
if (0 === bccomp('0', $amount)) {
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
}
diff --git a/app/Services/Internal/Update/GroupUpdateService.php b/app/Services/Internal/Update/GroupUpdateService.php
index c97f6daf78..9a7e1151d2 100644
--- a/app/Services/Internal/Update/GroupUpdateService.php
+++ b/app/Services/Internal/Update/GroupUpdateService.php
@@ -48,6 +48,7 @@ class GroupUpdateService
*/
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup
{
+ Log::debug(sprintf('Now in %s', __METHOD__));
Log::debug('Now in group update service', $data);
/** @var array $transactions */
$transactions = $data['transactions'] ?? [];
@@ -117,6 +118,7 @@ class GroupUpdateService
*/
private function updateTransactionJournal(TransactionGroup $transactionGroup, TransactionJournal $journal, array $data): void
{
+ Log::debug(sprintf('Now in %s', __METHOD__));
if (empty($data)) {
return;
}
@@ -141,6 +143,7 @@ class GroupUpdateService
*/
private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array
{
+ Log::debug(sprintf('Now in %s', __METHOD__));
// updated or created transaction journals:
$updated = [];
/**
diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php
index 61ed19b2f2..adb6525aaa 100644
--- a/app/Services/Internal/Update/JournalUpdateService.php
+++ b/app/Services/Internal/Update/JournalUpdateService.php
@@ -126,6 +126,7 @@ class JournalUpdateService
*/
public function update(): void
{
+ Log::debug(sprintf('Now in %s', __METHOD__));
Log::debug(sprintf('Now in JournalUpdateService for journal #%d.', $this->transactionJournal->id));
if ($this->removeReconciliation()) {
@@ -690,11 +691,13 @@ class JournalUpdateService
*/
private function updateAmount(): void
{
+ Log::debug(sprintf('Now in %s', __METHOD__));
if (!$this->hasFields(['amount'])) {
return;
}
$value = $this->data['amount'] ?? '';
+ Log::debug(sprintf('Amount is now "%s"', $value));
try {
$amount = $this->getAmount($value);
} catch (FireflyException $e) {
diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php
index cc0d51b459..d01a141854 100644
--- a/app/Support/Request/ConvertsDataTypes.php
+++ b/app/Support/Request/ConvertsDataTypes.php
@@ -216,7 +216,7 @@ trait ConvertsDataTypes
*
* @return float|null
*/
- protected function float(string $field): ?float
+ protected function convertFloat(string $field): ?float
{
$res = $this->get($field);
if (null === $res) {