Skip to content

Firefly III: a personal finances manager

Various code cleanup and refactoring. Restored cache.

James Cole39ea9e85a77e
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index 2d0f070bbb..c4401c21dd 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -226,10 +226,7 @@ class AccountController extends Controller
// grouped other months thing:
// oldest transaction in account:
- $start = $repository->firstUseDate($account);
- if ($start->year == 1900) {
- $start = new Carbon;
- }
+ $start = $repository->firstUseDate($account);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range);
$end = Navigation::endOfX(new Carbon, $range);
@@ -244,8 +241,9 @@ class AccountController extends Controller
if ($cache->has()) {
- // $entries = $cache->get();
- // return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
+ $entries = $cache->get();
+
+ return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
}
// only include asset accounts when this account is an asset:
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index dd67f0f60d..bce6c9650b 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -16,6 +16,7 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use DB;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Transaction;
@@ -85,13 +86,15 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
+ * Returns the date of the first time the Account has been used, or today if it has never been used.
+ *
* @param Account $account
*
* @return Carbon
*/
public function firstUseDate(Account $account): Carbon
{
- $first = new Carbon('1900-01-01');
+ $first = new Carbon;
/** @var Transaction $first */
$date = $account->transactions()
@@ -108,13 +111,22 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
+ * Returns the transaction from a journal that is related to a given account. Since a journal generally only contains
+ * two transactions, this will return one of the two. This method fails horribly when the journal has more than two transactions,
+ * but luckily it isn't used for such folly.
+ *
* @param TransactionJournal $journal
* @param Account $account
*
* @return Transaction
+ * @throws FireflyException
*/
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction
{
+ $count = $journal->transactions()->count();
+ if ($count > 2) {
+ throw new FireflyException(sprintf('Cannot use getFirstTransaction on journal #%d', $journal->id));
+ }
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
if (is_null($transaction)) {
$transaction = new Transaction;
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 83f4b9b84d..c02789239a 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -14,6 +14,7 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
@@ -47,6 +48,8 @@ interface AccountRepositoryInterface
public function destroy(Account $account, Account $moveTo): bool;
/**
+ * Returns the date of the first time the Account has been used, or today if it has never been used.
+ *
* @param Account $account
*
* @return Carbon
@@ -54,10 +57,15 @@ interface AccountRepositoryInterface
public function firstUseDate(Account $account): Carbon;
/**
+ * Returns the transaction from a journal that is related to a given account. Since a journal generally only contains
+ * two transactions, this will return one of the two. This method fails horribly when the journal has more than two transactions,
+ * but luckily it isn't used for such folly.
+ *
* @param TransactionJournal $journal
* @param Account $account
*
* @return Transaction
+ * @throws FireflyException
*/
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction;