1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* IndexController.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Http\Controllers\Recurring; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use Carbon\Carbon; |
28
|
|
|
use FireflyIII\Http\Controllers\Controller; |
29
|
|
|
use FireflyIII\Models\Recurrence; |
30
|
|
|
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; |
31
|
|
|
use FireflyIII\Support\Http\Controllers\GetConfigurationData; |
32
|
|
|
use FireflyIII\Transformers\RecurrenceTransformer; |
33
|
|
|
use Illuminate\Http\Request; |
34
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
35
|
|
|
use Illuminate\Support\Collection; |
36
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* Class IndexController |
41
|
|
|
*/ |
42
|
|
|
class IndexController extends Controller |
43
|
|
|
{ |
44
|
|
|
use GetConfigurationData; |
|
|
|
|
45
|
|
|
/** @var RecurringRepositoryInterface Recurring repository */ |
46
|
|
|
private $recurring; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* IndexController constructor. |
50
|
|
|
* |
51
|
|
|
* @codeCoverageIgnore |
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
parent::__construct(); |
56
|
|
|
|
57
|
|
|
// translations: |
58
|
|
|
$this->middleware( |
59
|
|
|
function ($request, $next) { |
60
|
|
|
app('view')->share('mainTitleIcon', 'fa-paint-brush'); |
61
|
|
|
app('view')->share('title', (string)trans('firefly.recurrences')); |
62
|
|
|
|
63
|
|
|
$this->recurring = app(RecurringRepositoryInterface::class); |
64
|
|
|
|
65
|
|
|
return $next($request); |
66
|
|
|
} |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* TODO the notes of a recurrence are pretty pointless at this moment. |
72
|
|
|
* Show all recurring transactions. |
73
|
|
|
* |
74
|
|
|
* @param Request $request |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
77
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
|
|
public function index(Request $request) |
81
|
|
|
{ |
82
|
|
|
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); |
83
|
|
|
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data; |
84
|
|
|
$collection = $this->recurring->get(); |
85
|
|
|
|
86
|
|
|
// split collection |
87
|
|
|
$total = $collection->count(); |
88
|
|
|
/** @var Collection $recurrences */ |
89
|
|
|
$recurrences = $collection->slice(($page - 1) * $pageSize, $pageSize); |
90
|
|
|
|
91
|
|
|
/** @var RecurrenceTransformer $transformer */ |
92
|
|
|
$transformer = app(RecurrenceTransformer::class); |
93
|
|
|
$transformer->setParameters(new ParameterBag); |
94
|
|
|
|
95
|
|
|
$recurring = []; |
96
|
|
|
/** @var Recurrence $recurrence */ |
97
|
|
|
foreach ($recurrences as $recurrence) { |
98
|
|
|
$today = new Carbon; |
99
|
|
|
$year = new Carbon; |
100
|
|
|
$year->addYear(); |
101
|
|
|
if($recurrence->first_date > $today) { |
102
|
|
|
$today =clone $recurrence->first_date; |
103
|
|
|
$year = clone $today; |
104
|
|
|
$year->addYear(); |
105
|
|
|
} |
106
|
|
|
$array = $transformer->transform($recurrence); |
107
|
|
|
$array['first_date'] = new Carbon($array['first_date']); |
108
|
|
|
$array['repeat_until'] = null === $array['repeat_until'] ? null : new Carbon($array['repeat_until']); |
109
|
|
|
$array['latest_date'] = null === $array['latest_date'] ? null : new Carbon($array['latest_date']); |
110
|
|
|
$array['occurrences'] = array_slice($this->recurring->getOccurrencesInRange($recurrence->recurrenceRepetitions->first(), $today, $year),0,1); |
111
|
|
|
$recurring[] = $array; |
112
|
|
|
} |
113
|
|
|
$paginator = new LengthAwarePaginator($recurring, $total, $pageSize, $page); |
114
|
|
|
$paginator->setPath(route('recurring.index')); |
115
|
|
|
|
116
|
|
|
$this->verifyRecurringCronJob(); |
117
|
|
|
|
118
|
|
|
return view('recurring.index', compact('paginator', 'page', 'pageSize', 'total')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|