Refactor some code for recurrences.

This commit is contained in:
James Cole
2019-06-08 06:19:21 +02:00
parent 7c2c24d330
commit 85f9c256a1
21 changed files with 369 additions and 468 deletions

View File

@@ -130,7 +130,7 @@ class AccountValidator
}
/**
* @param int|null $accountId
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool
@@ -195,19 +195,23 @@ class AccountValidator
*/
private function canCreateTypes(array $accountTypes): bool
{
Log::debug('Can we create any of these types?', $accountTypes);
/** @var string $accountType */
foreach ($accountTypes as $accountType) {
if ($this->canCreateType($accountType)) {
Log::debug(sprintf('YES, we can create a %s', $accountType));
return true;
}
}
Log::debug('NO, we cant create any of those.');
return false;
}
/**
* @param array $validTypes
* @param int|null $accountId
* @param array $validTypes
* @param int|null $accountId
* @param string|null $accountName
*
* @return Account|null
@@ -282,7 +286,7 @@ class AccountValidator
}
/**
* @param int|null $accountId
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool
@@ -360,7 +364,7 @@ class AccountValidator
}
/**
* @param int|null $accountId
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool
@@ -391,7 +395,7 @@ class AccountValidator
}
/**
* @param int|null $accountId
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool
@@ -409,6 +413,20 @@ class AccountValidator
return false;
}
// if there's an ID it must be of the "validTypes".
if (null !== $accountId && 0 !== $accountId) {
$found = $this->accountRepository->findNull($accountId);
if (null !== $found) {
$type = $found->accountType->type;
if (in_array($type, $validTypes)) {
return true;
}
$this->destError = (string)trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
return false;
}
}
// if the account can be created anyway don't need to search.
if (true === $this->canCreateTypes($validTypes)) {
@@ -420,7 +438,7 @@ class AccountValidator
}
/**
* @param int|null $accountId
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool

View File

@@ -26,8 +26,8 @@ namespace FireflyIII\Validation;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Validation\Validator;
use Log;
/**
* Trait TransactionValidation
@@ -41,15 +41,16 @@ trait TransactionValidation
*/
public function validateAccountInformation(Validator $validator): void
{
$data = $validator->getData();
$transactions = $data['transactions'] ?? [];
Log::debug('Now in validateAccountInformation()');
$data = $validator->getData();
$transactionType = $data['type'] ?? 'invalid';
$transactions = $data['transactions'] ?? [];
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
foreach ($transactions as $index => $transaction) {
$transactionType = $transaction['type'] ?? 'invalid';
$accountValidator->setTransactionType($transactionType);
// validate source account.
@@ -224,7 +225,7 @@ trait TransactionValidation
foreach ($transactions as $index => $transaction) {
$originalType = $this->getOriginalType($transaction['transaction_journal_id'] ?? 0);
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0);
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0);
$transactionType = $transaction['type'] ?? $originalType;
$accountValidator->setTransactionType($transactionType);