From 6efe5cfd4d2b34640f9aba085a6779b60defd93a Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 19 Oct 2019 19:17:14 +0200 Subject: [PATCH] Fix #2694 --- .../Controllers/Account/IndexController.php | 67 +++++++++++++++++-- .../Account/AccountRepository.php | 26 ++++++- .../Account/AccountRepositoryInterface.php | 7 ++ resources/lang/en_US/firefly.php | 5 ++ resources/views/v1/accounts/index.twig | 26 ++++++- routes/breadcrumbs.php | 7 ++ routes/web.php | 10 +++ 7 files changed, 138 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php index 15769a7890..b0b7525ca3 100644 --- a/app/Http/Controllers/Account/IndexController.php +++ b/app/Http/Controllers/Account/IndexController.php @@ -63,20 +63,19 @@ class IndexController extends Controller } /** - * Show list of accounts. - * * @param Request $request - * @param string $objectType + * @param string $objectType * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function index(Request $request, string $objectType) + public function inactive(Request $request, string $objectType) { $objectType = $objectType ?? 'asset'; - $subTitle = (string)trans(sprintf('firefly.%s_accounts', $objectType)); + $inactivePage = true; + $subTitle = (string)trans(sprintf('firefly.%s_accounts_inactive', $objectType)); $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType)); $types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType)); - $collection = $this->repository->getAccountsByType($types); + $collection = $this->repository->getInactiveAccountsByType($types); $total = $collection->count(); $page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); $pageSize = (int)app('preferences')->get('listPageSize', 50)->data; @@ -105,11 +104,65 @@ class IndexController extends Controller } ); + // make paginator: + $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page); + $accounts->setPath(route('accounts.inactive.index', [$objectType])); + + return view('accounts.index', compact('objectType','inactivePage', 'subTitleIcon', 'subTitle', 'page', 'accounts')); + + } + + /** + * Show list of accounts. + * + * @param Request $request + * @param string $objectType + * + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function index(Request $request, string $objectType) + { + $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->getActiveAccountsByType($types); + $total = $collection->count(); + $page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); + $pageSize = (int)app('preferences')->get('listPageSize', 50)->data; + $accounts = $collection->slice(($page - 1) * $pageSize, $pageSize); + $inactiveCount = $this->repository->getInactiveAccountsByType($types)->count(); + + + unset($collection); + /** @var Carbon $start */ + $start = clone session('start', Carbon::now()->startOfMonth()); + /** @var Carbon $end */ + $end = clone session('end', Carbon::now()->endOfMonth()); + $start->subDay(); + + $ids = $accounts->pluck('id')->toArray(); + $startBalances = app('steam')->balancesByAccounts($accounts, $start); + $endBalances = app('steam')->balancesByAccounts($accounts, $end); + $activities = app('steam')->getLastActivities($ids); + + $accounts->each( + function (Account $account) use ($activities, $startBalances, $endBalances) { + $account->lastActivityDate = $this->isInArray($activities, $account->id); + $account->startBalance = $this->isInArray($startBalances, $account->id); + $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(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', [$objectType])); - return view('accounts.index', compact('objectType', 'subTitleIcon', 'subTitle', 'page', 'accounts')); + return view('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts')); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 73de3a9a02..048e3457a5 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -318,9 +318,8 @@ class AccountRepository implements AccountRepositoryInterface $query->where('active', 1); $query->orderBy('accounts.account_type_id', 'ASC'); $query->orderBy('accounts.name', 'ASC'); - $result = $query->get(['accounts.*']); - return $result; + return $query->get(['accounts.*']); } /** @@ -606,4 +605,27 @@ class AccountRepository implements AccountRepositoryInterface return $account; } + + /** + * @param array $types + * + * @return Collection + */ + public function getInactiveAccountsByType(array $types): Collection + { + /** @var Collection $result */ + $query = $this->user->accounts()->with( + ['accountmeta' => function (HasMany $query) { + $query->where('name', 'account_role'); + }] + ); + if (count($types) > 0) { + $query->accountTypeIn($types); + } + $query->where('active', 0); + $query->orderBy('accounts.account_type_id', 'ASC'); + $query->orderBy('accounts.name', 'ASC'); + + return $query->get(['accounts.*']); + } } diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 54793896ca..07c2727505 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -142,6 +142,13 @@ interface AccountRepositoryInterface */ public function getActiveAccountsByType(array $types): Collection; + /** + * @param array $types + * + * @return Collection + */ + public function getInactiveAccountsByType(array $types): Collection; + /** * @return Account */ diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index fe2c84dfc7..539daf219d 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -759,6 +759,9 @@ return [ 'list_inactive_rule' => 'inactive rule', // accounts: + 'inactive_account_link' => 'You have :count inactive (archived) accounts, which you can view on this separate page.', + 'all_accounts_inactive' => 'These are your inactive accounts.', + 'active_account_link' => 'This link goes back to your active accounts.', 'account_missing_transaction' => 'Account #:id (":name") cannot be viewed directly, but Firefly is missing redirect information.', 'details_for_asset' => 'Details for asset account ":name"', 'details_for_expense' => 'Details for expense account ":name"', @@ -786,7 +789,9 @@ return [ 'make_new_revenue_account' => 'Create a new revenue account', 'make_new_liabilities_account' => 'Create a new liability', 'asset_accounts' => 'Asset accounts', + 'asset_accounts_inactive' => 'Asset accounts (inactive)', 'expense_accounts' => 'Expense accounts', + 'expense_accounts_inactive' => 'Expense accounts (inactive)', 'revenue_accounts' => 'Revenue accounts', 'cash_accounts' => 'Cash accounts', 'Cash account' => 'Cash account', diff --git a/resources/views/v1/accounts/index.twig b/resources/views/v1/accounts/index.twig index 2a133a2553..ec6088e853 100644 --- a/resources/views/v1/accounts/index.twig +++ b/resources/views/v1/accounts/index.twig @@ -35,7 +35,31 @@ {% include 'list.accounts' %} diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 3150015541..fc08ecd78b 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -88,6 +88,13 @@ try { $breadcrumbs->push(trans('firefly.' . strtolower(e($what)) . '_accounts'), route('accounts.index', [$what])); } ); + Breadcrumbs::register( // inactive + 'accounts.inactive.index', + static function (BreadcrumbsGenerator $breadcrumbs, string $what) { + $breadcrumbs->parent('home'); + $breadcrumbs->push(trans('firefly.' . strtolower(e($what)) . '_accounts_inactive'), route('accounts.inactive.index', [$what])); + } + ); Breadcrumbs::register( 'accounts.create', diff --git a/routes/web.php b/routes/web.php index 280609bce9..b25a01c4e1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -108,9 +108,19 @@ Route::group( ); +//// show inactive +// + /** * Account Controller */ +Route::group( + ['middleware' => 'user-full-auth', 'namespace' => 'FireflyIII\Http\Controllers', 'prefix' => 'inactive-accounts', 'as' => 'accounts.'], static function () { + Route::get('{objectType}', ['uses' => 'Account\IndexController@inactive', 'as' => 'inactive.index'])->where( + 'objectType', 'revenue|asset|expense|liabilities' + ); +} +); Route::group( ['middleware' => 'user-full-auth', 'namespace' => 'FireflyIII\Http\Controllers', 'prefix' => 'accounts', 'as' => 'accounts.'], static function () {