mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
User can submit new journal through API.
This commit is contained in:
415
app/Validation/AccountValidator.php
Normal file
415
app/Validation/AccountValidator.php
Normal file
@@ -0,0 +1,415 @@
|
||||
<?php
|
||||
/**
|
||||
* AccountValidator.php
|
||||
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Validation;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class AccountValidator
|
||||
*/
|
||||
class AccountValidator
|
||||
{
|
||||
/** @var bool */
|
||||
public $createMode;
|
||||
/** @var string */
|
||||
public $destError;
|
||||
/** @var Account */
|
||||
public $destination;
|
||||
/** @var Account */
|
||||
public $source;
|
||||
/** @var string */
|
||||
public $sourceError;
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepository;
|
||||
/** @var array */
|
||||
private $combinations;
|
||||
/** @var string */
|
||||
private $transactionType;
|
||||
|
||||
/**
|
||||
* AccountValidator constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->createMode = false;
|
||||
$this->destError = 'No error yet.';
|
||||
$this->sourceError = 'No error yet.';
|
||||
$this->combinations = config('firefly.source_dests');
|
||||
/** @var AccountRepositoryInterface accountRepository */
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $transactionType
|
||||
*/
|
||||
public function setTransactionType(string $transactionType): void
|
||||
{
|
||||
$this->transactionType = ucfirst($transactionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $destinationId
|
||||
* @param $destinationName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDestination(?int $destinationId, $destinationName): bool
|
||||
{
|
||||
|
||||
Log::debug(sprintf('Now in AccountValidator::validateDestination(%d, "%s")', $destinationId, $destinationName));
|
||||
if (null === $this->source) {
|
||||
Log::error('Source is NULL');
|
||||
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.';
|
||||
|
||||
return false;
|
||||
}
|
||||
switch ($this->transactionType) {
|
||||
default:
|
||||
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType);
|
||||
Log::error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
|
||||
|
||||
$result = false;
|
||||
break;
|
||||
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$result = $this->validateWithdrawalDestination($destinationId, $destinationName);
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$result = $this->validateDepositDestination($destinationId, $destinationName);
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
$result = $this->validateTransferDestination($destinationId, $destinationName);
|
||||
break;
|
||||
//case TransactionType::OPENING_BALANCE:
|
||||
//case TransactionType::RECONCILIATION:
|
||||
// die(sprintf('Cannot handle type "%s"', $this->transactionType));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
switch ($this->transactionType) {
|
||||
default:
|
||||
$result = false;
|
||||
$this->sourceError = sprintf('Cannot handle type "%s"', $this->transactionType);
|
||||
Log::error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will always return false.', $this->transactionType));
|
||||
break;
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$result = $this->validateWithdrawalSource($accountId, $accountName);
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$result = $this->validateDepositSource($accountId, $accountName);
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
$result = $this->validateTransferSource($accountId, $accountName);
|
||||
break;
|
||||
//case TransactionType::OPENING_BALANCE:
|
||||
//case TransactionType::RECONCILIATION:
|
||||
// die(sprintf('Cannot handle type "%s"', $this->transactionType));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function canCreateType(string $accountType): bool
|
||||
{
|
||||
$result = false;
|
||||
switch ($accountType) {
|
||||
default:
|
||||
Log::error(sprintf('AccountValidator::validateSource cannot handle "%s".', $this->transactionType));
|
||||
break;
|
||||
case AccountType::ASSET:
|
||||
case AccountType::LOAN:
|
||||
case AccountType::MORTGAGE:
|
||||
case AccountType::DEBT:
|
||||
$result = false;
|
||||
break;
|
||||
case AccountType::EXPENSE:
|
||||
case AccountType::REVENUE:
|
||||
$result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountTypes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function canCreateTypes(array $accountTypes): bool
|
||||
{
|
||||
/** @var string $accountType */
|
||||
foreach ($accountTypes as $accountType) {
|
||||
if ($this->canCreateType($accountType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $validTypes
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account
|
||||
{
|
||||
$result = null;
|
||||
|
||||
// find by ID
|
||||
if ($accountId > 0) {
|
||||
$first = $this->accountRepository->findNull($accountId);
|
||||
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
|
||||
$result = $first;
|
||||
}
|
||||
}
|
||||
|
||||
// find by name:
|
||||
if (null === $result && '' !== $accountName) {
|
||||
$second = $this->accountRepository->findByName($accountName, $validTypes);
|
||||
if (null !== $second) {
|
||||
$result = $second;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateDepositDestination(?int $accountId, $accountName): bool
|
||||
{
|
||||
$result = null;
|
||||
Log::debug(sprintf('Now in validateDepositDestination(%d, "%s")', $accountId, $accountName));
|
||||
|
||||
// source can be any of the following types.
|
||||
$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.deposit_dest_need_data');
|
||||
Log::error('Both values are NULL, cant create deposit destination.');
|
||||
$result = false;
|
||||
}
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
Log::debug('Can create some of these types, so return true.');
|
||||
$this->createDestinationAccount($accountName);
|
||||
$result = true;
|
||||
}
|
||||
|
||||
if (null === $result) {
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
|
||||
if (null === $search) {
|
||||
$this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
$result = false;
|
||||
}
|
||||
if (null !== $search) {
|
||||
$this->destination = $search;
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
$result = $result ?? false;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateDepositSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
$result = null;
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL return false,
|
||||
// because the source of a deposit can't be created.
|
||||
// (this never happens).
|
||||
$this->sourceError = (string)trans('validation.deposit_source_need_data');
|
||||
$result = false;
|
||||
}
|
||||
|
||||
// if the account can be created anyway we don't need to search.
|
||||
if (null === $result && true === $this->canCreateTypes($validTypes)) {
|
||||
// set the source to be a (dummy) revenue account.
|
||||
$result = true;
|
||||
}
|
||||
$result = $result ?? false;
|
||||
|
||||
// don't expect to end up here:
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateTransferDestination(?int $accountId, $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in validateTransferDestination(%d, "%s")', $accountId, $accountName));
|
||||
// source can be any of the following types.
|
||||
$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 transfer can't be created.
|
||||
$this->destError = (string)trans('validation.transfer_dest_need_data');
|
||||
Log::error('Both values are NULL, cant create transfer destination.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
|
||||
if (null === $search) {
|
||||
$this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->destination = $search;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateTransferSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the source of a withdrawal can't be created.
|
||||
$this->sourceError = (string)trans('validation.transfer_source_need_data');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->source = $search;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateWithdrawalDestination(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
// source can be any of the following types.
|
||||
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL return false,
|
||||
// because the destination of a withdrawal can never be created automatically.
|
||||
$this->destError = (string)trans('validation.withdrawal_dest_need_data');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// if the account can be created anyway don't need to search.
|
||||
if (true === $this->canCreateTypes($validTypes)) {
|
||||
|
||||
return true;
|
||||
}
|
||||
// don't expect to end up here:
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateWithdrawalSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
// if both values are NULL we return false,
|
||||
// because the source of a withdrawal can't be created.
|
||||
$this->sourceError = (string)trans('validation.withdrawal_source_need_data');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
|
||||
if (null === $search) {
|
||||
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->source = $search;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -23,82 +23,59 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Validation;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Trait TransactionValidation
|
||||
*/
|
||||
trait TransactionValidation
|
||||
{
|
||||
|
||||
/**
|
||||
* Validates the given account information. Switches on given transaction type.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function validateAccountInformation(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$idField = 'description';
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
// get transaction type:
|
||||
if (!isset($data['type'])) {
|
||||
// the journal may exist in the request:
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $this->route()->parameter('transaction');
|
||||
if (null !== $transaction) {
|
||||
$transactionType = strtolower($transaction->transactionJournal->transactionType->type);
|
||||
}
|
||||
}
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = $transaction['destination_name'] ?? null;
|
||||
$sourceAccount = null;
|
||||
$destinationAccount = null;
|
||||
switch ($transactionType) {
|
||||
case 'withdrawal':
|
||||
$idField = 'transactions.' . $index . '.source_id';
|
||||
$nameField = 'transactions.' . $index . '.source_name';
|
||||
$sourceAccount = $this->assetAccountExists($validator, $sourceId, $sourceName, $idField, $nameField);
|
||||
$idField = 'transactions.' . $index . '.destination_id';
|
||||
$destinationAccount = $this->opposingAccountExists($validator, AccountType::EXPENSE, $destinationId, $destinationName, $idField);
|
||||
break;
|
||||
case 'deposit':
|
||||
$idField = 'transactions.' . $index . '.source_id';
|
||||
$sourceAccount = $this->opposingAccountExists($validator, AccountType::REVENUE, $sourceId, $sourceName, $idField);
|
||||
$transactionType = $transaction['type'] ?? 'invalid';
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
$idField = 'transactions.' . $index . '.destination_id';
|
||||
$nameField = 'transactions.' . $index . '.destination_name';
|
||||
$destinationAccount = $this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
|
||||
break;
|
||||
case 'transfer':
|
||||
$idField = 'transactions.' . $index . '.source_id';
|
||||
$nameField = 'transactions.' . $index . '.source_name';
|
||||
$sourceAccount = $this->assetAccountExists($validator, $sourceId, $sourceName, $idField, $nameField);
|
||||
// validate source account.
|
||||
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$validSource = $accountValidator->validateSource($sourceId, $sourceName);
|
||||
|
||||
$idField = 'transactions.' . $index . '.destination_id';
|
||||
$nameField = 'transactions.' . $index . '.destination_name';
|
||||
$destinationAccount = $this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
|
||||
break;
|
||||
default:
|
||||
$validator->errors()->add($idField, (string)trans('validation.invalid_account_info'));
|
||||
|
||||
return;
|
||||
// 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);
|
||||
|
||||
return;
|
||||
}
|
||||
// add some errors in case of same account submitted:
|
||||
if (null !== $sourceAccount && null !== $destinationAccount && $sourceAccount->id === $destinationAccount->id) {
|
||||
$validator->errors()->add($idField, (string)trans('validation.source_equals_destination'));
|
||||
// validate destination account
|
||||
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = $transaction['destination_name'] ?? null;
|
||||
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName);
|
||||
// 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);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,18 +87,17 @@ trait TransactionValidation
|
||||
*/
|
||||
public function validateDescriptions(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$journalDescription = (string)($data['description'] ?? null);
|
||||
$validDescriptions = 0;
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$validDescriptions = 0;
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
if ('' !== (string)($transaction['description'] ?? null)) {
|
||||
$validDescriptions++;
|
||||
}
|
||||
}
|
||||
|
||||
// no valid descriptions and empty journal description? error.
|
||||
if (0 === $validDescriptions && '' === $journalDescription) {
|
||||
// no valid descriptions?
|
||||
if (0 === $validDescriptions) {
|
||||
$validator->errors()->add('description', (string)trans('validation.filled', ['attribute' => (string)trans('validation.attributes.description')]));
|
||||
}
|
||||
}
|
||||
@@ -149,21 +125,15 @@ trait TransactionValidation
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when any transaction descriptions are equal to the journal description.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*/
|
||||
public function validateJournalDescription(Validator $validator): void
|
||||
public function validateGroupDescription(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$journalDescription = (string)($data['description'] ?? null);
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$description = (string)($transaction['description'] ?? null);
|
||||
// description cannot be equal to journal description.
|
||||
if ($description === $journalDescription) {
|
||||
$validator->errors()->add('transactions.' . $index . '.description', (string)trans('validation.equal_description'));
|
||||
}
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$groupTitle = $data['group_title'] ?? '';
|
||||
if ('' === $groupTitle && \count($transactions) > 1) {
|
||||
$validator->errors()->add('group_title', (string)trans('validation.group_title_mandatory'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,126 +209,129 @@ trait TransactionValidation
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when the user submits a split transaction (more than 1 transactions)
|
||||
* but does not give them a description.
|
||||
* All types of splits must be equal.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*/
|
||||
public function validateSplitDescriptions(Validator $validator): void
|
||||
public function validateTransactionTypes(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$types = [];
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$description = (string)($transaction['description'] ?? null);
|
||||
// filled description is mandatory for split transactions.
|
||||
if ('' === $description && \count($transactions) > 1) {
|
||||
$validator->errors()->add(
|
||||
'transactions.' . $index . '.description',
|
||||
(string)trans('validation.filled', ['attribute' => (string)trans('validation.attributes.transaction_description')])
|
||||
);
|
||||
}
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an error when this asset account is invalid.
|
||||
*
|
||||
* @noinspection MoreThanThreeArgumentsInspection
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @param int|null $accountId
|
||||
* @param null|string $accountName
|
||||
* @param string $idField
|
||||
* @param string $nameField
|
||||
*
|
||||
* @return null|Account
|
||||
*/
|
||||
protected function assetAccountExists(Validator $validator, ?int $accountId, ?string $accountName, string $idField, string $nameField): ?Account
|
||||
{
|
||||
/** @var User $admin */
|
||||
$admin = auth()->user();
|
||||
$accountId = (int)$accountId;
|
||||
$accountName = (string)$accountName;
|
||||
// both empty? hard exit.
|
||||
if ($accountId < 1 && '' === $accountName) {
|
||||
$validator->errors()->add($idField, (string)trans('validation.filled', ['attribute' => $idField]));
|
||||
|
||||
return null;
|
||||
}
|
||||
// ID belongs to user and is asset account:
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($admin);
|
||||
$set = $repository->getAccountsById([$accountId]);
|
||||
Log::debug(sprintf('Count of accounts found by ID %d is: %d', $accountId, $set->count()));
|
||||
if (1 === $set->count()) {
|
||||
/** @var Account $first */
|
||||
$first = $set->first();
|
||||
if ($first->accountType->type !== AccountType::ASSET) {
|
||||
$validator->errors()->add($idField, (string)trans('validation.belongs_user'));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// we ignore the account name at this point.
|
||||
return $first;
|
||||
}
|
||||
|
||||
$account = $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
if (null === $account) {
|
||||
$validator->errors()->add($nameField, (string)trans('validation.belongs_user'));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an error when the given opposing account (of type $type) is invalid.
|
||||
* Empty data is allowed, system will default to cash.
|
||||
*
|
||||
* @noinspection MoreThanThreeArgumentsInspection
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @param string $type
|
||||
* @param int|null $accountId
|
||||
* @param null|string $accountName
|
||||
* @param string $idField
|
||||
*
|
||||
* @return null|Account
|
||||
*/
|
||||
protected function opposingAccountExists(Validator $validator, string $type, ?int $accountId, ?string $accountName, string $idField): ?Account
|
||||
{
|
||||
/** @var User $admin */
|
||||
$admin = auth()->user();
|
||||
$accountId = (int)$accountId;
|
||||
$accountName = (string)$accountName;
|
||||
// both empty? done!
|
||||
if ($accountId < 1 && '' === $accountName) {
|
||||
return null;
|
||||
}
|
||||
if (0 !== $accountId) {
|
||||
// ID belongs to user and is $type account:
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($admin);
|
||||
$set = $repository->getAccountsById([$accountId]);
|
||||
if (1 === $set->count()) {
|
||||
/** @var Account $first */
|
||||
$first = $set->first();
|
||||
if ($first->accountType->type !== $type) {
|
||||
$validator->errors()->add($idField, (string)trans('validation.belongs_user'));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// we ignore the account name at this point.
|
||||
return $first;
|
||||
}
|
||||
}
|
||||
|
||||
// not having an opposing account by this name is NOT a problem.
|
||||
return null;
|
||||
}
|
||||
// /**
|
||||
// * Throws an error when this asset account is invalid.
|
||||
// *
|
||||
// * @noinspection MoreThanThreeArgumentsInspection
|
||||
// *
|
||||
// * @param Validator $validator
|
||||
// * @param int|null $accountId
|
||||
// * @param null|string $accountName
|
||||
// * @param string $idField
|
||||
// * @param string $nameField
|
||||
// *
|
||||
// * @return null|Account
|
||||
// */
|
||||
// protected function assetAccountExists(Validator $validator, ?int $accountId, ?string $accountName, string $idField, string $nameField): ?Account
|
||||
// {
|
||||
// /** @var User $admin */
|
||||
// $admin = auth()->user();
|
||||
// $accountId = (int)$accountId;
|
||||
// $accountName = (string)$accountName;
|
||||
// // both empty? hard exit.
|
||||
// if ($accountId < 1 && '' === $accountName) {
|
||||
// $validator->errors()->add($idField, (string)trans('validation.filled', ['attribute' => $idField]));
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
// // ID belongs to user and is asset account:
|
||||
// /** @var AccountRepositoryInterface $repository */
|
||||
// $repository = app(AccountRepositoryInterface::class);
|
||||
// $repository->setUser($admin);
|
||||
// $set = $repository->getAccountsById([$accountId]);
|
||||
// Log::debug(sprintf('Count of accounts found by ID %d is: %d', $accountId, $set->count()));
|
||||
// if (1 === $set->count()) {
|
||||
// /** @var Account $first */
|
||||
// $first = $set->first();
|
||||
// if ($first->accountType->type !== AccountType::ASSET) {
|
||||
// $validator->errors()->add($idField, (string)trans('validation.belongs_user'));
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// // we ignore the account name at this point.
|
||||
// return $first;
|
||||
// }
|
||||
//
|
||||
// $account = $repository->findByName($accountName, [AccountType::ASSET]);
|
||||
// if (null === $account) {
|
||||
// $validator->errors()->add($nameField, (string)trans('validation.belongs_user'));
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// return $account;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Throws an error when the given opposing account (of type $type) is invalid.
|
||||
// * Empty data is allowed, system will default to cash.
|
||||
// *
|
||||
// * @noinspection MoreThanThreeArgumentsInspection
|
||||
// *
|
||||
// * @param Validator $validator
|
||||
// * @param string $type
|
||||
// * @param int|null $accountId
|
||||
// * @param null|string $accountName
|
||||
// * @param string $idField
|
||||
// *
|
||||
// * @return null|Account
|
||||
// */
|
||||
// protected function opposingAccountExists(Validator $validator, string $type, ?int $accountId, ?string $accountName, string $idField): ?Account
|
||||
// {
|
||||
// /** @var User $admin */
|
||||
// $admin = auth()->user();
|
||||
// $accountId = (int)$accountId;
|
||||
// $accountName = (string)$accountName;
|
||||
// // both empty? done!
|
||||
// if ($accountId < 1 && '' === $accountName) {
|
||||
// return null;
|
||||
// }
|
||||
// if (0 !== $accountId) {
|
||||
// // ID belongs to user and is $type account:
|
||||
// /** @var AccountRepositoryInterface $repository */
|
||||
// $repository = app(AccountRepositoryInterface::class);
|
||||
// $repository->setUser($admin);
|
||||
// $set = $repository->getAccountsById([$accountId]);
|
||||
// if (1 === $set->count()) {
|
||||
// /** @var Account $first */
|
||||
// $first = $set->first();
|
||||
// if ($first->accountType->type !== $type) {
|
||||
// $validator->errors()->add($idField, (string)trans('validation.belongs_user'));
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// // we ignore the account name at this point.
|
||||
// return $first;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // not having an opposing account by this name is NOT a problem.
|
||||
// return null;
|
||||
// }
|
||||
}
|
||||
|
Reference in New Issue
Block a user