Skip to content

Firefly III: a personal finances manager

Merge branch 'develop' of github.com:firefly-iii/firefly-iii into develop

James Cole81173e834099
diff --cc app/Support/Steam.php
index 9a6317d28d,804a386e44..d805f8d6a7
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@@ -70,16 -70,16 +70,16 @@@ class Stea
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
- return $cache->get();
+ // return $cache->get();
}
- $balances = [];
- $formatted = $start->format('Y-m-d');
- $startBalance = $this->finalAccountBalance($account, $start);
- $defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
- $accountCurrency = $this->getAccountCurrency($account);
- $hasCurrency = null !== $accountCurrency;
- $currency = $accountCurrency ?? $defaultCurrency;
+ $balances = [];
+ $formatted = $start->format('Y-m-d');
+ $startBalance = $this->finalAccountBalance($account, $start);
+ $defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
+ $accountCurrency = $this->getAccountCurrency($account);
+ $hasCurrency = null !== $accountCurrency;
+ $currency = $accountCurrency ?? $defaultCurrency;
Log::debug(sprintf('Currency is %s', $currency->code));
if (!$hasCurrency) {
Log::debug(sprintf('Also set start balance in %s', $defaultCurrency->code));
@@@ -97,25 -97,29 +97,26 @@@
// sums up the balance changes per day, for foreign, native and normal amounts.
- $set = $account->transactions()
- ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
- ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
- ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
- ->groupBy('transaction_journals.date')
- ->groupBy('transactions.transaction_currency_id')
- ->groupBy('transactions.foreign_currency_id')
- ->orderBy('transaction_journals.date', 'ASC')
- ->whereNull('transaction_journals.deleted_at')
- ->get(
- [ // @phpstan-ignore-line
- 'transaction_journals.date',
- 'transactions.transaction_currency_id',
- DB::raw('SUM(transactions.amount) AS modified'),
- 'transactions.foreign_currency_id',
- DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
- DB::raw('SUM(transactions.native_amount) AS modified_native'),
- ]
- )
- ;
-
- $currentBalance = $startBalance;
+ $set = $account->transactions()
+ ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))
+ ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
+ ->groupBy('transaction_journals.date')
+ ->groupBy('transactions.transaction_currency_id')
+ ->orderBy('transaction_journals.date', 'ASC')
+ ->whereNull('transaction_journals.deleted_at')
+ ->get(
+ [ // @phpstan-ignore-line
+ 'transaction_journals.date',
+ 'transactions.transaction_currency_id',
+ DB::raw('SUM(transactions.amount) AS sum_of_day'),
+ ]
+ );
+
+ $currentBalance = $startBalance;
+ $converter = new ExchangeRateConverter();
+
+
/** @var Transaction $entry */
foreach ($set as $entry) {
// normal, native and foreign amount
@@@ -126,64 -132,42 +127,64 @@@
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
/** @var TransactionCurrency $entryCurrency */
- $entryCurrency = $currencies[$entry->transaction_currency_id];
+ $entryCurrency = $currencies[$entry->transaction_currency_id];
Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s')));
+ $currentBalance[$entryCurrency->code] ??= '0';
+ $currentBalance[$entryCurrency->code] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]);
- // if convert to native, if NOT convert to native.
- if ($convertToNative) {
- Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $this->bcround($modified, 2), $this->bcround($foreignModified, 2), $this->bcround($nativeModified, 2)));
- // if the currency is the default currency add to native balance + currency balance
- if ($entry->transaction_currency_id === $defaultCurrency->id) {
- Log::debug('Add amount to native.');
- $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $modified);
- }
-
- // add to native balance.
- if ($entry->foreign_currency_id !== $defaultCurrency->id) {
- // this check is not necessary, because if the foreign currency is the same as the default currency, the native amount is zero.
- // so adding this would mean nothing.
- $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
- }
- if ($entry->foreign_currency_id === $defaultCurrency->id) {
- $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified);
- }
- // add to balance if is the same.
- if ($entry->transaction_currency_id === $accountCurrency?->id) {
- $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
- }
- // add currency balance
- $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
+ // if not convert to native, add the amount to "balance" and "native_balance" alike:
+ if(!$convertToNative) {
+ $currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
+ $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $sumOfDay);
}
- if (!$convertToNative) {
- Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));
- // add to balance, as expected.
- $currentBalance['balance'] = bcadd($currentBalance['balance'] ?? '0', $modified);
- // add to GBP, as expected.
- $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
+ // if convert to native add the converted amount to native balance.
+ if($convertToNative) {
+ $nativeSumOfDay = $converter->convert($entryCurrency, $defaultCurrency, $carbon, $sumOfDay);
+ $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeSumOfDay);
+ // only add to "balance" if it has the correct currency code (the same as "native")
+ if($defaultCurrency->code === $entryCurrency->code) {
+ $currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
+ }
}
+
-
+// // if convert to native, if NOT convert to native.
+// if ($convertToNative) {
+// // convert for this day to native
+// $currentNative = '0'; // TODO
+// //$currentBalance['native_balance']
+//
+//// Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $this->bcround($modified, 2), $this->bcround($foreignModified, 2), $this->bcround($nativeModified, 2)));
+//// // if the currency is the default currency add to native balance + currency balance
+//// if ($entry->transaction_currency_id === $defaultCurrency->id) {
+//// Log::debug('Add amount to native.');
+//// $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $modified);
+//// }
+////
+//// // add to native balance.
+//// if ($entry->foreign_currency_id !== $defaultCurrency->id) {
+//// // this check is not necessary, because if the foreign currency is the same as the default currency, the native amount is zero.
+//// // so adding this would mean nothing.
+//// $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
+//// }
+//// if ($entry->foreign_currency_id === $defaultCurrency->id) {
+//// $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified);
+//// }
+//// // add to balance if is the same.
+//// if ($entry->transaction_currency_id === $accountCurrency?->id) {
+//// $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
+//// }
+//// // add currency balance
+//// $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
+// }
+// if (!$convertToNative) {
+// Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));
+// // add to balance, as expected.
+// $currentBalance['balance'] = bcadd($currentBalance['balance'] ?? '0', $modified);
+// // add to GBP, as expected.
+// $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
+// }
++
$balances[$carbon->format('Y-m-d')] = $currentBalance;
Log::debug('Updated entry', $currentBalance);
}