1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AccountController.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
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Api\V1\Controllers; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Api\V1\Requests\AccountStoreRequest; |
27
|
|
|
use FireflyIII\Api\V1\Requests\AccountUpdateRequest; |
28
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
29
|
|
|
use FireflyIII\Models\Account; |
30
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
31
|
|
|
use FireflyIII\Support\Http\Api\AccountFilter; |
32
|
|
|
use FireflyIII\Support\Http\Api\TransactionFilter; |
33
|
|
|
use FireflyIII\Transformers\AccountTransformer; |
34
|
|
|
use FireflyIII\Transformers\AttachmentTransformer; |
35
|
|
|
use FireflyIII\Transformers\PiggyBankTransformer; |
36
|
|
|
use FireflyIII\Transformers\TransactionGroupTransformer; |
37
|
|
|
use FireflyIII\User; |
38
|
|
|
use Illuminate\Http\JsonResponse; |
39
|
|
|
use Illuminate\Http\Request; |
40
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
41
|
|
|
use Illuminate\Support\Collection; |
42
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
43
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
44
|
|
|
use League\Fractal\Resource\Item; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class AccountController. |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
class AccountController extends Controller |
51
|
|
|
{ |
52
|
|
|
use AccountFilter, TransactionFilter; |
53
|
|
|
/** @var AccountRepositoryInterface The account repository */ |
54
|
|
|
private $repository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* AccountController constructor. |
58
|
|
|
* |
59
|
|
|
* @codeCoverageIgnore |
60
|
|
|
*/ |
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
parent::__construct(); |
64
|
|
|
$this->middleware( |
65
|
|
|
function ($request, $next) { |
66
|
|
|
/** @var User $user */ |
67
|
|
|
$user = auth()->user(); |
68
|
|
|
// @var AccountRepositoryInterface repository |
69
|
|
|
$this->repository = app(AccountRepositoryInterface::class); |
70
|
|
|
$this->repository->setUser($user); |
71
|
|
|
|
72
|
|
|
return $next($request); |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Account $account |
79
|
|
|
* |
80
|
|
|
* @return JsonResponse |
81
|
|
|
* @codeCoverageIgnore |
82
|
|
|
*/ |
83
|
|
|
public function attachments(Account $account): JsonResponse |
84
|
|
|
{ |
85
|
|
|
$manager = $this->getManager(); |
86
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
87
|
|
|
$collection = $this->repository->getAttachments($account); |
88
|
|
|
|
89
|
|
|
$count = $collection->count(); |
90
|
|
|
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
91
|
|
|
|
92
|
|
|
// make paginator: |
93
|
|
|
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); |
94
|
|
|
$paginator->setPath(route('api.v1.accounts.attachments', [$account->id]) . $this->buildParams()); |
95
|
|
|
|
96
|
|
|
/** @var AttachmentTransformer $transformer */ |
97
|
|
|
$transformer = app(AttachmentTransformer::class); |
98
|
|
|
$transformer->setParameters($this->parameters); |
99
|
|
|
|
100
|
|
|
$resource = new FractalCollection($attachments, $transformer, 'attachments'); |
101
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
102
|
|
|
|
103
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove the specified resource from storage. |
108
|
|
|
* |
109
|
|
|
* @param Account $account |
110
|
|
|
* |
111
|
|
|
* @codeCoverageIgnore |
112
|
|
|
* @return JsonResponse |
113
|
|
|
*/ |
114
|
|
|
public function delete(Account $account): JsonResponse |
115
|
|
|
{ |
116
|
|
|
$this->repository->destroy($account, null); |
117
|
|
|
|
118
|
|
|
return response()->json([], 204); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Display a listing of the resource. |
123
|
|
|
* |
124
|
|
|
* @param Request $request |
125
|
|
|
* |
126
|
|
|
* @codeCoverageIgnore |
127
|
|
|
* @return JsonResponse |
128
|
|
|
*/ |
129
|
|
|
public function index(Request $request): JsonResponse |
130
|
|
|
{ |
131
|
|
|
$manager = $this->getManager(); |
132
|
|
|
$type = $request->get('type') ?? 'all'; |
133
|
|
|
$this->parameters->set('type', $type); |
134
|
|
|
|
135
|
|
|
// types to get, page size: |
136
|
|
|
$types = $this->mapAccountTypes($this->parameters->get('type')); |
137
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
138
|
|
|
|
139
|
|
|
// get list of accounts. Count it and split it. |
140
|
|
|
$collection = $this->repository->getAccountsByType($types); |
141
|
|
|
$count = $collection->count(); |
142
|
|
|
$accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
143
|
|
|
|
144
|
|
|
// make paginator: |
145
|
|
|
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); |
146
|
|
|
$paginator->setPath(route('api.v1.accounts.index') . $this->buildParams()); |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** @var AccountTransformer $transformer */ |
150
|
|
|
$transformer = app(AccountTransformer::class); |
151
|
|
|
$transformer->setParameters($this->parameters); |
152
|
|
|
|
153
|
|
|
$resource = new FractalCollection($accounts, $transformer, 'accounts'); |
154
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
155
|
|
|
|
156
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* List all piggies. |
162
|
|
|
* |
163
|
|
|
* @param Account $account |
164
|
|
|
* |
165
|
|
|
* @return JsonResponse |
166
|
|
|
* @codeCoverageIgnore |
167
|
|
|
* |
168
|
|
|
*/ |
169
|
|
|
public function piggyBanks(Account $account): JsonResponse |
170
|
|
|
{ |
171
|
|
|
// create some objects: |
172
|
|
|
$manager = $this->getManager(); |
173
|
|
|
|
174
|
|
|
// types to get, page size: |
175
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
176
|
|
|
|
177
|
|
|
// get list of budgets. Count it and split it. |
178
|
|
|
$collection = $this->repository->getPiggyBanks($account); |
179
|
|
|
$count = $collection->count(); |
180
|
|
|
$piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
181
|
|
|
|
182
|
|
|
// make paginator: |
183
|
|
|
$paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page')); |
184
|
|
|
$paginator->setPath(route('api.v1.accounts.piggy_banks', [$account->id]) . $this->buildParams()); |
185
|
|
|
|
186
|
|
|
/** @var PiggyBankTransformer $transformer */ |
187
|
|
|
$transformer = app(PiggyBankTransformer::class); |
188
|
|
|
$transformer->setParameters($this->parameters); |
189
|
|
|
|
190
|
|
|
$resource = new FractalCollection($piggyBanks, $transformer, 'piggy_banks'); |
191
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
192
|
|
|
|
193
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Show single instance. |
199
|
|
|
* |
200
|
|
|
* @param Account $account |
201
|
|
|
* |
202
|
|
|
* @return JsonResponse |
203
|
|
|
*/ |
204
|
|
|
public function show(Account $account): JsonResponse |
205
|
|
|
{ |
206
|
|
|
$manager = $this->getManager(); |
207
|
|
|
|
208
|
|
|
/** @var AccountTransformer $transformer */ |
209
|
|
|
$transformer = app(AccountTransformer::class); |
210
|
|
|
$transformer->setParameters($this->parameters); |
211
|
|
|
$resource = new Item($account, $transformer, 'accounts'); |
212
|
|
|
|
213
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Store a new instance. |
218
|
|
|
* |
219
|
|
|
* @param AccountStoreRequest $request |
220
|
|
|
* |
221
|
|
|
* @return JsonResponse |
222
|
|
|
*/ |
223
|
|
|
public function store(AccountStoreRequest $request): JsonResponse |
224
|
|
|
{ |
225
|
|
|
$data = $request->getAllAccountData(); |
226
|
|
|
$account = $this->repository->store($data); |
227
|
|
|
$manager = $this->getManager(); |
228
|
|
|
|
229
|
|
|
/** @var AccountTransformer $transformer */ |
230
|
|
|
$transformer = app(AccountTransformer::class); |
231
|
|
|
$transformer->setParameters($this->parameters); |
232
|
|
|
|
233
|
|
|
$resource = new Item($account, $transformer, 'accounts'); |
234
|
|
|
|
235
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Show all transaction groups related to the account. |
240
|
|
|
* |
241
|
|
|
* @codeCoverageIgnore |
242
|
|
|
* |
243
|
|
|
* @param Request $request |
244
|
|
|
* @param Account $account |
245
|
|
|
* |
246
|
|
|
* @return JsonResponse |
247
|
|
|
* |
248
|
|
|
*/ |
249
|
|
|
public function transactions(Request $request, Account $account): JsonResponse |
250
|
|
|
{ |
251
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
252
|
|
|
$type = $request->get('type') ?? 'default'; |
253
|
|
|
$this->parameters->set('type', $type); |
254
|
|
|
|
255
|
|
|
// user can overrule page size with limit parameter. |
256
|
|
|
$limit = $this->parameters->get('limit'); |
257
|
|
|
if (null !== $limit && $limit > 0) { |
258
|
|
|
$pageSize = $limit; |
259
|
|
|
} |
260
|
|
|
$types = $this->mapTransactionTypes($this->parameters->get('type')); |
261
|
|
|
$manager = $this->getManager(); |
262
|
|
|
/** @var User $admin */ |
263
|
|
|
$admin = auth()->user(); |
264
|
|
|
|
265
|
|
|
// use new group collector: |
266
|
|
|
/** @var GroupCollectorInterface $collector */ |
267
|
|
|
$collector = app(GroupCollectorInterface::class); |
268
|
|
|
$collector->setUser($admin)->setAccounts(new Collection([$account])) |
269
|
|
|
->withAPIInformation()->setLimit($pageSize)->setPage($this->parameters->get('page'))->setTypes($types); |
270
|
|
|
|
271
|
|
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { |
272
|
|
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$paginator = $collector->getPaginatedGroups(); |
276
|
|
|
$paginator->setPath(route('api.v1.accounts.transactions', [$account->id]) . $this->buildParams()); |
277
|
|
|
$groups = $paginator->getCollection(); |
278
|
|
|
|
279
|
|
|
/** @var TransactionGroupTransformer $transformer */ |
280
|
|
|
$transformer = app(TransactionGroupTransformer::class); |
281
|
|
|
$transformer->setParameters($this->parameters); |
282
|
|
|
|
283
|
|
|
$resource = new FractalCollection($groups, $transformer, 'transactions'); |
284
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
285
|
|
|
|
286
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Update account. |
291
|
|
|
* |
292
|
|
|
* @param AccountUpdateRequest $request |
293
|
|
|
* @param Account $account |
294
|
|
|
* |
295
|
|
|
* @return JsonResponse |
296
|
|
|
*/ |
297
|
|
|
public function update(AccountUpdateRequest $request, Account $account): JsonResponse |
298
|
|
|
{ |
299
|
|
|
$data = $request->getUpdateData(); |
300
|
|
|
$data['type'] = config('firefly.shortNamesByFullName.' . $account->accountType->type); |
301
|
|
|
$this->repository->update($account, $data); |
302
|
|
|
$manager = $this->getManager(); |
303
|
|
|
|
304
|
|
|
/** @var AccountTransformer $transformer */ |
305
|
|
|
$transformer = app(AccountTransformer::class); |
306
|
|
|
$transformer->setParameters($this->parameters); |
307
|
|
|
$resource = new Item($account, $transformer, 'accounts'); |
308
|
|
|
|
309
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|