From 9cc1bfb4b518e80a5596f988f8ddd3ef8f037cdd Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 16 Feb 2018 22:14:53 +0100 Subject: [PATCH] Improve code for test coverage --- app/Api/V1/Controllers/AccountController.php | 1 + app/Http/Controllers/AccountController.php | 8 ++- app/Models/Account.php | 50 ------------------- app/Models/Note.php | 2 +- .../Account/AccountRepository.php | 43 ++++++++++++++++ .../Account/AccountRepositoryInterface.php | 33 +++++++++--- app/Support/Steam.php | 2 +- 7 files changed, 75 insertions(+), 64 deletions(-) diff --git a/app/Api/V1/Controllers/AccountController.php b/app/Api/V1/Controllers/AccountController.php index 5355126e68..40ed973b92 100644 --- a/app/Api/V1/Controllers/AccountController.php +++ b/app/Api/V1/Controllers/AccountController.php @@ -113,6 +113,7 @@ class AccountController extends Controller // make paginator: $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams()); // present to user. $manager->setSerializer(new JsonApiSerializer($baseUrl)); diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 25a02e8457..afde27a421 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -162,7 +162,7 @@ class AccountController extends Controller * * @throws FireflyException */ - public function edit(Request $request, Account $account) + public function edit(Request $request, Account $account, AccountRepositoryInterface $repository) { $what = config('firefly.shortNamesByFullName')[$account->accountType->type]; $subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]); @@ -183,10 +183,8 @@ class AccountController extends Controller // pre fill some useful values. // the opening balance is tricky: - $openingBalanceAmount = $account->getOpeningBalanceAmount(); - $openingBalanceAmount = '0' === $account->getOpeningBalanceAmount() ? '' : $openingBalanceAmount; - $openingBalanceDate = $account->getOpeningBalanceDate(); - $openingBalanceDate = 1900 === $openingBalanceDate->year ? null : $openingBalanceDate->format('Y-m-d'); + $openingBalanceAmount = strval($repository->getOpeningBalanceAmount($account)); + $openingBalanceDate = $repository->getOpeningBalanceDate($account); $currency = $this->currencyRepos->find(intval($account->getMeta('currency_id'))); $preFilled = [ diff --git a/app/Models/Account.php b/app/Models/Account.php index ddcd8e9a0c..b70870c148 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -240,56 +240,6 @@ class Account extends Model return $journal; } - /** - * 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 (null === $journal) { - return '0'; - } - - $count = $journal->transactions()->count(); - if (2 !== $count) { - throw new FireflyException(sprintf('Cannot use getFirstTransaction on journal #%d', $journal->id)); - } - $transaction = $journal->transactions()->where('account_id', $this->id)->first(); - if (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 - */ - 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 (null === $journal) { - return $date; - } - - return $journal->date; - } - /** * @codeCoverageIgnore * Get all of the notes. diff --git a/app/Models/Note.php b/app/Models/Note.php index e823788b31..e0ca98c8f2 100644 --- a/app/Models/Note.php +++ b/app/Models/Note.php @@ -41,7 +41,7 @@ class Note extends Model 'deleted_at' => 'datetime', ]; /** @var array */ - protected $fillable = ['title', 'text']; + protected $fillable = ['title', 'text', 'noteable_id', 'noteable_type']; /** * @codeCoverageIgnore diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 9f3dcb20b1..469a83667d 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -124,6 +124,49 @@ class AccountRepository implements AccountRepositoryInterface return $account->notes()->first(); } + /** + * Returns the amount of the opening balance for this account. + * + * @return string + */ + public function getOpeningBalanceAmount(Account $account): ?string + { + + $journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transactions.account_id', $account->id) + ->transactionTypes([TransactionType::OPENING_BALANCE]) + ->first(['transaction_journals.*']); + if (null === $journal) { + return null; + } + $transaction = $journal->transactions()->where('account_id', $account->id)->first(); + if (null === $transaction) { + return null; + } + + return strval($transaction->amount); + } + + /** + * Return date of opening balance as string or null. + * + * @param Account $account + * + * @return null|string + */ + public function getOpeningBalanceDate(Account $account): ?string + { + $journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transactions.account_id', $account->id) + ->transactionTypes([TransactionType::OPENING_BALANCE]) + ->first(['transaction_journals.*']); + if (null === $journal) { + return null; + } + + return $journal->date->format('Y-m-d'); + } + /** * Returns the date of the very last transaction in this account. * diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index f3b13b6864..170eaf8508 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -62,13 +62,6 @@ interface AccountRepositoryInterface */ public function find(int $accountId): Account; - /** - * @param int $accountId - * - * @return Account|null - */ - public function findNull(int $accountId): ?Account; - /** * @param string $number * @param array $types @@ -93,6 +86,13 @@ interface AccountRepositoryInterface */ public function findByName(string $name, array $types): ?Account; + /** + * @param int $accountId + * + * @return Account|null + */ + public function findNull(int $accountId): ?Account; + /** * Return account type by string. * @@ -135,6 +135,25 @@ interface AccountRepositoryInterface */ public function getNote(Account $account): ?Note; + /** + * Returns the amount of the opening balance for this account. + * + * @param Account $account + * + * @return string + */ + public function getOpeningBalanceAmount(Account $account): ?string; + + + /** + * Return date of opening balance as string or null. + * + * @param Account $account + * + * @return null|string + */ + public function getOpeningBalanceDate(Account $account): ?string; + /** * Find or create the opposing reconciliation account. * diff --git a/app/Support/Steam.php b/app/Support/Steam.php index c0f5f291f0..93ce41c4fd 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -54,7 +54,7 @@ class Steam $currencyId = intval($account->getMeta('currency_id')); // use system default currency: if (0 === $currencyId) { - $currency = app('amount')->getDefaultCurrency(); + $currency = app('amount')->getDefaultCurrencyByUser($account->user); $currencyId = $currency->id; } // first part: get all balances in own currency: