Skip to content

Firefly III: a personal finances manager

Recalculate amounts.

James Cole76075401f922
diff --git a/app/Console/Commands/Correction/RecalculateNativeAmounts.php b/app/Console/Commands/Correction/RecalculateNativeAmounts.php
index 90ca01c508..fc6b3decfc 100644
--- a/app/Console/Commands/Correction/RecalculateNativeAmounts.php
+++ b/app/Console/Commands/Correction/RecalculateNativeAmounts.php
@@ -22,6 +22,7 @@
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
+use FireflyIII\Handlers\Observer\TransactionObserver;
use FireflyIII\Models\Account;
use FireflyIII\Models\AutoBudget;
use FireflyIII\Models\AvailableBudget;
@@ -35,6 +36,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\UserGroup\UserGroupRepositoryInterface;
use FireflyIII\Repositories\UserGroups\PiggyBank\PiggyBankRepositoryInterface;
+use FireflyIII\Support\Facades\Preferences;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
@@ -57,11 +59,6 @@ class RecalculateNativeAmounts extends Command
*/
public function handle(): int
{
- // // !!! this array is also in PreferencesEventHandler + RecalculateNativeAmountsCommand
-// // !!! this array is also in PreferencesEventHandler + RecalculateNativeAmountsCommand
-// 'transactions' => ['native_amount', 'native_foreign_amount'], // works
-
-
Log::debug('Will update all native amounts. This may take some time.');
$this->friendlyWarning('Recalculating native amounts for all objects. This may take some time!');
/** @var UserGroupRepositoryInterface $repository */
@@ -70,8 +67,7 @@ class RecalculateNativeAmounts extends Command
foreach ($repository->getAll() as $userGroup) {
$this->recalculateForGroup($userGroup);
}
-
-
+ $this->friendlyInfo('Recalculated all native amounts.');
return 0;
}
@@ -81,6 +77,7 @@ class RecalculateNativeAmounts extends Command
$this->recalculateAccounts($userGroup);
// do a check with the group's currency so we can skip some stuff.
+ Preferences::mark();
$currency = app('amount')->getDefaultCurrencyByUserGroup($userGroup);
$this->recalculatePiggyBanks($userGroup, $currency);
@@ -209,11 +206,13 @@ class RecalculateNativeAmounts extends Command
->orWhereNot('transactions.foreign_currency_id', $currency->id);
})
->get(['transactions.id']);
+ TransactionObserver::$recalculate = false;
foreach ($set as $item) {
// here we are.
$transaction = Transaction::find($item->id);
$transaction->touch();
}
+ TransactionObserver::$recalculate = true;
Log::debug(sprintf('Recalculated %d transactions.', $set->count()));
}
}
diff --git a/app/Handlers/Events/PreferencesEventHandler.php b/app/Handlers/Events/PreferencesEventHandler.php
index 00f5e3ff7c..36c9a397ce 100644
--- a/app/Handlers/Events/PreferencesEventHandler.php
+++ b/app/Handlers/Events/PreferencesEventHandler.php
@@ -117,7 +117,7 @@ class PreferencesEventHandler
private function resetTransactions(UserGroup $userGroup): void
{
// custom query because of the potential size of this update.
- DB::table('transactions')
+ $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) {
@@ -125,5 +125,6 @@ class PreferencesEventHandler
->orWhereNotNull('native_foreign_amount');
})
->update(['native_amount' => null, 'native_foreign_amount' => null]);
+ Log::debug(sprintf('Updated %d transactions.', $success));
}
}
diff --git a/app/Handlers/Observer/TransactionObserver.php b/app/Handlers/Observer/TransactionObserver.php
index 23b0fac316..683503817c 100644
--- a/app/Handlers/Observer/TransactionObserver.php
+++ b/app/Handlers/Observer/TransactionObserver.php
@@ -33,6 +33,7 @@ use Illuminate\Support\Facades\Log;
*/
class TransactionObserver
{
+ public static bool $recalculate = true;
public function deleting(?Transaction $transaction): void
{
app('log')->debug('Observe "deleting" of a transaction.');
@@ -42,7 +43,7 @@ class TransactionObserver
public function updated(Transaction $transaction): void
{
Log::debug('Observe "updated" of a transaction.');
- if (config('firefly.feature_flags.running_balance_column')) {
+ if (config('firefly.feature_flags.running_balance_column') && self::$recalculate) {
if (1 === bccomp($transaction->amount, '0')) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
@@ -55,7 +56,7 @@ class TransactionObserver
{
Log::debug('Observe "created" of a transaction.');
if (config('firefly.feature_flags.running_balance_column')) {
- if (1 === bccomp($transaction->amount, '0')) {
+ if (1 === bccomp($transaction->amount, '0') && self::$recalculate) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
}