mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
PHPStorm can order methods by alphabet, who knew.
This commit is contained in:
@@ -71,136 +71,6 @@ trait TransactionValidation
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given account information. Switches on given transaction type.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function validateAccountInformationUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
app('log')->debug('Now in validateAccountInformationUpdate()');
|
||||
if ($validator->errors()->count() > 0) {
|
||||
app('log')->debug('Validator already has errors, so return.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
/**
|
||||
* @var null|int $index
|
||||
* @var array $transaction
|
||||
*/
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
if (!is_int($index)) {
|
||||
throw new FireflyException('Invalid data submitted: transaction is not array.');
|
||||
}
|
||||
$this->validateSingleUpdate($validator, $index, $transaction, $transactionGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when there are no transactions in the array of data.
|
||||
*/
|
||||
public function validateOneRecurrenceTransaction(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateOneRecurrenceTransaction()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
// need at least one transaction
|
||||
if (0 === count($transactions)) {
|
||||
$validator->errors()->add('transactions', (string) trans('validation.at_least_one_transaction'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when there are no transactions in the array of data.
|
||||
*/
|
||||
public function validateOneTransaction(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateOneTransaction');
|
||||
if ($validator->errors()->count() > 0) {
|
||||
app('log')->debug('Validator already has errors, so return.');
|
||||
|
||||
return;
|
||||
}
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
// need at least one transaction
|
||||
if (0 === count($transactions)) {
|
||||
$validator->errors()->add('transactions.0.description', (string) trans('validation.at_least_one_transaction'));
|
||||
app('log')->debug('Added error: at_least_one_transaction.');
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Added NO errors.');
|
||||
}
|
||||
|
||||
public function validateTransactionArray(Validator $validator): void
|
||||
{
|
||||
if ($validator->errors()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
foreach (array_keys($transactions) as $key) {
|
||||
if (!is_int($key)) {
|
||||
$validator->errors()->add('transactions.0.description', (string) trans('validation.at_least_one_transaction'));
|
||||
app('log')->debug('Added error: at_least_one_transaction.');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All types of splits must be equal.
|
||||
*/
|
||||
public function validateTransactionTypes(Validator $validator): void
|
||||
{
|
||||
if ($validator->errors()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Now in validateTransactionTypes()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
$types = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$types[] = $transaction['type'] ?? 'invalid';
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
if (count($unique) > 1) {
|
||||
$validator->errors()->add('transactions.0.type', (string) trans('validation.transaction_types_equal'));
|
||||
|
||||
return;
|
||||
}
|
||||
$first = $unique[0] ?? 'invalid';
|
||||
if ('invalid' === $first) {
|
||||
$validator->errors()->add('transactions.0.type', (string) trans('validation.invalid_transaction_type'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All types of splits must be equal.
|
||||
*/
|
||||
public function validateTransactionTypesForUpdate(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateTransactionTypesForUpdate()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
$types = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$originalType = $this->getOriginalType((int) ($transaction['transaction_journal_id'] ?? 0));
|
||||
// if type is not set, fall back to the type of the journal, if one is given.
|
||||
$types[] = $transaction['type'] ?? $originalType;
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
if (count($unique) > 1) {
|
||||
app('log')->warning('Add error for mismatch transaction types.');
|
||||
$validator->errors()->add('transactions.0.type', (string) trans('validation.transaction_types_equal'));
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('No errors in validateTransactionTypesForUpdate()');
|
||||
}
|
||||
|
||||
protected function getTransactionsArray(Validator $validator): array
|
||||
{
|
||||
app('log')->debug('Now in getTransactionsArray');
|
||||
@@ -238,10 +108,10 @@ trait TransactionValidation
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
// validate source account.
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int) $transaction['source_id'] : null;
|
||||
$sourceName = array_key_exists('source_name', $transaction) ? (string) $transaction['source_name'] : null;
|
||||
$sourceIban = array_key_exists('source_iban', $transaction) ? (string) $transaction['source_iban'] : null;
|
||||
$sourceNumber = array_key_exists('source_number', $transaction) ? (string) $transaction['source_number'] : null;
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = array_key_exists('source_name', $transaction) ? (string)$transaction['source_name'] : null;
|
||||
$sourceIban = array_key_exists('source_iban', $transaction) ? (string)$transaction['source_iban'] : null;
|
||||
$sourceNumber = array_key_exists('source_number', $transaction) ? (string)$transaction['source_number'] : null;
|
||||
$source = [
|
||||
'id' => $sourceId,
|
||||
'name' => $sourceName,
|
||||
@@ -258,10 +128,10 @@ trait TransactionValidation
|
||||
return;
|
||||
}
|
||||
// validate destination account
|
||||
$destinationId = array_key_exists('destination_id', $transaction) ? (int) $transaction['destination_id'] : null;
|
||||
$destinationName = array_key_exists('destination_name', $transaction) ? (string) $transaction['destination_name'] : null;
|
||||
$destinationIban = array_key_exists('destination_iban', $transaction) ? (string) $transaction['destination_iban'] : null;
|
||||
$destinationNumber = array_key_exists('destination_number', $transaction) ? (string) $transaction['destination_number'] : null;
|
||||
$destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = array_key_exists('destination_name', $transaction) ? (string)$transaction['destination_name'] : null;
|
||||
$destinationIban = array_key_exists('destination_iban', $transaction) ? (string)$transaction['destination_iban'] : null;
|
||||
$destinationNumber = array_key_exists('destination_number', $transaction) ? (string)$transaction['destination_number'] : null;
|
||||
$destination = [
|
||||
'id' => $destinationId,
|
||||
'name' => $destinationName,
|
||||
@@ -308,92 +178,6 @@ trait TransactionValidation
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateSingleUpdate(Validator $validator, int $index, array $transaction, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
app('log')->debug('Now validating single account update in validateSingleUpdate()');
|
||||
|
||||
// if no account types are given, just skip the check.
|
||||
if (
|
||||
!array_key_exists('source_id', $transaction)
|
||||
&& !array_key_exists('source_name', $transaction)
|
||||
&& !array_key_exists('destination_id', $transaction)
|
||||
&& !array_key_exists('destination_name', $transaction)) {
|
||||
app('log')->debug('No account data has been submitted so will not validating account info.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// create validator:
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
// get the transaction type using the original transaction group:
|
||||
$accountValidator->setTransactionType($this->getTransactionType($transactionGroup, []));
|
||||
|
||||
// validate if the submitted source ID/name/iban/number are valid
|
||||
if (
|
||||
array_key_exists('source_id', $transaction)
|
||||
|| array_key_exists('source_name', $transaction)
|
||||
|| array_key_exists('source_iban', $transaction)
|
||||
|| array_key_exists('source_number', $transaction)
|
||||
) {
|
||||
app('log')->debug('Will try to validate source account information.');
|
||||
$sourceId = (int) ($transaction['source_id'] ?? 0);
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$sourceIban = $transaction['source_iban'] ?? null;
|
||||
$sourceNumber = $transaction['source_number'] ?? null;
|
||||
$validSource = $accountValidator->validateSource(
|
||||
['id' => $sourceId, 'name' => $sourceName, 'iban' => $sourceIban, 'number' => $sourceNumber]
|
||||
);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
app('log')->warning('Looks like the source account is not valid so complain to the user about it.');
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_iban', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_number', $index), $accountValidator->sourceError);
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Source account info is valid.');
|
||||
}
|
||||
|
||||
if (
|
||||
array_key_exists('destination_id', $transaction)
|
||||
|| array_key_exists('destination_name', $transaction)
|
||||
|| array_key_exists('destination_iban', $transaction)
|
||||
|| array_key_exists('destination_number', $transaction)
|
||||
) {
|
||||
app('log')->debug('Will try to validate destination account information.');
|
||||
// at this point the validator may not have a source account, because it was never submitted for validation.
|
||||
// must add it ourselves or the validator can never check if the destination is correct.
|
||||
// the $transaction array must have a journal id or it's just one, this was validated before.
|
||||
if (null === $accountValidator->source) {
|
||||
app('log')->debug('Account validator has no source account, must find it.');
|
||||
$source = $this->getOriginalSource($transaction, $transactionGroup);
|
||||
if (null !== $source) {
|
||||
app('log')->debug('Found a source!');
|
||||
$accountValidator->source = $source;
|
||||
}
|
||||
}
|
||||
$destinationId = (int) ($transaction['destination_id'] ?? 0);
|
||||
$destinationName = $transaction['destination_name'] ?? null;
|
||||
$destinationIban = $transaction['destination_iban'] ?? null;
|
||||
$destinationNumber = $transaction['destination_number'] ?? null;
|
||||
$array = ['id' => $destinationId, 'name' => $destinationName, 'iban' => $destinationIban, 'number' => $destinationNumber];
|
||||
$validDestination = $accountValidator->validateDestination($array);
|
||||
// do something with result:
|
||||
if (false === $validDestination) {
|
||||
app('log')->warning('Looks like the destination account is not valid so complain to the user about it.');
|
||||
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
|
||||
}
|
||||
app('log')->debug('Destination account info is valid.');
|
||||
}
|
||||
app('log')->debug('Done with validateSingleUpdate().');
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO describe this method.
|
||||
*
|
||||
@@ -459,17 +243,17 @@ trait TransactionValidation
|
||||
|
||||
// no foreign currency information is present:
|
||||
if (!$this->hasForeignCurrencyInfo($transaction)) {
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string) trans('validation.require_foreign_currency'));
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_currency'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// wrong currency information is present
|
||||
$foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false;
|
||||
$foreignCurrencyId = (int) ($transaction['foreign_currency_id'] ?? 0);
|
||||
$foreignCurrencyId = (int)($transaction['foreign_currency_id'] ?? 0);
|
||||
app('log')->debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction);
|
||||
if ($foreignCurrencyCode !== $sourceCurrency->code && $foreignCurrencyId !== $sourceCurrency->id) {
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_currency_code', $index), (string) trans('validation.require_foreign_src'));
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_currency_code', $index), (string)trans('validation.require_foreign_src'));
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -486,19 +270,19 @@ trait TransactionValidation
|
||||
|
||||
// no foreign currency information is present:
|
||||
if (!$this->hasForeignCurrencyInfo($transaction)) {
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string) trans('validation.require_foreign_currency'));
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_currency'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// wrong currency information is present
|
||||
$foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false;
|
||||
$foreignCurrencyId = (int) ($transaction['foreign_currency_id'] ?? 0);
|
||||
$foreignCurrencyId = (int)($transaction['foreign_currency_id'] ?? 0);
|
||||
app('log')->debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction);
|
||||
if ($foreignCurrencyCode !== $destinationCurrency->code && $foreignCurrencyId !== $destinationCurrency->id) {
|
||||
app('log')->debug(sprintf('No match on code, "%s" vs "%s"', $foreignCurrencyCode, $destinationCurrency->code));
|
||||
app('log')->debug(sprintf('No match on ID, #%d vs #%d', $foreignCurrencyId, $destinationCurrency->id));
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string) trans('validation.require_foreign_dest'));
|
||||
$validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_dest'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -536,16 +320,130 @@ trait TransactionValidation
|
||||
if ('' === $transaction['foreign_amount']) {
|
||||
return false;
|
||||
}
|
||||
if (0 === bccomp('0', (string) $transaction['foreign_amount'])) {
|
||||
if (0 === bccomp('0', (string)$transaction['foreign_amount'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given account information. Switches on given transaction type.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function validateAccountInformationUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
app('log')->debug('Now in validateAccountInformationUpdate()');
|
||||
if ($validator->errors()->count() > 0) {
|
||||
app('log')->debug('Validator already has errors, so return.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
/**
|
||||
* @var null|int $index
|
||||
* @var array $transaction
|
||||
*/
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
if (!is_int($index)) {
|
||||
throw new FireflyException('Invalid data submitted: transaction is not array.');
|
||||
}
|
||||
$this->validateSingleUpdate($validator, $index, $transaction, $transactionGroup);
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateSingleUpdate(Validator $validator, int $index, array $transaction, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
app('log')->debug('Now validating single account update in validateSingleUpdate()');
|
||||
|
||||
// if no account types are given, just skip the check.
|
||||
if (
|
||||
!array_key_exists('source_id', $transaction)
|
||||
&& !array_key_exists('source_name', $transaction)
|
||||
&& !array_key_exists('destination_id', $transaction)
|
||||
&& !array_key_exists('destination_name', $transaction)) {
|
||||
app('log')->debug('No account data has been submitted so will not validating account info.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// create validator:
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
// get the transaction type using the original transaction group:
|
||||
$accountValidator->setTransactionType($this->getTransactionType($transactionGroup, []));
|
||||
|
||||
// validate if the submitted source ID/name/iban/number are valid
|
||||
if (
|
||||
array_key_exists('source_id', $transaction)
|
||||
|| array_key_exists('source_name', $transaction)
|
||||
|| array_key_exists('source_iban', $transaction)
|
||||
|| array_key_exists('source_number', $transaction)
|
||||
) {
|
||||
app('log')->debug('Will try to validate source account information.');
|
||||
$sourceId = (int)($transaction['source_id'] ?? 0);
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$sourceIban = $transaction['source_iban'] ?? null;
|
||||
$sourceNumber = $transaction['source_number'] ?? null;
|
||||
$validSource = $accountValidator->validateSource(
|
||||
['id' => $sourceId, 'name' => $sourceName, 'iban' => $sourceIban, 'number' => $sourceNumber]
|
||||
);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
app('log')->warning('Looks like the source account is not valid so complain to the user about it.');
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_iban', $index), $accountValidator->sourceError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_number', $index), $accountValidator->sourceError);
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Source account info is valid.');
|
||||
}
|
||||
|
||||
if (
|
||||
array_key_exists('destination_id', $transaction)
|
||||
|| array_key_exists('destination_name', $transaction)
|
||||
|| array_key_exists('destination_iban', $transaction)
|
||||
|| array_key_exists('destination_number', $transaction)
|
||||
) {
|
||||
app('log')->debug('Will try to validate destination account information.');
|
||||
// at this point the validator may not have a source account, because it was never submitted for validation.
|
||||
// must add it ourselves or the validator can never check if the destination is correct.
|
||||
// the $transaction array must have a journal id or it's just one, this was validated before.
|
||||
if (null === $accountValidator->source) {
|
||||
app('log')->debug('Account validator has no source account, must find it.');
|
||||
$source = $this->getOriginalSource($transaction, $transactionGroup);
|
||||
if (null !== $source) {
|
||||
app('log')->debug('Found a source!');
|
||||
$accountValidator->source = $source;
|
||||
}
|
||||
}
|
||||
$destinationId = (int)($transaction['destination_id'] ?? 0);
|
||||
$destinationName = $transaction['destination_name'] ?? null;
|
||||
$destinationIban = $transaction['destination_iban'] ?? null;
|
||||
$destinationNumber = $transaction['destination_number'] ?? null;
|
||||
$array = ['id' => $destinationId, 'name' => $destinationName, 'iban' => $destinationIban, 'number' => $destinationNumber];
|
||||
$validDestination = $accountValidator->validateDestination($array);
|
||||
// do something with result:
|
||||
if (false === $validDestination) {
|
||||
app('log')->warning('Looks like the destination account is not valid so complain to the user about it.');
|
||||
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
|
||||
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
|
||||
}
|
||||
app('log')->debug('Destination account info is valid.');
|
||||
}
|
||||
app('log')->debug('Done with validateSingleUpdate().');
|
||||
}
|
||||
|
||||
private function getTransactionType(TransactionGroup $group, array $transactions): string
|
||||
{
|
||||
return $transactions[0]['type'] ?? strtolower((string) $group->transactionJournals()->first()?->transactionType->type);
|
||||
return $transactions[0]['type'] ?? strtolower((string)$group->transactionJournals()->first()?->transactionType->type);
|
||||
}
|
||||
|
||||
private function getOriginalSource(array $transaction, TransactionGroup $transactionGroup): ?Account
|
||||
@@ -558,7 +456,7 @@ trait TransactionValidation
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($transactionGroup->transactionJournals as $journal) {
|
||||
$journalId = (int) ($transaction['transaction_journal_id'] ?? 0);
|
||||
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
|
||||
if ($journal->id === $journalId) {
|
||||
return $journal->transactions()->where('amount', '<', 0)->first()?->account;
|
||||
}
|
||||
@@ -567,6 +465,108 @@ trait TransactionValidation
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when there are no transactions in the array of data.
|
||||
*/
|
||||
public function validateOneRecurrenceTransaction(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateOneRecurrenceTransaction()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
// need at least one transaction
|
||||
if (0 === count($transactions)) {
|
||||
$validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when there are no transactions in the array of data.
|
||||
*/
|
||||
public function validateOneTransaction(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateOneTransaction');
|
||||
if ($validator->errors()->count() > 0) {
|
||||
app('log')->debug('Validator already has errors, so return.');
|
||||
|
||||
return;
|
||||
}
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
// need at least one transaction
|
||||
if (0 === count($transactions)) {
|
||||
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
|
||||
app('log')->debug('Added error: at_least_one_transaction.');
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Added NO errors.');
|
||||
}
|
||||
|
||||
public function validateTransactionArray(Validator $validator): void
|
||||
{
|
||||
if ($validator->errors()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
foreach (array_keys($transactions) as $key) {
|
||||
if (!is_int($key)) {
|
||||
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
|
||||
app('log')->debug('Added error: at_least_one_transaction.');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All types of splits must be equal.
|
||||
*/
|
||||
public function validateTransactionTypes(Validator $validator): void
|
||||
{
|
||||
if ($validator->errors()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Now in validateTransactionTypes()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
$types = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$types[] = $transaction['type'] ?? 'invalid';
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
if (count($unique) > 1) {
|
||||
$validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal'));
|
||||
|
||||
return;
|
||||
}
|
||||
$first = $unique[0] ?? 'invalid';
|
||||
if ('invalid' === $first) {
|
||||
$validator->errors()->add('transactions.0.type', (string)trans('validation.invalid_transaction_type'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All types of splits must be equal.
|
||||
*/
|
||||
public function validateTransactionTypesForUpdate(Validator $validator): void
|
||||
{
|
||||
app('log')->debug('Now in validateTransactionTypesForUpdate()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
$types = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$originalType = $this->getOriginalType((int)($transaction['transaction_journal_id'] ?? 0));
|
||||
// if type is not set, fall back to the type of the journal, if one is given.
|
||||
$types[] = $transaction['type'] ?? $originalType;
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
if (count($unique) > 1) {
|
||||
app('log')->warning('Add error for mismatch transaction types.');
|
||||
$validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal'));
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug('No errors in validateTransactionTypesForUpdate()');
|
||||
}
|
||||
|
||||
private function getOriginalType(int $journalId): string
|
||||
{
|
||||
if (0 === $journalId) {
|
||||
@@ -608,22 +608,22 @@ trait TransactionValidation
|
||||
default:
|
||||
case 'withdrawal':
|
||||
if (count($sources) > 1) {
|
||||
$validator->errors()->add('transactions.0.source_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'deposit':
|
||||
if (count($dests) > 1) {
|
||||
$validator->errors()->add('transactions.0.destination_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'transfer':
|
||||
if (count($sources) > 1 || count($dests) > 1) {
|
||||
$validator->errors()->add('transactions.0.source_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -657,14 +657,14 @@ trait TransactionValidation
|
||||
$result = $this->compareAccountData($type, $comparison);
|
||||
if (false === $result) {
|
||||
if ('withdrawal' === $type) {
|
||||
$validator->errors()->add('transactions.0.source_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
if ('deposit' === $type) {
|
||||
$validator->errors()->add('transactions.0.destination_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
if ('transfer' === $type) {
|
||||
$validator->errors()->add('transactions.0.source_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string) trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
||||
}
|
||||
app('log')->warning('Add error about equal accounts.');
|
||||
|
||||
@@ -683,7 +683,7 @@ trait TransactionValidation
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
// source or destination may be omitted. If this is the case, use the original source / destination name + ID.
|
||||
$originalData = $this->getOriginalData((int) ($transaction['transaction_journal_id'] ?? 0));
|
||||
$originalData = $this->getOriginalData((int)($transaction['transaction_journal_id'] ?? 0));
|
||||
|
||||
// get field.
|
||||
$comparison[$field][] = $transaction[$field] ?? $originalData[$field];
|
||||
|
Reference in New Issue
Block a user