mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Recalculate amounts.
This commit is contained in:
parent
e2a20dd63d
commit
76075401f9
@ -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()));
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user