2024-12-01 06:48:15 +01:00
|
|
|
<?php
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2025-01-03 08:15:09 +01:00
|
|
|
|
2024-12-01 06:48:15 +01:00
|
|
|
/*
|
2025-01-03 08:15:09 +01:00
|
|
|
* UpgradesMultiPiggyBanks.php
|
|
|
|
* Copyright (c) 2025 james@firefly-iii.org.
|
2024-12-01 06:48:15 +01:00
|
|
|
*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2025-01-03 08:15:09 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-01 06:48:15 +01:00
|
|
|
namespace FireflyIII\Console\Commands\Upgrade;
|
|
|
|
|
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
2024-12-27 06:48:58 +01:00
|
|
|
class UpgradesMultiPiggyBanks extends Command
|
2024-12-01 06:48:15 +01:00
|
|
|
{
|
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
|
|
|
public const string CONFIG_NAME = '620_make_multi_piggies';
|
|
|
|
|
2024-12-27 06:48:58 +01:00
|
|
|
protected $description = 'Upgrade piggy banks so they can use multiple accounts.';
|
2024-12-01 06:48:15 +01:00
|
|
|
|
2024-12-27 06:48:58 +01:00
|
|
|
protected $signature = 'upgrade:620-piggy-banks {--F|force : Force the execution of this command.}';
|
2024-12-01 06:48:15 +01:00
|
|
|
private AccountRepositoryInterface $accountRepository;
|
2024-12-22 08:43:12 +01:00
|
|
|
private PiggyBankRepositoryInterface $repository;
|
2024-12-01 06:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
|
|
|
$this->friendlyInfo('This command has already been executed.');
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$this->upgradePiggyBanks();
|
|
|
|
$this->friendlyInfo('Upgraded all piggy banks.');
|
|
|
|
|
|
|
|
$this->markAsExecuted();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isExecuted(): bool
|
|
|
|
{
|
|
|
|
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
|
|
|
if (null !== $configVar) {
|
|
|
|
return (bool) $configVar->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function upgradePiggyBanks(): void
|
|
|
|
{
|
|
|
|
$this->repository = app(PiggyBankRepositoryInterface::class);
|
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
$set = PiggyBank::whereNotNull('account_id')->get();
|
|
|
|
Log::debug(sprintf('Will update %d piggy banks(s).', $set->count()));
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-01 06:48:15 +01:00
|
|
|
/** @var PiggyBank $piggyBank */
|
|
|
|
foreach ($set as $piggyBank) {
|
|
|
|
$this->upgradePiggyBank($piggyBank);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function upgradePiggyBank(PiggyBank $piggyBank): void
|
|
|
|
{
|
2025-06-09 05:58:47 +02:00
|
|
|
if (null === $piggyBank->account) {
|
2025-06-09 05:50:23 +02:00
|
|
|
// #10432 account has been deleted, delete piggy bank.
|
|
|
|
$piggyBank->delete();
|
2025-06-09 05:58:47 +02:00
|
|
|
|
2025-06-09 05:50:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-12-01 06:48:15 +01:00
|
|
|
$this->repository->setUser($piggyBank->account->user);
|
|
|
|
$this->accountRepository->setUser($piggyBank->account->user);
|
2024-12-14 17:32:03 +01:00
|
|
|
$repetition = $this->repository->getRepetition($piggyBank, true);
|
2025-01-19 11:54:40 +01:00
|
|
|
$currency = $this->accountRepository->getAccountCurrency($piggyBank->account) ?? app('amount')->getNativeCurrencyByUserGroup($piggyBank->account->user->userGroup);
|
2024-12-01 06:48:15 +01:00
|
|
|
|
|
|
|
// update piggy bank to have a currency.
|
|
|
|
$piggyBank->transaction_currency_id = $currency->id;
|
2024-12-21 07:12:11 +01:00
|
|
|
$piggyBank->saveQuietly();
|
2024-12-01 06:48:15 +01:00
|
|
|
|
|
|
|
// store current amount in account association.
|
|
|
|
$piggyBank->accounts()->sync([$piggyBank->account->id => ['current_amount' => $repetition->current_amount]]);
|
2024-12-14 05:45:54 +01:00
|
|
|
$piggyBank->account_id = null;
|
2024-12-21 07:12:11 +01:00
|
|
|
$piggyBank->saveQuietly();
|
2024-12-01 06:48:15 +01:00
|
|
|
|
|
|
|
// remove all repetitions (no longer used)
|
|
|
|
$piggyBank->piggyBankRepetitions()->delete();
|
|
|
|
|
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
|
|
|
|
private function markAsExecuted(): void
|
|
|
|
{
|
|
|
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
|
|
|
}
|
2024-12-01 06:48:15 +01:00
|
|
|
}
|