1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * AccountController.php |
||
5 | * Copyright (c) 2018 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III. |
||
8 | * |
||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation, either version 3 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * Firefly III is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License |
||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | |||
24 | namespace FireflyIII\Api\V1\Controllers; |
||
25 | |||
26 | use FireflyIII\Api\V1\Requests\AccountRequest; |
||
27 | use FireflyIII\Models\Account; |
||
28 | use FireflyIII\Models\AccountType; |
||
29 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
30 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||
31 | use FireflyIII\Transformers\AccountTransformer; |
||
32 | use Illuminate\Http\Request; |
||
33 | use Illuminate\Pagination\LengthAwarePaginator; |
||
34 | use League\Fractal\Manager; |
||
35 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||
36 | use League\Fractal\Resource\Collection as FractalCollection; |
||
37 | use League\Fractal\Resource\Item; |
||
38 | use League\Fractal\Serializer\JsonApiSerializer; |
||
39 | use Preferences; |
||
40 | |||
41 | /** |
||
42 | * Class AccountController |
||
43 | */ |
||
44 | class AccountController extends Controller |
||
45 | { |
||
46 | /** @var CurrencyRepositoryInterface */ |
||
47 | private $currencyRepository; |
||
48 | /** @var AccountRepositoryInterface */ |
||
49 | private $repository; |
||
50 | |||
51 | /** |
||
52 | * AccountController constructor. |
||
53 | * |
||
54 | * @throws \FireflyIII\Exceptions\FireflyException |
||
55 | */ |
||
56 | public function __construct() |
||
57 | { |
||
58 | parent::__construct(); |
||
59 | $this->middleware( |
||
60 | function ($request, $next) { |
||
61 | // @var AccountRepositoryInterface repository |
||
62 | $this->repository = app(AccountRepositoryInterface::class); |
||
63 | $this->repository->setUser(auth()->user()); |
||
64 | |||
65 | $this->currencyRepository = app(CurrencyRepositoryInterface::class); |
||
66 | $this->currencyRepository->setUser(auth()->user()); |
||
67 | |||
68 | return $next($request); |
||
69 | } |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Remove the specified resource from storage. |
||
75 | * |
||
76 | * @param \FireflyIII\Models\Account $account |
||
77 | * |
||
78 | * @return \Illuminate\Http\Response |
||
79 | */ |
||
80 | public function delete(Account $account) |
||
81 | { |
||
82 | $this->repository->destroy($account, null); |
||
83 | |||
84 | return response()->json([], 204); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Display a listing of the resource. |
||
89 | * |
||
90 | * @param Request $request |
||
91 | * |
||
92 | * @return \Illuminate\Http\JsonResponse |
||
93 | */ |
||
94 | public function index(Request $request) |
||
95 | { |
||
96 | // create some objects: |
||
97 | $manager = new Manager(); |
||
98 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
99 | |||
100 | // read type from URI |
||
101 | $type = $request->get('type') ?? 'all'; |
||
102 | $this->parameters->set('type', $type); |
||
103 | |||
104 | // types to get, page size: |
||
105 | $types = $this->mapTypes($this->parameters->get('type')); |
||
106 | $pageSize = (int)Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
107 | |||
108 | // get list of accounts. Count it and split it. |
||
109 | $collection = $this->repository->getAccountsByType($types); |
||
110 | $count = $collection->count(); |
||
111 | $accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
112 | |||
113 | // make paginator: |
||
114 | $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); |
||
115 | $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams()); |
||
116 | |||
117 | // present to user. |
||
118 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
119 | $resource = new FractalCollection($accounts, new AccountTransformer($this->parameters), 'accounts'); |
||
120 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
121 | |||
122 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param Request $request |
||
127 | * @param Account $account |
||
128 | * |
||
129 | * @return \Illuminate\Http\JsonResponse |
||
130 | */ |
||
131 | public function show(Request $request, Account $account) |
||
132 | { |
||
133 | $manager = new Manager(); |
||
134 | |||
135 | // add include parameter: |
||
136 | $include = $request->get('include') ?? ''; |
||
137 | $manager->parseIncludes($include); |
||
138 | |||
139 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
140 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
141 | $resource = new Item($account, new AccountTransformer($this->parameters), 'accounts'); |
||
142 | |||
143 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param AccountRequest $request |
||
148 | * |
||
149 | * @return \Illuminate\Http\JsonResponse |
||
150 | */ |
||
151 | public function store(AccountRequest $request) |
||
152 | { |
||
153 | $data = $request->getAll(); |
||
154 | // if currency ID is 0, find the currency by the code: |
||
155 | if (0 === $data['currency_id']) { |
||
156 | $currency = $this->currencyRepository->findByCodeNull($data['currency_code']); |
||
157 | $data['currency_id'] = null === $currency ? 0 : $currency->id; |
||
158 | } |
||
159 | $account = $this->repository->store($data); |
||
160 | $manager = new Manager(); |
||
161 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
162 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
163 | |||
164 | $resource = new Item($account, new AccountTransformer($this->parameters), 'accounts'); |
||
165 | |||
166 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Update account. |
||
171 | * |
||
172 | * @param AccountRequest $request |
||
173 | * @param Account $account |
||
174 | * |
||
175 | * @return \Illuminate\Http\JsonResponse |
||
176 | */ |
||
177 | public function update(AccountRequest $request, Account $account) |
||
178 | { |
||
179 | $data = $request->getAll(); |
||
180 | // if currency ID is 0, find the currency by the code: |
||
181 | if (0 === $data['currency_id']) { |
||
182 | $currency = $this->currencyRepository->findByCodeNull($data['currency_code']); |
||
183 | $data['currency_id'] = null === $currency ? 0 : $currency->id; |
||
184 | } |
||
185 | // set correct type: |
||
186 | $data['type'] = config('firefly.shortNamesByFullName.' . $account->accountType->type); |
||
0 ignored issues
–
show
|
|||
187 | $this->repository->update($account, $data); |
||
188 | $manager = new Manager(); |
||
189 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
190 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
191 | |||
192 | $resource = new Item($account, new AccountTransformer($this->parameters), 'accounts'); |
||
193 | |||
194 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param string $type |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | private function mapTypes(string $type): array |
||
203 | { |
||
204 | $types = [ |
||
205 | 'all' => [ |
||
206 | AccountType::DEFAULT, |
||
207 | AccountType::CASH, |
||
208 | AccountType::ASSET, |
||
209 | AccountType::EXPENSE, |
||
210 | AccountType::REVENUE, |
||
211 | AccountType::INITIAL_BALANCE, |
||
212 | AccountType::BENEFICIARY, |
||
213 | AccountType::IMPORT, |
||
214 | AccountType::RECONCILIATION, |
||
215 | AccountType::LOAN, |
||
216 | ], |
||
217 | 'asset' => [ |
||
218 | AccountType::DEFAULT, |
||
219 | AccountType::ASSET, |
||
220 | ], |
||
221 | 'cash' => [ |
||
222 | AccountType::CASH, |
||
223 | ], |
||
224 | 'expense' => [ |
||
225 | AccountType::EXPENSE, |
||
226 | AccountType::BENEFICIARY, |
||
227 | ], |
||
228 | 'revenue' => [ |
||
229 | AccountType::REVENUE, |
||
230 | ], |
||
231 | 'special' => [ |
||
232 | AccountType::CASH, |
||
233 | AccountType::INITIAL_BALANCE, |
||
234 | AccountType::IMPORT, |
||
235 | AccountType::RECONCILIATION, |
||
236 | AccountType::LOAN, |
||
237 | ], |
||
238 | 'hidden' => [ |
||
239 | AccountType::INITIAL_BALANCE, |
||
240 | AccountType::IMPORT, |
||
241 | AccountType::RECONCILIATION, |
||
242 | AccountType::LOAN, |
||
243 | ], |
||
244 | AccountType::DEFAULT => [AccountType::DEFAULT], |
||
245 | AccountType::CASH => [AccountType::CASH], |
||
246 | AccountType::ASSET => [AccountType::ASSET], |
||
247 | AccountType::EXPENSE => [AccountType::EXPENSE], |
||
248 | AccountType::REVENUE => [AccountType::REVENUE], |
||
249 | AccountType::INITIAL_BALANCE => [AccountType::INITIAL_BALANCE], |
||
250 | AccountType::BENEFICIARY => [AccountType::BENEFICIARY], |
||
251 | AccountType::IMPORT => [AccountType::IMPORT], |
||
252 | AccountType::RECONCILIATION => [AccountType::RECONCILIATION], |
||
253 | AccountType::LOAN => [AccountType::LOAN], |
||
254 | ]; |
||
255 | if (isset($types[$type])) { |
||
256 | return $types[$type]; |
||
257 | } |
||
258 | |||
259 | return $types['all']; // @codeCoverageIgnore |
||
260 | } |
||
261 | } |
||
262 |