mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 17:33:45 +00:00
Refactored some methods surrounding the opening balance of an account.
This commit is contained in:
@@ -127,7 +127,6 @@ class AccountController extends Controller
|
|||||||
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
|
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
|
||||||
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
|
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
|
||||||
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
|
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
|
||||||
$openingBalance = $account->openingBalanceTransaction($account);
|
|
||||||
|
|
||||||
// put previous url in session if not redirect from store (not "return_to_edit").
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
||||||
if (session('accounts.edit.fromUpdate') !== true) {
|
if (session('accounts.edit.fromUpdate') !== true) {
|
||||||
@@ -138,19 +137,17 @@ class AccountController extends Controller
|
|||||||
// pre fill some useful values.
|
// pre fill some useful values.
|
||||||
|
|
||||||
// the opening balance is tricky:
|
// the opening balance is tricky:
|
||||||
$openingBalanceAmount = null;
|
$openingBalanceAmount = $account->getOpeningBalanceAmount();
|
||||||
|
$openingBalanceAmount = $account->getOpeningBalanceAmount() === '0' ? '' : $openingBalanceAmount;
|
||||||
if ($openingBalance->id) {
|
$openingBalanceDate = $account->getOpeningBalanceDate();
|
||||||
$transaction = $repository->getFirstTransaction($openingBalance, $account);
|
$openingBalanceDate = $openingBalanceDate->year === 1900 ? null : $openingBalanceDate->format('Y-m-d');
|
||||||
$openingBalanceAmount = $transaction->amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
$preFilled = [
|
$preFilled = [
|
||||||
'accountNumber' => $account->getMeta('accountNumber'),
|
'accountNumber' => $account->getMeta('accountNumber'),
|
||||||
'accountRole' => $account->getMeta('accountRole'),
|
'accountRole' => $account->getMeta('accountRole'),
|
||||||
'ccType' => $account->getMeta('ccType'),
|
'ccType' => $account->getMeta('ccType'),
|
||||||
'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),
|
'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),
|
||||||
'openingBalanceDate' => $openingBalance->id ? $openingBalance->date->format('Y-m-d') : null,
|
'openingBalanceDate' => $openingBalanceDate,
|
||||||
'openingBalance' => $openingBalanceAmount,
|
'openingBalance' => $openingBalanceAmount,
|
||||||
'virtualBalance' => round($account->virtual_balance, 2),
|
'virtualBalance' => round($account->virtual_balance, 2),
|
||||||
];
|
];
|
||||||
|
@@ -13,6 +13,7 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace FireflyIII\Models;
|
namespace FireflyIII\Models;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use Crypt;
|
use Crypt;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use Illuminate\Contracts\Encryption\DecryptException;
|
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
|
$journal = TransactionJournal
|
||||||
::sortCorrectly()
|
::sortCorrectly()
|
||||||
@@ -223,10 +227,41 @@ class Account extends Model
|
|||||||
->transactionTypes([TransactionType::OPENING_BALANCE])
|
->transactionTypes([TransactionType::OPENING_BALANCE])
|
||||||
->first(['transaction_journals.*']);
|
->first(['transaction_journals.*']);
|
||||||
if (is_null($journal)) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -85,54 +85,6 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
return true;
|
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.
|
* Returns the date of the very first transaction in this account.
|
||||||
*
|
*
|
||||||
|
@@ -47,28 +47,6 @@ interface AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function destroy(Account $account, Account $moveTo): bool;
|
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.
|
* Returns the date of the very first transaction in this account.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user