mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
Update code for piggy banks.
This commit is contained in:
@@ -163,7 +163,7 @@ class CorrectAmounts extends Command
|
||||
|
||||
private function fixRepetitions(): void
|
||||
{
|
||||
$set = PiggyBankRepetition::where('currentamount', '<', 0)->get();
|
||||
$set = PiggyBankRepetition::where('current_amount', '<', 0)->get();
|
||||
$count = $set->count();
|
||||
if (0 === $count) {
|
||||
$this->friendlyPositive('All piggy bank repetition amounts are positive.');
|
||||
@@ -181,7 +181,7 @@ class CorrectAmounts extends Command
|
||||
|
||||
private function fixPiggyBanks(): void
|
||||
{
|
||||
$set = PiggyBank::where('targetamount', '<', 0)->get();
|
||||
$set = PiggyBank::where('target_amount', '<', 0)->get();
|
||||
$count = $set->count();
|
||||
if (0 === $count) {
|
||||
$this->friendlyPositive('All piggy bank amounts are positive.');
|
||||
|
@@ -83,8 +83,8 @@ class ForceDecimalSize extends Command
|
||||
'currency_exchange_rates' => ['rate', 'user_rate'],
|
||||
'limit_repetitions' => ['amount'],
|
||||
'piggy_bank_events' => ['amount'],
|
||||
'piggy_bank_repetitions' => ['currentamount'],
|
||||
'piggy_banks' => ['targetamount'],
|
||||
'piggy_bank_repetitions' => ['current_amount'],
|
||||
'piggy_banks' => ['target_amount'],
|
||||
'recurrences_transactions' => ['amount', 'foreign_amount'],
|
||||
'transactions' => ['amount', 'foreign_amount'],
|
||||
];
|
||||
|
118
app/Console/Commands/Upgrade/UpgradeMultiPiggyBanks.php
Normal file
118
app/Console/Commands/Upgrade/UpgradeMultiPiggyBanks.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/*
|
||||
* UpgradeMultiPiggyBanks.php
|
||||
* Copyright (c) 2024 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/.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
class UpgradeMultiPiggyBanks extends Command
|
||||
{
|
||||
use ShowsFriendlyMessages;
|
||||
|
||||
public const string CONFIG_NAME = '620_make_multi_piggies';
|
||||
|
||||
protected $description = 'Upgrade piggybanks so they can use multiple accounts.';
|
||||
|
||||
protected $signature = 'firefly-iii:upgrade-multi-piggies {--F|force : Force the execution of this command.}';
|
||||
|
||||
private PiggyBankRepositoryInterface $repository;
|
||||
private AccountRepositoryInterface $accountRepository;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isExecuted(): bool
|
||||
{
|
||||
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
||||
if (null !== $configVar) {
|
||||
return (bool) $configVar->data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function markAsExecuted(): void
|
||||
{
|
||||
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
||||
}
|
||||
|
||||
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()));
|
||||
/** @var PiggyBank $piggyBank */
|
||||
foreach ($set as $piggyBank) {
|
||||
$this->upgradePiggyBank($piggyBank);
|
||||
}
|
||||
}
|
||||
|
||||
private function upgradePiggyBank(PiggyBank $piggyBank): void
|
||||
{
|
||||
$this->repository->setUser($piggyBank->account->user);
|
||||
$this->accountRepository->setUser($piggyBank->account->user);
|
||||
$repetition = $this->repository->getRepetition($piggyBank);
|
||||
$currency = $this->accountRepository->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrencyByUserGroup($piggyBank->account->user->userGroup);
|
||||
|
||||
// update piggy bank to have a currency.
|
||||
$piggyBank->transaction_currency_id = $currency->id;
|
||||
$piggyBank->save();
|
||||
|
||||
// store current amount in account association.
|
||||
$piggyBank->accounts()->sync([$piggyBank->account->id => ['current_amount' => $repetition->current_amount]]);
|
||||
$piggyBank->account_id = null;
|
||||
$piggyBank->save();
|
||||
|
||||
// remove all repetitions (no longer used)
|
||||
$piggyBank->piggyBankRepetitions()->delete();
|
||||
|
||||
}
|
||||
}
|
@@ -6,7 +6,6 @@ use Illuminate\Console\Command;
|
||||
|
||||
/**
|
||||
* Class UpgradeSkeleton.
|
||||
* TODO DONT FORGET TO ADD THIS TO THE DOCKER BUILD
|
||||
*/
|
||||
class UpgradeSkeleton extends Command
|
||||
{
|
||||
|
@@ -34,15 +34,16 @@ class PiggyBankObserver
|
||||
{
|
||||
public function created(PiggyBank $piggyBank): void
|
||||
{
|
||||
app('log')->debug('Observe "created" of a piggy bank.');
|
||||
$repetition = new PiggyBankRepetition();
|
||||
$repetition->piggyBank()->associate($piggyBank);
|
||||
$repetition->start_date = $piggyBank->start_date;
|
||||
$repetition->start_date_tz = $piggyBank->start_date->format('e');
|
||||
$repetition->target_date = $piggyBank->target_date;
|
||||
$repetition->target_date_tz = $piggyBank->target_date?->format('e');
|
||||
$repetition->current_amount = '0';
|
||||
$repetition->save();
|
||||
app('log')->debug('Observe "created" of a piggy bank. DO NOTHING.');
|
||||
|
||||
// $repetition = new PiggyBankRepetition();
|
||||
// $repetition->piggyBank()->associate($piggyBank);
|
||||
// $repetition->start_date = $piggyBank->start_date;
|
||||
// $repetition->start_date_tz = $piggyBank->start_date->format('e');
|
||||
// $repetition->target_date = $piggyBank->target_date;
|
||||
// $repetition->target_date_tz = $piggyBank->target_date?->format('e');
|
||||
// $repetition->current_amount = '0';
|
||||
// $repetition->save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user