firefly-iii/app/Validation/TransactionValidation.php

789 lines
34 KiB
PHP
Raw Normal View History

2018-07-05 11:02:02 -05:00
<?php
/**
* TransactionValidation.php
2020-02-16 06:58:22 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-05 11:02:02 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-05 11:02:02 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-05 11:02:02 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-05 11:02:02 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-05 11:02:02 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-05 11:02:02 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Validation;
2023-04-25 22:55:31 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
2023-03-11 00:09:27 -06:00
use FireflyIII\Models\AccountType;
2019-06-02 09:33:25 -05:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
2019-06-04 13:42:11 -05:00
use FireflyIII\Models\TransactionJournal;
2022-10-02 13:13:32 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Models\UserGroup;
2023-03-11 00:09:27 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Validation\Validator;
2018-07-05 11:02:02 -05:00
/**
* Trait TransactionValidation
*/
trait TransactionValidation
{
/**
* Validates the given account information. Switches on given transaction type.
*
* Inclusion of user and/or group is optional.
2018-07-05 11:02:02 -05:00
*/
public function validateAccountInformation(Validator $validator, ?User $user = null, ?UserGroup $userGroup = null): void
2018-07-05 11:02:02 -05:00
{
if ($validator->errors()->count() > 0) {
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in validateAccountInformation (TransactionValidation) ()');
$transactions = $this->getTransactionsArray($validator);
$data = $validator->getData();
2019-06-07 23:19:21 -05:00
$transactionType = $data['type'] ?? 'invalid';
2020-02-05 13:37:23 -06:00
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
2023-12-20 12:35:52 -06:00
2020-03-15 09:31:59 -05:00
/**
2023-12-20 12:35:52 -06:00
* @var null|int $index
2023-11-30 22:15:59 -06:00
* @var array $transaction
2020-03-15 09:31:59 -05:00
*/
foreach ($transactions as $index => $transaction) {
$transaction['user'] = $user;
$transaction['user_group'] = $userGroup;
if (!is_int($index)) {
2020-10-24 09:49:05 -05:00
continue;
}
2020-03-15 09:31:59 -05:00
$this->validateSingleAccount($validator, $index, $transactionType, $transaction);
}
}
2021-03-21 03:15:40 -05:00
protected function getTransactionsArray(Validator $validator): array
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in getTransactionsArray');
2021-03-21 03:15:40 -05:00
$data = $validator->getData();
$transactions = [];
2023-11-05 09:55:16 -06:00
if (array_key_exists('transactions', $data) && is_array($data['transactions'])) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Transactions key exists and is array.');
$transactions = $data['transactions'];
}
2023-11-05 09:55:16 -06:00
if (array_key_exists('transactions', $data) && !is_array($data['transactions'])) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Transactions key exists but is NOT array, its a %s', gettype($data['transactions'])));
}
2021-03-21 03:15:40 -05:00
return $transactions;
}
2023-12-22 13:12:38 -06:00
/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
2020-03-15 09:31:59 -05:00
protected function validateSingleAccount(Validator $validator, int $index, string $transactionType, array $transaction): void
{
app('log')->debug(sprintf('Now in validateSingleAccount(%d)', $index));
2023-12-20 12:35:52 -06:00
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
2018-07-05 11:02:02 -05:00
if (array_key_exists('user', $transaction) && null !== $transaction['user']) {
$accountValidator->setUser($transaction['user']);
}
if (array_key_exists('user_group', $transaction) && null !== $transaction['user_group']) {
$accountValidator->setUserGroup($transaction['user_group']);
}
$transactionType = $transaction['type'] ?? $transactionType;
2020-03-15 09:31:59 -05:00
$accountValidator->setTransactionType($transactionType);
2020-03-15 09:31:59 -05:00
// 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 = [
2021-12-18 05:35:17 -06:00
'id' => $sourceId,
'name' => $sourceName,
'iban' => $sourceIban,
'number' => $sourceNumber,
];
$validSource = $accountValidator->validateSource($source);
2018-07-05 11:02:02 -05:00
2020-03-15 09:31:59 -05:00
// do something with result:
if (false === $validSource) {
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
2018-07-05 11:02:02 -05:00
2020-03-15 09:31:59 -05:00
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;
2022-10-02 13:13:32 -05:00
$destination = [
2021-12-18 05:35:17 -06:00
'id' => $destinationId,
'name' => $destinationName,
'iban' => $destinationIban,
'number' => $destinationNumber,
];
2022-10-02 13:13:32 -05:00
$validDestination = $accountValidator->validateDestination($destination);
2020-03-15 09:31:59 -05:00
// do something with result:
if (false === $validDestination) {
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
2018-07-05 11:02:02 -05:00
}
2022-10-02 13:13:32 -05:00
// sanity check for reconciliation accounts. They can't both be null.
$this->sanityCheckReconciliation($validator, $transactionType, $index, $source, $destination);
2023-06-21 05:34:58 -05:00
// sanity check for currency information.
$this->sanityCheckForeignCurrency($validator, $accountValidator, $transaction, $transactionType, $index);
}
/**
2023-11-30 22:15:59 -06:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2023-06-21 05:34:58 -05:00
*/
protected function sanityCheckReconciliation(Validator $validator, string $transactionType, int $index, array $source, array $destination): void
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in sanityCheckReconciliation');
2023-12-20 12:35:52 -06:00
if (TransactionType::RECONCILIATION === ucfirst($transactionType)
&& null === $source['id'] && null === $source['name'] && null === $destination['id'] && null === $destination['name']
2023-06-21 05:34:58 -05:00
) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Both are NULL, error!');
2023-06-21 05:34:58 -05:00
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), trans('validation.reconciliation_either_account'));
}
2023-12-20 12:35:52 -06:00
if (TransactionType::RECONCILIATION === $transactionType
&& (null !== $source['id'] || null !== $source['name'])
&& (null !== $destination['id'] || null !== $destination['name'])) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Both are not NULL, error!');
2023-06-21 05:34:58 -05:00
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), trans('validation.reconciliation_either_account'));
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), trans('validation.reconciliation_either_account'));
}
}
/**
* TODO describe this method.
*
2023-11-30 22:15:59 -06:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2023-12-22 13:12:38 -06:00
* @SuppressWarnings(PHPMD.NPathComplexity)
2023-06-21 05:34:58 -05:00
*/
private function sanityCheckForeignCurrency(
Validator $validator,
AccountValidator $accountValidator,
array $transaction,
string $transactionType,
int $index
2023-12-20 12:35:52 -06:00
): void {
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in sanityCheckForeignCurrency()');
2023-06-21 05:34:58 -05:00
if (0 !== $validator->errors()->count()) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Already have errors, return');
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
if (null === $accountValidator->source) {
2023-10-29 00:33:43 -05:00
app('log')->debug('No source, return');
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
if (null === $accountValidator->destination) {
2023-10-29 00:33:43 -05:00
app('log')->debug('No destination, return');
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
$source = $accountValidator->source;
$destination = $accountValidator->destination;
2023-06-21 05:34:58 -05:00
2023-10-29 00:33:43 -05:00
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));
2023-06-21 05:34:58 -05:00
if (!$this->isLiabilityOrAsset($source) || !$this->isLiabilityOrAsset($destination)) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Any account must be liability or asset account to continue.');
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$defaultCurrency = app('amount')->getDefaultCurrency();
$sourceCurrency = $accountRepository->getAccountCurrency($source) ?? $defaultCurrency;
$destinationCurrency = $accountRepository->getAccountCurrency($destination) ?? $defaultCurrency;
// if both accounts have the same currency, continue.
if ($sourceCurrency->code === $destinationCurrency->code) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Both accounts have the same currency, continue.');
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Source account expects %s', $sourceCurrency->code));
app('log')->debug(sprintf('Destination account expects %s', $destinationCurrency->code));
2023-06-21 05:34:58 -05:00
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Amount is %s', $transaction['amount']));
2023-06-21 05:34:58 -05:00
if (TransactionType::DEPOSIT === ucfirst($transactionType)) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Processing as a "%s"', $transactionType));
2023-06-21 05:34:58 -05:00
// use case: deposit from liability account to an asset account
// the foreign amount must be in the currency of the source
// the amount must be in the currency of the destination
// 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'));
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
// wrong currency information is present
$foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false;
$foreignCurrencyId = (int)($transaction['foreign_currency_id'] ?? 0);
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction);
2023-11-05 12:41:37 -06:00
if ($foreignCurrencyCode !== $sourceCurrency->code && $foreignCurrencyId !== $sourceCurrency->id) {
$validator->errors()->add(sprintf('transactions.%d.foreign_currency_code', $index), (string)trans('validation.require_foreign_src'));
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
}
if (TransactionType::TRANSFER === ucfirst($transactionType) || TransactionType::WITHDRAWAL === ucfirst($transactionType)) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Processing as a "%s"', $transactionType));
2023-06-21 05:34:58 -05:00
// use case: withdrawal from asset account to a liability account.
// the foreign amount must be in the currency of the destination
// the amount must be in the currency of the source
// use case: transfer between accounts with different currencies.
// the foreign amount must be in the currency of the destination
// the amount must be in the currency of the source
// 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'));
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return;
}
// wrong currency information is present
$foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false;
$foreignCurrencyId = (int)($transaction['foreign_currency_id'] ?? 0);
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction);
2023-11-05 12:41:37 -06:00
if ($foreignCurrencyCode !== $destinationCurrency->code && $foreignCurrencyId !== $destinationCurrency->id) {
2023-10-29 00:33:43 -05:00
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'));
2023-06-21 05:34:58 -05:00
}
}
}
private function isLiabilityOrAsset(Account $account): bool
{
return $this->isLiability($account) || $this->isAsset($account);
}
private function isLiability(Account $account): bool
{
2023-11-04 05:31:14 -05:00
$type = $account->accountType->type;
2023-06-21 05:34:58 -05:00
if (in_array($type, config('firefly.valid_liabilities'), true)) {
return true;
}
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
return false;
}
private function isAsset(Account $account): bool
{
2023-11-04 05:31:14 -05:00
$type = $account->accountType->type;
2023-12-20 12:35:52 -06:00
return AccountType::ASSET === $type;
2023-06-21 05:34:58 -05:00
}
private function hasForeignCurrencyInfo(array $transaction): bool
{
if (!array_key_exists('foreign_currency_code', $transaction) && !array_key_exists('foreign_currency_id', $transaction)) {
return false;
}
if (!array_key_exists('foreign_amount', $transaction)) {
return false;
}
if ('' === $transaction['foreign_amount']) {
return false;
}
if (0 === bccomp('0', (string)$transaction['foreign_amount'])) {
2023-06-21 05:34:58 -05:00
return false;
}
2023-06-24 01:27:28 -05:00
2023-12-20 12:35:52 -06:00
return true;
2023-03-11 00:09:27 -06:00
}
/**
* 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().');
}
2023-06-21 05:34:58 -05:00
private function getTransactionType(TransactionGroup $group, array $transactions): string
2020-03-15 09:31:59 -05:00
{
return $transactions[0]['type'] ?? strtolower((string)$group->transactionJournals()->first()?->transactionType->type);
2021-03-21 03:15:40 -05:00
}
2020-02-05 13:37:23 -06:00
2021-03-21 03:15:40 -05:00
private function getOriginalSource(array $transaction, TransactionGroup $transactionGroup): ?Account
{
if (1 === $transactionGroup->transactionJournals->count()) {
$journal = $transactionGroup->transactionJournals->first();
2023-11-28 23:30:35 -06:00
return $journal?->transactions()->where('amount', '<', 0)->first()?->account;
2021-03-21 03:15:40 -05:00
}
2023-12-20 12:35:52 -06:00
2021-03-21 03:15:40 -05:00
/** @var TransactionJournal $journal */
foreach ($transactionGroup->transactionJournals as $journal) {
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
2023-11-05 12:41:37 -06:00
if ($journal->id === $journalId) {
2023-11-28 23:30:35 -06:00
return $journal->transactions()->where('amount', '<', 0)->first()?->account;
2021-03-21 03:15:40 -05:00
}
2018-07-05 11:02:02 -05:00
}
2021-03-21 03:15:40 -05:00
return null;
2018-07-05 11:02:02 -05:00
}
/**
* 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()');
}
2023-06-21 05:34:58 -05:00
private function getOriginalType(int $journalId): string
{
if (0 === $journalId) {
return 'invalid';
}
2023-12-20 12:35:52 -06:00
/** @var null|TransactionJournal $journal */
2023-06-21 05:34:58 -05:00
$journal = TransactionJournal::with(['transactionType'])->find($journalId);
if (null !== $journal) {
return strtolower($journal->transactionType->type);
2023-05-29 06:56:55 -05:00
}
2023-06-21 05:34:58 -05:00
return 'invalid';
2019-06-04 13:42:11 -05:00
}
2019-04-06 04:08:46 -05:00
private function validateEqualAccounts(Validator $validator): void
{
if ($validator->errors()->count() > 0) {
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in validateEqualAccounts()');
2020-03-19 09:16:59 -05:00
$transactions = $this->getTransactionsArray($validator);
2020-02-05 13:37:23 -06:00
2019-04-06 04:08:46 -05:00
// needs to be split
if (count($transactions) < 2) {
return;
}
$type = $transactions[0]['type'] ?? 'withdrawal';
$sources = [];
$dests = [];
2019-04-06 04:08:46 -05:00
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);
2023-12-20 12:35:52 -06:00
2019-04-06 04:08:46 -05:00
switch ($type) {
2020-10-03 10:53:23 -05:00
default:
2019-04-06 04:08:46 -05:00
case 'withdrawal':
if (count($sources) > 1) {
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
2019-04-06 04:08:46 -05:00
}
2023-12-20 12:35:52 -06:00
2019-04-06 04:08:46 -05:00
break;
2023-12-20 12:35:52 -06:00
2019-04-06 04:08:46 -05:00
case 'deposit':
if (count($dests) > 1) {
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
2019-04-06 04:08:46 -05:00
}
2023-12-20 12:35:52 -06:00
2019-04-06 04:08:46 -05:00
break;
2023-12-20 12:35:52 -06:00
case 'transfer':
2019-04-06 04:08:46 -05:00
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'));
2019-04-06 04:08:46 -05:00
}
2023-12-20 12:35:52 -06:00
2019-04-06 04:08:46 -05:00
break;
}
}
2021-03-21 03:15:40 -05:00
private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void
2019-04-06 04:08:46 -05:00
{
if ($validator->errors()->count() > 0) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Validator already has errors, so return.');
2023-12-20 12:35:52 -06:00
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in validateEqualAccountsForUpdate()');
2021-03-21 03:15:40 -05:00
$transactions = $this->getTransactionsArray($validator);
if (2 !== count($transactions)) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Less than 2 transactions, do nothing.');
2021-03-21 03:15:40 -05:00
return;
}
$type = $this->getTransactionType($transactionGroup, $transactions);
2021-03-21 03:15:40 -05:00
// compare source IDs, destination IDs, source names and destination names.
2021-03-21 03:15:40 -05:00
// 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
2022-10-02 13:13:32 -05:00
// but if IDs are equal don't bother with the names.
$comparison = $this->collectComparisonData($transactions);
$result = $this->compareAccountData($type, $comparison);
2021-03-21 03:15:40 -05:00
if (false === $result) {
if ('withdrawal' === $type) {
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
2021-03-21 03:15:40 -05:00
}
if ('deposit' === $type) {
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
2021-03-21 03:15:40 -05:00
}
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'));
2021-03-21 03:15:40 -05:00
}
2022-10-30 08:44:49 -05:00
app('log')->warning('Add error about equal accounts.');
2021-03-21 03:15:40 -05:00
return;
}
2023-10-29 00:33:43 -05:00
app('log')->debug('No errors found in validateEqualAccountsForUpdate');
2020-10-18 23:41:01 -05:00
}
2023-06-21 05:34:58 -05:00
private function collectComparisonData(array $transactions): array
{
$fields = ['source_id', 'destination_id', 'source_name', 'destination_name'];
$comparison = [];
foreach ($fields as $field) {
$comparison[$field] = [];
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
/** @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));
2023-06-21 05:34:58 -05:00
// get field.
$comparison[$field][] = $transaction[$field] ?? $originalData[$field];
}
}
return $comparison;
}
private function getOriginalData(int $journalId): array
{
$return = [
2023-06-21 05:34:58 -05:00
'source_id' => 0,
'source_name' => '',
'destination_id' => 0,
'destination_name' => '',
];
if (0 === $journalId) {
return $return;
}
2023-12-20 12:35:52 -06:00
/** @var null|Transaction $source */
$source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first();
2023-06-21 05:34:58 -05:00
if (null !== $source) {
$return['source_id'] = $source->account_id;
$return['source_name'] = $source->account->name;
}
2023-12-20 12:35:52 -06:00
/** @var null|Transaction $destination */
2023-06-21 05:34:58 -05:00
$destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first();
2023-11-28 23:30:35 -06:00
if (null !== $destination) {
2023-06-21 05:34:58 -05:00
$return['destination_id'] = $destination->account_id;
$return['destination_name'] = $destination->account->name;
}
return $return;
}
private function compareAccountData(string $type, array $comparison): bool
{
return match ($type) {
default => $this->compareAccountDataWithdrawal($comparison),
'deposit' => $this->compareAccountDataDeposit($comparison),
2023-06-21 05:34:58 -05:00
'transfer' => $this->compareAccountDataTransfer($comparison),
};
}
private function compareAccountDataWithdrawal(array $comparison): bool
{
if ($this->arrayEqual($comparison['source_id'])) {
// source ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['source_name'])) {
// source names are equal, return void.
return true;
}
return false;
}
private function arrayEqual(array $array): bool
{
return 1 === count(array_unique($array));
}
private function compareAccountDataDeposit(array $comparison): bool
{
if ($this->arrayEqual($comparison['destination_id'])) {
// destination ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['destination_name'])) {
// destination names are equal, return void.
return true;
}
return false;
}
private function compareAccountDataTransfer(array $comparison): bool
{
if ($this->arrayEqual($comparison['source_id'])) {
// source ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['source_name'])) {
// source names are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['destination_id'])) {
// destination ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['destination_name'])) {
// destination names are equal, return void.
return true;
}
return false;
}
}