Skip to content

Firefly III: a personal finances manager

Fix #11541

James Cole151be7df8af7
diff --git a/app/Support/Models/AccountBalanceCalculator.php b/app/Support/Models/AccountBalanceCalculator.php
index 35a4a98a53..a7065d23c3 100644
--- a/app/Support/Models/AccountBalanceCalculator.php
+++ b/app/Support/Models/AccountBalanceCalculator.php
@@ -32,6 +32,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\FireflyConfig;
+use FireflyIII\Support\Facades\Steam;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -97,10 +98,9 @@ class AccountBalanceCalculator
return '0';
}
- Log::debug(sprintf('getLatestBalance: notBefore date is "%s", calculating', $notBefore->format('Y-m-d')));
$query = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->whereNull('transactions.deleted_at')
- ->where('transaction_journals.transaction_currency_id', $currencyId)
+ ->where('transactions.transaction_currency_id', $currencyId)
->whereNull('transaction_journals.deleted_at')
// this order is the same as GroupCollector
->orderBy('transaction_journals.date', 'DESC')
@@ -108,23 +108,32 @@ class AccountBalanceCalculator
->orderBy('transaction_journals.id', 'DESC')
->orderBy('transaction_journals.description', 'DESC')
->orderBy('transactions.amount', 'DESC')
- ->where('transactions.account_id', $accountId)
- ;
- $notBefore->startOfDay();
+ ->where('transactions.account_id', $accountId);
$query->where('transaction_journals.date', '<', $notBefore);
$first = $query->first(['transactions.id', 'transactions.balance_dirty', 'transactions.transaction_currency_id', 'transaction_journals.date', 'transactions.account_id', 'transactions.amount', 'transactions.balance_after']);
+
+ if(null === $first) {
+ Log::debug(sprintf('Found no transactions for currency #%d and account #%d, return 0.', $currencyId, $accountId));
+ return '0';
+ }
+
+
$balance = (string)($first->balance_after ?? '0');
- Log::debug(sprintf('getLatestBalance: found balance: %s in transaction #%d', $balance, $first->id ?? 0));
+ Log::debug(sprintf('getLatestBalance: found balance: %s in transaction #%d on moment %s', Steam::bcround($balance,2), $first->id ?? 0, $notBefore->format('Y-m-d H:i:s')));
return $balance;
}
private function optimizedCalculation(Collection $accounts, ?Carbon $notBefore = null): void
{
- Log::debug('start of optimizedCalculation');
+ if ($notBefore instanceof Carbon) {
+ $notBefore->startOfDay();
+ }
+
+ Log::debug(sprintf('start of optimizedCalculation with date "%s"', $notBefore?->format('Y-m-d H:i:s')));
if ($accounts->count() > 0) {
- Log::debug(sprintf('Limited to %d account(s)', $accounts->count()));
+ Log::debug(sprintf('Limited to %d account(s): %s', $accounts->count(), join(', ', $accounts->pluck('id')->toArray())));
}
// collect all transactions and the change they make.
$balances = [];
@@ -137,24 +146,23 @@ class AccountBalanceCalculator
->orderBy('transaction_journals.order', 'desc')
->orderBy('transaction_journals.id', 'asc')
->orderBy('transaction_journals.description', 'asc')
- ->orderBy('transactions.amount', 'asc')
- ;
+ ->orderBy('transactions.amount', 'asc');
if ($accounts->count() > 0) {
$query->whereIn('transactions.account_id', $accounts->pluck('id')->toArray());
}
if ($notBefore instanceof Carbon) {
- $notBefore->startOfDay();
$query->where('transaction_journals.date', '>=', $notBefore);
}
$set = $query->get(['transactions.id', 'transactions.balance_dirty', 'transactions.transaction_currency_id', 'transaction_journals.date', 'transactions.account_id', 'transactions.amount']);
- Log::debug(sprintf('Counted %d transaction(s)', $set->count()));
+ Log::debug(sprintf('Found %d transaction(s)', $set->count()));
// the balance value is an array.
// first entry is the balance, second is the date.
/** @var Transaction $entry */
foreach ($set as $entry) {
+ Log::debug(sprintf('Processing transaction #%d with currency #%d and amount %s', $entry->id, $entry->transaction_currency_id, Steam::bcround($entry->amount)));
// start with empty array:
$balances[$entry->account_id] ??= [];
$balances[$entry->account_id][$entry->transaction_currency_id] ??= [$this->getLatestBalance($entry->account_id, $entry->transaction_currency_id, $notBefore), null];
@@ -162,6 +170,9 @@ class AccountBalanceCalculator
// before and after are easy:
$before = $balances[$entry->account_id][$entry->transaction_currency_id][0];
$after = bcadd($before, (string)$entry->amount);
+
+ Log::debug(sprintf('Before:%s, after:%s', Steam::bcround($before,2), Steam::bcround($after, 2)));
+
if (true === $entry->balance_dirty || $accounts->count() > 0) {
// update the transaction:
$entry->balance_before = $before;
diff --git a/resources/assets/v1/src/components/transactions/ForeignAmountSelect.vue b/resources/assets/v1/src/components/transactions/ForeignAmountSelect.vue
index 53fd2c26c5..d1e1373b20 100644
--- a/resources/assets/v1/src/components/transactions/ForeignAmountSelect.vue
+++ b/resources/assets/v1/src/components/transactions/ForeignAmountSelect.vue
@@ -121,8 +121,10 @@ export default {
let srcType = this.source.type ? this.source.type.toLowerCase() : 'invalid';
let tType = this.transactionType ? this.transactionType.toLowerCase() : 'invalid';
let liabilities = ['loan', 'debt', 'mortgage'];
+ let asset = ['asset account'];
let sourceIsLiability = liabilities.indexOf(srcType) !== -1;
let destIsLiability = liabilities.indexOf(destType) !== -1;
+ let destIsAsset =asset.indexOf(destType) !== -1;
// console.log(srcType + ' (source) is a liability: ' + sourceIsLiability);
@@ -131,18 +133,15 @@ export default {
if (tType === 'transfer' || destIsLiability || sourceIsLiability) {
console.log('Source or dest is a liability.')
console.log('Source is liability OR dest is liability, OR transfer. Lock list on currency of destination.');
+ console.log(this.destination.type);
// console.log('Length of currencies is ' + this.currencies.length);
// console.log(this.currencies);
this.liability = true;
// lock dropdown list on currencyID of destination UNLESS dest is not liab
for (const key in this.currencies) {
if (this.currencies.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- if (
- parseInt(this.currencies[key].id) === parseInt(this.destination.currency_id) || !destIsLiability
- ) {
- console.log('Enable currency!!');
- console.log(this.currencies[key]);
- // console.log(this.destination);
+ if (parseInt(this.currencies[key].id) === parseInt(this.destination.currency_id) || (!destIsLiability && 'transfer' !== tType)) {
+ console.log('Enable currency: ' + this.currencies[key].attributes.code + '.');
this.enabledCurrencies.push(this.currencies[key]);
}
}
diff --git a/resources/views/list/groups.twig b/resources/views/list/groups.twig
index a5178409db..ccb43f7820 100644
--- a/resources/views/list/groups.twig
+++ b/resources/views/list/groups.twig
@@ -314,7 +314,12 @@
{% if account.id == transaction.source_account_id %}
<span title="Transfer, source">{{ formatAmountBySymbol(transaction.source_balance_after, transaction.currency_symbol, transaction.currency_decimal_places) }}</span>
{% else %}
- <span title="Transfer, dest">{{ formatAmountBySymbol(transaction.destination_balance_after, transaction.currency_symbol, transaction.currency_decimal_places) }}</span>
+ {% if null == transaction.foreign_currency_id %}
+ <span title="Transfer, dest, normal currency">{{ formatAmountBySymbol(transaction.destination_balance_after, transaction.currency_symbol, transaction.currency_decimal_places) }}</span>
+ {% endif %}
+ {% if null != transaction.foreign_currency_id %}
+ <span title="Transfer, dest, foreign currency">{{ formatAmountBySymbol(transaction.destination_balance_after, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}</span>
+ {% endif %}
{% endif %}
{% else %}
&nbsp;