mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
106 lines
3.0 KiB
PHP
106 lines
3.0 KiB
PHP
|
<?php
|
||
|
/**
|
||
|
* CCLiabilities.php
|
||
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||
|
*
|
||
|
* This file is part of Firefly III.
|
||
|
*
|
||
|
* Firefly III is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* Firefly III 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 General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace FireflyIII\Console\Commands\Upgrade;
|
||
|
|
||
|
|
||
|
use FireflyIII\Models\Account;
|
||
|
use FireflyIII\Models\AccountType;
|
||
|
use Illuminate\Console\Command;
|
||
|
use Illuminate\Support\Collection;
|
||
|
|
||
|
/**
|
||
|
* Class CCLiabilities
|
||
|
*/
|
||
|
class CCLiabilities extends Command
|
||
|
{
|
||
|
|
||
|
|
||
|
public const CONFIG_NAME = '4780_cc_liabilities';
|
||
|
/**
|
||
|
* The console command description.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $description = 'Convert old credit card liabilities.';
|
||
|
/**
|
||
|
* The name and signature of the console command.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $signature = 'firefly-iii:cc-liabilities {--F|force : Force the execution of this command.}';
|
||
|
|
||
|
/**
|
||
|
* Execute the console command.
|
||
|
*
|
||
|
* @return int
|
||
|
*/
|
||
|
public function handle(): int
|
||
|
{
|
||
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||
|
$this->warn('This command has already been executed.');
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
$ccType = AccountType::where('type', AccountType::CREDITCARD)->first();
|
||
|
$debtType = AccountType::where('type', AccountType::DEBT)->first();
|
||
|
if (null === $ccType || null === $debtType) {
|
||
|
return 0;
|
||
|
}
|
||
|
/** @var Collection $accounts */
|
||
|
$accounts = Account::where('account_type_id', $ccType->id)->get();
|
||
|
foreach ($accounts as $account) {
|
||
|
$account->account_type_id = $debtType->id;
|
||
|
$account->save();
|
||
|
$this->line(sprintf('Converted credit card liability account "%s" (#%d) to generic debt liability.', $account->name, $account->id));
|
||
|
}
|
||
|
if ($accounts->count() > 0) {
|
||
|
$this->info('Credit card liability types are no longer supported and have been converted to generic debts. See: http://bit.ly/FF3-credit-cards');
|
||
|
}
|
||
|
$this->markAsExecuted();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return bool
|
||
|
*/
|
||
|
private function isExecuted(): bool
|
||
|
{
|
||
|
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
||
|
if (null !== $configVar) {
|
||
|
return (bool)$configVar->data;
|
||
|
}
|
||
|
|
||
|
return false; // @codeCoverageIgnore
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
private function markAsExecuted(): void
|
||
|
{
|
||
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
||
|
}
|
||
|
}
|