Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Move methods to calculation service for #11982
James Cole1893a33d84d6
diff --git a/app/Listeners/System/RecalculatesPrimaryCurrencyAmounts.php b/app/Listeners/System/RecalculatesPrimaryCurrencyAmounts.phpindex 14bf4ade23..811cac0370 100644--- a/app/Listeners/System/RecalculatesPrimaryCurrencyAmounts.php+++ b/app/Listeners/System/RecalculatesPrimaryCurrencyAmounts.php@@ -25,46 +25,24 @@ declare(strict_types=1);namespace FireflyIII\Listeners\System;use FireflyIII\Events\Preferences\UserGroupChangedPrimaryCurrency;-use FireflyIII\Models\Budget;-use FireflyIII\Models\PiggyBank;-use FireflyIII\Models\UserGroup;-use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;-use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;+++++use FireflyIII\Services\Internal\Recalculate\PrimaryAmountRecalculationService;use FireflyIII\Support\Facades\Amount;-use Illuminate\Database\Query\Builder;-use Illuminate\Support\Facades\DB;++use Illuminate\Support\Facades\Log;class RecalculatesPrimaryCurrencyAmounts{public function handle(UserGroupChangedPrimaryCurrency $event): void{- // Reset the primary currency amounts for all objects that have it.- Log::debug('Resetting primary currency amounts for all objects.');-- $tables = [- // !!! this array is also in the migration- 'accounts' => ['native_virtual_balance'],- 'available_budgets' => ['native_amount'],- 'bills' => ['native_amount_min', 'native_amount_max'],- ];- foreach ($tables as $table => $columns) {- Log::debug(sprintf('Now processing table "%s"', $table));- foreach ($columns as $column) {- Log::debug(sprintf('Resetting column "%s" in table "%s".', $column, $table));- DB::table($table)->where('user_group_id', $event->userGroup->id)->update([$column => null]);- }- }-- $this->resetPiggyBanks($event->userGroup);- $this->resetBudgets($event->userGroup);- $this->resetTransactions($event->userGroup);- Log::debug('Have now reset all primary amounts to NULL.');// fire laravel command to recalculate them all.if (Amount::convertToPrimary()) {Log::debug('Will now convert amounts to primary currency.');-$calculator = new PrimaryAmountRecalculationService();$calculator->recalculate();@@ -73,86 +51,7 @@ class RecalculatesPrimaryCurrencyAmountsLog::debug('Will NOT convert to primary currency.');}- private function resetBudget(Budget $budget): void- {- foreach ($budget->autoBudgets as $autoBudget) {- if ('' === (string) $autoBudget->native_amount) {- continue;- }- Log::debug(sprintf('Resetting native_amount for budget #%d and auto budget #%d.', $budget->id, $autoBudget->id));- $autoBudget->native_amount = null;- $autoBudget->saveQuietly();- }- foreach ($budget->budgetlimits as $limit) {- if ('' !== (string) $limit->native_amount) {- Log::debug(sprintf('Resetting native_amount for budget #%d and budget limit #%d.', $budget->id, $limit->id));- $limit->native_amount = null;- $limit->saveQuietly();- }- }- }-- private function resetBudgets(UserGroup $userGroup): void- {- $repository = app(BudgetRepositoryInterface::class);- $repository->setUserGroup($userGroup);- $set = $repository->getBudgets();- Log::debug(sprintf('Reset primary currency of %d budget(s).', $set->count()));- /** @var Budget $budget */- foreach ($set as $budget) {- $this->resetBudget($budget);- }- }- private function resetPiggyBank(PiggyBank $piggyBank): void- {- if ('' !== (string) $piggyBank->native_target_amount) {- Log::debug(sprintf('Resetting native_target_amount for piggy bank #%d.', $piggyBank->id));- $piggyBank->native_target_amount = null;- $piggyBank->saveQuietly();- }- foreach ($piggyBank->accounts as $account) {- if ('' !== (string) $account->pivot->native_current_amount) {- Log::debug(sprintf('Resetting native_current_amount for piggy bank #%d and account #%d.', $piggyBank->id, $account->id));- $account->pivot->native_current_amount = null;- $account->pivot->save();- }- }- foreach ($piggyBank->piggyBankEvents as $event) {- if ('' !== (string) $event->native_amount) {- Log::debug(sprintf('Resetting native_amount for piggy bank #%d and event #%d.', $piggyBank->id, $event->id));- $event->native_amount = null;- $event->saveQuietly();- }- }- }-- private function resetPiggyBanks(UserGroup $userGroup): void- {- $repository = app(PiggyBankRepositoryInterface::class);- $repository->setUserGroup($userGroup);- $piggyBanks = $repository->getPiggyBanks();- Log::debug(sprintf('Reset primary currency of %d piggy bank(s).', $piggyBanks->count()));-- /** @var PiggyBank $piggyBank */- foreach ($piggyBanks as $piggyBank) {- $this->resetPiggyBank($piggyBank);- }- }-- private function resetTransactions(UserGroup $userGroup): void- {- // custom query because of the potential size of this update.- $success = DB::table('transactions')- ->join('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')- ->where('transaction_journals.user_group_id', $userGroup->id)- ->where(static function (Builder $q): void {- $q->whereNotNull('native_amount')->orWhereNotNull('native_foreign_amount');- })- ->update(['native_amount' => null, 'native_foreign_amount' => null])- ;- Log::debug(sprintf('Reset %d transactions.', $success));- }}