Improve account CRUD and tests.

This commit is contained in:
James Cole
2019-06-22 10:25:34 +02:00
parent 150818e673
commit 0f70cc5780
18 changed files with 480 additions and 255 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Http\Controllers\ModelInformation;
use Illuminate\Http\Request;
use Log;
@@ -37,11 +38,13 @@ use Log;
*/
class CreateController extends Controller
{
use ModelInformation;
/** @var AccountRepositoryInterface The account repository */
private $repository;
/**
* CreateController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
@@ -122,11 +125,9 @@ class CreateController extends Controller
// update preferences if necessary:
$frontPage = app('preferences')->get('frontPageAccounts', [])->data;
if (AccountType::ASSET === $account->accountType->type && count($frontPage) > 0) {
// @codeCoverageIgnoreStart
if (AccountType::ASSET === $account->accountType->type) {
$frontPage[] = $account->id;
app('preferences')->set('frontPageAccounts', $frontPage);
// @codeCoverageIgnoreEnd
}
// redirect to previous URL.
$redirect = redirect($this->getPreviousUri('accounts.create.uri'));
@@ -140,40 +141,6 @@ class CreateController extends Controller
return $redirect;
}
/**
* @codeCoverageIgnore
* @return array
*/
protected function getRoles(): array
{
$roles = [];
foreach (config('firefly.accountRoles') as $role) {
$roles[$role] = (string)trans(sprintf('firefly.account_role_%s', $role));
}
return $roles;
}
/**
* @codeCoverageIgnore
* @return array
*/
protected function getLiabilityTypes(): array
{
// types of liability:
$debt = $this->repository->getAccountTypeByType(AccountType::DEBT);
$loan = $this->repository->getAccountTypeByType(AccountType::LOAN);
$mortgage = $this->repository->getAccountTypeByType(AccountType::MORTGAGE);
/** @noinspection NullPointerExceptionInspection */
$liabilityTypes = [
$debt->id => (string)trans(sprintf('firefly.account_type_%s', AccountType::DEBT)),
$loan->id => (string)trans(sprintf('firefly.account_type_%s', AccountType::LOAN)),
$mortgage->id => (string)trans(sprintf('firefly.account_type_%s', AccountType::MORTGAGE)),
];
asort($liabilityTypes);
return $liabilityTypes;
}
}

View File

