Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Update code for piggy banks.
James Colef2fab5d4eef9
diff --git a/app/Console/Commands/Correction/CorrectAmounts.php b/app/Console/Commands/Correction/CorrectAmounts.phpindex 639c96edec..10e65e07be 100644--- a/app/Console/Commands/Correction/CorrectAmounts.php+++ b/app/Console/Commands/Correction/CorrectAmounts.php@@ -163,7 +163,7 @@ class CorrectAmounts extends Commandprivate 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 Commandprivate 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.');diff --git a/app/Console/Commands/System/ForceDecimalSize.php b/app/Console/Commands/System/ForceDecimalSize.phpindex 426e7fbcbd..9f3c42a12c 100644--- a/app/Console/Commands/System/ForceDecimalSize.php+++ b/app/Console/Commands/System/ForceDecimalSize.php@@ -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'],];diff --git a/app/Console/Commands/Upgrade/UpgradeMultiPiggyBanks.php b/app/Console/Commands/Upgrade/UpgradeMultiPiggyBanks.phpnew file mode 100644index 0000000000..f987a0b3d5--- /dev/null+++ b/app/Console/Commands/Upgrade/UpgradeMultiPiggyBanks.php@@ -0,0 +1,118 @@+<?php+/*+ * UpgradeMultiPiggyBanks.php+ * Copyright (c) 2024 [email protected].+ *+ * 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();++ }+}diff --git a/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub b/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stubindex d2f5b627c0..5d2dba01f5 100644--- a/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub+++ b/app/Console/Commands/Upgrade/UpgradeSkeleton.php.stub@@ -6,7 +6,6 @@ use Illuminate\Console\Command;/*** Class UpgradeSkeleton.- * TODO DONT FORGET TO ADD THIS TO THE DOCKER BUILD*/class UpgradeSkeleton extends Command{diff --git a/app/Handlers/Observer/PiggyBankObserver.php b/app/Handlers/Observer/PiggyBankObserver.phpindex 40bde63a1a..74eb37b211 100644--- a/app/Handlers/Observer/PiggyBankObserver.php+++ b/app/Handlers/Observer/PiggyBankObserver.php@@ -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();}/**