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