firefly-iii/app/Http/Controllers/Recurring/IndexController.php

133 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* IndexController.php
2020-01-31 00:32:04 -06: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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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-08 00:59:58 -05:00
/** @noinspection PhpMethodParametersCountMismatchInspection */
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Recurring;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Recurrence;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
2018-08-13 12:07:46 -05:00
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use FireflyIII\Transformers\RecurrenceTransformer;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
*
* Class IndexController
*/
class IndexController extends Controller
{
2018-08-13 12:07:46 -05:00
use GetConfigurationData;
2020-10-12 23:35:33 -05:00
private RecurringRepositoryInterface $recurringRepos;
/**
2018-07-21 01:06:24 -05:00
* IndexController constructor.
2019-09-04 02:45:02 -05:00
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-paint-brush');
app('view')->share('title', (string) trans('firefly.recurrences'));
2020-10-12 23:35:33 -05:00
$this->recurringRepos = app(RecurringRepositoryInterface::class);
return $next($request);
}
);
}
/**
* TODO the notes of a recurrence are pretty pointless at this moment.
2018-07-21 01:06:24 -05:00
* Show all recurring transactions.
*
* @param Request $request
*
* @throws FireflyException
2019-08-17 03:47:10 -05:00
*
* @return Factory|View
*/
public function index(Request $request)
{
$page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2020-10-12 23:35:33 -05:00
$collection = $this->recurringRepos->get();
2020-09-11 00:12:33 -05:00
$today = today(config('app.timezone'));
$year = today(config('app.timezone'));
// split collection
$total = $collection->count();
$recurrences = $collection->slice(($page - 1) * $pageSize, $pageSize);
2018-12-17 00:09:44 -06:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters(new ParameterBag);
2019-02-13 10:38:41 -06:00
$recurring = [];
/** @var Recurrence $recurrence */
foreach ($recurrences as $recurrence) {
2019-12-20 04:57:57 -06:00
$year->addYear();
if ($recurrence->first_date > $today) {
$today = clone $recurrence->first_date;
$year = clone $today;
2019-12-20 04:57:57 -06:00
$year->addYear();
}
$array = $transformer->transform($recurrence);
$array['first_date'] = new Carbon($array['first_date']);
$array['repeat_until'] = null === $array['repeat_until'] ? null : new Carbon($array['repeat_until']);
$array['latest_date'] = null === $array['latest_date'] ? null : new Carbon($array['latest_date']);
2020-06-12 13:56:58 -05:00
// lazy but OK
$array['attachments'] = $recurrence->attachments()->count();
2020-04-21 23:09:29 -05:00
// make carbon objects out of occurrences
foreach ($array['repetitions'] as $repIndex => $repetition) {
foreach ($repetition['occurrences'] as $occIndex => $occurrence) {
$array['repetitions'][$repIndex]['occurrences'][$occIndex] = new Carbon($occurrence);
}
2020-04-21 23:09:29 -05:00
}
$recurring[] = $array;
}
$paginator = new LengthAwarePaginator($recurring, $total, $pageSize, $page);
$paginator->setPath(route('recurring.index'));
2018-08-13 12:07:46 -05:00
$this->verifyRecurringCronJob();
return view('recurring.index', compact('paginator', 'today', 'page', 'pageSize', 'total'));
}
}