1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RecurrenceController.php |
4
|
|
|
* Copyright (c) 2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III 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 General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Api\V1\Controllers; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Api\V1\Requests\RecurrenceRequest; |
27
|
|
|
use FireflyIII\Exceptions\FireflyException; |
28
|
|
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface; |
29
|
|
|
use FireflyIII\Helpers\Filter\InternalTransferFilter; |
30
|
|
|
use FireflyIII\Models\Recurrence; |
31
|
|
|
use FireflyIII\Models\TransactionType; |
32
|
|
|
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; |
33
|
|
|
use FireflyIII\Support\Cronjobs\RecurringCronjob; |
34
|
|
|
use FireflyIII\Support\Http\Api\TransactionFilter; |
35
|
|
|
use FireflyIII\Transformers\RecurrenceTransformer; |
36
|
|
|
use FireflyIII\Transformers\TransactionTransformer; |
37
|
|
|
use FireflyIII\User; |
38
|
|
|
use Illuminate\Http\JsonResponse; |
39
|
|
|
use Illuminate\Http\Request; |
40
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
41
|
|
|
use League\Fractal\Manager; |
42
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
43
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
44
|
|
|
use League\Fractal\Resource\Item; |
45
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
46
|
|
|
use Log; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class RecurrenceController |
50
|
|
|
*/ |
51
|
|
|
class RecurrenceController extends Controller |
52
|
|
|
{ |
53
|
|
|
use TransactionFilter; |
54
|
|
|
/** @var RecurringRepositoryInterface The recurring transaction repository */ |
55
|
|
|
private $repository; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* RecurrenceController constructor. |
59
|
|
|
*/ |
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
parent::__construct(); |
63
|
|
|
$this->middleware( |
64
|
|
|
function ($request, $next) { |
65
|
|
|
/** @var User $user */ |
66
|
|
|
$user = auth()->user(); |
67
|
|
|
|
68
|
|
|
/** @var RecurringRepositoryInterface repository */ |
69
|
|
|
$this->repository = app(RecurringRepositoryInterface::class); |
70
|
|
|
$this->repository->setUser($user); |
71
|
|
|
|
72
|
|
|
return $next($request); |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Delete the resource. |
79
|
|
|
* |
80
|
|
|
* @param Recurrence $recurrence |
81
|
|
|
* |
82
|
|
|
* @return JsonResponse |
83
|
|
|
*/ |
84
|
|
|
public function delete(Recurrence $recurrence): JsonResponse |
85
|
|
|
{ |
86
|
|
|
$this->repository->destroy($recurrence); |
87
|
|
|
|
88
|
|
|
return response()->json([], 204); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* List all of them. |
93
|
|
|
* |
94
|
|
|
* @param Request $request |
95
|
|
|
* |
96
|
|
|
* @return JsonResponse] |
97
|
|
|
*/ |
98
|
|
|
public function index(Request $request): JsonResponse |
99
|
|
|
{ |
100
|
|
|
// create some objects: |
101
|
|
|
$manager = new Manager; |
102
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
103
|
|
|
|
104
|
|
|
// types to get, page size: |
105
|
|
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
106
|
|
|
|
107
|
|
|
// get list of budgets. Count it and split it. |
108
|
|
|
$collection = $this->repository->getAll(); |
109
|
|
|
$count = $collection->count(); |
110
|
|
|
$piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
111
|
|
|
|
112
|
|
|
// make paginator: |
113
|
|
|
$paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page')); |
114
|
|
|
$paginator->setPath(route('api.v1.recurrences.index') . $this->buildParams()); |
115
|
|
|
|
116
|
|
|
// present to user. |
117
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
118
|
|
|
|
119
|
|
|
/** @var RecurrenceTransformer $transformer */ |
120
|
|
|
$transformer = app(RecurrenceTransformer::class); |
121
|
|
|
$transformer->setParameters($this->parameters); |
122
|
|
|
|
123
|
|
|
$resource = new FractalCollection($piggyBanks, $transformer, 'recurrences'); |
124
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
125
|
|
|
|
126
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* List single resource. |
132
|
|
|
* |
133
|
|
|
* @param Request $request |
134
|
|
|
* @param Recurrence $recurrence |
135
|
|
|
* |
136
|
|
|
* @return JsonResponse |
137
|
|
|
*/ |
138
|
|
|
public function show(Request $request, Recurrence $recurrence): JsonResponse |
139
|
|
|
{ |
140
|
|
|
$manager = new Manager(); |
141
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
142
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
143
|
|
|
|
144
|
|
|
/** @var RecurrenceTransformer $transformer */ |
145
|
|
|
$transformer = app(RecurrenceTransformer::class); |
146
|
|
|
$transformer->setParameters($this->parameters); |
147
|
|
|
|
148
|
|
|
$resource = new Item($recurrence, $transformer, 'recurrences'); |
149
|
|
|
|
150
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Store new object. |
157
|
|
|
* |
158
|
|
|
* @param RecurrenceRequest $request |
159
|
|
|
* |
160
|
|
|
* @return JsonResponse |
161
|
|
|
*/ |
162
|
|
|
public function store(RecurrenceRequest $request): JsonResponse |
163
|
|
|
{ |
164
|
|
|
$recurrence = $this->repository->store($request->getAll()); |
165
|
|
|
$manager = new Manager(); |
166
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
167
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
168
|
|
|
|
169
|
|
|
/** @var RecurrenceTransformer $transformer */ |
170
|
|
|
$transformer = app(RecurrenceTransformer::class); |
171
|
|
|
$transformer->setParameters($this->parameters); |
172
|
|
|
|
173
|
|
|
$resource = new Item($recurrence, $transformer, 'recurrences'); |
174
|
|
|
|
175
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Show transactions for this recurrence. |
180
|
|
|
* |
181
|
|
|
* @param Request $request |
182
|
|
|
* @param Recurrence $recurrence |
183
|
|
|
* |
184
|
|
|
* @return JsonResponse |
185
|
|
|
*/ |
186
|
|
|
public function transactions(Request $request, Recurrence $recurrence): JsonResponse |
187
|
|
|
{ |
188
|
|
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
189
|
|
|
$type = $request->get('type') ?? 'default'; |
190
|
|
|
$this->parameters->set('type', $type); |
191
|
|
|
|
192
|
|
|
$types = $this->mapTransactionTypes($this->parameters->get('type')); |
193
|
|
|
$manager = new Manager(); |
194
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
195
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
196
|
|
|
|
197
|
|
|
// whatever is returned by the query, it must be part of these journals: |
198
|
|
|
$journalIds = $this->repository->getJournalIds($recurrence); |
199
|
|
|
|
200
|
|
|
/** @var User $admin */ |
201
|
|
|
$admin = auth()->user(); |
202
|
|
|
/** @var TransactionCollectorInterface $collector */ |
203
|
|
|
$collector = app(TransactionCollectorInterface::class); |
204
|
|
|
$collector->setUser($admin); |
205
|
|
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); |
206
|
|
|
$collector->setAllAssetAccounts(); |
207
|
|
|
$collector->setJournalIds($journalIds); |
208
|
|
|
|
209
|
|
|
if (\in_array(TransactionType::TRANSFER, $types, true)) { |
210
|
|
|
$collector->removeFilter(InternalTransferFilter::class); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { |
214
|
|
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
215
|
|
|
} |
216
|
|
|
$collector->setLimit($pageSize)->setPage($this->parameters->get('page')); |
217
|
|
|
$collector->setTypes($types); |
218
|
|
|
$paginator = $collector->getPaginatedTransactions(); |
219
|
|
|
$paginator->setPath(route('api.v1.transactions.index') . $this->buildParams()); |
220
|
|
|
$transactions = $paginator->getCollection(); |
221
|
|
|
|
222
|
|
|
/** @var TransactionTransformer $transformer */ |
223
|
|
|
$transformer = app(TransactionTransformer::class); |
224
|
|
|
$transformer->setParameters($this->parameters); |
225
|
|
|
|
226
|
|
|
$resource = new FractalCollection($transactions, $transformer, 'transactions'); |
227
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
228
|
|
|
|
229
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return JsonResponse |
234
|
|
|
* @throws FireflyException |
235
|
|
|
*/ |
236
|
|
|
public function trigger(): JsonResponse |
237
|
|
|
{ |
238
|
|
|
/** @var RecurringCronjob $recurring */ |
239
|
|
|
$recurring = app(RecurringCronjob::class); |
240
|
|
|
try { |
241
|
|
|
$result = $recurring->fire(); |
242
|
|
|
} catch (FireflyException $e) { |
243
|
|
|
Log::error($e->getMessage()); |
244
|
|
|
throw new FireflyException('Could not fire recurring cron job.'); |
245
|
|
|
} |
246
|
|
|
if (false === $result) { |
247
|
|
|
return response()->json([], 204); |
248
|
|
|
} |
249
|
|
|
if (true === $result) { |
|
|
|
|
250
|
|
|
return response()->json(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return response()->json([], 418); // @codeCoverageIgnore |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Update single recurrence. |
258
|
|
|
* |
259
|
|
|
* @param RecurrenceRequest $request |
260
|
|
|
* @param Recurrence $recurrence |
261
|
|
|
* |
262
|
|
|
* @return JsonResponse |
263
|
|
|
*/ |
264
|
|
|
public function update(RecurrenceRequest $request, Recurrence $recurrence): JsonResponse |
265
|
|
|
{ |
266
|
|
|
$data = $request->getAll(); |
267
|
|
|
$category = $this->repository->update($recurrence, $data); |
268
|
|
|
$manager = new Manager(); |
269
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
270
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
271
|
|
|
/** @var RecurrenceTransformer $transformer */ |
272
|
|
|
$transformer = app(RecurrenceTransformer::class); |
273
|
|
|
$transformer->setParameters($this->parameters); |
274
|
|
|
|
275
|
|
|
$resource = new Item($category, $transformer, 'recurrences'); |
276
|
|
|
|
277
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|