Files
firefly-iii/app/Validation/Account/LiabilityValidation.php

116 lines
4.2 KiB
PHP
Raw Normal View History

2021-04-10 17:26:00 +02:00
<?php
2021-08-10 19:31:55 +02:00
2021-04-10 17:26:00 +02:00
/*
* LiabilityValidation.php
* Copyright (c) 2021 james@firefly-iii.org
*
* 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/>.
*/
2021-08-10 19:31:55 +02:00
declare(strict_types=1);
2021-04-10 17:26:00 +02:00
namespace FireflyIII\Validation\Account;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
2021-04-10 17:26:00 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
/**
* Trait LiabilityValidation
*/
trait LiabilityValidation
{
2021-12-18 12:35:17 +01:00
protected function validateLCDestination(array $array): bool
2021-04-10 17:26:00 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateLCDestination', $array);
2022-12-27 07:01:13 +01:00
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$validTypes = config('firefly.valid_liabilities');
2021-04-10 17:26:00 +02:00
2022-12-27 07:01:13 +01:00
// if the ID is not null the source account should be a dummy account of the type liability credit.
// the ID of the destination must belong to a liability.
if (null !== $accountId) {
2025-01-03 09:15:52 +01:00
if (AccountTypeEnum::LIABILITY_CREDIT->value !== $this->source?->accountType?->type) {
2023-10-29 06:32:00 +01:00
app('log')->error('Source account is not a liability.');
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return false;
}
$result = $this->findExistingAccount($validTypes, $array);
if (null === $result) {
2023-10-29 06:32:00 +01:00
app('log')->error('Destination account is not a liability.');
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return false;
}
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return true;
2021-04-10 17:26:00 +02:00
}
2022-12-27 07:01:13 +01:00
if (null !== $accountName && '' !== $accountName) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Destination ID is null, now we can assume the destination is a (new) liability credit account.');
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return true;
2021-04-10 17:26:00 +02:00
}
2023-10-29 06:32:00 +01:00
app('log')->error('Destination ID is null, but destination name is also NULL.');
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return false;
2021-04-10 17:26:00 +02:00
}
/**
2022-12-27 07:01:13 +01:00
* Source of a liability credit must be a liability or liability credit account.
*/
2021-12-18 12:35:17 +01:00
protected function validateLCSource(array $array): bool
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateLCSource', $array);
2022-12-27 07:01:13 +01:00
// if the array has an ID and ID is not null, try to find it and check type.
// this account must be a liability
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
2022-12-27 07:01:13 +01:00
if (null !== $accountId) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Source ID is not null, assume were looking for a liability.');
2022-12-27 07:01:13 +01:00
// find liability credit:
$result = $this->findExistingAccount(config('firefly.valid_liabilities'), $array);
if (null === $result) {
2023-10-29 06:32:00 +01:00
app('log')->error('Did not find a liability account, return false.');
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return false;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Return true, found #%d ("%s")', $result->id, $result->name));
2023-03-11 07:09:27 +01:00
$this->setSource($result);
2023-12-20 19:35:52 +01:00
2022-12-27 07:01:13 +01:00
return true;
}
// if array has name and is not null, return true.
2021-12-18 12:35:17 +01:00
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
2022-12-27 07:01:13 +01:00
$result = true;
if ('' === $accountName || null === $accountName) {
2023-10-29 06:32:00 +01:00
app('log')->error('Array must have a name, is not the case, return false.');
$result = false;
}
if (true === $result) {
2023-10-29 06:32:00 +01:00
app('log')->error('Array has a name, return true.');
// set the source to be a (dummy) revenue account.
2022-10-30 14:24:37 +01:00
$account = new Account();
2025-01-03 09:15:52 +01:00
$accountType = AccountType::whereType(AccountTypeEnum::LIABILITY_CREDIT->value)->first();
$account->accountType = $accountType;
2023-03-11 07:09:27 +01:00
$this->setSource($account);
}
return $result;
}
2021-05-13 06:17:53 +02:00
}