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

102 lines
3.3 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;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Log;
/**
* Trait LiabilityValidation
*/
trait LiabilityValidation
{
/**
2021-12-18 12:35:17 +01:00
* @param array $array
2021-04-10 17:26:00 +02:00
*
* @return bool
*/
2021-12-18 12:35:17 +01:00
protected function validateLCDestination(array $array): bool
2021-04-10 17:26:00 +02:00
{
2021-12-18 12:35:17 +01:00
Log::debug('Now in validateLCDestination', $array);
2021-04-10 17:26:00 +02:00
$result = null;
2021-12-18 12:35:17 +01:00
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
2021-04-10 17:26:00 +02:00
$validTypes = config('firefly.valid_liabilities');
if (null === $accountId) {
2022-03-29 15:00:29 +02:00
$this->sourceError = (string) trans('validation.lc_destination_need_data');
2021-04-10 17:26:00 +02:00
$result = false;
}
Log::debug('Destination ID is not null.');
2021-06-30 06:17:38 +02:00
$search = $this->accountRepository->find($accountId);
2021-04-10 17:26:00 +02:00
// 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;
}
/**
* Source of an liability credit must be a liability.
*
2021-12-18 12:35:17 +01:00
* @param array $array
*
* @return bool
*/
2021-12-18 12:35:17 +01:00
protected function validateLCSource(array $array): bool
{
2021-12-18 12:35:17 +01:00
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
2022-03-29 15:00:29 +02:00
$result = true;
2021-12-18 12:35:17 +01:00
Log::debug('Now in validateLCDestination', $array);
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;
}
2021-05-13 06:17:53 +02:00
}