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
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-05 11:02:02 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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
|
|
|
*
|
2019-10-01 23:37:26 -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
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-05 11:02:02 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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;
|
|
|
|
|
2021-03-09 23:34:03 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2019-06-02 09:33:25 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2019-04-06 01:10:50 -05:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
2019-06-04 13:42:11 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-07-05 11:02:02 -05:00
|
|
|
use Illuminate\Validation\Validator;
|
2019-06-07 23:19:21 -05:00
|
|
|
use Log;
|
2018-07-05 11:02:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait TransactionValidation
|
|
|
|
*/
|
|
|
|
trait TransactionValidation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validates the given account information. Switches on given transaction type.
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
|
|
|
public function validateAccountInformation(Validator $validator): void
|
|
|
|
{
|
2021-12-18 05:35:17 -06:00
|
|
|
Log::debug('Now in validateAccountInformation (TransactionValidation) ()');
|
2020-02-06 14:54:47 -06:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
|
|
|
$data = $validator->getData();
|
2019-06-09 02:58:55 -05:00
|
|
|
|
2019-06-07 23:19:21 -05:00
|
|
|
$transactionType = $data['type'] ?? 'invalid';
|
2020-02-05 13:37:23 -06:00
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
|
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @var array $transaction
|
|
|
|
*/
|
|
|
|
foreach ($transactions as $index => $transaction) {
|
2021-03-09 23:34:03 -06:00
|
|
|
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
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getTransactionsArray(Validator $validator): array
|
|
|
|
{
|
|
|
|
$data = $validator->getData();
|
|
|
|
$transactions = $data['transactions'] ?? [];
|
|
|
|
if (!is_countable($transactions)) {
|
|
|
|
Log::error(sprintf('Transactions array is not countable, because its a %s', gettype($transactions)));
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// a superfluous check but you never know.
|
|
|
|
if (!is_array($transactions)) {
|
|
|
|
Log::error(sprintf('Transactions array is not an array, because its a %s', gettype($transactions)));
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transactions;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
* @param int $index
|
|
|
|
* @param string $transactionType
|
|
|
|
* @param array $transaction
|
|
|
|
*/
|
|
|
|
protected function validateSingleAccount(Validator $validator, int $index, string $transactionType, array $transaction): void
|
|
|
|
{
|
2019-03-31 06:36:49 -05:00
|
|
|
/** @var AccountValidator $accountValidator */
|
|
|
|
$accountValidator = app(AccountValidator::class);
|
2018-07-05 11:02:02 -05:00
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
$transactionType = $transaction['type'] ?? $transactionType;
|
|
|
|
$accountValidator->setTransactionType($transactionType);
|
2019-03-31 06:36:49 -05:00
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
// validate source account.
|
2021-12-18 05:35:17 -06:00
|
|
|
$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;
|
|
|
|
$array = [
|
|
|
|
'id' => $sourceId,
|
|
|
|
'name' => $sourceName,
|
|
|
|
'iban' => $sourceIban,
|
|
|
|
'number' => $sourceNumber,
|
|
|
|
];
|
|
|
|
$validSource = $accountValidator->validateSource($array);
|
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
|
2021-12-18 05:35:17 -06:00
|
|
|
$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;
|
|
|
|
$array = [
|
|
|
|
'id' => $destinationId,
|
|
|
|
'name' => $destinationName,
|
|
|
|
'iban' => $destinationIban,
|
|
|
|
'number' => $destinationNumber,
|
|
|
|
];
|
|
|
|
$validDestination = $accountValidator->validateDestination($array);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:09:55 -05:00
|
|
|
/**
|
|
|
|
* Validates the given account information. Switches on given transaction type.
|
|
|
|
*
|
2021-03-09 23:34:03 -06:00
|
|
|
* @param Validator $validator
|
|
|
|
* @param TransactionGroup $transactionGroup
|
2019-08-26 12:09:55 -05:00
|
|
|
*/
|
2021-03-09 23:34:03 -06:00
|
|
|
public function validateAccountInformationUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
2019-08-26 12:09:55 -05:00
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Now in validateAccountInformationUpdate()');
|
2020-02-06 14:54:47 -06:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
2020-02-05 13:37:23 -06:00
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @var array $transaction
|
|
|
|
*/
|
|
|
|
foreach ($transactions as $index => $transaction) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$this->validateSingleUpdate($validator, $index, $transaction, $transactionGroup);
|
2020-03-15 09:31:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-09 23:34:03 -06:00
|
|
|
* @param Validator $validator
|
|
|
|
* @param int $index
|
|
|
|
* @param array $transaction
|
|
|
|
* @param TransactionGroup $transactionGroup
|
2020-03-15 09:31:59 -05:00
|
|
|
*/
|
2021-03-09 23:34:03 -06:00
|
|
|
protected function validateSingleUpdate(Validator $validator, int $index, array $transaction, TransactionGroup $transactionGroup): void
|
2020-03-15 09:31:59 -05:00
|
|
|
{
|
2021-03-09 23:34:03 -06:00
|
|
|
Log::debug('Now validating single account update in validateSingleUpdate()');
|
2020-03-15 09:31:59 -05:00
|
|
|
|
|
|
|
// if no account types are given, just skip the check.
|
2021-04-06 01:51:27 -05:00
|
|
|
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)) {
|
2021-03-09 23:34:03 -06:00
|
|
|
Log::debug('No account data has been submitted so will not validating account info.');
|
|
|
|
|
2020-03-15 09:31:59 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
// create validator:
|
|
|
|
/** @var AccountValidator $accountValidator */
|
|
|
|
$accountValidator = app(AccountValidator::class);
|
2019-08-26 12:09:55 -05:00
|
|
|
|
2021-03-09 23:34:03 -06:00
|
|
|
// get the transaction type using the original transaction group:
|
|
|
|
$accountValidator->setTransactionType($this->getTransactionType($transactionGroup, []));
|
2019-08-26 12:09:55 -05:00
|
|
|
|
2021-03-09 23:34:03 -06:00
|
|
|
// validate if the submitted source and / or name are valid
|
|
|
|
if (array_key_exists('source_id', $transaction) || array_key_exists('source_name', $transaction)) {
|
|
|
|
Log::debug('Will try to validate source account information.');
|
|
|
|
$sourceId = (int)($transaction['source_id'] ?? 0);
|
|
|
|
$sourceName = $transaction['source_name'] ?? null;
|
2021-12-19 01:47:02 -06:00
|
|
|
$validSource = $accountValidator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
|
2019-08-26 12:09:55 -05:00
|
|
|
|
2021-03-09 23:34:03 -06:00
|
|
|
// do something with result:
|
|
|
|
if (false === $validSource) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::debug('Source account info is valid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('destination_id', $transaction) || array_key_exists('destination_name', $transaction)) {
|
|
|
|
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) {
|
|
|
|
Log::debug('Account validator has no source account, must find it.');
|
|
|
|
$source = $this->getOriginalSource($transaction, $transactionGroup);
|
|
|
|
if (null !== $source) {
|
|
|
|
Log::debug('Found a source!');
|
|
|
|
$accountValidator->source = $source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$destinationId = (int)($transaction['destination_id'] ?? 0);
|
|
|
|
$destinationName = $transaction['destination_name'] ?? null;
|
2021-12-18 05:35:17 -06:00
|
|
|
$array = ['id' => $destinationId, 'name' => $destinationName,];
|
|
|
|
$validDestination = $accountValidator->validateDestination($array);
|
2021-03-09 23:34:03 -06:00
|
|
|
// do something with result:
|
|
|
|
if (false === $validDestination) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Log::debug('Destination account info is valid.');
|
2019-08-26 12:09:55 -05:00
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
Log::debug('Done with validateSingleUpdate().');
|
2019-08-26 12:09:55 -05:00
|
|
|
}
|
|
|
|
|
2018-07-05 11:02:02 -05:00
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param TransactionGroup $group
|
|
|
|
* @param array $transactions
|
2018-07-05 11:02:02 -05:00
|
|
|
*
|
2021-03-21 03:15:40 -05:00
|
|
|
* @return string
|
2018-07-05 11:02:02 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function getTransactionType(TransactionGroup $group, array $transactions): string
|
2018-07-05 11:02:02 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
return $transactions[0]['type'] ?? strtolower($group->transactionJournals()->first()->transactionType->type);
|
|
|
|
}
|
2020-02-05 13:37:23 -06:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
|
|
|
* @param array $transaction
|
|
|
|
* @param TransactionGroup $transactionGroup
|
|
|
|
*
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
private function getOriginalSource(array $transaction, TransactionGroup $transactionGroup): ?Account
|
|
|
|
{
|
|
|
|
if (1 === $transactionGroup->transactionJournals->count()) {
|
|
|
|
$journal = $transactionGroup->transactionJournals->first();
|
|
|
|
|
|
|
|
return $journal->transactions()->where('amount', '<', 0)->first()->account;
|
|
|
|
}
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($transactionGroup->transactionJournals as $journal) {
|
|
|
|
if ((int)$journal->id === (int)$transaction['transaction_journal_id']) {
|
|
|
|
return $journal->transactions()->where('amount', '<', 0)->first()->account;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-10-24 09:49:05 -05:00
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* Adds an error to the validator when there are no transactions in the array of data.
|
|
|
|
*
|
2020-10-24 09:49:05 -05:00
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
public function validateOneRecurrenceTransaction(Validator $validator): void
|
2021-03-09 23:34:03 -06:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
Log::debug('Now in validateOneRecurrenceTransaction()');
|
2020-10-24 09:49:05 -05:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
2021-03-09 23:34:03 -06:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
// need at least one transaction
|
2021-06-12 12:32:34 -05:00
|
|
|
if (empty($transactions)) {
|
2021-03-21 03:15:40 -05:00
|
|
|
$validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction'));
|
2020-10-24 09:49:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:09:55 -05:00
|
|
|
/**
|
|
|
|
* Adds an error to the validator when there are no transactions in the array of data.
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
|
|
|
public function validateOneTransaction(Validator $validator): void
|
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Now in validateOneTransaction()');
|
2020-02-06 14:54:47 -06:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
2019-08-26 12:09:55 -05:00
|
|
|
// need at least one transaction
|
2021-06-12 12:32:34 -05:00
|
|
|
if (empty($transactions)) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Added error: at_least_one_transaction.');
|
2021-03-09 23:34:03 -06:00
|
|
|
|
2020-02-22 04:05:16 -06:00
|
|
|
return;
|
2019-08-26 12:09:55 -05:00
|
|
|
}
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Added NO errors.');
|
2019-08-26 12:09:55 -05:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
|
|
|
public function validateTransactionArray(Validator $validator): void
|
|
|
|
{
|
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
|
|
|
foreach ($transactions as $key => $value) {
|
|
|
|
if (!is_int($key)) {
|
|
|
|
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
|
|
|
|
Log::debug('Added error: at_least_one_transaction.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 11:02:02 -05:00
|
|
|
/**
|
2019-03-31 06:36:49 -05:00
|
|
|
* All types of splits must be equal.
|
2018-07-05 11:02:02 -05:00
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
2019-03-31 06:36:49 -05:00
|
|
|
public function validateTransactionTypes(Validator $validator): void
|
2018-07-05 11:02:02 -05:00
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Now in validateTransactionTypes()');
|
2020-02-06 14:54:47 -06:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
2020-02-05 13:37:23 -06:00
|
|
|
|
|
|
|
$types = [];
|
2020-03-15 09:31:59 -05:00
|
|
|
foreach ($transactions as $transaction) {
|
2019-03-31 06:36:49 -05:00
|
|
|
$types[] = $transaction['type'] ?? 'invalid';
|
2018-07-05 11:02:02 -05:00
|
|
|
}
|
2019-03-31 06:36:49 -05:00
|
|
|
$unique = array_unique($types);
|
|
|
|
if (count($unique) > 1) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal'));
|
2018-07-05 11:02:02 -05:00
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
return;
|
2018-07-05 11:02:02 -05:00
|
|
|
}
|
2019-03-31 06:36:49 -05:00
|
|
|
$first = $unique[0] ?? 'invalid';
|
|
|
|
if ('invalid' === $first) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$validator->errors()->add('transactions.0.type', (string)trans('validation.invalid_transaction_type'));
|
2018-07-05 11:02:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 01:10:50 -05:00
|
|
|
/**
|
|
|
|
* All types of splits must be equal.
|
|
|
|
*
|
2021-09-18 03:26:12 -05:00
|
|
|
* @param Validator $validator
|
2019-04-06 01:10:50 -05:00
|
|
|
*/
|
2021-05-24 01:06:56 -05:00
|
|
|
public function validateTransactionTypesForUpdate(Validator $validator): void
|
2019-04-06 01:10:50 -05:00
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Now in validateTransactionTypesForUpdate()');
|
2020-02-06 14:54:47 -06:00
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
|
|
|
$types = [];
|
2020-03-15 09:31:59 -05:00
|
|
|
foreach ($transactions as $transaction) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$originalType = $this->getOriginalType((int)($transaction['transaction_journal_id'] ?? 0));
|
2019-06-04 13:42:11 -05:00
|
|
|
// if type is not set, fall back to the type of the journal, if one is given.
|
|
|
|
$types[] = $transaction['type'] ?? $originalType;
|
2019-04-06 01:10:50 -05:00
|
|
|
}
|
|
|
|
$unique = array_unique($types);
|
|
|
|
if (count($unique) > 1) {
|
2021-03-09 23:34:03 -06:00
|
|
|
Log::warning('Add error for mismatch transaction types.');
|
|
|
|
$validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal'));
|
|
|
|
|
|
|
|
return;
|
2019-04-06 01:10:50 -05:00
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
Log::debug('No errors in validateTransactionTypesForUpdate()');
|
2019-04-06 01:10:50 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 12:09:55 -05:00
|
|
|
/**
|
|
|
|
* @param int $journalId
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getOriginalType(int $journalId): string
|
|
|
|
{
|
|
|
|
if (0 === $journalId) {
|
|
|
|
return 'invalid';
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
2021-04-06 01:51:27 -05:00
|
|
|
/** @var TransactionJournal|null $journal */
|
2019-08-26 12:09:55 -05:00
|
|
|
$journal = TransactionJournal::with(['transactionType'])->find($journalId);
|
|
|
|
if (null !== $journal) {
|
|
|
|
return strtolower($journal->transactionType->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'invalid';
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
|
|
|
|
2019-04-06 04:08:46 -05:00
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
|
|
|
private function validateEqualAccounts(Validator $validator): void
|
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
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 = [];
|
|
|
|
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);
|
|
|
|
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) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
2019-04-06 04:08:46 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'deposit':
|
|
|
|
if (count($dests) > 1) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
2019-04-06 04:08:46 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case'transfer':
|
|
|
|
if (count($sources) > 1 || count($dests) > 1) {
|
2021-03-09 23:34:03 -06:00
|
|
|
$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
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param Validator $validator
|
|
|
|
* @param TransactionGroup $transactionGroup
|
2019-04-06 04:08:46 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
2019-04-06 04:08:46 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
Log::debug('Now in validateEqualAccountsForUpdate()');
|
|
|
|
$transactions = $this->getTransactionsArray($validator);
|
|
|
|
|
|
|
|
if (2 !== count($transactions)) {
|
|
|
|
Log::debug('Less than 2 transactions, do nothing.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$type = $this->getTransactionType($transactionGroup, $transactions);
|
|
|
|
|
|
|
|
// compare source ID's, destination ID's, 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 ID's are equal don't bother with the names.
|
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
if ('deposit' === $type) {
|
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
Log::warning('Add error about equal accounts.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::debug('No errors found in validateEqualAccountsForUpdate');
|
2020-10-18 23:41:01 -05:00
|
|
|
}
|
2019-06-02 09:33:25 -05:00
|
|
|
|
2020-10-18 23:41:01 -05:00
|
|
|
/**
|
|
|
|
* @param array $transactions
|
2021-03-09 23:34:03 -06:00
|
|
|
*
|
2020-10-18 23:41:01 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function collectComparisonData(array $transactions): array
|
|
|
|
{
|
2019-06-02 09:33:25 -05:00
|
|
|
$fields = ['source_id', 'destination_id', 'source_name', 'destination_name'];
|
|
|
|
$comparison = [];
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$comparison[$field] = [];
|
|
|
|
/** @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.
|
2021-03-09 23:34:03 -06:00
|
|
|
$originalData = $this->getOriginalData((int)($transaction['transaction_journal_id'] ?? 0));
|
2019-06-02 09:33:25 -05:00
|
|
|
|
|
|
|
// get field.
|
|
|
|
$comparison[$field][] = $transaction[$field] ?? $originalData[$field];
|
|
|
|
}
|
2019-04-06 04:08:46 -05:00
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
|
2020-10-18 23:41:01 -05:00
|
|
|
return $comparison;
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
|
|
|
* @param int $journalId
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getOriginalData(int $journalId): array
|
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'source_id' => 0,
|
|
|
|
'source_name' => '',
|
|
|
|
'destination_id' => 0,
|
|
|
|
'destination_name' => '',
|
|
|
|
];
|
|
|
|
if (0 === $journalId) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
/** @var Transaction $source */
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
/** @var Transaction $destination */
|
|
|
|
$destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first();
|
|
|
|
if (null !== $source) {
|
|
|
|
$return['destination_id'] = $destination->account_id;
|
|
|
|
$return['destination_name'] = $destination->account->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2020-10-18 23:41:01 -05:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param array $comparison
|
2021-03-09 23:34:03 -06:00
|
|
|
*
|
2020-10-18 23:41:01 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function compareAccountData(string $type, array $comparison): bool
|
|
|
|
{
|
2021-09-18 03:21:29 -05:00
|
|
|
return match ($type) {
|
|
|
|
default => $this->compareAccountDataWithdrawal($comparison),
|
|
|
|
'deposit' => $this->compareAccountDataDeposit($comparison),
|
|
|
|
'transfer' => $this->compareAccountDataTransfer($comparison),
|
|
|
|
};
|
2020-10-18 23:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $comparison
|
2021-03-09 23:34:03 -06:00
|
|
|
*
|
2020-10-18 23:41:01 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function compareAccountDataWithdrawal(array $comparison): bool
|
2020-10-18 23:41:01 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
|
2020-10-18 23:41:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param array $array
|
2021-03-09 23:34:03 -06:00
|
|
|
*
|
2020-10-18 23:41:01 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function arrayEqual(array $array): bool
|
2020-10-18 23:41:01 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
return 1 === count(array_unique($array));
|
2020-10-18 23:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $comparison
|
2021-03-09 23:34:03 -06:00
|
|
|
*
|
2020-10-18 23:41:01 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2021-03-09 23:34:03 -06:00
|
|
|
|
2020-10-18 23:41:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param array $comparison
|
|
|
|
*
|
|
|
|
* @return bool
|
2020-10-18 23:41:01 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
private function compareAccountDataTransfer(array $comparison): bool
|
2020-10-18 23:41:01 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
if ($this->arrayEqual($comparison['source_id'])) {
|
|
|
|
// source ID's are equal, return void.
|
|
|
|
return true;
|
2020-10-18 23:41:01 -05:00
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
if ($this->arrayEqual($comparison['source_name'])) {
|
|
|
|
// source names are equal, return void.
|
|
|
|
return true;
|
2019-04-06 04:08:46 -05:00
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
if ($this->arrayEqual($comparison['destination_id'])) {
|
|
|
|
// destination ID's are equal, return void.
|
|
|
|
return true;
|
2021-03-09 23:34:03 -06:00
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
if ($this->arrayEqual($comparison['destination_name'])) {
|
|
|
|
// destination names are equal, return void.
|
|
|
|
return true;
|
2021-03-09 23:34:03 -06:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return false;
|
2019-04-06 04:08:46 -05:00
|
|
|
}
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|