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

194 lines
8.7 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;
2020-09-21 15:30:54 +02:00
use Exception;
use FireflyIII\Exceptions\FireflyException;
2018-07-14 11:45:05 +02:00
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;
2020-09-21 15:30:54 +02:00
use Log;
2018-07-14 11:45:05 +02:00
/**
*
* Class IndexController
*/
class IndexController extends Controller
{
2018-08-09 16:16:27 +02:00
use BasicDataSupport;
2020-09-21 15:30:54 +02:00
private AccountRepositoryInterface $repository;
2018-07-14 11:45:05 +02:00
/**
* 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');
2021-03-27 15:13:48 +01: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);
}
);
}
/**
* @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
* @throws FireflyException
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;
2021-03-27 15:13:48 +01:00
$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();
2021-03-27 15:13:48 +01:00
$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) {
2021-10-19 06:32:10 +02:00
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
2018-08-05 18:59:15 +02:00
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance);
$account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', '');
2021-03-27 15:13:48 +01: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));
2021-10-04 14:57:30 +02:00
$account->current_debt = '0';
2022-01-03 06:35:10 +01:00
$account->iban = implode(' ', str_split((string)$account->iban, 4));
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 prefixView('accounts.index', compact('objectType', 'inactivePage', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
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
2020-09-21 15:30:54 +02:00
* @throws Exception
2019-10-19 19:17:14 +02:00
*/
public function index(Request $request, string $objectType)
{
2020-09-21 15:30:54 +02:00
Log::debug(sprintf('Now at %s', __METHOD__));
$objectType = $objectType ?? 'asset';
2021-03-27 15:13:48 +01:00
$subTitle = (string)trans(sprintf('firefly.%s_accounts', $objectType));
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
$types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
2021-03-27 15:13:48 +01:00
$this->repository->resetAccountOrder();
2019-10-19 19:17:14 +02:00
$collection = $this->repository->getActiveAccountsByType($types);
$total = $collection->count();
2021-03-27 15:13:48 +01:00
$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();
2020-09-21 15:30:54 +02:00
Log::debug(sprintf('Count of collection: %d, count of accounts: %d', $total, $accounts->count()));
2019-10-19 19:17:14 +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) {
// See reference nr. 68
2021-10-19 06:32:10 +02:00
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
2021-04-11 06:41:21 +02:00
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
$account->difference = bcsub($account->endBalance, $account->startBalance);
$account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', '');
$account->interestPeriod = (string)trans(
sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))
);
2021-04-11 06:41:21 +02:00
$account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
$account->location = $this->repository->getLocation($account);
$account->liability_direction = $this->repository->getMetaValue($account, 'liability_direction');
2021-05-02 06:39:18 +02:00
$account->current_debt = $this->repository->getMetaValue($account, 'current_debt') ?? '-';
2022-01-03 06:35:10 +01:00
$account->iban = implode(' ', str_split((string)$account->iban, 4));
2019-10-19 19:17:14 +02:00
}
);
2018-07-14 11:45:05 +02:00
// make paginator:
2020-09-21 15:30:54 +02:00
Log::debug(sprintf('Count of accounts before LAP: %d', $accounts->count()));
/** @var LengthAwarePaginator $accounts */
2018-07-14 11:45:05 +02:00
$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
2020-09-21 15:30:54 +02:00
Log::debug(sprintf('Count of accounts after LAP (1): %d', $accounts->count()));
Log::debug(sprintf('Count of accounts after LAP (2): %d', $accounts->getCollection()->count()));
return prefixView('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
2018-07-14 11:45:05 +02:00
}
}