mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add routine that caches account balances. Add it to the /flush routine as well.
This commit is contained in:
parent
d356d39d43
commit
cebfaa32bf
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:unify-group-accounts',
|
||||||
'firefly-iii:trigger-credit-recalculation',
|
'firefly-iii:trigger-credit-recalculation',
|
||||||
'firefly-iii:migrate-preferences',
|
'firefly-iii:migrate-preferences',
|
||||||
|
'firefly-iii:correct-balance-amounts',
|
||||||
];
|
];
|
||||||
foreach ($commands as $command) {
|
foreach ($commands as $command) {
|
||||||
$this->friendlyLine(sprintf('Now executing command "%s"', $command));
|
$this->friendlyLine(sprintf('Now executing command "%s"', $command));
|
||||||
|
@ -23,7 +23,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Handlers\Observer;
|
namespace FireflyIII\Handlers\Observer;
|
||||||
|
|
||||||
|
use DB;
|
||||||
|
use FireflyIII\Models\AccountBalance;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class TransactionObserver
|
* Class TransactionObserver
|
||||||
@ -35,4 +38,20 @@ class TransactionObserver
|
|||||||
app('log')->debug('Observe "deleting" of a transaction.');
|
app('log')->debug('Observe "deleting" of a transaction.');
|
||||||
$transaction?->transactionJournal?->delete();
|
$transaction?->transactionJournal?->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updated(Transaction $transaction): void
|
||||||
|
{
|
||||||
|
app('log')->debug('Observe "updated" of a transaction.');
|
||||||
|
// refresh account balance:
|
||||||
|
/** @var stdClass $result */
|
||||||
|
$result = Transaction::groupBy(['account_id', 'transaction_currency_id'])->where('account_id', $transaction->account_id)->first(['account_id', 'transaction_currency_id', DB::raw('SUM(amount) as amount_sum')]);
|
||||||
|
if (null !== $result) {
|
||||||
|
$account = (int) $result->account_id;
|
||||||
|
$currency = (int) $result->transaction_currency_id;
|
||||||
|
$sum = $result->amount_sum;
|
||||||
|
|
||||||
|
AccountBalance::updateOrCreate(['account_id' => $account, 'transaction_currency_id' => $currency], ['balance' => $sum]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,14 +86,15 @@ class DebugController extends Controller
|
|||||||
{
|
{
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range', 'temp-mfa-secret', 'temp-mfa-codes']);
|
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range', 'temp-mfa-secret', 'temp-mfa-codes']);
|
||||||
app('log')->debug('Call cache:clear...');
|
|
||||||
|
|
||||||
Artisan::call('cache:clear');
|
Artisan::call('cache:clear');
|
||||||
app('log')->debug('Call config:clear...');
|
|
||||||
Artisan::call('config:clear');
|
Artisan::call('config:clear');
|
||||||
app('log')->debug('Call route:clear...');
|
|
||||||
Artisan::call('route:clear');
|
Artisan::call('route:clear');
|
||||||
app('log')->debug('Call twig:clean...');
|
Artisan::call('view:clear');
|
||||||
|
|
||||||
|
// also do some recalculations.
|
||||||
|
Artisan::call('firefly-iii:correct-balance-amounts');
|
||||||
|
Artisan::call('firefly-iii:trigger-credit-recalculation');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Artisan::call('twig:clean');
|
Artisan::call('twig:clean');
|
||||||
@ -101,7 +102,6 @@ class DebugController extends Controller
|
|||||||
throw new FireflyException($e->getMessage(), 0, $e);
|
throw new FireflyException($e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
app('log')->debug('Call view:clear...');
|
|
||||||
Artisan::call('view:clear');
|
Artisan::call('view:clear');
|
||||||
|
|
||||||
return redirect(route('index'));
|
return redirect(route('index'));
|
||||||
|
12
app/Models/AccountBalance.php
Normal file
12
app/Models/AccountBalance.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class AccountBalance extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['account_id', 'transaction_currency_id', 'balance'];
|
||||||
|
use HasFactory;
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
if (!Schema::hasTable('account_balances')) {
|
||||||
|
Schema::create('account_balances', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->timestamps();
|
||||||
|
$table->integer('account_id', false, true);
|
||||||
|
$table->integer('transaction_currency_id', false, true);
|
||||||
|
$table->decimal('balance', 32, 12);
|
||||||
|
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
|
||||||
|
$table->foreign('transaction_currency_id')->references('id')->on('transaction_currencies')->onDelete('cascade');
|
||||||
|
$table->unique(['account_id','transaction_currency_id'],'unique_account_currency');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('account_balances');
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user