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

270 lines
10 KiB
PHP
Raw Normal View History

2023-01-08 07:43:16 +01:00
<?php
/*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2023-01-08 07:43:16 +01:00
use FireflyIII\Models\Account;
2023-01-15 09:22:34 +01:00
use FireflyIII\Models\AccountType;
2023-01-08 19:47:39 +01:00
use FireflyIII\Models\Transaction;
2023-01-08 07:43:16 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
use FireflyIII\Services\Internal\Support\CreditRecalculateService;
use FireflyIII\User;
use Illuminate\Console\Command;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class UpgradeLiabilitiesEight
*/
class UpgradeLiabilitiesEight extends Command
{
use ShowsFriendlyMessages;
2023-01-15 15:47:25 +01:00
public const CONFIG_NAME = '600_upgrade_liabilities';
protected $description = 'Upgrade liabilities to new 6.0.0 structure.';
protected $signature = 'firefly-iii:liabilities-600 {--F|force : Force the execution of this command.}';
2023-01-08 07:43:16 +01:00
/**
* Execute the console command.
*
* @return int
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2023-01-08 07:43:16 +01:00
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2023-01-08 07:43:16 +01:00
return 0;
}
$this->upgradeLiabilities();
2023-01-08 18:41:56 +01:00
$this->markAsExecuted();
2023-01-08 07:43:16 +01:00
return 0;
}
/**
2023-06-21 12:34:58 +02:00
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2023-01-08 07:43:16 +01:00
*/
2023-06-21 12:34:58 +02:00
private function isExecuted(): bool
2023-01-08 07:43:16 +01:00
{
2023-06-21 12:34:58 +02:00
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool)$configVar->data;
2023-01-08 07:43:16 +01:00
}
2023-06-21 12:34:58 +02:00
return false;
2023-01-08 07:43:16 +01:00
}
/**
2023-05-29 13:56:55 +02:00
*
2023-01-08 07:43:16 +01:00
*/
2023-06-21 12:34:58 +02:00
private function upgradeLiabilities(): void
2023-01-08 07:43:16 +01:00
{
2023-06-21 12:34:58 +02:00
$users = User::get();
/** @var User $user */
foreach ($users as $user) {
$this->upgradeForUser($user);
}
}
2023-01-08 07:43:16 +01:00
2023-06-21 12:34:58 +02:00
/**
* @param User $user
*/
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);
$service = app(CreditRecalculateService::class);
$service->setAccount($account);
$service->recalculate();
}
}
2023-01-15 13:49:28 +01:00
2023-06-21 12:34:58 +02:00
/**
* @param Account $account
*/
private function upgradeLiability(Account $account): void
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($account->user);
2023-01-08 07:43:16 +01:00
2023-06-21 12:34:58 +02:00
$direction = $repository->getMetaValue($account, 'liability_direction');
if ('credit' === $direction && $this->hasBadOpening($account)) {
$this->deleteCreditTransaction($account);
$this->reverseOpeningBalance($account);
$this->friendlyInfo(sprintf('Corrected opening balance for liability #%d ("%s")', $account->id, $account->name));
}
if ('credit' === $direction) {
$count = $this->deleteTransactions($account);
if ($count > 0) {
$this->friendlyInfo(sprintf('Removed %d old format transaction(s) for liability #%d ("%s")', $count, $account->id, $account->name));
2023-01-15 09:22:34 +01:00
}
}
2023-01-08 07:43:16 +01:00
}
/**
2023-06-21 12:34:58 +02:00
* @param Account $account
2023-05-29 13:56:55 +02:00
*
2023-01-08 07:43:16 +01:00
* @return bool
*/
private function hasBadOpening(Account $account): bool
{
$openingBalanceType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first();
$liabilityType = TransactionType::whereType(TransactionType::LIABILITY_CREDIT)->first();
$openingJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
->first(['transaction_journals.*']);
if (null === $openingJournal) {
return false;
}
$liabilityJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->where('transaction_journals.transaction_type_id', $liabilityType->id)
->first(['transaction_journals.*']);
if (null === $liabilityJournal) {
return false;
}
if (!$openingJournal->date->isSameDay($liabilityJournal->date)) {
return false;
}
return true;
}
2023-01-08 19:47:39 +01:00
2023-02-22 18:14:14 +01:00
/**
2023-06-21 12:34:58 +02:00
* @param Account $account
2023-05-07 20:17:29 +02:00
*
2023-06-21 12:34:58 +02:00
* @return void
2023-05-07 20:17:29 +02:00
*/
2023-06-21 12:34:58 +02:00
private function deleteCreditTransaction(Account $account): void
2023-05-07 20:17:29 +02:00
{
2023-06-21 12:34:58 +02:00
$liabilityType = TransactionType::whereType(TransactionType::LIABILITY_CREDIT)->first();
$liabilityJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->where('transaction_journals.transaction_type_id', $liabilityType->id)
->first(['transaction_journals.*']);
if (null !== $liabilityJournal) {
$group = $liabilityJournal->transactionGroup;
$service = new TransactionGroupDestroyService();
$service->destroy($group);
return;
}
2023-02-22 18:14:14 +01:00
}
2023-01-08 19:47:39 +01:00
/**
2023-06-21 12:34:58 +02:00
* @param Account $account
2023-05-29 13:56:55 +02:00
*
2023-01-08 19:47:39 +01:00
* @return void
*/
private function reverseOpeningBalance(Account $account): void
{
$openingBalanceType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first();
/** @var TransactionJournal $openingJournal */
$openingJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
->first(['transaction_journals.*']);
2023-02-12 07:23:57 +01:00
/** @var Transaction|null $source */
2023-01-15 09:22:34 +01:00
$source = $openingJournal->transactions()->where('amount', '<', 0)->first();
2023-02-12 07:23:57 +01:00
/** @var Transaction|null $dest */
2023-01-15 09:22:34 +01:00
$dest = $openingJournal->transactions()->where('amount', '>', 0)->first();
2023-11-05 09:40:45 +01:00
if (null !== $source && null !== $dest) {
2023-01-15 09:22:34 +01:00
$sourceId = $source->account_id;
$destId = $dest->account_id;
$dest->account_id = $sourceId;
2023-01-08 19:47:39 +01:00
$source->account_id = $destId;
$source->save();
$dest->save();
2023-05-29 13:56:55 +02:00
2023-01-08 19:47:39 +01:00
return;
}
2023-10-29 06:31:13 +01:00
app('log')->warning('Did not find opening balance.');
2023-01-08 19:47:39 +01:00
}
2023-02-22 18:14:14 +01:00
/**
2023-11-04 11:31:14 +01:00
* @param Account $account
2023-02-22 18:14:14 +01:00
*
2023-06-21 12:34:58 +02:00
* @return int
2023-02-22 18:14:14 +01:00
*/
2023-11-04 11:31:14 +01:00
private function deleteTransactions(Account $account): int
2023-02-22 18:14:14 +01:00
{
2023-06-21 12:34:58 +02:00
$count = 0;
$journals = TransactionJournal::leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transactions.account_id', $account->id)->get(['transaction_journals.*']);
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$delete = false;
/** @var Transaction $source */
$source = $journal->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $dest */
$dest = $journal->transactions()->where('amount', '>', 0)->first();
// if source is this liability and destination is expense, remove transaction.
// if source is revenue and destination is liability, remove transaction.
2023-11-05 19:41:37 +01:00
if ($source->account_id === $account->id && $dest->account->accountType->type === AccountType::EXPENSE) {
2023-06-21 12:34:58 +02:00
$delete = true;
}
2023-11-05 19:41:37 +01:00
if ($dest->account_id === $account->id && $source->account->accountType->type === AccountType::REVENUE) {
2023-06-21 12:34:58 +02:00
$delete = true;
}
// overruled. No transaction will be deleted, ever.
2023-11-01 18:45:15 +01:00
// code is kept in place so I can revisit my reasoning.
2023-06-21 12:34:58 +02:00
$delete = false;
2023-11-04 14:09:51 +01:00
// if ($delete) {
2023-11-01 18:45:15 +01:00
$service = app(TransactionGroupDestroyService::class);
$service->destroy($journal->transactionGroup);
$count++;
2023-11-04 14:09:51 +01:00
// }
2023-05-07 20:17:29 +02:00
}
2023-06-21 12:34:58 +02:00
return $count;
2023-05-07 20:17:29 +02:00
}
/**
2023-06-21 12:34:58 +02:00
*
2023-05-07 20:17:29 +02:00
*/
2023-06-21 12:34:58 +02:00
private function markAsExecuted(): void
2023-05-07 20:17:29 +02:00
{
2023-06-21 12:34:58 +02:00
app('fireflyconfig')->set(self::CONFIG_NAME, true);
2023-02-22 18:14:14 +01:00
}
2023-01-08 07:43:16 +01:00
}