@@ -39,6 +39,7 @@ class DeleteController extends Controller
/**
* DeleteController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
@@ -66,16 +67,16 @@ class DeleteController extends Controller
*/
public function delete(Account $account)
{
$typeName = config('firefly.shortNamesByFullName.' . $account->accountType->type);
$subTitle = (string)trans('firefly.delete_' . $typeName . '_account', ['name' => $account->name]);
$typeName = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type));
$subTitle = (string)trans(sprintf('firefly.delete_%s_account', $typeName), ['name' => $account->name]);
$accountList = app('expandedform')->makeSelectListWithEmpty($this->repository->getAccountsByType([$account->accountType->type]));
$what = $typeName;
$objectType = $typeName;
unset($accountList[$account->id]);
// put previous url in session
$this->rememberPreviousUri('accounts.delete.uri');
return view('accounts.delete', compact('account', 'subTitle', 'accountList', 'what'));
return view('accounts.delete', compact('account', 'subTitle', 'accountList', 'objectType'));
}
/**
@@ -89,13 +90,13 @@ class DeleteController extends Controller
public function destroy(Request $request, Account $account)
{
$type = $account->accountType->type;
$typeName = config('firefly.shortNamesByFullName.' . $type);
$typeName = config(sprintf('firefly.shortNamesByFullName.%s', $type));
$name = $account->name;
$moveTo = $this->repository->findNull((int)$request->get('move_account_before_delete'));
$this->repository->destroy($account, $moveTo);
$request->session()->flash('success', (string)trans('firefly.' . $typeName . '_deleted', ['name' => $name]));
$request->session()->flash('success', (string)trans(sprintf('firefly.%s_deleted', $typeName), ['name' => $name]));
app('preferences')->mark();
return redirect($this->getPreviousUri('accounts.delete.uri'));

View File

@@ -27,9 +27,9 @@ namespace FireflyIII\Http\Controllers\Account;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\ModelInformation;
use Illuminate\Http\Request;
/**
@@ -38,6 +38,7 @@ use Illuminate\Http\Request;
*/
class EditController extends Controller
{
use ModelInformation;
/** @var CurrencyRepositoryInterface The currency repository */
private $currencyRepos;
/** @var AccountRepositoryInterface The account repository */
@@ -78,24 +79,11 @@ class EditController extends Controller
*/
public function edit(Request $request, Account $account, AccountRepositoryInterface $repository)
{
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
$subTitle = (string)trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$roles = [];
foreach (config('firefly.accountRoles') as $role) {
$roles[$role] = (string)trans('firefly.account_role_' . $role);
}
// types of liability:
$debt = $this->repository->getAccountTypeByType(AccountType::DEBT);
$loan = $this->repository->getAccountTypeByType(AccountType::LOAN);
$mortgage = $this->repository->getAccountTypeByType(AccountType::MORTGAGE);
$liabilityTypes = [
$debt->id => (string)trans('firefly.account_type_' . AccountType::DEBT),
$loan->id => (string)trans('firefly.account_type_' . AccountType::LOAN),
$mortgage->id => (string)trans('firefly.account_type_' . AccountType::MORTGAGE),
];
asort($liabilityTypes);
$objectType = config('firefly.shortNamesByFullName')[$account->accountType->type];
$subTitle = (string)trans(sprintf('firefly.edit_%s_account', $objectType), ['name' => $account->name]);
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
$roles = $this->getRoles();
$liabilityTypes = $this->getLiabilityTypes();
// interest calculation periods:
$interestPeriods = [
@@ -146,7 +134,7 @@ class EditController extends Controller
$request->session()->flash('preFilled', $preFilled);
return view(
'accounts.edit', compact('account', 'currency', 'subTitle', 'subTitleIcon', 'what', 'roles', 'preFilled', 'liabilityTypes', 'interestPeriods')
'accounts.edit', compact('account', 'currency', 'subTitle', 'subTitleIcon', 'objectType', 'roles', 'preFilled', 'liabilityTypes', 'interestPeriods')
);
}

View File

@@ -43,6 +43,7 @@ class IndexController extends Controller
/**
* IndexController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
@@ -65,16 +66,16 @@ class IndexController extends Controller
* Show list of accounts.
*
* @param Request $request
* @param string $what
* @param string $objectType
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request, string $what)
public function index(Request $request, string $objectType)
{
$what = $what ?? 'asset';
$subTitle = (string)trans('firefly.' . $what . '_accounts');
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$types = config('firefly.accountTypesByIdentifier.' . $what);
$objectType = $objectType ?? 'asset';
$subTitle = (string)trans(sprintf('firefly.%s_accounts', $objectType));
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
$types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
$collection = $this->repository->getAccountsByType($types);
$total = $collection->count();
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
@@ -99,16 +100,16 @@ class IndexController extends Controller
$account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance);
$account->interest = round($this->repository->getMetaValue($account, 'interest'), 6);
$account->interestPeriod = (string)trans('firefly.interest_calc_' . $this->repository->getMetaValue($account, 'interest_period'));
$account->accountTypeString = (string)trans('firefly.account_type_' . $account->accountType->type);
$account->interestPeriod = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')));
$account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
}
);
// make paginator:
$accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
$accounts->setPath(route('accounts.index', [$what]));
$accounts->setPath(route('accounts.index', [$objectType]));
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
return view('accounts.index', compact('objectType', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
}

View File

@@ -111,8 +111,7 @@ class AccountFormRequest extends Request
'interest_period' => 'in:daily,monthly,yearly',
];
// TODO verify if this will work.
if ('liabilities' === $this->get('what')) {
if ('liabilities' === $this->get('objectType')) {
$rules['opening_balance'] = ['numeric', 'required'];
$rules['opening_balance_date'] = 'date|required';
}