Refactored some methods surrounding the opening balance of an account.

This commit is contained in:
James Cole 2016-10-10 07:01:14 +02:00
parent 71804af624
commit 7180a40cd8
4 changed files with 47 additions and 85 deletions

View File

@ -124,10 +124,9 @@ class AccountController extends Controller
public 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),
];

View File

@ -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,9 +213,12 @@ class Account extends Model
}
/**
* @return TransactionJournal|null
* Returns the amount of the opening balance for this account.
*
* @return string
* @throws FireflyException
*/
public function openingBalanceTransaction(): TransactionJournal
public function getOpeningBalanceAmount(): string
{
$journal = TransactionJournal
::sortCorrectly()
@ -223,10 +227,41 @@ class Account extends Model
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (is_null($journal)) {
return new TransactionJournal;
return '0';
}
return $journal;
$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 getOpeningBalanceDate(): Carbon
{
$date = new Carbon('1900-01-01');
$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 $date;
}
return $journal->date;
}
/**

View File

@ -85,54 +85,6 @@ class AccountRepository implements AccountRepositoryInterface
return 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.
*

View File

@ -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.
*