firefly-iii/app/Console/Commands/Upgrade/CCLiabilities.php

118 lines
3.5 KiB
PHP
Raw Normal View History

2019-03-20 12:31:00 -05:00
<?php
/**
* CCLiabilities.php
2020-01-23 13:35:02 -06:00
* Copyright (c) 2020 james@firefly-iii.org
2019-03-20 12:31:00 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-03-20 12:31:00 -05:00
*
* 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.
2019-03-20 12:31:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2019-03-20 12:31:00 -05:00
* 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.
2019-03-20 12:31:00 -05:00
*
* 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/>.
2019-03-20 12:31:00 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
2021-09-18 03:20:19 -05:00
use FireflyIII\Exceptions\FireflyException;
2019-03-20 12:31:00 -05:00
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 = '480_cc_liabilities';
2019-03-20 12:31:00 -05:00
/**
* 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
2021-09-18 03:20:19 -05:00
* @throws FireflyException
2019-03-20 12:31:00 -05:00
*/
public function handle(): int
{
2019-03-23 12:58:06 -05:00
$start = microtime(true);
2019-06-10 13:14:00 -05:00
2021-04-07 00:28:43 -05:00
2019-03-20 12:31:00 -05:00
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
2021-04-07 00:28:43 -05:00
2019-06-10 13:14:00 -05:00
2019-03-20 12:31:00 -05:00
$ccType = AccountType::where('type', AccountType::CREDITCARD)->first();
$debtType = AccountType::where('type', AccountType::DEBT)->first();
if (null === $ccType || null === $debtType) {
2019-06-10 13:14:00 -05:00
$this->info('No incorrectly stored credit card liabilities.');
2019-06-13 08:48:35 -05:00
2019-03-20 12:31:00 -05:00
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) {
2021-09-18 03:20:19 -05:00
$this->info('Credit card liability types are no longer supported and have been converted to generic debts. See: https://bit.ly/FF3-credit-cards');
2019-03-20 12:31:00 -05:00
}
2019-03-23 12:58:06 -05:00
if (0 === $accounts->count()) {
$this->info('No incorrectly stored credit card liabilities.');
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified credit card liabilities in %s seconds', $end));
2019-03-20 12:31:00 -05:00
$this->markAsExecuted();
2020-03-21 09:43:41 -05:00
2019-03-20 12:31:00 -05:00
return 0;
}
/**
* @return bool
2021-09-18 03:20:19 -05:00
* @throws FireflyException
2019-03-20 12:31:00 -05:00
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2021-03-11 23:30:40 -06:00
return (bool)$configVar->data;
2019-03-20 12:31:00 -05:00
}
return false;
2019-03-20 12:31:00 -05:00
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2019-08-17 05:09:03 -05:00
}