diff --git a/app/Http/Controllers/Account/ShowController.php b/app/Http/Controllers/Account/ShowController.php index db7adc3e5f..6032d1fb78 100644 --- a/app/Http/Controllers/Account/ShowController.php +++ b/app/Http/Controllers/Account/ShowController.php @@ -91,6 +91,11 @@ class ShowController extends Controller if (AccountType::INITIAL_BALANCE === $account->accountType->type) { return $this->redirectToOriginalAccount($account); } + // a basic thing to determin if this account is a liability: + if ($this->repository->isLiability($account)) { + return redirect(route('accounts.show.all', [$account->id])); + } + /** @var Carbon $start */ $start = $start ?? session('start'); /** @var Carbon $end */ @@ -122,9 +127,13 @@ class ShowController extends Controller $transactions->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')])); $showAll = false; + return view( 'accounts.show', - compact('account', 'showAll', 'what', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri') + compact( + 'account', 'showAll', 'what', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', + 'chartUri' + ) ); } @@ -144,6 +153,7 @@ class ShowController extends Controller if (AccountType::INITIAL_BALANCE === $account->accountType->type) { return $this->redirectToOriginalAccount($account); // @codeCoverageIgnore } + $isLiability = $this->repository->isLiability($account); $end = new Carbon; $today = new Carbon; $start = $this->repository->oldestJournalDate($account) ?? Carbon::now()->startOfMonth(); @@ -167,7 +177,7 @@ class ShowController extends Controller return view( 'accounts.show', - compact('account', 'showAll', 'currency', 'today', 'chartUri', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end') + compact('account', 'showAll','isLiability', 'currency', 'today', 'chartUri', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end') ); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 27e1c0a695..12d57fd97b 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -270,6 +270,30 @@ class AccountRepository implements AccountRepositoryInterface return $factory->findOrCreate('Cash account', $type->type); } + /** + * @param $account + * + * @return string + */ + public function getInterestPerDay(Account $account): string + { + $interest = $this->getMetaValue($account, 'interest'); + $interestPeriod = $this->getMetaValue($account, 'interest_period'); + Log::debug(sprintf('Start with interest of %s percent', $interest)); + + // calculate + if ('monthly' === $interestPeriod) { + $interest = bcdiv(bcmul($interest, '12'), '365'); // per year + Log::debug(sprintf('Interest is now (monthly to daily) %s percent', $interest)); + } + if ('yearly' === $interestPeriod) { + $interest = bcdiv($interest, '365'); // per year + Log::debug(sprintf('Interest is now (yearly to daily) %s percent', $interest)); + } + + return $interest; + } + /** * Return meta value for account. Null if not found. * @@ -382,6 +406,57 @@ class AccountRepository implements AccountRepositoryInterface return $account; } + /** + * @param Account $account + * + * @return bool + */ + public function isLiability(Account $account): bool + { + return \in_array($account->accountType->type, [AccountType::CREDITCARD, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true); + } + + /** + * Returns the date of the very last transaction in this account. + * + * @param Account $account + * + * @return TransactionJournal|null + */ + public function latestJournal(Account $account): ?TransactionJournal + { + $first = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->where('transaction_journals.user_id', $this->user->id) + ->orderBy('transaction_journals.id', 'DESC') + ->first(['transaction_journals.id']); + if (null !== $first) { + return TransactionJournal::find((int)$first->id); + } + + return null; + } + + /** + * Returns the date of the very last transaction in this account. + * + * @param Account $account + * + * @return Carbon|null + */ + public function latestJournalDate(Account $account): ?Carbon + { + $result = null; + $journal = $this->latestJournal($account); + if (null !== $journal) { + $result = $journal->date; + } + + return $result; + } + /** * Returns the date of the very first transaction in this account. * diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index d5e02ab19c..77099390d3 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -123,6 +123,13 @@ interface AccountRepositoryInterface */ public function getCashAccount(): Account; + /** + * @param $account + * + * @return string + */ + public function getInterestPerDay(Account $account): string; + /** * Return meta value for account. Null if not found. * @@ -151,7 +158,6 @@ interface AccountRepositoryInterface */ public function getOpeningBalanceAmount(Account $account): ?string; - /** * Return date of opening balance as string or null. * @@ -170,6 +176,31 @@ interface AccountRepositoryInterface */ public function getReconciliation(Account $account): ?Account; + /** + * @param Account $account + * + * @return bool + */ + public function isLiability(Account $account): bool; + + /** + * Returns the date of the very first transaction in this account. + * + * @param Account $account + * + * @return TransactionJournal|null + */ + public function latestJournal(Account $account): ?TransactionJournal; + + /** + * Returns the date of the very last transaction in this account. + * + * @param Account $account + * + * @return Carbon|null + */ + public function latestJournalDate(Account $account): ?Carbon; + /** * Returns the date of the very first transaction in this account. * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index a56b92ba7c..b3ff588e14 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -902,7 +902,7 @@ return [ 'errors' => 'Errors', 'debt_start_date' => 'Start date of debt', 'debt_start_amount' => 'Start amount of debt', - 'debt_start_amount_help' => 'Please enter a positive amount. You may also enter the current amount, if the original amount was way-back-when.', + 'debt_start_amount_help' => 'Please enter the original amount of this liability as a positive number. You may also enter the current amount. Make sure to edit the date below to match.', 'store_new_liabilities_account' => 'Store new liability', 'edit_liabilities_account' => 'Edit liability ":name"', diff --git a/resources/views/accounts/show.twig b/resources/views/accounts/show.twig index 8e13be1ad7..7dc2787cd3 100644 --- a/resources/views/accounts/show.twig +++ b/resources/views/accounts/show.twig @@ -6,30 +6,51 @@ {% block content %} -
+ {% if isLiability %} + This chart pre-calculates rent starting from the last transaction you've entered. It's just an estimation. + {% endif %} +
+