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

117 lines
4.5 KiB
PHP
Raw Normal View History

2018-07-14 11:45:05 +02:00
<?php
/**
* IndexController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
/** @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;
2018-07-14 11:45:05 +02:00
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
/**
*
* 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');
2018-07-15 09:38:49 +02:00
app('view')->share('title', (string)trans('firefly.accounts'));
2018-07-14 11:45:05 +02:00
$this->repository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Show list of accounts.
*
2018-07-14 11:45:05 +02:00
* @param Request $request
2019-06-22 10:25:34 +02:00
* @param string $objectType
2018-07-14 11:45:05 +02:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2019-06-22 10:25:34 +02:00
public function index(Request $request, string $objectType)
2018-07-14 11:45:05 +02:00
{
2019-06-22 10:25:34 +02:00
$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));
2018-07-14 11:45:05 +02:00
$collection = $this->repository->getAccountsByType($types);
$total = $collection->count();
2019-05-04 20:58:43 +02:00
$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);
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);
$account->interest = round($this->repository->getMetaValue($account, 'interest'), 6);
2019-06-22 10:25:34 +02:00
$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
}
);
// 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-06-22 10:25:34 +02:00
return view('accounts.index', compact('objectType', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
2018-07-14 11:45:05 +02:00
}
}