1 | <?php |
||
2 | /** |
||
3 | * RecurrenceController.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\RecurrenceStoreRequest; |
||
27 | use FireflyIII\Api\V1\Requests\RecurrenceUpdateRequest; |
||
28 | use FireflyIII\Exceptions\FireflyException; |
||
29 | use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
||
30 | use FireflyIII\Models\Recurrence; |
||
31 | use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; |
||
32 | use FireflyIII\Support\Cronjobs\RecurringCronjob; |
||
33 | use FireflyIII\Support\Http\Api\TransactionFilter; |
||
34 | use FireflyIII\Transformers\RecurrenceTransformer; |
||
35 | use FireflyIII\Transformers\TransactionGroupTransformer; |
||
36 | use FireflyIII\User; |
||
37 | use Illuminate\Http\JsonResponse; |
||
38 | use Illuminate\Http\Request; |
||
39 | use Illuminate\Pagination\LengthAwarePaginator; |
||
40 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||
41 | use League\Fractal\Resource\Collection as FractalCollection; |
||
42 | use League\Fractal\Resource\Item; |
||
43 | use Log; |
||
44 | |||
45 | /** |
||
46 | * Class RecurrenceController |
||
47 | */ |
||
48 | class RecurrenceController extends Controller |
||
49 | { |
||
50 | use TransactionFilter; |
||
51 | /** @var RecurringRepositoryInterface The recurring transaction repository */ |
||
52 | private $repository; |
||
53 | |||
54 | /** |
||
55 | * RecurrenceController constructor. |
||
56 | * |
||
57 | * @codeCoverageIgnore |
||
58 | */ |
||
59 | public function __construct() |
||
60 | { |
||
61 | parent::__construct(); |
||
62 | $this->middleware( |
||
63 | function ($request, $next) { |
||
64 | /** @var User $user */ |
||
65 | $user = auth()->user(); |
||
66 | |||
67 | /** @var RecurringRepositoryInterface repository */ |
||
68 | $this->repository = app(RecurringRepositoryInterface::class); |
||
69 | $this->repository->setUser($user); |
||
70 | |||
71 | return $next($request); |
||
72 | } |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Delete the resource. |
||
78 | * |
||
79 | * @param Recurrence $recurrence |
||
80 | * |
||
81 | * @return JsonResponse |
||
82 | * @codeCoverageIgnore |
||
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 | * @return JsonResponse |
||
95 | * @codeCoverageIgnore |
||
96 | */ |
||
97 | public function index(): JsonResponse |
||
98 | { |
||
99 | $manager = $this->getManager(); |
||
100 | |||
101 | // types to get, page size: |
||
102 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
103 | |||
104 | // get list of budgets. Count it and split it. |
||
105 | $collection = $this->repository->getAll(); |
||
106 | $count = $collection->count(); |
||
107 | $piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
108 | |||
109 | // make paginator: |
||
110 | $paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page')); |
||
111 | $paginator->setPath(route('api.v1.recurrences.index') . $this->buildParams()); |
||
112 | |||
113 | /** @var RecurrenceTransformer $transformer */ |
||
114 | $transformer = app(RecurrenceTransformer::class); |
||
115 | $transformer->setParameters($this->parameters); |
||
116 | |||
117 | $resource = new FractalCollection($piggyBanks, $transformer, 'recurrences'); |
||
118 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
119 | |||
120 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
121 | |||
122 | } |
||
123 | |||
124 | /** |
||
125 | * List single resource. |
||
126 | * |
||
127 | * @param Recurrence $recurrence |
||
128 | * |
||
129 | * @return JsonResponse |
||
130 | * @codeCoverageIgnore |
||
131 | */ |
||
132 | public function show(Recurrence $recurrence): JsonResponse |
||
133 | { |
||
134 | $manager = $this->getManager(); |
||
135 | |||
136 | /** @var RecurrenceTransformer $transformer */ |
||
137 | $transformer = app(RecurrenceTransformer::class); |
||
138 | $transformer->setParameters($this->parameters); |
||
139 | |||
140 | $resource = new Item($recurrence, $transformer, 'recurrences'); |
||
141 | |||
142 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
143 | |||
144 | |||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Store new object. |
||
149 | * |
||
150 | * @param RecurrenceStoreRequest $request |
||
151 | * |
||
152 | * @throws FireflyException |
||
153 | * @return JsonResponse |
||
154 | */ |
||
155 | public function store(RecurrenceStoreRequest $request): JsonResponse |
||
156 | { |
||
157 | $data = $request->getAll(); |
||
158 | $recurrence = $this->repository->store($data); |
||
159 | $manager = $this->getManager(); |
||
160 | |||
161 | /** @var RecurrenceTransformer $transformer */ |
||
162 | $transformer = app(RecurrenceTransformer::class); |
||
163 | $transformer->setParameters($this->parameters); |
||
164 | |||
165 | $resource = new Item($recurrence, $transformer, 'recurrences'); |
||
166 | |||
167 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Show transactions for this recurrence. |
||
172 | * |
||
173 | * @param Request $request |
||
174 | * @param Recurrence $recurrence |
||
175 | * |
||
176 | * @return JsonResponse |
||
177 | * @codeCoverageIgnore |
||
178 | */ |
||
179 | public function transactions(Request $request, Recurrence $recurrence): JsonResponse |
||
180 | { |
||
181 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
182 | $type = $request->get('type') ?? 'default'; |
||
183 | $this->parameters->set('type', $type); |
||
184 | |||
185 | $types = $this->mapTransactionTypes($this->parameters->get('type')); |
||
186 | $manager = $this->getManager(); |
||
187 | // whatever is returned by the query, it must be part of these journals: |
||
188 | $journalIds = $this->repository->getJournalIds($recurrence); |
||
189 | |||
190 | /** @var User $admin */ |
||
191 | $admin = auth()->user(); |
||
192 | |||
193 | // use new group collector: |
||
194 | /** @var GroupCollectorInterface $collector */ |
||
195 | $collector = app(GroupCollectorInterface::class); |
||
196 | $collector |
||
197 | ->setUser($admin) |
||
198 | // filter on journal IDs. |
||
199 | ->setJournalIds($journalIds) |
||
200 | // all info needed for the API: |
||
201 | ->withAPIInformation() |
||
202 | // set page size: |
||
203 | ->setLimit($pageSize) |
||
204 | // set page to retrieve |
||
205 | ->setPage($this->parameters->get('page')) |
||
206 | // set types of transactions to return. |
||
207 | ->setTypes($types); |
||
208 | |||
209 | if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { |
||
210 | $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
||
211 | } |
||
212 | $paginator = $collector->getPaginatedGroups(); |
||
213 | $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams()); |
||
214 | $transactions = $paginator->getCollection(); |
||
215 | |||
216 | /** @var TransactionGroupTransformer $transformer */ |
||
217 | $transformer = app(TransactionGroupTransformer::class); |
||
218 | $transformer->setParameters($this->parameters); |
||
219 | |||
220 | $resource = new FractalCollection($transactions, $transformer, 'transactions'); |
||
221 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
222 | |||
223 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @throws FireflyException |
||
228 | * @codeCoverageIgnore |
||
229 | * @return JsonResponse |
||
230 | */ |
||
231 | public function trigger(): JsonResponse |
||
232 | { |
||
233 | /** @var RecurringCronjob $recurring */ |
||
234 | $recurring = app(RecurringCronjob::class); |
||
235 | try { |
||
236 | $result = $recurring->fire(); |
||
237 | } catch (FireflyException $e) { |
||
238 | Log::error($e->getMessage()); |
||
239 | throw new FireflyException('200022: Error in cron job.'); |
||
240 | } |
||
241 | if (false === $result) { |
||
242 | return response()->json([], 204); |
||
243 | } |
||
244 | if (true === $result) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
245 | return response()->json(); |
||
246 | } |
||
247 | |||
248 | return response()->json([], 418); // @codeCoverageIgnore |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Update single recurrence. |
||
253 | * |
||
254 | * @param RecurrenceUpdateRequest $request |
||
255 | * @param Recurrence $recurrence |
||
256 | * |
||
257 | * @return JsonResponse |
||
258 | */ |
||
259 | public function update(RecurrenceUpdateRequest $request, Recurrence $recurrence): JsonResponse |
||
260 | { |
||
261 | $data = $request->getAll(); |
||
262 | $category = $this->repository->update($recurrence, $data); |
||
263 | $manager = $this->getManager(); |
||
264 | |||
265 | /** @var RecurrenceTransformer $transformer */ |
||
266 | $transformer = app(RecurrenceTransformer::class); |
||
267 | $transformer->setParameters($this->parameters); |
||
268 | |||
269 | $resource = new Item($category, $transformer, 'recurrences'); |
||
270 | |||
271 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
272 | |||
273 | } |
||
274 | } |
||
275 |