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

211 lines
7.7 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
2018-07-14 11:45:05 +02:00
* ShowController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +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.
2017-10-21 08:40:00 +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);
2016-05-20 08:57:45 +02:00
2018-07-14 11:45:05 +02:00
namespace FireflyIII\Http\Controllers\Account;
2015-02-09 07:23:39 +01:00
use Carbon\Carbon;
use Exception;
2019-05-29 18:28:28 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-07-14 11:45:05 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
2016-11-21 20:23:25 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\PeriodOverview;
use FireflyIII\Support\Http\Controllers\UserNavigation;
use Illuminate\Http\RedirectResponse;
2016-12-28 13:02:56 +01:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Collection;
2015-03-02 15:44:06 +01:00
use View;
2015-06-03 21:25:11 +02:00
/**
2018-07-14 11:45:05 +02:00
* Class ShowController
2018-07-14 15:22:21 +02:00
*
*/
2018-07-14 11:45:05 +02:00
class ShowController extends Controller
{
use UserNavigation, PeriodOverview;
/** @var CurrencyRepositoryInterface The currency repository */
2018-01-14 10:48:17 +01:00
private $currencyRepos;
/** @var AccountRepositoryInterface The account repository */
2018-01-14 10:48:17 +01:00
private $repository;
2015-02-11 07:35:10 +01:00
/**
* ShowController constructor.
*
2019-06-23 05:53:01 +02:00
* @codeCoverageIgnore
2015-02-11 07:35:10 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
2019-09-04 07:51:31 +02:00
app('view')->share('showCategory', true);
2016-10-29 07:44:46 +02:00
// translations:
$this->middleware(
function ($request, $next) {
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', (string) trans('firefly.accounts'));
2016-10-29 07:44:46 +02:00
2018-01-14 10:48:17 +01:00
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
}
2015-02-21 12:16:41 +01:00
/**
2017-09-16 09:24:48 +02:00
* Show an account.
2017-10-05 11:49:06 +02:00
*
* @param Request $request
* @param Account $account
2018-03-11 16:24:07 +01:00
* @param Carbon|null $start
* @param Carbon|null $end
*
2019-06-23 05:53:01 +02:00
* @throws Exception
* @return RedirectResponse|Redirector|View
2015-02-21 12:16:41 +01:00
*/
2018-02-09 16:47:01 +01:00
public function show(Request $request, Account $account, Carbon $start = null, Carbon $end = null)
2015-02-21 12:16:41 +01:00
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableAccount($account)) {
return $this->redirectAccountToAccount($account); // @codeCoverageIgnore
}
2018-07-27 04:46:21 +02:00
/** @var Carbon $start */
2018-07-14 11:45:05 +02:00
$start = $start ?? session('start');
2018-07-27 04:46:21 +02:00
/** @var Carbon $end */
2018-08-06 19:14:30 +02:00
$end = $end ?? session('end');
2019-06-23 05:53:01 +02:00
2018-02-09 19:11:55 +01:00
if ($end < $start) {
2019-06-23 05:53:01 +02:00
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
2018-02-09 19:11:55 +01:00
}
$location = $this->repository->getLocation($account);
$objectType = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type));
$today = new Carbon;
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $account->accountType->type));
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
$fStart = $start->formatLocalized($this->monthAndDayFormat);
$fEnd = $end->formatLocalized($this->monthAndDayFormat);
$subTitle = (string) trans('firefly.journals_in_period_for_account', ['name' => $account->name, 'start' => $fStart, 'end' => $fEnd]);
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$firstTransaction = $this->repository->oldestJournalDate($account) ?? $start;
$periods = $this->getAccountPeriodOverview($account, $firstTransaction, $end);
2018-03-25 18:26:35 +02:00
2019-05-29 18:28:28 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector
->setAccounts(new Collection([$account]))
->setLimit($pageSize)
2019-09-04 07:51:31 +02:00
->setPage($page)->withAccountInformation()->withCategoryInformation()
2019-05-29 18:28:28 +02:00
->setRange($start, $end);
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]));
$showAll = false;
$balance = app('steam')->balance($account, $end);
2018-03-25 18:26:35 +02:00
return view(
'accounts.show',
compact(
'account',
'showAll',
'objectType',
'currency',
'today',
'periods',
'subTitleIcon',
'groups',
'subTitle',
'start',
'end',
'chartUri',
'location',
'balance'
)
2018-03-25 18:26:35 +02:00
);
}
/**
* Show an account.
*
* @param Request $request
* @param Account $account
*
* @throws Exception
* @return RedirectResponse|Redirector|View
2018-03-25 18:26:35 +02:00
*/
public function showAll(Request $request, Account $account)
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableAccount($account)) {
return $this->redirectAccountToAccount($account); // @codeCoverageIgnore
2018-03-25 18:26:35 +02:00
}
2019-08-17 08:29:35 +02:00
2019-12-30 14:41:16 +01:00
$location = $this->repository->getLocation($account);
$isLiability = $this->repository->isLiability($account);
2019-06-23 05:53:01 +02:00
$objectType = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type));
2018-03-25 20:52:21 +02:00
$end = new Carbon;
2018-03-25 18:26:35 +02:00
$today = new Carbon;
2018-08-07 19:29:10 +02:00
$start = $this->repository->oldestJournalDate($account) ?? Carbon::now()->startOfMonth();
2018-03-25 18:26:35 +02:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2019-06-23 05:53:01 +02:00
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
$subTitle = (string) trans('firefly.all_journals_for_account', ['name' => $account->name]);
2019-06-23 05:53:01 +02:00
$periods = new Collection;
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-09-04 07:51:31 +02:00
$collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page)->withAccountInformation()->withCategoryInformation();
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('accounts.show.all', [$account->id]));
2018-03-25 20:52:21 +02:00
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$showAll = true;
2019-12-30 14:41:16 +01:00
$balance = app('steam')->balance($account, $end);
2017-01-08 10:19:10 +01:00
return view(
'accounts.show',
compact(
'account',
'showAll',
'location',
'objectType',
'isLiability',
'currency',
'today',
'chartUri',
'periods',
'subTitleIcon',
'groups',
'subTitle',
'start',
'end',
'balance'
)
);
2016-05-15 15:08:59 +02:00
}
}