Files
firefly-iii/app/Console/Commands/Upgrade/UpgradeLiabilities.php

190 lines
5.8 KiB
PHP
Raw Normal View History

2021-04-23 19:13:38 +02:00
<?php
2021-08-10 19:31:55 +02:00
/*
* UpgradeLiabilities.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-05-13 06:17:53 +02:00
declare(strict_types=1);
2021-04-23 19:13:38 +02:00
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2021-04-23 19:13:38 +02:00
use FireflyIII\Factory\AccountMetaFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2021-04-27 07:55:54 +02:00
use FireflyIII\Services\Internal\Support\CreditRecalculateService;
2021-04-23 19:13:38 +02:00
use FireflyIII\User;
use Illuminate\Console\Command;
2022-10-30 12:24:51 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2021-04-23 19:13:38 +02:00
/**
* Class UpgradeLiabilities
*/
class UpgradeLiabilities extends Command
{
use ShowsFriendlyMessages;
2021-04-23 19:13:38 +02:00
public const CONFIG_NAME = '560_upgrade_liabilities';
protected $description = 'Upgrade liabilities to new 5.6.0 structure.';
protected $signature = 'firefly-iii:upgrade-liabilities {--F|force : Force the execution of this command.}';
2021-04-23 19:13:38 +02:00
/**
* Execute the console command.
*
* @return int
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2021-04-23 19:13:38 +02:00
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2021-04-23 19:13:38 +02:00
return 0;
}
$this->upgradeLiabilities();
2021-04-23 19:15:03 +02:00
$this->markAsExecuted();
2021-04-23 19:13:38 +02:00
return 0;
}
/**
* @return bool
2022-10-30 12:24:51 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2021-04-23 19:13:38 +02:00
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2022-10-30 12:24:51 +01:00
return (bool)$configVar->data;
2021-04-23 19:13:38 +02:00
}
return false;
}
/**
*
*/
2023-06-21 12:34:58 +02:00
private function upgradeLiabilities(): void
2021-04-23 19:13:38 +02:00
{
2023-06-21 12:34:58 +02:00
$users = User::get();
/** @var User $user */
foreach ($users as $user) {
$this->upgradeForUser($user);
}
2021-04-23 19:13:38 +02:00
}
/**
2023-06-21 12:34:58 +02:00
* @param User $user
2021-04-23 19:13:38 +02:00
*/
private function upgradeForUser(User $user): void
{
$accounts = $user->accounts()
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereIn('account_types.type', config('firefly.valid_liabilities'))
->get(['accounts.*']);
/** @var Account $account */
foreach ($accounts as $account) {
$this->upgradeLiability($account);
2021-04-27 07:55:54 +02:00
$service = app(CreditRecalculateService::class);
$service->setAccount($account);
$service->recalculate();
2021-04-23 19:13:38 +02:00
}
}
2023-05-07 20:17:29 +02:00
/**
2023-06-21 12:34:58 +02:00
* @param Account $account
2021-04-23 19:13:38 +02:00
*/
private function upgradeLiability(Account $account): void
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($account->user);
// get opening balance, and correct if necessary.
$openingBalance = $repository->getOpeningBalance($account);
if (null !== $openingBalance) {
// correct if necessary
$this->correctOpeningBalance($account, $openingBalance);
}
2023-01-08 07:43:16 +01:00
// add liability direction property (if it does not yet exist!)
$value = $repository->getMetaValue($account, 'liability_direction');
if (null === $value) {
/** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class);
$factory->crud($account, 'liability_direction', 'debit');
}
2021-04-23 19:13:38 +02:00
}
2023-06-21 12:34:58 +02:00
/**
* @param Account $account
* @param TransactionJournal $openingBalance
*/
private function correctOpeningBalance(Account $account, TransactionJournal $openingBalance): void
{
$source = $this->getSourceTransaction($openingBalance);
$destination = $this->getDestinationTransaction($openingBalance);
if (null === $source || null === $destination) {
return;
}
// source MUST be the liability.
2023-11-05 19:41:37 +01:00
if ($destination->account_id === $account->id) {
2023-06-21 12:34:58 +02:00
// so if not, switch things around:
2023-11-05 19:41:37 +01:00
$sourceAccountId = $source->account_id;
2023-06-21 12:34:58 +02:00
$source->account_id = $destination->account_id;
$destination->account_id = $sourceAccountId;
$source->save();
$destination->save();
}
}
/**
* @param TransactionJournal $journal
*
* @return Transaction|null
*/
private function getSourceTransaction(TransactionJournal $journal): ?Transaction
{
return $journal->transactions()->where('amount', '<', 0)->first();
}
/**
* @param TransactionJournal $journal
*
* @return Transaction|null
*/
private function getDestinationTransaction(TransactionJournal $journal): ?Transaction
{
return $journal->transactions()->where('amount', '>', 0)->first();
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2021-04-23 19:13:38 +02:00
}