Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Refactored some methods surrounding the opening balance of an account.
James Cole7180a40cd872
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.phpindex 20430d4e80..898f98e03b 100644--- a/app/Http/Controllers/AccountController.php+++ b/app/Http/Controllers/AccountController.php@@ -124,10 +124,9 @@ class AccountController extends Controllerpublic function edit(ARI $repository, Account $account){- $what = config('firefly.shortNamesByFullName')[$account->accountType->type];- $subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);- $subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);- $openingBalance = $account->openingBalanceTransaction($account);+ $what = config('firefly.shortNamesByFullName')[$account->accountType->type];+ $subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);+ $subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);// put previous url in session if not redirect from store (not "return_to_edit").if (session('accounts.edit.fromUpdate') !== true) {@@ -138,19 +137,17 @@ class AccountController extends Controller// pre fill some useful values.// the opening balance is tricky:- $openingBalanceAmount = null;-- if ($openingBalance->id) {- $transaction = $repository->getFirstTransaction($openingBalance, $account);- $openingBalanceAmount = $transaction->amount;- }+ $openingBalanceAmount = $account->getOpeningBalanceAmount();+ $openingBalanceAmount = $account->getOpeningBalanceAmount() === '0' ? '' : $openingBalanceAmount;+ $openingBalanceDate = $account->getOpeningBalanceDate();+ $openingBalanceDate = $openingBalanceDate->year === 1900 ? null : $openingBalanceDate->format('Y-m-d');$preFilled = ['accountNumber' => $account->getMeta('accountNumber'),'accountRole' => $account->getMeta('accountRole'),'ccType' => $account->getMeta('ccType'),'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),- 'openingBalanceDate' => $openingBalance->id ? $openingBalance->date->format('Y-m-d') : null,+ 'openingBalanceDate' => $openingBalanceDate,'openingBalance' => $openingBalanceAmount,'virtualBalance' => round($account->virtual_balance, 2),];diff --git a/app/Models/Account.php b/app/Models/Account.phpindex 4f2d859f84..d9325d1dea 100644--- a/app/Models/Account.php+++ b/app/Models/Account.php@@ -13,6 +13,7 @@ declare(strict_types = 1);namespace FireflyIII\Models;+use Carbon\Carbon;use Crypt;use FireflyIII\Exceptions\FireflyException;use Illuminate\Contracts\Encryption\DecryptException;@@ -212,10 +213,44 @@ class Account extends Model}/**- * @return TransactionJournal|null+ * Returns the amount of the opening balance for this account.+ *+ * @return string+ * @throws FireflyException+ */+ public function getOpeningBalanceAmount(): string+ {+ $journal = TransactionJournal+ ::sortCorrectly()+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')+ ->where('transactions.account_id', $this->id)+ ->transactionTypes([TransactionType::OPENING_BALANCE])+ ->first(['transaction_journals.*']);+ if (is_null($journal)) {+ return '0';+ }++ $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', $this->id)->first();+ if (is_null($transaction)) {+ return '0';+ }++ return strval($transaction->amount);+ }++ /**+ * Returns the date of the opening balance for this account. If no date, will return 01-01-1900+ *+ * @return Carbon+ * @throws FireflyException*/- public function openingBalanceTransaction(): TransactionJournal+ public function getOpeningBalanceDate(): Carbon{+ $date = new Carbon('1900-01-01');$journal = TransactionJournal::sortCorrectly()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')@@ -223,10 +258,10 @@ class Account extends Model->transactionTypes([TransactionType::OPENING_BALANCE])->first(['transaction_journals.*']);if (is_null($journal)) {- return new TransactionJournal;+ return $date;}- return $journal;+ return $journal->date;}/**diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.phpindex a9849afaad..e31e258d44 100644--- a/app/Repositories/Account/AccountRepository.php+++ b/app/Repositories/Account/AccountRepository.php@@ -85,54 +85,6 @@ class AccountRepository implements AccountRepositoryInterfacereturn true;}-- /**- * 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;- }-- return $transaction;- }-- /**- * Returns the date of the very last transaction in this account.- *- * @param Account $account- *- * @return Carbon- */- public function newestJournalDate(Account $account): Carbon- {- /** @var TransactionJournal $journal */- $journal = TransactionJournal::- leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')- ->where('transactions.account_id', $account->id)- ->sortCorrectly()- ->first(['transaction_journals.*']);- if (is_null($journal)) {- return new Carbon('1900-01-01');- }-- return $journal->date;- }-/*** Returns the date of the very first transaction in this account.*diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.phpindex 6743371d97..d3abb50be9 100644--- a/app/Repositories/Account/AccountRepositoryInterface.php+++ b/app/Repositories/Account/AccountRepositoryInterface.php@@ -47,28 +47,6 @@ interface AccountRepositoryInterface*/public function destroy(Account $account, Account $moveTo): bool;- /**- * 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;-- /**- * Returns the date of the very last transaction in this account.- *- * @param Account $account- *- * @return Carbon- */- public function newestJournalDate(Account $account): Carbon;-/*** Returns the date of the very first transaction in this account.*