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
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-14 11:45:05 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-14 11:45:05 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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;
|
2021-09-18 10:26:12 +02:00
|
|
|
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;
|
2020-03-17 15:01:00 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2018-07-14 11:45:05 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2020-03-17 15:01:00 +01:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2018-07-20 20:53:48 +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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return Factory|View
|
2023-12-20 19:35:52 +01:00
|
|
|
*
|
2021-09-18 10:26:12 +02: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
|
|
|
{
|
2024-12-26 05:25:46 +01: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 */
|
2024-12-26 05:25:46 +01:00
|
|
|
$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 */
|
2024-12-26 05:25:46 +01:00
|
|
|
$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';
|
2025-01-22 05:24:12 +01:00
|
|
|
$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:
|
2024-12-26 05:25:46 +01:00
|
|
|
$accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
|
2019-10-19 19:17:14 +02:00
|
|
|
$accounts->setPath(route('accounts.inactive.index', [$objectType]));
|
|
|
|
|
2022-01-29 14:11:12 +01:00
|
|
|
return view('accounts.index', compact('objectType', 'inactivePage', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
|
2019-10-19 19:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show list of accounts.
|
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @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__));
|
2024-12-26 05:25:46 +01:00
|
|
|
$subTitle = (string) trans(sprintf('firefly.%s_accounts', $objectType));
|
|
|
|
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
|
|
|
|
$types = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
|
2020-07-26 15:05:48 +02:00
|
|
|
|
2021-03-27 15:13:48 +01:00
|
|
|
$this->repository->resetAccountOrder();
|
2020-07-26 15:05:48 +02:00
|
|
|
|
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 */
|
2024-12-26 05:25:46 +01:00
|
|
|
$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 */
|
2024-12-26 05:25:46 +01:00
|
|
|
$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 {
|
2024-12-26 05:25:46 +01:00
|
|
|
$interest = (string) $this->repository->getMetaValue($account, 'interest');
|
|
|
|
$interest = '' === $interest ? '0' : $interest;
|
|
|
|
$currency = $this->repository->getAccountCurrency($account);
|
2022-12-24 05:48:04 +01:00
|
|
|
|
2024-12-26 05:25:46 +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(
|
2021-09-18 10:26:12 +02:00
|
|
|
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 */
|
2024-12-26 05:25:46 +01: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
|
|
|
|
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
|
|
|
|
2022-01-29 14:11:12 +01: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:13:41 +01:00
|
|
|
|
2024-12-25 07:10:02 +01:00
|
|
|
return $result;
|
|
|
|
}
|
2018-07-14 11:45:05 +02:00
|
|
|
}
|