James Cole
2022-10-04 19:12:57 +02:00
parent c9f7e96450
commit bab4c05e5f
6 changed files with 43 additions and 8 deletions

View File

@@ -48,6 +48,7 @@ class UpdateRequest extends FormRequest
private array $dateFields; private array $dateFields;
private array $integerFields; private array $integerFields;
private array $stringFields; private array $stringFields;
private array $floatFields;
private array $textareaFields; private array $textareaFields;
/** /**
@@ -84,12 +85,15 @@ class UpdateRequest extends FormRequest
'notes', 'notes',
]; ];
$this->convertStringFields = [ $this->floatFields = [
'amount',
'foreign_amount',
];
$this->stringFields = [
'type', 'type',
'currency_code', 'currency_code',
'foreign_currency_code', 'foreign_currency_code',
'amount',
'foreign_amount',
'description', 'description',
'source_name', 'source_name',
'source_iban', 'source_iban',
@@ -163,6 +167,7 @@ class UpdateRequest extends FormRequest
$current = $this->getDateData($current, $transaction); $current = $this->getDateData($current, $transaction);
$current = $this->getBooleanData($current, $transaction); $current = $this->getBooleanData($current, $transaction);
$current = $this->getArrayData($current, $transaction); $current = $this->getArrayData($current, $transaction);
$current = $this->getFloatData($current, $transaction);
$return[] = $current; $return[] = $current;
} }
@@ -196,7 +201,7 @@ class UpdateRequest extends FormRequest
*/ */
private function getStringData(array $current, array $transaction): array private function getStringData(array $current, array $transaction): array
{ {
foreach ($this->convertStringFields as $fieldName) { foreach ($this->stringFields as $fieldName) {
if (array_key_exists($fieldName, $transaction)) { if (array_key_exists($fieldName, $transaction)) {
$current[$fieldName] = $this->clearString((string) $transaction[$fieldName], false); $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;
}
} }

View File

@@ -99,7 +99,7 @@ class RecurrenceFormRequest extends FormRequest
]; ];
// fill in foreign currency data // 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_amount'] = $this->convertString('foreign_amount');
$return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id'); $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'; $rules['repetitions'] = 'required|numeric|between:0,254';
} }
// if foreign amount, currency must be different. // 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'; $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'); $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')) { switch ($this->convertString('transaction_type')) {
case strtolower(TransactionType::WITHDRAWAL): case strtolower(TransactionType::WITHDRAWAL):
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts'; $rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';

View File

@@ -293,6 +293,7 @@ trait JournalServiceTrait
if ('' === $amount) { if ('' === $amount) {
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $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)) { if (0 === bccomp('0', $amount)) {
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount)); throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
} }

View File

@@ -48,6 +48,7 @@ class GroupUpdateService
*/ */
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup
{ {
Log::debug(sprintf('Now in %s', __METHOD__));
Log::debug('Now in group update service', $data); Log::debug('Now in group update service', $data);
/** @var array $transactions */ /** @var array $transactions */
$transactions = $data['transactions'] ?? []; $transactions = $data['transactions'] ?? [];
@@ -117,6 +118,7 @@ class GroupUpdateService
*/ */
private function updateTransactionJournal(TransactionGroup $transactionGroup, TransactionJournal $journal, array $data): void private function updateTransactionJournal(TransactionGroup $transactionGroup, TransactionJournal $journal, array $data): void
{ {
Log::debug(sprintf('Now in %s', __METHOD__));
if (empty($data)) { if (empty($data)) {
return; return;
} }
@@ -141,6 +143,7 @@ class GroupUpdateService
*/ */
private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array
{ {
Log::debug(sprintf('Now in %s', __METHOD__));
// updated or created transaction journals: // updated or created transaction journals:
$updated = []; $updated = [];
/** /**

View File

@@ -126,6 +126,7 @@ class JournalUpdateService
*/ */
public function update(): void public function update(): void
{ {
Log::debug(sprintf('Now in %s', __METHOD__));
Log::debug(sprintf('Now in JournalUpdateService for journal #%d.', $this->transactionJournal->id)); Log::debug(sprintf('Now in JournalUpdateService for journal #%d.', $this->transactionJournal->id));
if ($this->removeReconciliation()) { if ($this->removeReconciliation()) {
@@ -690,11 +691,13 @@ class JournalUpdateService
*/ */
private function updateAmount(): void private function updateAmount(): void
{ {
Log::debug(sprintf('Now in %s', __METHOD__));
if (!$this->hasFields(['amount'])) { if (!$this->hasFields(['amount'])) {
return; return;
} }
$value = $this->data['amount'] ?? ''; $value = $this->data['amount'] ?? '';
Log::debug(sprintf('Amount is now "%s"', $value));
try { try {
$amount = $this->getAmount($value); $amount = $this->getAmount($value);
} catch (FireflyException $e) { } catch (FireflyException $e) {

View File

@@ -216,7 +216,7 @@ trait ConvertsDataTypes
* *
* @return float|null * @return float|null
*/ */
protected function float(string $field): ?float protected function convertFloat(string $field): ?float
{ {
$res = $this->get($field); $res = $this->get($field);
if (null === $res) { if (null === $res) {