mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Add routine that caches account balances. Add it to the /flush routine as well.
This commit is contained in:
49
app/Console/Commands/Correction/CorrectBalanceAmounts.php
Normal file
49
app/Console/Commands/Correction/CorrectBalanceAmounts.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Console\Commands\Correction;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Models\AccountBalance;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use Illuminate\Console\Command;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class CorrectionSkeleton
|
||||
* TODO DONT FORGET TO ADD THIS TO THE DOCKER BUILD
|
||||
*/
|
||||
class CorrectBalanceAmounts extends Command
|
||||
{
|
||||
protected $description = 'Recalculate all account balance amounts';
|
||||
|
||||
protected $signature = 'firefly-iii:correct-balance-amounts';
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$this->correctBalanceAmounts();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function correctBalanceAmounts(): void
|
||||
{
|
||||
$result = Transaction
|
||||
::groupBy(['account_id', 'transaction_currency_id'])
|
||||
->get(['account_id', 'transaction_currency_id', DB::raw('SUM(amount) as amount_sum')]);
|
||||
/** @var stdClass $entry */
|
||||
foreach ($result as $entry) {
|
||||
$account = (int) $entry->account_id;
|
||||
$currency = (int) $entry->transaction_currency_id;
|
||||
$sum = $entry->amount_sum;
|
||||
|
||||
AccountBalance::updateOrCreate(
|
||||
['account_id' => $account, 'transaction_currency_id' => $currency],
|
||||
['balance' => $sum]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@@ -74,6 +74,7 @@ class CorrectDatabase extends Command
|
||||
'firefly-iii:unify-group-accounts',
|
||||
'firefly-iii:trigger-credit-recalculation',
|
||||
'firefly-iii:migrate-preferences',
|
||||
'firefly-iii:correct-balance-amounts',
|
||||
];
|
||||
foreach ($commands as $command) {
|
||||
$this->friendlyLine(sprintf('Now executing command "%s"', $command));
|
||||
|
Reference in New Issue
Block a user