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

208 lines
9.6 KiB
PHP
Raw Normal View History

2018-07-14 11:45:05 +02:00
<?php
2022-12-29 19:41:57 +01:00
2018-07-14 11:45:05 +02:00
/**
* 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
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Account;
use Carbon\Carbon;
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;
2024-12-25 11:59:15 +01:00
use FireflyIII\Support\Facades\Steam;
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;
2020-09-21 15:30:54 +02:00
private AccountRepositoryInterface $repository;
2018-07-14 11:45:05 +02:00
/**
* IndexController constructor.
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');
2024-12-22 08:43:12 +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);
}
);
}
/**
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
* @throws FireflyException
2023-12-22 07:58:35 +01:00
* */
2019-10-19 19:17:14 +02:00
public function inactive(Request $request, string $objectType)
2018-07-14 11:45:05 +02:00
{
$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->getInactiveAccountsByType($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);
2018-07-14 11:45:05 +02:00
unset($collection);
2023-12-20 19:35:52 +01:00
2018-07-14 11:45:05 +02:00
/** @var Carbon $start */
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
2023-12-20 19:35:52 +01:00
2018-07-14 11:45:05 +02:00
/** @var Carbon $end */
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
2018-07-14 11:45:05 +02:00
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
2024-12-22 20:32:58 +01:00
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
2018-07-14 11:45:05 +02:00
$activities = app('steam')->getLastActivities($ids);
2024-12-23 06:55:14 +01:00
2018-07-14 11:45:05 +02:00
$accounts->each(
2023-12-21 05:07:26 +01:00
function (Account $account) use ($activities, $startBalances, $endBalances): void {
2024-12-25 07:10:02 +01:00
$currency = $this->repository->getAccountCurrency($account);
2021-10-19 06:32:10 +02:00
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
2024-12-25 11:59:15 +01:00
$account->startBalances = Steam::filterAccountBalance($startBalances[$account->id] ?? [], $account, $this->convertToNative, $currency);
$account->endBalances = Steam::filterAccountBalance($endBalances[$account->id] ?? [], $account, $this->convertToNative, $currency);
2024-12-25 07:10:02 +01:00
$account->differences = $this->subtract($account->startBalances, $account->endBalances);
2022-12-24 05:06:39 +01:00
$account->interest = app('steam')->bcround($this->repository->getMetaValue($account, 'interest'), 4);
2024-12-22 08:43:12 +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';
$account->currency = $currency ?? $this->defaultCurrency;
2024-12-22 08:43:12 +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);
2019-10-19 19:17:14 +02:00
$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
}
/**
* Show list of accounts.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2022-03-29 15:10:05 +02:00
* @throws FireflyException
2023-12-22 07:58:35 +01:00
* */
2019-10-19 19:17:14 +02:00
public function index(Request $request, string $objectType)
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now at %s', __METHOD__));
$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();
2024-12-22 08:43:12 +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();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count of collection: %d, count of accounts: %d', $total, $accounts->count()));
2020-09-21 15:30:54 +02:00
2019-10-19 19:17:14 +02:00
unset($collection);
2023-12-20 19:35:52 +01:00
2019-10-19 19:17:14 +02:00
/** @var Carbon $start */
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
2023-12-20 19:35:52 +01:00
2019-10-19 19:17:14 +02:00
/** @var Carbon $end */
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
2019-10-19 19:17:14 +02:00
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
2024-12-22 20:32:58 +01:00
$startBalances = app('steam')->finalAccountsBalance($accounts, $start);
$endBalances = app('steam')->finalAccountsBalance($accounts, $end);
2019-10-19 19:17:14 +02:00
$activities = app('steam')->getLastActivities($ids);
2024-12-24 19:03:47 +01:00
2024-12-25 07:10:02 +01:00
2019-10-19 19:17:14 +02:00
$accounts->each(
2023-12-21 05:07:26 +01:00
function (Account $account) use ($activities, $startBalances, $endBalances): void {
$interest = (string) $this->repository->getMetaValue($account, 'interest');
$interest = '' === $interest ? '0' : $interest;
$currency = $this->repository->getAccountCurrency($account);
2022-12-24 05:48:04 +01:00
$account->startBalances = Steam::filterAccountBalance($startBalances[$account->id] ?? [], $account, $this->convertToNative, $currency);
$account->endBalances = Steam::filterAccountBalance($endBalances[$account->id] ?? [], $account, $this->convertToNative, $currency);
2024-12-25 07:10:02 +01:00
$account->differences = $this->subtract($account->startBalances, $account->endBalances);
2021-10-19 06:32:10 +02:00
$account->lastActivityDate = $this->isInArrayDate($activities, $account->id);
2022-12-24 05:48:04 +01:00
$account->interest = app('steam')->bcround($interest, 4);
2024-12-22 08:43:12 +01:00
$account->interestPeriod = (string) trans(
sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))
);
2024-12-22 08:43:12 +01:00
$account->accountTypeString = (string) trans(sprintf('firefly.account_type_%s', $account->accountType->type));
2021-04-11 06:41:21 +02:00
$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') ?? '-';
2024-12-25 11:59:15 +01:00
$account->currency = $currency ?? $this->defaultCurrency;
2024-12-22 08:43:12 +01:00
$account->iban = implode(' ', str_split((string) $account->iban, 4));
2024-12-25 07:10:02 +01:00
2019-10-19 19:17:14 +02:00
}
);
2018-07-14 11:45:05 +02:00
// make paginator:
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count of accounts before LAP: %d', $accounts->count()));
2023-12-20 19:35:52 +01:00
2020-09-21 15:30:54 +02:00
/** @var LengthAwarePaginator $accounts */
$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
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count of accounts after LAP (1): %d', $accounts->count()));
app('log')->debug(sprintf('Count of accounts after LAP (2): %d', $accounts->getCollection()->count()));
2020-09-21 15:30:54 +02:00
return view('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
2018-07-14 11:45:05 +02:00
}
2024-12-25 07:10:02 +01:00
2025-01-04 15:16:11 +01:00
private function subtract(array $startBalances, array $endBalances): array
2024-12-25 07:10:02 +01:00
{
$result = [];
foreach ($endBalances as $key => $value) {
$result[$key] = bcsub($value, $startBalances[$key] ?? '0');
}
2024-12-25 07:10:02 +01:00
return $result;
}
2018-07-14 11:45:05 +02:00
}