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

169 lines
6.2 KiB
PHP
Raw Normal View History

2019-04-19 07:00:19 +02:00
<?php
/**
* IndexController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-04-19 07:00:19 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-04-19 07:00:19 +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.
2019-04-19 07:00:19 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-04-19 07:00:19 +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.
2019-04-19 07:00:19 +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/>.
2019-04-19 07:00:19 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Transaction;
2021-03-28 11:46:23 +02:00
2019-04-19 07:00:19 +02:00
use Carbon\Carbon;
use Exception;
2019-04-19 07:00:19 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2019-04-19 07:00:19 +02:00
use FireflyIII\Support\Http\Controllers\PeriodOverview;
use Illuminate\Contracts\View\Factory;
2019-04-19 07:00:19 +02:00
use Illuminate\Http\Request;
use Illuminate\View\View;
2019-04-19 07:00:19 +02:00
/**
* Class IndexController
*/
class IndexController extends Controller
{
use PeriodOverview;
2020-10-24 17:27:36 +02:00
private JournalRepositoryInterface $repository;
/**
* IndexController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
2019-09-04 07:51:31 +02:00
app('view')->share('showCategory', true);
// translations:
$this->middleware(
function ($request, $next) {
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2022-03-29 14:58:06 +02:00
app('view')->share('title', (string) trans('firefly.transactions'));
$this->repository = app(JournalRepositoryInterface::class);
return $next($request);
}
);
}
2019-04-19 07:00:19 +02:00
/**
* Index for a range of transactions.
*
* @param Request $request
* @param string $objectType
2019-04-19 07:00:19 +02:00
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Factory|View
2022-03-29 15:10:05 +02:00
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
2019-04-19 07:00:19 +02:00
*/
public function index(Request $request, string $objectType, Carbon $start = null, Carbon $end = null)
2019-04-19 07:00:19 +02:00
{
if ('transfers' === $objectType) {
2021-05-01 08:36:19 +02:00
$objectType = 'transfer';
}
2021-05-01 09:47:21 +02:00
$subTitleIcon = config('firefly.transactionIconsByType.' . $objectType);
$types = config('firefly.transactionTypesByType.' . $objectType);
2022-03-29 14:58:06 +02:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2019-04-19 07:00:19 +02:00
if (null === $start) {
$start = session('start');
$end = session('end');
}
if (null === $end) {
2020-05-01 06:24:24 +02:00
// get last transaction ever?
$last = $this->repository->getLast();
$end = $last ? $last->date : session('end');
2019-04-19 07:00:19 +02:00
}
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
$path = route('transactions.index', [$objectType, $start->format('Y-m-d'), $end->format('Y-m-d')]);
2022-03-27 20:24:13 +02:00
$startStr = $start->isoFormat($this->monthAndDayFormat);
$endStr = $end->isoFormat($this->monthAndDayFormat);
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans(sprintf('firefly.title_%s_between', $objectType), ['start' => $startStr, 'end' => $endStr]);
$firstJournal = $this->repository->firstNull();
2022-10-30 14:24:28 +01:00
$startPeriod = null === $firstJournal ? new Carbon() : $firstJournal->date;
$endPeriod = clone $end;
$periods = $this->getTransactionPeriodOverview($objectType, $startPeriod, $endPeriod);
2019-04-19 07:00:19 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)
->setTypes($types)
->setLimit($pageSize)
->setPage($page)
->withBudgetInformation()
->withCategoryInformation()
->withAccountInformation()
->withAttachmentInformation();
2019-04-19 07:00:19 +02:00
$groups = $collector->getPaginatedGroups();
$groups->setPath($path);
return view('transactions.index', compact('subTitle', 'objectType', 'subTitleIcon', 'groups', 'periods', 'start', 'end'));
}
/**
* Index for ALL transactions.
*
* @param Request $request
* @param string $objectType
*
* @return Factory|View
2022-03-29 15:10:05 +02:00
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function indexAll(Request $request, string $objectType)
{
2020-05-01 06:24:24 +02:00
$subTitleIcon = config('firefly.transactionIconsByType.' . $objectType);
$types = config('firefly.transactionTypesByType.' . $objectType);
2022-03-29 14:58:06 +02:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
$path = route('transactions.index.all', [$objectType]);
2020-10-24 17:27:36 +02:00
$first = $this->repository->firstNull();
2022-10-30 14:24:28 +01:00
$start = null === $first ? new Carbon() : $first->date;
2020-05-01 06:24:24 +02:00
$last = $this->repository->getLast();
2020-09-11 07:12:33 +02:00
$end = $last ? $last->date : today(config('app.timezone'));
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans('firefly.all_' . $objectType);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)
2019-07-20 06:22:37 +02:00
->setTypes($types)
->setLimit($pageSize)
->setPage($page)
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
->withAttachmentInformation();
$groups = $collector->getPaginatedGroups();
$groups->setPath($path);
return view('transactions.index', compact('subTitle', 'objectType', 'subTitleIcon', 'groups', 'start', 'end'));
2019-04-19 07:00:19 +02:00
}
2019-08-17 12:09:03 +02:00
}