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())); /** @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, true); $currency = $this->accountRepository->getAccountCurrency($piggyBank->account) ?? app('amount')->getNativeCurrencyByUserGroup($piggyBank->account->user->userGroup); // update piggy bank to have a currency. $piggyBank->transaction_currency_id = $currency->id; $piggyBank->saveQuietly(); // store current amount in account association. $piggyBank->accounts()->sync([$piggyBank->account->id => ['current_amount' => $repetition->current_amount]]); $piggyBank->account_id = null; $piggyBank->saveQuietly(); // remove all repetitions (no longer used) $piggyBank->piggyBankRepetitions()->delete(); } private function markAsExecuted(): void { app('fireflyconfig')->set(self::CONFIG_NAME, true); } }