1 | <?php |
||
2 | /** |
||
3 | * BudgetController.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 Exception; |
||
27 | use FireflyIII\Api\V1\Requests\BudgetLimitRequest; |
||
28 | use FireflyIII\Api\V1\Requests\BudgetStoreRequest; |
||
29 | use FireflyIII\Api\V1\Requests\BudgetUpdateRequest; |
||
30 | use FireflyIII\Exceptions\FireflyException; |
||
31 | use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
||
32 | use FireflyIII\Models\Budget; |
||
33 | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; |
||
34 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||
35 | use FireflyIII\Support\Http\Api\TransactionFilter; |
||
36 | use FireflyIII\Transformers\AttachmentTransformer; |
||
37 | use FireflyIII\Transformers\BudgetLimitTransformer; |
||
38 | use FireflyIII\Transformers\BudgetTransformer; |
||
39 | use FireflyIII\Transformers\TransactionGroupTransformer; |
||
40 | use FireflyIII\User; |
||
41 | use Illuminate\Http\JsonResponse; |
||
42 | use Illuminate\Http\Request; |
||
43 | use Illuminate\Pagination\LengthAwarePaginator; |
||
44 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||
45 | use League\Fractal\Resource\Collection as FractalCollection; |
||
46 | use League\Fractal\Resource\Item; |
||
47 | |||
48 | /** |
||
49 | * Class BudgetController. |
||
50 | * |
||
51 | */ |
||
52 | class BudgetController extends Controller |
||
53 | { |
||
54 | use TransactionFilter; |
||
55 | /** @var BudgetLimitRepositoryInterface */ |
||
56 | private $blRepository; |
||
57 | /** @var BudgetRepositoryInterface The budget repository */ |
||
58 | private $repository; |
||
59 | |||
60 | /** |
||
61 | * BudgetController constructor. |
||
62 | * |
||
63 | * @codeCoverageIgnore |
||
64 | */ |
||
65 | public function __construct() |
||
66 | { |
||
67 | parent::__construct(); |
||
68 | $this->middleware( |
||
69 | function ($request, $next) { |
||
70 | /** @var User $admin */ |
||
71 | $admin = auth()->user(); |
||
72 | |||
73 | $this->repository = app(BudgetRepositoryInterface::class); |
||
74 | $this->blRepository = app(BudgetLimitRepositoryInterface::class); |
||
75 | $this->repository->setUser($admin); |
||
76 | $this->blRepository->setUser($admin); |
||
77 | |||
78 | return $next($request); |
||
79 | } |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Display a listing of the resource. |
||
85 | * |
||
86 | * @param Budget $budget |
||
87 | * |
||
88 | * @return JsonResponse |
||
89 | * @codeCoverageIgnore |
||
90 | */ |
||
91 | public function budgetLimits(Budget $budget): JsonResponse |
||
92 | { |
||
93 | $manager = $this->getManager(); |
||
94 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
95 | $this->parameters->set('budget_id', $budget->id); |
||
96 | $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end')); |
||
97 | $count = $collection->count(); |
||
98 | $budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
99 | $paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page')); |
||
100 | $paginator->setPath(route('api.v1.budgets.budget_limits', [$budget->id]) . $this->buildParams()); |
||
101 | |||
102 | /** @var BudgetLimitTransformer $transformer */ |
||
103 | $transformer = app(BudgetLimitTransformer::class); |
||
104 | $transformer->setParameters($this->parameters); |
||
105 | |||
106 | |||
107 | $resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits'); |
||
108 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
109 | |||
110 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param Budget $budget |
||
115 | * |
||
116 | * @return JsonResponse |
||
117 | * @codeCoverageIgnore |
||
118 | */ |
||
119 | public function attachments(Budget $budget): JsonResponse |
||
120 | { |
||
121 | $manager = $this->getManager(); |
||
122 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
123 | $collection = $this->repository->getAttachments($budget); |
||
124 | |||
125 | $count = $collection->count(); |
||
126 | $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
127 | |||
128 | // make paginator: |
||
129 | $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); |
||
130 | $paginator->setPath(route('api.v1.budgets.attachments', [$budget->id]) . $this->buildParams()); |
||
131 | |||
132 | /** @var AttachmentTransformer $transformer */ |
||
133 | $transformer = app(AttachmentTransformer::class); |
||
134 | $transformer->setParameters($this->parameters); |
||
135 | |||
136 | $resource = new FractalCollection($attachments, $transformer, 'attachments'); |
||
137 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
138 | |||
139 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Remove the specified resource from storage. |
||
144 | * |
||
145 | * @param Budget $budget |
||
146 | * |
||
147 | * @return JsonResponse |
||
148 | * @codeCoverageIgnore |
||
149 | */ |
||
150 | public function delete(Budget $budget): JsonResponse |
||
151 | { |
||
152 | $this->repository->destroy($budget); |
||
153 | |||
154 | return response()->json([], 204); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Display a listing of the resource. |
||
159 | * |
||
160 | * @return JsonResponse |
||
161 | * @codeCoverageIgnore |
||
162 | */ |
||
163 | public function index(): JsonResponse |
||
164 | { |
||
165 | $manager = $this->getManager(); |
||
166 | |||
167 | // types to get, page size: |
||
168 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
169 | |||
170 | // get list of budgets. Count it and split it. |
||
171 | $collection = $this->repository->getBudgets(); |
||
172 | $count = $collection->count(); |
||
173 | $budgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
174 | |||
175 | // make paginator: |
||
176 | $paginator = new LengthAwarePaginator($budgets, $count, $pageSize, $this->parameters->get('page')); |
||
177 | $paginator->setPath(route('api.v1.budgets.index') . $this->buildParams()); |
||
178 | |||
179 | /** @var BudgetTransformer $transformer */ |
||
180 | $transformer = app(BudgetTransformer::class); |
||
181 | $transformer->setParameters($this->parameters); |
||
182 | |||
183 | $resource = new FractalCollection($budgets, $transformer, 'budgets'); |
||
184 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
185 | |||
186 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Show a budget. |
||
191 | * |
||
192 | * @param Budget $budget |
||
193 | * |
||
194 | * @return JsonResponse |
||
195 | * @codeCoverageIgnore |
||
196 | */ |
||
197 | public function show(Budget $budget): JsonResponse |
||
198 | { |
||
199 | $manager = $this->getManager(); |
||
200 | |||
201 | /** @var BudgetTransformer $transformer */ |
||
202 | $transformer = app(BudgetTransformer::class); |
||
203 | $transformer->setParameters($this->parameters); |
||
204 | |||
205 | $resource = new Item($budget, $transformer, 'budgets'); |
||
206 | |||
207 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Store a budget. |
||
212 | * |
||
213 | * @param BudgetStoreRequest $request |
||
214 | * |
||
215 | * @throws FireflyException |
||
216 | * |
||
217 | * @return JsonResponse |
||
218 | */ |
||
219 | public function store(BudgetStoreRequest $request): JsonResponse |
||
220 | { |
||
221 | $budget = $this->repository->store($request->getAll()); |
||
222 | $manager = $this->getManager(); |
||
223 | |||
224 | /** @var BudgetTransformer $transformer */ |
||
225 | $transformer = app(BudgetTransformer::class); |
||
226 | $transformer->setParameters($this->parameters); |
||
227 | |||
228 | $resource = new Item($budget, $transformer, 'budgets'); |
||
229 | |||
230 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Store a newly created resource in storage. |
||
235 | * |
||
236 | * @param BudgetLimitRequest $request |
||
237 | * @param Budget $budget |
||
238 | * |
||
239 | * @throws Exception |
||
240 | * @return JsonResponse |
||
241 | */ |
||
242 | public function storeBudgetLimit(BudgetLimitRequest $request, Budget $budget): JsonResponse |
||
243 | { |
||
244 | $data = $request->getAll(); |
||
245 | $data['budget'] = $budget; |
||
246 | $budgetLimit = $this->blRepository->storeBudgetLimit($data); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
247 | $manager = $this->getManager(); |
||
248 | |||
249 | /** @var BudgetLimitTransformer $transformer */ |
||
250 | $transformer = app(BudgetLimitTransformer::class); |
||
251 | $transformer->setParameters($this->parameters); |
||
252 | |||
253 | $resource = new Item($budgetLimit, $transformer, 'budget_limits'); |
||
254 | |||
255 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Show all transactions. |
||
260 | * |
||
261 | * @param Request $request |
||
262 | * |
||
263 | * @param Budget $budget |
||
264 | * |
||
265 | * @return JsonResponse |
||
266 | * @codeCoverageIgnore |
||
267 | */ |
||
268 | public function transactions(Request $request, Budget $budget): JsonResponse |
||
269 | { |
||
270 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
271 | |||
272 | // user can overrule page size with limit parameter. |
||
273 | $limit = $this->parameters->get('limit'); |
||
274 | if (null !== $limit && $limit > 0) { |
||
275 | $pageSize = $limit; |
||
276 | } |
||
277 | |||
278 | $type = $request->get('type') ?? 'default'; |
||
279 | $this->parameters->set('type', $type); |
||
280 | |||
281 | $types = $this->mapTransactionTypes($this->parameters->get('type')); |
||
282 | $manager = $this->getManager(); |
||
283 | |||
284 | /** @var User $admin */ |
||
285 | $admin = auth()->user(); |
||
286 | |||
287 | // use new group collector: |
||
288 | /** @var GroupCollectorInterface $collector */ |
||
289 | $collector = app(GroupCollectorInterface::class); |
||
290 | $collector |
||
291 | ->setUser($admin) |
||
292 | // filter on budget. |
||
293 | ->setBudget($budget) |
||
294 | // all info needed for the API: |
||
295 | ->withAPIInformation() |
||
296 | // set page size: |
||
297 | ->setLimit($pageSize) |
||
298 | // set page to retrieve |
||
299 | ->setPage($this->parameters->get('page')) |
||
300 | // set types of transactions to return. |
||
301 | ->setTypes($types); |
||
302 | |||
303 | if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { |
||
304 | $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
||
305 | } |
||
306 | |||
307 | $paginator = $collector->getPaginatedGroups(); |
||
308 | $paginator->setPath(route('api.v1.budgets.transactions', [$budget->id]) . $this->buildParams()); |
||
309 | $transactions = $paginator->getCollection(); |
||
310 | |||
311 | /** @var TransactionGroupTransformer $transformer */ |
||
312 | $transformer = app(TransactionGroupTransformer::class); |
||
313 | $transformer->setParameters($this->parameters); |
||
314 | |||
315 | |||
316 | $resource = new FractalCollection($transactions, $transformer, 'transactions'); |
||
317 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
318 | |||
319 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Update a budget. |
||
324 | * |
||
325 | * @param BudgetUpdateRequest $request |
||
326 | * @param Budget $budget |
||
327 | * |
||
328 | * @return JsonResponse |
||
329 | */ |
||
330 | public function update(BudgetUpdateRequest $request, Budget $budget): JsonResponse |
||
331 | { |
||
332 | $data = $request->getAll(); |
||
333 | $budget = $this->repository->update($budget, $data); |
||
334 | $manager = $this->getManager(); |
||
335 | |||
336 | /** @var BudgetTransformer $transformer */ |
||
337 | $transformer = app(BudgetTransformer::class); |
||
338 | $transformer->setParameters($this->parameters); |
||
339 | |||
340 | $resource = new Item($budget, $transformer, 'budgets'); |
||
341 | |||
342 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
343 | |||
344 | } |
||
345 | |||
346 | } |
||
347 |