Improve code for test coverage

This commit is contained in:
James Cole 2018-02-16 22:14:53 +01:00
parent 278b7ac52b
commit 9cc1bfb4b5
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
7 changed files with 75 additions and 64 deletions

View File

@ -113,6 +113,7 @@ class AccountController extends Controller
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());
// present to user. // present to user.
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));

View File

@ -162,7 +162,7 @@ class AccountController extends Controller
* *
* @throws FireflyException * @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]; $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]);
@ -183,10 +183,8 @@ 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 = $account->getOpeningBalanceAmount(); $openingBalanceAmount = strval($repository->getOpeningBalanceAmount($account));
$openingBalanceAmount = '0' === $account->getOpeningBalanceAmount() ? '' : $openingBalanceAmount; $openingBalanceDate = $repository->getOpeningBalanceDate($account);
$openingBalanceDate = $account->getOpeningBalanceDate();
$openingBalanceDate = 1900 === $openingBalanceDate->year ? null : $openingBalanceDate->format('Y-m-d');
$currency = $this->currencyRepos->find(intval($account->getMeta('currency_id'))); $currency = $this->currencyRepos->find(intval($account->getMeta('currency_id')));
$preFilled = [ $preFilled = [

View File

@ -240,56 +240,6 @@ class Account extends Model
return $journal; 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 * @codeCoverageIgnore
* Get all of the notes. * Get all of the notes.

View File

@ -41,7 +41,7 @@ class Note extends Model
'deleted_at' => 'datetime', 'deleted_at' => 'datetime',
]; ];
/** @var array */ /** @var array */
protected $fillable = ['title', 'text']; protected $fillable = ['title', 'text', 'noteable_id', 'noteable_type'];
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore

View File

@ -124,6 +124,49 @@ class AccountRepository implements AccountRepositoryInterface
return $account->notes()->first(); 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. * Returns the date of the very last transaction in this account.
* *

View File

@ -62,13 +62,6 @@ interface AccountRepositoryInterface
*/ */
public function find(int $accountId): Account; public function find(int $accountId): Account;
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account;
/** /**
* @param string $number * @param string $number
* @param array $types * @param array $types
@ -93,6 +86,13 @@ interface AccountRepositoryInterface
*/ */
public function findByName(string $name, array $types): ?Account; 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. * Return account type by string.
* *
@ -135,6 +135,25 @@ interface AccountRepositoryInterface
*/ */
public function getNote(Account $account): ?Note; 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. * Find or create the opposing reconciliation account.
* *

View File

@ -54,7 +54,7 @@ class Steam
$currencyId = intval($account->getMeta('currency_id')); $currencyId = intval($account->getMeta('currency_id'));
// use system default currency: // use system default currency:
if (0 === $currencyId) { if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrency(); $currency = app('amount')->getDefaultCurrencyByUser($account->user);
$currencyId = $currency->id; $currencyId = $currency->id;
} }
// first part: get all balances in own currency: // first part: get all balances in own currency: