mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Is now capable of updating transactions over the API.
This commit is contained in:
@@ -27,6 +27,7 @@ use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -50,6 +51,8 @@ class AccountValidator
|
||||
private $combinations;
|
||||
/** @var string */
|
||||
private $transactionType;
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* AccountValidator constructor.
|
||||
@@ -69,9 +72,19 @@ class AccountValidator
|
||||
*/
|
||||
public function setTransactionType(string $transactionType): void
|
||||
{
|
||||
Log::debug(sprintf('Transaction type for validator is now %s', ucfirst($transactionType)));
|
||||
$this->transactionType = ucfirst($transactionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->accountRepository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $destinationId
|
||||
* @param $destinationName
|
||||
@@ -83,7 +96,7 @@ class AccountValidator
|
||||
|
||||
Log::debug(sprintf('Now in AccountValidator::validateDestination(%d, "%s")', $destinationId, $destinationName));
|
||||
if (null === $this->source) {
|
||||
Log::error('Source is NULL');
|
||||
Log::error('Source is NULL, always FALSE.');
|
||||
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.';
|
||||
|
||||
return false;
|
||||
@@ -121,9 +134,10 @@ class AccountValidator
|
||||
*/
|
||||
public function validateSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in AccountValidator::validateSource(%d, "%s")', $accountId, $accountName));
|
||||
switch ($this->transactionType) {
|
||||
default:
|
||||
$result = false;
|
||||
$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;
|
||||
@@ -241,7 +255,6 @@ class AccountValidator
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -249,15 +262,18 @@ class AccountValidator
|
||||
// otherwise try to find the account:
|
||||
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
|
||||
if (null === $search) {
|
||||
Log::debug('findExistingAccount() returned NULL, so the result is false.');
|
||||
$this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
$result = false;
|
||||
}
|
||||
if (null !== $search) {
|
||||
Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
|
||||
$this->destination = $search;
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
$result = $result ?? false;
|
||||
Log::debug(sprintf('validateDepositDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true)));
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -270,6 +286,7 @@ class AccountValidator
|
||||
*/
|
||||
private function validateDepositSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in validateDepositSource(%d, "%s")', $accountId, $accountName));
|
||||
$result = null;
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
@@ -281,10 +298,25 @@ class AccountValidator
|
||||
$result = false;
|
||||
}
|
||||
|
||||
// if the user submits an ID only but that ID is not of the correct type,
|
||||
// return false.
|
||||
if (null !== $accountId && null === $accountName) {
|
||||
$search = $this->accountRepository->findNull($accountId);
|
||||
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
|
||||
Log::debug(sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
|
||||
$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;
|
||||
|
||||
// set the source to be a (dummy) revenue account.
|
||||
$account = new Account;
|
||||
$accountType = AccountType::whereType(AccountType::REVENUE)->first();
|
||||
$account->accountType = $accountType;
|
||||
$this->source = $account;
|
||||
}
|
||||
$result = $result ?? false;
|
||||
|
||||
@@ -332,6 +364,7 @@ class AccountValidator
|
||||
*/
|
||||
private function validateTransferSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in validateTransferSource(%d, "%s")', $accountId, $accountName));
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
@@ -362,6 +395,7 @@ class AccountValidator
|
||||
*/
|
||||
private function validateWithdrawalDestination(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in validateWithdrawalDestination(%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)) {
|
||||
@@ -377,6 +411,7 @@ class AccountValidator
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// don't expect to end up here:
|
||||
return false;
|
||||
}
|
||||
@@ -389,6 +424,7 @@ class AccountValidator
|
||||
*/
|
||||
private function validateWithdrawalSource(?int $accountId, ?string $accountName): bool
|
||||
{
|
||||
Log::debug(sprintf('Now in validateWithdrawalSource(%d, "%s")', $accountId, $accountName));
|
||||
// source can be any of the following types.
|
||||
$validTypes = array_keys($this->combinations[$this->transactionType]);
|
||||
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
||||
|
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Validation;
|
||||
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
/**
|
||||
@@ -31,6 +32,16 @@ use Illuminate\Validation\Validator;
|
||||
*/
|
||||
trait TransactionValidation
|
||||
{
|
||||
/**
|
||||
* If type is set, source + destination info is mandatory.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*/
|
||||
protected function validateAccountPresence(Validator $validator): void
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates the given account information. Switches on given transaction type.
|
||||
@@ -98,7 +109,9 @@ trait TransactionValidation
|
||||
|
||||
// no valid descriptions?
|
||||
if (0 === $validDescriptions) {
|
||||
$validator->errors()->add('description', (string)trans('validation.filled', ['attribute' => (string)trans('validation.attributes.description')]));
|
||||
$validator->errors()->add(
|
||||
'transactions.0.description', (string)trans('validation.filled', ['attribute' => (string)trans('validation.attributes.description')])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +161,7 @@ trait TransactionValidation
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
// need at least one transaction
|
||||
if (0 === \count($transactions)) {
|
||||
$validator->errors()->add('description', (string)trans('validation.at_least_one_transaction'));
|
||||
$validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +246,47 @@ trait TransactionValidation
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All types of splits must be equal.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*/
|
||||
public function validateTransactionTypesForUpdate(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$types = [];
|
||||
foreach ($transactions as $index => $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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
* @param TransactionGroup $transactionGroup
|
||||
*/
|
||||
private function validateJournalIds(Validator $validator, TransactionGroup $transactionGroup): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
if (count($transactions) < 2) {
|
||||
return;
|
||||
}
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
|
||||
$count = $transactionGroup->transactionJournals()->where('id', $journalId)->count();
|
||||
if (0 === $journalId || 0 === $count) {
|
||||
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), (string)trans('validation.need_id_in_edit'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Throws an error when this asset account is invalid.
|
||||
// *
|
||||
|
Reference in New Issue
Block a user