mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-22 08:56:39 -06:00
Fix currency validation.
This commit is contained in:
parent
b8a58f83ee
commit
0efb3d2dcf
@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Validation;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
/**
|
||||
@ -50,19 +51,41 @@ trait CurrencyValidation
|
||||
if (!is_array($transaction)) {
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists('foreign_amount', $transaction)) {
|
||||
|
||||
if (!array_key_exists('foreign_amount', $transaction) && !array_key_exists('foreign_currency_id', $transaction) && !array_key_exists('foreign_currency_code', $transaction)) {
|
||||
Log::debug('validateForeignCurrencyInformation: no foreign currency information present at all.');
|
||||
|
||||
continue;
|
||||
}
|
||||
$foreignAmount = (string) $transaction['foreign_amount'];
|
||||
$foreignAmount = (string) ($transaction['foreign_amount'] ?? '');
|
||||
$foreignId = $transaction['foreign_currency_id'] ?? null;
|
||||
$foreignCode = $transaction['foreign_currency_code'] ?? null;
|
||||
if ('' === $foreignAmount) {
|
||||
Log::debug('validateForeignCurrencyInformation: foreign amount is "".');
|
||||
if (
|
||||
(array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction))
|
||||
&& (null !== $foreignId || null !== $foreignCode)
|
||||
) {
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_amount'));
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_currency_id', (string) trans('validation.require_currency_amount'));
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_currency_code', (string) trans('validation.require_currency_amount'));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
// if foreign amount is present, then the currency must be as well.
|
||||
if (!(array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction)) && 0 !== bccomp('0', $transaction['foreign_amount'])) {
|
||||
|
||||
$compare = bccomp('0', $transaction['foreign_amount']);
|
||||
if (-1 === $compare) {
|
||||
Log::debug('validateForeignCurrencyInformation: array contains foreign amount info.');
|
||||
if (!array_key_exists('foreign_currency_id', $transaction) && !array_key_exists('foreign_currency_code', $transaction)) {
|
||||
Log::debug('validateForeignCurrencyInformation: array contains NO foreign currency info.');
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_info'));
|
||||
}
|
||||
// if the currency is present, then the amount must be present as well.
|
||||
if (array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction)) {
|
||||
}
|
||||
if (0 === $compare && ('' !== (string) $foreignId || '' !== (string) $foreignCode)) {
|
||||
Log::debug('validateForeignCurrencyInformation: array contains foreign currency info, but zero amount.');
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_currency_id', (string) trans('validation.require_currency_amount'));
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_currency_code', (string) trans('validation.require_currency_amount'));
|
||||
$validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_amount'));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user