James Cole 2022-10-04 19:12:57 +02:00
parent c9f7e96450
commit bab4c05e5f
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
6 changed files with 43 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -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';

View File

@ -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));
}

View File

@ -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 = [];
/**

View File

@ -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) {

View File

@ -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) {