mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 04:03:26 +00:00
Simplify method.
This commit is contained in:
@@ -375,26 +375,21 @@ trait TransactionValidation
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
* @param TransactionGroup $transactionGroup
|
||||
* @param TransactionGroup $group
|
||||
* @param array $transactions
|
||||
* @return string
|
||||
*/
|
||||
private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
||||
private function getTransactionType(TransactionGroup $group, array $transactions): string
|
||||
{
|
||||
Log::debug('Now in validateEqualAccountsForUpdate()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
// needs to be split
|
||||
if (count($transactions) < 2) {
|
||||
return;
|
||||
return $transactions[0]['type'] ?? strtolower($group->transactionJournals()->first()->transactionType->type);
|
||||
}
|
||||
$type = $transactions[0]['type'] ?? strtolower($transactionGroup->transactionJournals()->first()->transactionType->type);
|
||||
|
||||
// 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.
|
||||
|
||||
/**
|
||||
* @param array $transactions
|
||||
* @return array
|
||||
*/
|
||||
private function collectComparisonData(array $transactions): array
|
||||
{
|
||||
$fields = ['source_id', 'destination_id', 'source_name', 'destination_name'];
|
||||
$comparison = [];
|
||||
foreach ($fields as $field) {
|
||||
@@ -408,54 +403,118 @@ trait TransactionValidation
|
||||
$comparison[$field][] = $transaction[$field] ?? $originalData[$field];
|
||||
}
|
||||
}
|
||||
// TODO not the best way to loop this.
|
||||
return $comparison;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $comparison
|
||||
* @return bool
|
||||
*/
|
||||
private function compareAccountData(string $type, array $comparison): bool
|
||||
{
|
||||
switch ($type) {
|
||||
default:
|
||||
case 'withdrawal':
|
||||
if ($this->arrayEqual($comparison['source_id'])) {
|
||||
// source ID's are equal, return void.
|
||||
return;
|
||||
}
|
||||
if ($this->arrayEqual($comparison['source_name'])) {
|
||||
// source names are equal, return void.
|
||||
return;
|
||||
}
|
||||
// add error:
|
||||
$validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal'));
|
||||
break;
|
||||
return $this->compareAccountDataWithdrawal($comparison);
|
||||
case 'deposit':
|
||||
if ($this->arrayEqual($comparison['destination_id'])) {
|
||||
// destination ID's are equal, return void.
|
||||
return;
|
||||
}
|
||||
if ($this->arrayEqual($comparison['destination_name'])) {
|
||||
// destination names are equal, return void.
|
||||
return;
|
||||
}
|
||||
// add error:
|
||||
$validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal'));
|
||||
break;
|
||||
return $this->compareAccountDataDeposit($comparison);
|
||||
case 'transfer':
|
||||
return $this->compareAccountDataTransfer($comparison);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $comparison
|
||||
* @return bool
|
||||
*/
|
||||
private function compareAccountDataTransfer(array $comparison): bool
|
||||
{
|
||||
if ($this->arrayEqual($comparison['source_id'])) {
|
||||
// source ID's are equal, return void.
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
if ($this->arrayEqual($comparison['source_name'])) {
|
||||
// source names are equal, return void.
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
if ($this->arrayEqual($comparison['destination_id'])) {
|
||||
// destination ID's are equal, return void.
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
if ($this->arrayEqual($comparison['destination_name'])) {
|
||||
// destination names are equal, return void.
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $comparison
|
||||
* @return bool
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $comparison
|
||||
* @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;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
* @param TransactionGroup $transactionGroup
|
||||
*/
|
||||
private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
Log::debug('Now in validateEqualAccountsForUpdate()');
|
||||
$transactions = $this->getTransactionsArray($validator);
|
||||
|
||||
if (2 !== count($transactions)) {
|
||||
return;
|
||||
}
|
||||
// add error:
|
||||
$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'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user