Skip to content

Firefly III: a personal finances manager

Update validation

James Colea2957b9e809b
diff --git a/app/Validation/Account/LiabilityValidation.php b/app/Validation/Account/LiabilityValidation.php
new file mode 100644
index 0000000000..bc29bee4a4
--- /dev/null
+++ b/app/Validation/Account/LiabilityValidation.php
@@ -0,0 +1,96 @@
+<?php
+/*
+ * LiabilityValidation.php
+ * Copyright (c) 2021 [email protected]
+ *
+ * This file is part of Firefly III (https://github.com/firefly-iii).
+ *
+ * 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.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+namespace FireflyIII\Validation\Account;
+
+
+use FireflyIII\Models\Account;
+use FireflyIII\Models\AccountType;
+use Log;
+
+/**
+ * Trait LiabilityValidation
+ */
+trait LiabilityValidation
+{
+
+ /**
+ * Source of an liability credit must be a liability.
+ *
+ * @param string|null $accountName
+ *
+ * @return bool
+ */
+ protected function validateLCSource(?string $accountName): bool
+ {
+ $result = true;
+ Log::debug(sprintf('Now in validateLCDestination("%s")', $accountName));
+ if ('' === $accountName || null === $accountName) {
+ $result = false;
+ }
+ if (true === $result) {
+ // set the source to be a (dummy) revenue account.
+ $account = new Account;
+ $accountType = AccountType::whereType(AccountType::LIABILITY_CREDIT)->first();
+ $account->accountType = $accountType;
+ $this->source = $account;
+ }
+
+ return $result;
+ }
+
+ /**
+ * @param int|null $accountId
+ *
+ * @return bool
+ */
+ protected function validateLCDestination(?int $accountId): bool
+ {
+ Log::debug(sprintf('Now in validateLCDestination(%d)', $accountId));
+ $result = null;
+ $validTypes = config('firefly.valid_liabilities');
+
+ if (null === $accountId) {
+ $this->sourceError = (string)trans('validation.lc_destination_need_data');
+ $result = false;
+ }
+
+ Log::debug('Destination ID is not null.');
+ $search = $this->accountRepository->findNull($accountId);
+
+ // the source resulted in an account, but it's not of a valid type.
+ if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
+ $message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid destination.', $accountId, $search->accountType->type);
+ Log::debug($message);
+ $this->sourceError = $message;
+ $result = false;
+ }
+ // the source resulted in an account, AND it's of a valid type.
+ if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
+ Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name));
+ $this->source = $search;
+ $result = true;
+ }
+
+ return $result ?? false;
+ }
+
+}
\ No newline at end of file
diff --git a/app/Validation/AccountValidator.php b/app/Validation/AccountValidator.php
index d98e4d5247..cf3ffe03de 100644
--- a/app/Validation/AccountValidator.php
+++ b/app/Validation/AccountValidator.php
@@ -34,6 +34,7 @@ use FireflyIII\Validation\Account\OBValidation;
use FireflyIII\Validation\Account\ReconciliationValidation;
use FireflyIII\Validation\Account\TransferValidation;
use FireflyIII\Validation\Account\WithdrawalValidation;
+use FireflyIII\Validation\Account\LiabilityValidation;
use Log;
/**
@@ -41,7 +42,7 @@ use Log;
*/
class AccountValidator
{
- use AccountValidatorProperties, WithdrawalValidation, DepositValidation, TransferValidation, ReconciliationValidation, OBValidation;
+ use AccountValidatorProperties, WithdrawalValidation, DepositValidation, TransferValidation, ReconciliationValidation, OBValidation, LiabilityValidation;
public bool $createMode;
public string $destError;
@@ -129,6 +130,9 @@ class AccountValidator
case TransactionType::OPENING_BALANCE:
$result = $this->validateOBDestination($accountId, $accountName);
break;
+ case TransactionType::LIABILITY_CREDIT:
+ $result = $this->validateLCDestination($accountId);
+ break;
case TransactionType::RECONCILIATION:
$result = $this->validateReconciliationDestination($accountId);
break;
@@ -164,6 +168,10 @@ class AccountValidator
case TransactionType::OPENING_BALANCE:
$result = $this->validateOBSource($accountId, $accountName);
break;
+ case TransactionType::LIABILITY_CREDIT:
+ $result = $this->validateLCSource($accountName);
+ break;
+
case TransactionType::RECONCILIATION:
Log::debug('Calling validateReconciliationSource');
$result = $this->validateReconciliationSource($accountId);
@@ -201,7 +209,7 @@ class AccountValidator
*/
protected function canCreateType(string $accountType): bool
{
- $canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE];
+ $canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}