Add running balance

This commit is contained in:
James Cole
2024-07-29 19:51:04 +02:00
parent 2df4b40a28
commit 51958af422
4 changed files with 208 additions and 25 deletions

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Transaction;
use FireflyIII\Support\Models\AccountBalanceCalculator;
use Illuminate\Support\Facades\Log;
/**
* Class TransactionObserver
@@ -39,13 +40,19 @@ class TransactionObserver
public function updated(Transaction $transaction): void
{
app('log')->debug('Observe "updated" of a transaction.');
Log::debug('Observe "updated" of a transaction.');
if (1 === bccomp($transaction->amount, '0')) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
}
}
public function created(Transaction $transaction): void
{
app('log')->debug('Observe "created" of a transaction.');
Log::debug('Observe "created" of a transaction.');
if (1 === bccomp($transaction->amount, '0')) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
}
}
}

View File

@@ -108,6 +108,7 @@ class Transaction extends Model
'encrypted' => 'boolean', // model does not have these fields though
'bill_name_encrypted' => 'boolean',
'reconciled' => 'boolean',
'balance_dirty' => 'boolean',
'date' => 'datetime',
];
@@ -233,6 +234,13 @@ class Transaction extends Model
);
}
protected function balanceDirty(): Attribute
{
return Attribute::make(
get: static fn ($value) => (int)$value === 1,
);
}
/**
* Get the amount
*/

View File

@@ -28,6 +28,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountBalance;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class AccountBalanceCalculator
@@ -46,13 +47,24 @@ class AccountBalanceCalculator
public static function recalculateAll(): void
{
$object = new self();
$object->recalculateLatest(null);
//$object->recalculateLatest(null);
$object->optimizedCalculation(new Collection());
// $object->recalculateJournals(null, null);
}
public static function recalculateForJournal(TransactionJournal $transactionJournal): void
{
$object = new self();
// new optimized code, currently UNUSED:
// recalculate everything ON or AFTER the moment of this transaction.
// Transaction
// ::leftjoin('transaction_journals','transaction_journals.id','=','transactions.transaction_journal_id')
// ->where('transaction_journals.user_id', $transactionJournal->user_id)
// ->where('transaction_journals.date', '>=', $transactionJournal->date)
// ->update(['transactions.balance_dirty' => true]);
// $object->optimizedCalculation(new Collection());
foreach ($transactionJournal->transactions as $transaction) {
$object->recalculateLatest($transaction->account);
//$object->recalculateJournals($transaction->account, $transactionJournal);
@@ -80,6 +92,61 @@ class AccountBalanceCalculator
return $entry;
}
/**
* @param Collection $accounts
*
* @return void
*/
private function optimizedCalculation(Collection $accounts): void
{
Log::debug('start of optimizedCalculation');
if ($accounts->count() > 0) {
Log::debug(sprintf('Limited to %d account(s)', $accounts->count()));
}
// collect all transactions and the change they make.
$balances = [];
$count = 0;
$query = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
// this order is the same as GroupCollector, but in the exact reverse.
->orderBy('transaction_journals.date', 'asc')
->orderBy('transaction_journals.order', 'desc')
->orderBy('transaction_journals.id', 'asc')
->orderBy('transaction_journals.description', 'asc')
->orderBy('transactions.amount', 'asc');
if (count($accounts) > 0) {
$query->whereIn('transactions.account_id', $accounts->pluck('id')->toArray());
}
$set = $query->get(['transactions.id', 'transactions.balance_dirty', 'transactions.transaction_currency_id', 'transaction_journals.date', 'transactions.account_id', 'transactions.amount']);
/** @var Transaction $entry */
foreach ($set as $entry) {
// start with empty array:
$balances[$entry->account_id] ??= [];
$balances[$entry->account_id][$entry->transaction_currency_id] ??= '0';
// before and after are easy:
$before = $balances[$entry->account_id][$entry->transaction_currency_id];
$after = bcadd($before, $entry->amount);
if (true === $entry->balance_dirty) {
// update the transaction:
$entry->balance_before = $before;
$entry->balance_after = $after;
$entry->balance_dirty = false;
$entry->saveQuietly(); // do not observe this change, or we get stuck in a loop.
$count++;
}
// then update the array:
$balances[$entry->account_id][$entry->transaction_currency_id] = $after;
}
Log::debug(sprintf('end of optimizedCalculation, corrected %d balance(s)', $count));
// then update all transactions.
// ?? something with accounts?
}
private function getAccountBalanceByJournal(string $title, int $account, int $journal, int $currency): AccountBalance
{
$query = AccountBalance::where('title', $title)->where('account_id', $account)->where('transaction_journal_id', $journal)->where('transaction_currency_id', $currency);

View File

@@ -0,0 +1,101 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (!Schema::hasColumn('transactions', 'balance_before')) {
$table->decimal('balance_before', 32, 12)->nullable()->after('amount');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (!Schema::hasColumn('transactions', 'balance_after')) {
$table->decimal('balance_after', 32, 12)->nullable()->after('balance_before');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (!Schema::hasColumn('transactions', 'balance_dirty')) {
$table->boolean('balance_dirty')->default(true)->after('balance_after');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (Schema::hasColumn('transactions', 'balance_before')) {
$table->dropColumn('balance_before');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (Schema::hasColumn('transactions', 'balance_after')) {
$table->dropColumn('balance_after');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
try {
Schema::table(
'transactions',
static function (Blueprint $table): void {
if (Schema::hasColumn('transactions', 'balance_dirty')) {
$table->dropColumn('balance_dirty');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
}
};