Skip to content

Firefly III: a personal finances manager

Fixed some issues with the monthly report and missing amounts.

James Colebe868d37f2a1
diff --git a/app/Helpers/Collection/Account.php b/app/Helpers/Collection/Account.php
deleted file mode 100644
index e3fc70e702..0000000000
--- a/app/Helpers/Collection/Account.php
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php
-/**
- * Account.php
- * Copyright (C) 2016 [email protected]
- *
- * This software may be modified and distributed under the terms of the
- * Creative Commons Attribution-ShareAlike 4.0 International License.
- *
- * See the LICENSE file for details.
- */
-
-declare(strict_types = 1);
-namespace FireflyIII\Helpers\Collection;
-
-use Illuminate\Support\Collection;
-
-/**
- * Class Account
- *
- * @package FireflyIII\Helpers\Collection
- */
-class Account
-{
-
- /** @var Collection */
- protected $accounts;
- /** @var string */
- protected $difference = '';
- /** @var string */
- protected $end = '';
- /** @var string */
- protected $start = '';
-
- /**
- * Account constructor.
- */
- public function __construct()
- {
- $this->accounts = new Collection;
- }
-
- /**
- * @return Collection
- */
- public function getAccounts(): Collection
- {
- return $this->accounts;
- }
-
- /**
- * @param Collection $accounts
- */
- public function setAccounts(Collection $accounts)
- {
- $this->accounts = $accounts;
- }
-
- /**
- * @return string
- */
- public function getDifference(): string
- {
- return $this->difference;
- }
-
- /**
- * @param string $difference
- */
- public function setDifference(string $difference)
- {
- $this->difference = $difference;
- }
-
- /**
- * @return string
- */
- public function getEnd(): string
- {
- return $this->end;
- }
-
- /**
- * @param string $end
- */
- public function setEnd(string $end)
- {
- $this->end = $end;
- }
-
- /**
- * @return string
- */
- public function getStart(): string
- {
- return $this->start;
- }
-
- /**
- * @param string $start
- */
- public function setStart(string $start)
- {
- $this->start = $start;
- }
-
-
-}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 3a8d8a3ecc..d740291f68 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -288,22 +288,39 @@ class AccountRepository implements AccountRepositoryInterface
*
* @param Account $account
*
- * @return Carbon
+ * @return TransactionJournal
*/
- public function oldestJournalDate(Account $account): Carbon
+ public function oldestJournal(Account $account): TransactionJournal
{
- $first = new Carbon;
- $date = $account->transactions()
+ $first = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
->orderBy('transaction_journals.order', 'DESC')
+ ->where('transaction_journals.user_id', $this->user->id)
->orderBy('transaction_journals.id', 'ASC')
- ->first(['transaction_journals.date']);
- if (!is_null($date)) {
- $first = new Carbon($date->date);
+ ->first(['transaction_journals.id']);
+ if (!is_null($first)) {
+ return TransactionJournal::find(intval($first->id));
+ }
+
+ return new TransactionJournal();
+ }
+
+ /**
+ * Returns the date of the very first transaction in this account.
+ *
+ * @param Account $account
+ *
+ * @return Carbon
+ */
+ public function oldestJournalDate(Account $account): Carbon
+ {
+ $journal = $this->oldestJournal($account);
+ if (is_null($journal->id)) {
+ return new Carbon;
}
- return $first;
+ return $journal->date;
}
/**
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 6aca02047e..428e48144d 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -15,6 +15,7 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
+use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
/**
@@ -105,6 +106,15 @@ interface AccountRepositoryInterface
*/
public function newestJournalDate(Account $account): Carbon;
+ /**
+ * Returns the date of the very first transaction in this account.
+ *
+ * @param Account $account
+ *
+ * @return TransactionJournal
+ */
+ public function oldestJournal(Account $account): TransactionJournal;
+
/**
* Returns the date of the very first transaction in this account.
*
diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php
index 5a85a93201..d1300e6f1c 100644
--- a/app/Repositories/Account/AccountTasker.php
+++ b/app/Repositories/Account/AccountTasker.php
@@ -14,8 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
-use FireflyIII\Helpers\Collection\Account as AccountCollection;
-use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\User;
use Illuminate\Database\Query\JoinClause;
@@ -106,9 +104,9 @@ class AccountTasker implements AccountTaskerInterface
* @param Carbon $start
* @param Carbon $end
*
- * @return AccountCollection
+ * @return array
*/
- public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): AccountCollection
+ public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
{
$startAmount = '0';
$endAmount = '0';
@@ -116,45 +114,52 @@ class AccountTasker implements AccountTaskerInterface
$ids = $accounts->pluck('id')->toArray();
$yesterday = clone $start;
$yesterday->subDay();
- $startSet = Steam::balancesById($ids, $yesterday);
- $backupSet = Steam::balancesById($ids, $start);
- $endSet = Steam::balancesById($ids, $end);
+ $startSet = Steam::balancesById($ids, $yesterday);
+ $endSet = Steam::balancesById($ids, $end);
- Log::debug(
- sprintf(
- 'getAccountReport from %s to %s for %d accounts.',
- $start->format('Y-m-d'),
- $end->format('Y-m-d'),
- $accounts->count()
- )
- );
- $accounts->each(
- function (Account $account) use ($startSet, $endSet, $backupSet) {
- $account->startBalance = $startSet[$account->id] ?? '0';
- $account->endBalance = $endSet[$account->id] ?? '0';
-
- // check backup set just in case:
- if ($account->startBalance === '0' && isset($backupSet[$account->id])) {
- $account->startBalance = $backupSet[$account->id];
- }
- }
- );
+ Log::debug('Start of accountreport');
+
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class);
+
+ $return = [
+ 'start' => '0',
+ 'end' => '0',
+ 'difference' => '0',
+ 'accounts' => [],
+ ];
- // summarize:
foreach ($accounts as $account) {
- $startAmount = bcadd($startAmount, $account->startBalance);
- $endAmount = bcadd($endAmount, $account->endBalance);
- $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
- }
+ $id = $account->id;
+ $entry = [
+ 'name' => $account->name,
+ 'id' => $account->id,
+ 'start_balance' => '0',
+ 'end_balance' => '0',
+ ];
+
+ // get first journal date:
+ $first = $repository->oldestJournal($account);
+ $entry['start_balance'] = $startSet[$account->id] ?? '0';
+ $entry['end_balance'] = $endSet[$account->id] ?? '0';
+ if (!is_null($first->id) && $yesterday < $first->date && $end >= $first->date) {
+ // something about balance?
+ $entry['start_balance'] = $first->transactions()->where('account_id', $account->id)->first()->amount;
+ Log::debug(sprintf('Account was opened before %s, so opening balance is %f', $yesterday->format('Y-m-d'), $entry['start_balance']));
+ }
+ $return['start'] = bcadd($return['start'], $entry['start_balance']);
+ $return['end'] = bcadd($return['end'], $entry['end_balance']);
- $object = new AccountCollection;
- $object->setStart($startAmount);
- $object->setEnd($endAmount);
- $object->setDifference($diff);
- $object->setAccounts($accounts);
+ $return['accounts'][$id] = $entry;
+ }
+ foreach ($accounts as $account) {
+ $id = $account->id;
+ $diff = bcadd($diff, bcsub($return['accounts'][$id]['end_balance'], $return['accounts'][$id]['start_balance']));
+ }
+ $return['difference'] = bcsub($return['end'], $return['start']);
- return $object;
+ return $return;
}
/**
diff --git a/app/Repositories/Account/AccountTaskerInterface.php b/app/Repositories/Account/AccountTaskerInterface.php
index 67ab246ed9..57fbebaa79 100644
--- a/app/Repositories/Account/AccountTaskerInterface.php
+++ b/app/Repositories/Account/AccountTaskerInterface.php
@@ -14,7 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
-use FireflyIII\Helpers\Collection\Account as AccountCollection;
use Illuminate\Support\Collection;
/**
@@ -54,8 +53,8 @@ interface AccountTaskerInterface
* @param Carbon $start
* @param Carbon $end
*
- * @return AccountCollection
+ * @return array
*/
- public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): AccountCollection;
+ public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
}
diff --git a/app/Support/Steam.php b/app/Support/Steam.php
index 0e06913bca..0185a93caf 100644
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@ -152,7 +152,7 @@ class Steam
public function balancesById(array $ids, Carbon $date): array
{
- // abuse chart properties:
+ // cache this property.
$cache = new CacheProperties;
$cache->addProperty($ids);
$cache->addProperty('balances');
diff --git a/resources/views/reports/partials/accounts.twig b/resources/views/reports/partials/accounts.twig
index 3468f4c1d0..285e2a5d06 100644
--- a/resources/views/reports/partials/accounts.twig
+++ b/resources/views/reports/partials/accounts.twig
@@ -8,23 +8,23 @@
</tr>
</thead>
<tbody>
- {% for account in accountReport.getAccounts %}
+ {% for account in accountReport.accounts %}
<tr>
<td data-value="{{ account.name }}">
<a href="{{ route('accounts.show',account.id) }}" title="{{ account.name }}">{{ account.name }}</a>
</td>
- <td class="hidden-xs" data-value="{{ account.startBalance }}" style="text-align: right;">{{ account.startBalance|formatAmount }}</td>
- <td class="hidden-xs" data-value="{{ account.endBalance }}" style="text-align: right;">{{ account.endBalance|formatAmount }}</td>
- <td style="text-align: right;" data-value="{{ (account.endBalance - account.startBalance) }}">{{ (account.endBalance - account.startBalance)|formatAmount }}</td>
+ <td class="hidden-xs" data-value="{{ account.start_balance }}" style="text-align: right;">{{ account.start_balance|formatAmount }}</td>
+ <td class="hidden-xs" data-value="{{ account.end_balance }}" style="text-align: right;">{{ account.end_balance|formatAmount }}</td>
+ <td style="text-align: right;" data-value="{{ (account.end_balance - account.start_balance) }}">{{ (account.end_balance - account.start_balance)|formatAmount }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td><em>{{ 'sumOfSums'|_ }}</em></td>
- <td class="hidden-xs" style="text-align: right;">{{ accountReport.getStart|formatAmount }}</td>
- <td class="hidden-xs" style="text-align: right;">{{ accountReport.getEnd|formatAmount }}</td>
- <td style="text-align: right;">{{ accountReport.getDifference|formatAmount }}</td>
+ <td class="hidden-xs" style="text-align: right;">{{ accountReport.start|formatAmount }}</td>
+ <td class="hidden-xs" style="text-align: right;">{{ accountReport.end|formatAmount }}</td>
+ <td style="text-align: right;">{{ accountReport.difference|formatAmount }}</td>
</tr>
</tfoot>
</table>