Files
firefly-iii/app/Http/Controllers/Account/IndexController.php

187 lines
7.9 KiB
PHP
Raw Normal View History

2018-07-14 11:45:05 +02:00
<?php
/**
* IndexController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 11:45:05 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 11:45:05 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-14 11:45:05 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 11:45:05 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-14 11:45:05 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-14 11:45:05 +02:00
*/
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Account;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-08-09 16:16:27 +02:00
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
use Illuminate\Contracts\View\Factory;
2018-07-14 11:45:05 +02:00
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\View\View;
2018-07-14 11:45:05 +02:00
/**
*
* Class IndexController
*/
class IndexController extends Controller
{
2018-08-09 16:16:27 +02:00
use BasicDataSupport;
/** @var AccountRepositoryInterface The account repository */
2018-07-14 11:45:05 +02:00
private $repository;
/**
* IndexController constructor.
*
2019-06-22 10:25:34 +02:00
* @codeCoverageIgnore
2018-07-14 11:45:05 +02:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', (string) trans('firefly.accounts'));
2018-07-14 11:45:05 +02:00
$this->repository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
/**
* @param Request $request
2019-10-19 19:17:14 +02:00
* @param string $objectType
2018-07-14 11:45:05 +02:00
*
* @return Factory|View
2018-07-14 11:45:05 +02:00
*/
2019-10-19 19:17:14 +02:00
public function inactive(Request $request, string $objectType)
2018-07-14 11:45:05 +02:00
{
2019-06-22 10:25:34 +02:00
$objectType = $objectType ?? 'asset';
2019-10-19 19:17:14 +02:00
$inactivePage = true;
$subTitle = (string) trans(sprintf('firefly.%s_accounts_inactive', $objectType));
2019-06-22 10:25:34 +02:00
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
$types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
2019-10-19 19:17:14 +02:00
$collection = $this->repository->getInactiveAccountsByType($types);
2018-07-14 11:45:05 +02:00
$total = $collection->count();
$page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2019-05-04 20:58:43 +02:00
$accounts = $collection->slice(($page - 1) * $pageSize, $pageSize);
2018-07-14 11:45:05 +02:00
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) {
2018-08-05 18:59:15 +02:00
$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);
2020-06-29 06:44:17 +02:00
$account->interest = number_format((float) $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));
2018-07-14 11:45:05 +02:00
}
);
2019-10-19 19:17:14 +02:00
// 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'));
2019-10-19 19:17:14 +02:00
}
2020-07-10 06:48:39 +02:00
/**
* @return \Illuminate\Contracts\Foundation\Application|Factory|View
*/
2020-07-12 17:34:06 +02:00
public function emptyIndex(?string $objectType = null)
2020-07-10 06:48:39 +02:00
{
2020-07-12 17:34:06 +02:00
return view('accounts.empty-index', compact('objectType'));
2020-07-10 06:48:39 +02:00
}
2019-10-19 19:17:14 +02:00
/**
* Show list of accounts.
*
* @param Request $request
* @param string $objectType
2019-10-19 19:17:14 +02:00
*
* @return Factory|View
2019-10-19 19:17:14 +02:00
*/
public function index(Request $request, string $objectType)
{
2020-07-12 17:34:06 +02:00
// temp catch for layout.
if ('v2' === config('firefly.layout')) {
return $this->emptyIndex($objectType);
}
2019-10-19 19:17:14 +02:00
$objectType = $objectType ?? 'asset';
$subTitle = (string) trans(sprintf('firefly.%s_accounts', $objectType));
2019-10-19 19:17:14 +02:00
$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;
2019-10-19 19:17:14 +02:00
$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);
2020-06-29 06:44:17 +02:00
$account->interest = number_format((float) $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));
$account->location = $this->repository->getLocation($account);
2019-10-19 19:17:14 +02:00
}
);
2018-07-14 11:45:05 +02:00
// make paginator:
$accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
2019-06-22 10:25:34 +02:00
$accounts->setPath(route('accounts.index', [$objectType]));
2018-07-14 11:45:05 +02:00
2019-10-19 19:17:14 +02:00
return view('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
2018-07-14 11:45:05 +02:00
}
}