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

102 lines
3.8 KiB
PHP
Raw Normal View History

2020-03-21 08:11:14 +01:00
<?php
2020-06-30 19:05:35 +02:00
2020-03-21 08:11:14 +01:00
/**
* ReconciliationValidation.php
* Copyright (c) 2020 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/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2020-03-21 08:11:14 +01:00
namespace FireflyIII\Validation\Account;
2021-12-18 12:35:17 +01:00
use FireflyIII\Models\Account;
2020-03-21 08:11:14 +01:00
/**
* Trait ReconciliationValidation
*/
trait ReconciliationValidation
{
2022-03-29 15:00:29 +02:00
public ?Account $destination;
public ?Account $source;
2021-12-18 12:35:17 +01:00
protected function validateReconciliationDestination(array $array): bool
2020-03-21 08:11:14 +01:00
{
2022-10-02 20:13:32 +02:00
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
// if both are NULL, the destination is valid because the reconciliation
// is expected to be "negative", i.e. the money flows towards the
// destination to the asset account which is the source.
2020-03-21 08:11:14 +01:00
2022-10-02 20:13:32 +02:00
if (null === $accountId && null === $accountName) {
return true;
2020-03-21 08:11:14 +01:00
}
2022-10-02 20:13:32 +02:00
// after that, search for it expecting an asset account or a liability.
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateReconciliationDestination', $array);
2020-03-21 08:11:14 +01:00
2022-10-02 20:13:32 +02:00
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
$search = $this->findExistingAccount($validTypes, $array);
2022-10-02 20:13:32 +02:00
if (null === $search) {
2024-12-22 08:43:12 +01:00
$this->sourceError = (string) trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
2022-10-30 14:44:49 +01:00
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
2020-03-21 08:11:14 +01:00
2022-10-02 20:13:32 +02:00
return false;
2020-03-21 08:11:14 +01:00
}
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-10-29 06:33:43 +01:00
app('log')->debug('Valid source account!');
2020-03-21 08:11:14 +01:00
2022-10-02 20:13:32 +02:00
return true;
2020-03-21 08:11:14 +01:00
}
/**
2022-10-02 20:13:32 +02:00
* Basically the same check
2020-03-21 08:11:14 +01:00
*/
2021-12-18 12:35:17 +01:00
protected function validateReconciliationSource(array $array): bool
2020-03-21 08:11:14 +01:00
{
2022-10-02 20:13:32 +02:00
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
// if both are NULL, the source is valid because the reconciliation
// is expected to be "positive", i.e. the money flows from the
// source to the asset account that is the destination.
if (null === $accountId && null === $accountName) {
2023-10-29 06:33:43 +01:00
app('log')->debug('The source is valid because ID and name are NULL.');
2023-03-11 07:09:27 +01:00
$this->setSource(new Account());
2023-12-20 19:35:52 +01:00
2022-10-02 20:13:32 +02:00
return true;
2020-03-21 08:11:14 +01:00
}
2021-03-21 09:15:40 +01:00
2022-10-02 20:13:32 +02:00
// after that, search for it expecting an asset account or a liability.
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateReconciliationSource', $array);
2021-03-21 09:15:40 +01:00
2022-10-02 20:13:32 +02:00
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
$search = $this->findExistingAccount($validTypes, $array);
2022-10-02 20:13:32 +02:00
if (null === $search) {
2024-12-22 08:43:12 +01:00
$this->sourceError = (string) trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
2022-10-30 14:44:49 +01:00
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
2022-10-02 20:13:32 +02:00
return false;
2020-03-21 08:11:14 +01:00
}
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-10-29 06:33:43 +01:00
app('log')->debug('Valid source account!');
2021-03-21 09:15:40 +01:00
2022-10-02 20:13:32 +02:00
return true;
2020-03-21 08:11:14 +01:00
}
}