mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Various code cleanup and fixed alignments.
This commit is contained in:
@@ -41,13 +41,13 @@ trait DepositValidation
|
||||
app('log')->debug('Now in validateDepositDestination', $array);
|
||||
|
||||
// source can be any of the following types.
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the destination of a deposit can't be created.
|
||||
$this->destError = (string) trans('validation.deposit_dest_need_data');
|
||||
app('log')->error('Both values are NULL, cant create deposit destination.');
|
||||
$result = false;
|
||||
$result = false;
|
||||
}
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
@@ -94,10 +94,10 @@ trait DepositValidation
|
||||
|
||||
// null = we found nothing at all or didn't even search
|
||||
// false = invalid results
|
||||
$result = null;
|
||||
$result = null;
|
||||
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId
|
||||
&& null === $accountName
|
||||
&& null === $accountIban
|
||||
@@ -168,7 +168,7 @@ trait DepositValidation
|
||||
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
$result = true;
|
||||
$result = true;
|
||||
|
||||
// set the source to be a (dummy) revenue account.
|
||||
$account = new Account();
|
||||
|
||||
@@ -76,7 +76,7 @@ trait LiabilityValidation
|
||||
app('log')->debug('Now in validateLCSource', $array);
|
||||
// if the array has an ID and ID is not null, try to find it and check type.
|
||||
// this account must be a liability
|
||||
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
|
||||
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
|
||||
if (null !== $accountId) {
|
||||
app('log')->debug('Source ID is not null, assume were looking for a liability.');
|
||||
// find liability credit:
|
||||
@@ -95,7 +95,7 @@ trait LiabilityValidation
|
||||
// if array has name and is not null, return true.
|
||||
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
|
||||
|
||||
$result = true;
|
||||
$result = true;
|
||||
if ('' === $accountName || null === $accountName) {
|
||||
app('log')->error('Array must have a name, is not the case, return false.');
|
||||
$result = false;
|
||||
|
||||
@@ -40,13 +40,13 @@ trait OBValidation
|
||||
app('log')->debug('Now in validateOBDestination', $array);
|
||||
|
||||
// source can be any of the following types.
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source?->accountType->type] ?? [];
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source?->accountType->type] ?? [];
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the destination of a deposit can't be created.
|
||||
$this->destError = (string)trans('validation.ob_dest_need_data');
|
||||
app('log')->error('Both values are NULL, cant create OB destination.');
|
||||
$result = false;
|
||||
$result = false;
|
||||
}
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
@@ -84,9 +84,9 @@ trait OBValidation
|
||||
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
|
||||
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
|
||||
app('log')->debug('Now in validateOBSource', $array);
|
||||
$result = null;
|
||||
$result = null;
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL return false,
|
||||
@@ -104,7 +104,7 @@ trait OBValidation
|
||||
|
||||
// the source resulted in an account, but it's not of a valid type.
|
||||
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
|
||||
$message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type);
|
||||
$message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type);
|
||||
app('log')->debug($message);
|
||||
$this->sourceError = $message;
|
||||
$result = false;
|
||||
@@ -120,10 +120,10 @@ trait OBValidation
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
app('log')->debug('Result is still null.');
|
||||
$result = true;
|
||||
$result = true;
|
||||
|
||||
// set the source to be a (dummy) initial balance account.
|
||||
$account = new Account();
|
||||
$account = new Account();
|
||||
|
||||
/** @var AccountType $accountType */
|
||||
$accountType = AccountType::whereType(AccountType::INITIAL_BALANCE)->first();
|
||||
|
||||
@@ -50,8 +50,8 @@ trait ReconciliationValidation
|
||||
app('log')->debug('Now in validateReconciliationDestination', $array);
|
||||
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
|
||||
@@ -85,8 +85,8 @@ trait ReconciliationValidation
|
||||
app('log')->debug('Now in validateReconciliationSource', $array);
|
||||
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
|
||||
|
||||
@@ -37,7 +37,7 @@ trait TransferValidation
|
||||
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
|
||||
app('log')->debug('Now in validateTransferDestination', $array);
|
||||
// source can be any of the following types.
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the destination of a transfer can't be created.
|
||||
@@ -48,7 +48,7 @@ trait TransferValidation
|
||||
}
|
||||
|
||||
// or try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
|
||||
@@ -79,7 +79,7 @@ trait TransferValidation
|
||||
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
|
||||
app('log')->debug('Now in validateTransferSource', $array);
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName
|
||||
&& null === $accountIban && null === $accountNumber
|
||||
&& false === $this->canCreateTypes($validTypes)) {
|
||||
@@ -92,7 +92,7 @@ trait TransferValidation
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
app('log')->warning('Not a valid source, cant find it.', $validTypes);
|
||||
|
||||
@@ -38,7 +38,7 @@ trait WithdrawalValidation
|
||||
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
|
||||
app('log')->debug('Now in validateGenericSource', $array);
|
||||
// source can be any of the following types.
|
||||
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
||||
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
||||
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return TRUE
|
||||
// because we assume the user doesn't want to submit / change anything.
|
||||
@@ -49,7 +49,7 @@ trait WithdrawalValidation
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
|
||||
@@ -74,7 +74,7 @@ trait WithdrawalValidation
|
||||
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
|
||||
app('log')->debug('Now in validateWithdrawalDestination()', $array);
|
||||
// source can be any of the following types.
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
app('log')->debug('Source type can be: ', $validTypes);
|
||||
if (null === $accountId && null === $accountName && null === $accountIban && null === $accountNumber && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL return false,
|
||||
@@ -88,7 +88,7 @@ trait WithdrawalValidation
|
||||
if (null !== $accountId && 0 !== $accountId) {
|
||||
$found = $this->getRepository()->find($accountId);
|
||||
if (null !== $found) {
|
||||
$type = $found->accountType->type;
|
||||
$type = $found->accountType->type;
|
||||
if (in_array($type, $validTypes, true)) {
|
||||
$this->setDestination($found);
|
||||
|
||||
@@ -126,7 +126,7 @@ trait WithdrawalValidation
|
||||
|
||||
app('log')->debug('Now in validateWithdrawalSource', $array);
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && null === $accountNumber && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the source of a withdrawal can't be created.
|
||||
@@ -137,7 +137,7 @@ trait WithdrawalValidation
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
$search = $this->findExistingAccount($validTypes, $array);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
|
||||
|
||||
@@ -135,37 +135,37 @@ class AccountValidator
|
||||
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType);
|
||||
app('log')->error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
|
||||
|
||||
$result = false;
|
||||
$result = false;
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$result = $this->validateWithdrawalDestination($array);
|
||||
$result = $this->validateWithdrawalDestination($array);
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::DEPOSIT:
|
||||
$result = $this->validateDepositDestination($array);
|
||||
$result = $this->validateDepositDestination($array);
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::TRANSFER:
|
||||
$result = $this->validateTransferDestination($array);
|
||||
$result = $this->validateTransferDestination($array);
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::OPENING_BALANCE:
|
||||
$result = $this->validateOBDestination($array);
|
||||
$result = $this->validateOBDestination($array);
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::LIABILITY_CREDIT:
|
||||
$result = $this->validateLCDestination($array);
|
||||
$result = $this->validateLCDestination($array);
|
||||
|
||||
break;
|
||||
|
||||
case TransactionType::RECONCILIATION:
|
||||
$result = $this->validateReconciliationDestination($array);
|
||||
$result = $this->validateReconciliationDestination($array);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ trait ValidatesBulkTransactionQuery
|
||||
) {
|
||||
// find both accounts, must be same type.
|
||||
// already validated: belongs to this user.
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$source = $repository->find((int)$json['where']['account_id']);
|
||||
$dest = $repository->find((int)$json['update']['account_id']);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$source = $repository->find((int)$json['where']['account_id']);
|
||||
$dest = $repository->find((int)$json['update']['account_id']);
|
||||
if (null === $source) {
|
||||
$validator->errors()->add('query', sprintf((string)trans('validation.invalid_query_data'), 'where', 'account_id'));
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ class FireflyValidator extends Validator
|
||||
if (!is_string($value) || 6 !== strlen($value)) {
|
||||
return false;
|
||||
}
|
||||
$user = auth()->user();
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
app('log')->error('No user during validate2faCode');
|
||||
|
||||
@@ -183,10 +183,10 @@ class FireflyValidator extends Validator
|
||||
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'];
|
||||
|
||||
// take
|
||||
$first = substr($value, 0, 4);
|
||||
$last = substr($value, 4);
|
||||
$iban = $last.$first;
|
||||
$iban = trim(str_replace($search, $replace, $iban));
|
||||
$first = substr($value, 0, 4);
|
||||
$last = substr($value, 4);
|
||||
$iban = $last.$first;
|
||||
$iban = trim(str_replace($search, $replace, $iban));
|
||||
if ('' === $iban) {
|
||||
return false;
|
||||
}
|
||||
@@ -257,8 +257,8 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
// first, get the index from this string:
|
||||
$value ??= '';
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
|
||||
@@ -322,8 +322,8 @@ class FireflyValidator extends Validator
|
||||
public function validateRuleTriggerValue(string $attribute, string $value = null): bool
|
||||
{
|
||||
// first, get the index from this string:
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
|
||||
@@ -334,13 +334,13 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
// these trigger types need a numerical check:
|
||||
$numerical = ['amount_less', 'amount_more', 'amount_exactly'];
|
||||
$numerical = ['amount_less', 'amount_more', 'amount_exactly'];
|
||||
if (in_array($triggerType, $numerical, true)) {
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
// these trigger types need a simple strlen check:
|
||||
$length = [
|
||||
$length = [
|
||||
'source_account_starts',
|
||||
'source_account_ends',
|
||||
'source_account_is',
|
||||
@@ -479,7 +479,7 @@ class FireflyValidator extends Validator
|
||||
$accountId = (int) ($parameters[0] ?? 0.0);
|
||||
}
|
||||
|
||||
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
||||
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->whereNull('accounts.deleted_at')
|
||||
->where('accounts.user_id', auth()->user()->id)
|
||||
->where('account_meta.name', 'account_number')
|
||||
@@ -490,8 +490,8 @@ class FireflyValidator extends Validator
|
||||
// exclude current account from check.
|
||||
$query->where('account_meta.account_id', '!=', $accountId);
|
||||
}
|
||||
$set = $query->get(['account_meta.*']);
|
||||
$count = $set->count();
|
||||
$set = $query->get(['account_meta.*']);
|
||||
$count = $set->count();
|
||||
if (0 === $count) {
|
||||
return true;
|
||||
}
|
||||
@@ -499,7 +499,7 @@ class FireflyValidator extends Validator
|
||||
// pretty much impossible but still.
|
||||
return false;
|
||||
}
|
||||
$type = $this->data['objectType'] ?? 'unknown';
|
||||
$type = $this->data['objectType'] ?? 'unknown';
|
||||
if ('expense' !== $type && 'revenue' !== $type) {
|
||||
app('log')->warning(sprintf('Account number "%s" is not unique and account type "%s" cannot share its account number.', $value, $type));
|
||||
|
||||
@@ -569,7 +569,7 @@ class FireflyValidator extends Validator
|
||||
// get existing webhook value:
|
||||
if (0 !== $existingId) {
|
||||
/** @var null|Webhook $webhook */
|
||||
$webhook = auth()->user()->webhooks()->find($existingId);
|
||||
$webhook = auth()->user()->webhooks()->find($existingId);
|
||||
if (null === $webhook) {
|
||||
return false;
|
||||
}
|
||||
@@ -614,18 +614,18 @@ class FireflyValidator extends Validator
|
||||
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
|
||||
{
|
||||
[$table, $field] = $parameters;
|
||||
$exclude = (int) ($parameters[2] ?? 0.0);
|
||||
$exclude = (int) ($parameters[2] ?? 0.0);
|
||||
|
||||
/*
|
||||
* If other data (in $this->getData()) contains
|
||||
* ID field, set that field to be the $exclude.
|
||||
*/
|
||||
$data = $this->getData();
|
||||
$data = $this->getData();
|
||||
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) {
|
||||
$exclude = (int) $data['id'];
|
||||
}
|
||||
// get entries from table
|
||||
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||
->where('id', '!=', $exclude)
|
||||
->where($field, $value)
|
||||
->first([$field])
|
||||
@@ -695,11 +695,11 @@ class FireflyValidator extends Validator
|
||||
$deliveries = Webhook::getDeliveriesForValidation();
|
||||
|
||||
// integers
|
||||
$trigger = $triggers[$this->data['trigger']] ?? 0;
|
||||
$response = $responses[$this->data['response']] ?? 0;
|
||||
$delivery = $deliveries[$this->data['delivery']] ?? 0;
|
||||
$url = $this->data['url'];
|
||||
$userId = auth()->user()->id;
|
||||
$trigger = $triggers[$this->data['trigger']] ?? 0;
|
||||
$response = $responses[$this->data['response']] ?? 0;
|
||||
$delivery = $deliveries[$this->data['delivery']] ?? 0;
|
||||
$url = $this->data['url'];
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
return 0 === Webhook::whereUserId($userId)
|
||||
->where('trigger', $trigger)
|
||||
@@ -719,9 +719,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
|
||||
@@ -732,7 +732,7 @@ class FireflyValidator extends Validator
|
||||
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
||||
{
|
||||
/** @var null|array $search */
|
||||
$search = \Config::get('firefly.accountTypeByIdentifier.'.$type);
|
||||
$search = \Config::get('firefly.accountTypeByIdentifier.'.$type);
|
||||
|
||||
if (null === $search) {
|
||||
return false;
|
||||
@@ -743,7 +743,7 @@ class FireflyValidator extends Validator
|
||||
$accountTypeIds = $accountTypes->pluck('id')->toArray();
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
||||
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
@@ -777,10 +777,10 @@ class FireflyValidator extends Validator
|
||||
/** @var Account $existingAccount */
|
||||
$existingAccount = Account::find($accountId);
|
||||
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
@@ -796,10 +796,10 @@ class FireflyValidator extends Validator
|
||||
/** @var Account $existingAccount */
|
||||
$existingAccount = Account::find($this->data['id']);
|
||||
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first()
|
||||
;
|
||||
|
||||
@@ -91,7 +91,7 @@ trait GroupValidation
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
$count = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
|
||||
$count = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
|
||||
->leftJoin('transaction_groups', 'transaction_groups.id', 'transaction_journals.transaction_group_id')
|
||||
->where('transaction_journals.transaction_group_id', $transactionGroup->id)
|
||||
->where('transactions.reconciled', 1)->where('transactions.amount', '<', 0)->count('transactions.id')
|
||||
@@ -156,7 +156,7 @@ trait GroupValidation
|
||||
$data = $validator->getData();
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
$groupTitle = $data['group_title'] ?? '';
|
||||
$groupTitle = $data['group_title'] ?? '';
|
||||
if ('' === $groupTitle && count($transactions) > 1) {
|
||||
$validator->errors()->add('group_title', (string)trans('validation.group_title_mandatory'));
|
||||
}
|
||||
|
||||
@@ -42,9 +42,9 @@ trait RecurrenceValidation
|
||||
*/
|
||||
public function valUpdateAccountInfo(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$data = $validator->getData();
|
||||
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
|
||||
// grab model from parameter and try to set the transaction type from it
|
||||
if ('invalid' === $transactionType) {
|
||||
@@ -69,14 +69,14 @@ trait RecurrenceValidation
|
||||
}
|
||||
}
|
||||
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
app('log')->debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$transactionType = $transaction['type'] ?? $transactionType;
|
||||
$transactionType = $transaction['type'] ?? $transactionType;
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
if (
|
||||
@@ -88,9 +88,9 @@ trait RecurrenceValidation
|
||||
continue;
|
||||
}
|
||||
// validate source account.
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int) $transaction['source_id'] : null;
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$validSource = $accountValidator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int) $transaction['source_id'] : null;
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$validSource = $accountValidator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
@@ -264,8 +264,8 @@ trait RecurrenceValidation
|
||||
|
||||
return;
|
||||
}
|
||||
$nthDay = (int) ($parameters[0] ?? 0.0);
|
||||
$dayOfWeek = (int) ($parameters[1] ?? 0.0);
|
||||
$nthDay = (int) ($parameters[0] ?? 0.0);
|
||||
$dayOfWeek = (int) ($parameters[1] ?? 0.0);
|
||||
if ($nthDay < 1 || $nthDay > 5) {
|
||||
$validator->errors()->add(sprintf('repetitions.%d.moment', $index), (string) trans('validation.valid_recurrence_rep_moment'));
|
||||
|
||||
@@ -314,15 +314,15 @@ trait RecurrenceValidation
|
||||
|
||||
return;
|
||||
}
|
||||
$originalTrCount = $recurrence->recurrenceTransactions()->count();
|
||||
$originalTrCount = $recurrence->recurrenceTransactions()->count();
|
||||
if (1 === $submittedTrCount && 1 === $originalTrCount) {
|
||||
$first = $transactions[0]; // can safely assume index 0.
|
||||
$first = $transactions[0]; // can safely assume index 0.
|
||||
if (!array_key_exists('id', $first)) {
|
||||
app('log')->debug('Single count and no ID, done.');
|
||||
|
||||
return; // home safe!
|
||||
}
|
||||
$id = $first['id'];
|
||||
$id = $first['id'];
|
||||
if ('' === (string) $id) {
|
||||
app('log')->debug('Single count and empty ID, done.');
|
||||
|
||||
@@ -340,7 +340,7 @@ trait RecurrenceValidation
|
||||
}
|
||||
|
||||
app('log')->debug('Multi ID validation.');
|
||||
$idsMandatory = false;
|
||||
$idsMandatory = false;
|
||||
if ($submittedTrCount < $originalTrCount) {
|
||||
app('log')->debug(sprintf('User submits %d transaction, recurrence has %d transactions. All entries must have ID.', $submittedTrCount, $originalTrCount));
|
||||
$idsMandatory = true;
|
||||
@@ -357,7 +357,7 @@ trait RecurrenceValidation
|
||||
* 2. If the user submits more transactions than already present, count the number of existing transactions. At least those must be matched. After that, submit as many as you like.
|
||||
* 3. If the user submits the same number of transactions as already present, all but one must have an ID.
|
||||
*/
|
||||
$unmatchedIds = 0;
|
||||
$unmatchedIds = 0;
|
||||
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
app('log')->debug(sprintf('Now at %d/%d', $index + 1, $submittedTrCount));
|
||||
@@ -387,7 +387,7 @@ trait RecurrenceValidation
|
||||
}
|
||||
}
|
||||
// if too many don't match, but you haven't submitted more than already present:
|
||||
$maxUnmatched = max(1, $submittedTrCount - $originalTrCount);
|
||||
$maxUnmatched = max(1, $submittedTrCount - $originalTrCount);
|
||||
app('log')->debug(sprintf('Submitted: %d. Original: %d. User can submit %d unmatched transactions.', $submittedTrCount, $originalTrCount, $maxUnmatched));
|
||||
if ($unmatchedIds > $maxUnmatched) {
|
||||
app('log')->warning(sprintf('Too many unmatched transactions (%d).', $unmatchedIds));
|
||||
|
||||
@@ -162,17 +162,17 @@ trait TransactionValidation
|
||||
app('log')->debug('Now in validateTransactionTypes()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
$types = [];
|
||||
$types = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$types[] = $transaction['type'] ?? 'invalid';
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
$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';
|
||||
$first = $unique[0] ?? 'invalid';
|
||||
if ('invalid' === $first) {
|
||||
$validator->errors()->add('transactions.0.type', (string) trans('validation.invalid_transaction_type'));
|
||||
}
|
||||
@@ -189,9 +189,9 @@ trait TransactionValidation
|
||||
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;
|
||||
$types[] = $transaction['type'] ?? $originalType;
|
||||
}
|
||||
$unique = array_unique($types);
|
||||
$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'));
|
||||
@@ -225,7 +225,7 @@ trait TransactionValidation
|
||||
app('log')->debug(sprintf('Now in validateSingleAccount(%d)', $index));
|
||||
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
if (array_key_exists('user', $transaction) && null !== $transaction['user']) {
|
||||
$accountValidator->setUser($transaction['user']);
|
||||
@@ -234,21 +234,21 @@ trait TransactionValidation
|
||||
$accountValidator->setUserGroup($transaction['user_group']);
|
||||
}
|
||||
|
||||
$transactionType = $transaction['type'] ?? $transactionType;
|
||||
$transactionType = $transaction['type'] ?? $transactionType;
|
||||
$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;
|
||||
$source = [
|
||||
$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,
|
||||
'iban' => $sourceIban,
|
||||
'number' => $sourceNumber,
|
||||
];
|
||||
$validSource = $accountValidator->validateSource($source);
|
||||
$validSource = $accountValidator->validateSource($source);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
@@ -423,8 +423,8 @@ trait TransactionValidation
|
||||
|
||||
return;
|
||||
}
|
||||
$source = $accountValidator->source;
|
||||
$destination = $accountValidator->destination;
|
||||
$source = $accountValidator->source;
|
||||
$destination = $accountValidator->destination;
|
||||
|
||||
app('log')->debug(sprintf('Source: #%d "%s (%s)"', $source->id, $source->name, $source->accountType->type));
|
||||
app('log')->debug(sprintf('Destination: #%d "%s" (%s)', $destination->id, $destination->name, $source->accountType->type));
|
||||
@@ -594,15 +594,15 @@ trait TransactionValidation
|
||||
if (count($transactions) < 2) {
|
||||
return;
|
||||
}
|
||||
$type = $transactions[0]['type'] ?? 'withdrawal';
|
||||
$sources = [];
|
||||
$dests = [];
|
||||
$type = $transactions[0]['type'] ?? 'withdrawal';
|
||||
$sources = [];
|
||||
$dests = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$sources[] = sprintf('%d-%s', $transaction['source_id'] ?? 0, $transaction['source_name'] ?? '');
|
||||
$dests[] = sprintf('%d-%s', $transaction['destination_id'] ?? 0, $transaction['destination_name'] ?? '');
|
||||
}
|
||||
$sources = array_unique($sources);
|
||||
$dests = array_unique($dests);
|
||||
$sources = array_unique($sources);
|
||||
$dests = array_unique($dests);
|
||||
|
||||
switch ($type) {
|
||||
default:
|
||||
@@ -646,15 +646,15 @@ trait TransactionValidation
|
||||
|
||||
return;
|
||||
}
|
||||
$type = $this->getTransactionType($transactionGroup, $transactions);
|
||||
$type = $this->getTransactionType($transactionGroup, $transactions);
|
||||
|
||||
// compare source IDs, destination IDs, source names and destination names.
|
||||
// I think I can get away with one combination being equal, as long as the rest
|
||||
// of the code picks up on this as well.
|
||||
// either way all fields must be blank or all equal
|
||||
// but if IDs are equal don't bother with the names.
|
||||
$comparison = $this->collectComparisonData($transactions);
|
||||
$result = $this->compareAccountData($type, $comparison);
|
||||
$comparison = $this->collectComparisonData($transactions);
|
||||
$result = $this->compareAccountData($type, $comparison);
|
||||
if (false === $result) {
|
||||
if ('withdrawal' === $type) {
|
||||
$validator->errors()->add('transactions.0.source_id', (string) trans('validation.all_accounts_equal'));
|
||||
@@ -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];
|
||||
@@ -695,7 +695,7 @@ trait TransactionValidation
|
||||
|
||||
private function getOriginalData(int $journalId): array
|
||||
{
|
||||
$return = [
|
||||
$return = [
|
||||
'source_id' => 0,
|
||||
'source_name' => '',
|
||||
'destination_id' => 0,
|
||||
@@ -706,7 +706,7 @@ trait TransactionValidation
|
||||
}
|
||||
|
||||
/** @var null|Transaction $source */
|
||||
$source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first();
|
||||
$source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first();
|
||||
if (null !== $source) {
|
||||
$return['source_id'] = $source->account_id;
|
||||
$return['source_name'] = $source->account->name;
|
||||
|
||||
Reference in New Issue
Block a user