1 | <?php |
||
2 | /** |
||
3 | * AutoCompleteController.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 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Http\Controllers\Json; |
||
24 | |||
25 | use Amount; |
||
26 | use Carbon\Carbon; |
||
27 | use FireflyIII\Http\Controllers\Controller; |
||
28 | use FireflyIII\Models\Account; |
||
29 | use FireflyIII\Models\AccountType; |
||
30 | use FireflyIII\Models\PiggyBank; |
||
31 | use FireflyIII\Models\TransactionCurrency; |
||
32 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
33 | use FireflyIII\Repositories\Bill\BillRepositoryInterface; |
||
34 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||
35 | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; |
||
36 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||
37 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
38 | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; |
||
39 | use FireflyIII\Repositories\Tag\TagRepositoryInterface; |
||
40 | use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface; |
||
41 | use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface; |
||
42 | use Illuminate\Http\JsonResponse; |
||
43 | use Illuminate\Http\Request; |
||
44 | use Log; |
||
45 | |||
46 | /** |
||
47 | * Class AutoCompleteController. |
||
48 | * |
||
49 | * TODO autocomplete for transaction types. |
||
50 | * |
||
51 | */ |
||
52 | class AutoCompleteController extends Controller |
||
53 | { |
||
54 | |||
55 | /** |
||
56 | * @param Request $request |
||
57 | * |
||
58 | * @return JsonResponse |
||
59 | */ |
||
60 | public function accounts(Request $request): JsonResponse |
||
61 | { |
||
62 | $accountTypes = explode(',', $request->get('types') ?? ''); |
||
63 | $search = $request->get('search'); |
||
64 | /** @var AccountRepositoryInterface $repository */ |
||
65 | $repository = app(AccountRepositoryInterface::class); |
||
66 | |||
67 | // filter the account types: |
||
68 | $allowedAccountTypes = [AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,]; |
||
69 | $balanceTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,]; |
||
70 | $filteredAccountTypes = []; |
||
71 | foreach ($accountTypes as $type) { |
||
72 | if (in_array($type, $allowedAccountTypes, true)) { |
||
73 | $filteredAccountTypes[] = $type; |
||
74 | } |
||
75 | } |
||
76 | if (0 === count($filteredAccountTypes)) { |
||
77 | $filteredAccountTypes = $allowedAccountTypes; |
||
78 | } |
||
79 | Log::debug(sprintf('Now in accounts("%s"). Filtering results.', $search), $filteredAccountTypes); |
||
80 | |||
81 | $return = []; |
||
82 | $result = $repository->searchAccount((string) $search, $filteredAccountTypes); |
||
83 | $defaultCurrency = app('amount')->getDefaultCurrency(); |
||
84 | |||
85 | /** @var Account $account */ |
||
86 | foreach ($result as $account) { |
||
87 | $nameWithBalance = $account->name; |
||
88 | $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; |
||
89 | |||
90 | if (in_array($account->accountType->type, $balanceTypes, true)) { |
||
91 | $balance = app('steam')->balance($account, new Carbon); |
||
92 | $nameWithBalance = sprintf('%s (%s)', $account->name, app('amount')->formatAnything($currency, $balance, false)); |
||
93 | } |
||
94 | |||
95 | $return[] = [ |
||
96 | 'id' => $account->id, |
||
97 | 'name' => $account->name, |
||
98 | 'name_with_balance' => $nameWithBalance, |
||
99 | 'type' => $account->accountType->type, |
||
100 | 'currency_id' => $currency->id, |
||
101 | 'currency_name' => $currency->name, |
||
102 | 'currency_code' => $currency->code, |
||
103 | 'currency_decimal_places' => $currency->decimal_places, |
||
104 | ]; |
||
105 | } |
||
106 | |||
107 | |||
108 | return response()->json($return); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Searches in the titles of all transaction journals. |
||
113 | * The result is limited to the top 15 unique results. |
||
114 | * |
||
115 | * @param Request $request |
||
116 | * |
||
117 | * @return JsonResponse |
||
118 | */ |
||
119 | public function allJournals(Request $request): JsonResponse |
||
120 | { |
||
121 | $search = (string) $request->get('search'); |
||
122 | /** @var JournalRepositoryInterface $repository */ |
||
123 | $repository = app(JournalRepositoryInterface::class); |
||
124 | $result = $repository->searchJournalDescriptions($search); |
||
125 | |||
126 | // limit and unique |
||
127 | $filtered = $result->unique('description'); |
||
128 | $limited = $filtered->slice(0, 15); |
||
129 | $array = $limited->toArray(); |
||
130 | // duplicate 'description' value into 'name': |
||
131 | $array = array_map( |
||
132 | static function (array $journal) { |
||
133 | $journal['name'] = $journal['description']; |
||
134 | |||
135 | return $journal; |
||
136 | }, |
||
137 | $array |
||
138 | ); |
||
139 | |||
140 | return response()->json(array_values($array)); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Searches in the titles of all transaction journals. |
||
145 | * The result is limited to the top 15 unique results. |
||
146 | * |
||
147 | * If the query is numeric, it will append the journal with that particular ID. |
||
148 | * |
||
149 | * @param Request $request |
||
150 | * |
||
151 | * @return JsonResponse |
||
152 | */ |
||
153 | public function allJournalsWithID(Request $request): JsonResponse |
||
154 | { |
||
155 | $search = (string) $request->get('search'); |
||
156 | /** @var JournalRepositoryInterface $repository */ |
||
157 | $repository = app(JournalRepositoryInterface::class); |
||
158 | |||
159 | /** @var TransactionGroupRepositoryInterface $groupRepos */ |
||
160 | $groupRepos = app(TransactionGroupRepositoryInterface::class); |
||
161 | |||
162 | $result = $repository->searchJournalDescriptions($search); |
||
163 | $array = []; |
||
164 | if (is_numeric($search)) { |
||
165 | // search for group, not journal. |
||
166 | $firstResult = $groupRepos->find((int) $search); |
||
167 | if (null !== $firstResult) { |
||
168 | // group may contain multiple journals, each a result: |
||
169 | foreach ($firstResult->transactionJournals as $journal) { |
||
170 | $array[] = $journal->toArray(); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | // if not numeric, search ahead! |
||
175 | |||
176 | // limit and unique |
||
177 | $limited = $result->slice(0, 15); |
||
178 | $array = array_merge($array, $limited->toArray()); |
||
179 | foreach ($array as $index => $item) { |
||
180 | // give another key for consistency |
||
181 | $array[$index]['name'] = sprintf('#%d: %s', $item['transaction_group_id'], $item['description']); |
||
182 | } |
||
183 | |||
184 | |||
185 | return response()->json($array); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * An auto-complete specifically for asset accounts and liabilities, used when mass updating and for rules mostly. |
||
190 | * |
||
191 | * @param Request $request |
||
192 | * |
||
193 | * @return JsonResponse |
||
194 | */ |
||
195 | public function assetAccounts(Request $request): JsonResponse |
||
196 | { |
||
197 | $search = $request->get('search'); |
||
198 | /** @var AccountRepositoryInterface $repository */ |
||
199 | $repository = app(AccountRepositoryInterface::class); |
||
200 | |||
201 | // filter the account types: |
||
202 | $allowedAccountTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; |
||
203 | Log::debug(sprintf('Now in expenseAccounts(%s). Filtering results.', $search), $allowedAccountTypes); |
||
204 | |||
205 | $return = []; |
||
206 | $result = $repository->searchAccount((string) $search, $allowedAccountTypes); |
||
207 | |||
208 | /** @var Account $account */ |
||
209 | foreach ($result as $account) { |
||
210 | $return[] = [ |
||
211 | 'id' => $account->id, |
||
212 | 'name' => $account->name, |
||
213 | 'type' => $account->accountType->type, |
||
214 | ]; |
||
215 | } |
||
216 | |||
217 | return response()->json($return); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param Request $request |
||
222 | * |
||
223 | * @return JsonResponse |
||
224 | * @codeCoverageIgnore |
||
225 | */ |
||
226 | public function bills(Request $request): JsonResponse |
||
227 | { |
||
228 | $query = (string) $request->get('search'); |
||
229 | /** @var BillRepositoryInterface $repository */ |
||
230 | $repository = app(BillRepositoryInterface::class); |
||
231 | $result = $repository->searchBill($query); |
||
232 | |||
233 | return response()->json($result->toArray()); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param Request $request |
||
238 | * |
||
239 | * @return JsonResponse |
||
240 | * @codeCoverageIgnore |
||
241 | */ |
||
242 | public function budgets(Request $request): JsonResponse |
||
243 | { |
||
244 | $search = (string) $request->get('search'); |
||
245 | /** @var BudgetRepositoryInterface $repository */ |
||
246 | $repository = app(BudgetRepositoryInterface::class); |
||
247 | $result = $repository->searchBudget($search); |
||
248 | |||
249 | return response()->json($result->toArray()); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param Request $request |
||
254 | * |
||
255 | * @return JsonResponse |
||
256 | * @codeCoverageIgnore |
||
257 | */ |
||
258 | public function categories(Request $request): JsonResponse |
||
259 | { |
||
260 | $query = (string) $request->get('search'); |
||
261 | /** @var CategoryRepositoryInterface $repository */ |
||
262 | $repository = app(CategoryRepositoryInterface::class); |
||
263 | $result = $repository->searchCategory($query); |
||
264 | |||
265 | return response()->json($result->toArray()); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @return JsonResponse |
||
270 | * @codeCoverageIgnore |
||
271 | */ |
||
272 | public function currencies(): JsonResponse |
||
273 | { |
||
274 | /** @var CurrencyRepositoryInterface $repository */ |
||
275 | $repository = app(CurrencyRepositoryInterface::class); |
||
276 | $return = []; |
||
277 | $collection = $repository->getAll(); |
||
278 | |||
279 | /** @var TransactionCurrency $currency */ |
||
280 | foreach ($collection as $currency) { |
||
281 | $return[] = [ |
||
282 | 'id' => $currency->id, |
||
283 | 'name' => $currency->name, |
||
284 | 'code' => $currency->code, |
||
285 | 'symbol' => $currency->symbol, |
||
286 | 'enabled' => $currency->enabled, |
||
287 | 'decimal_places' => $currency->decimal_places, |
||
288 | ]; |
||
289 | } |
||
290 | |||
291 | return response()->json($return); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param Request $request |
||
296 | * |
||
297 | * @return JsonResponse |
||
298 | * @codeCoverageIgnore |
||
299 | */ |
||
300 | public function currencyNames(Request $request): JsonResponse |
||
301 | { |
||
302 | $query = (string) $request->get('search'); |
||
303 | /** @var CurrencyRepositoryInterface $repository */ |
||
304 | $repository = app(CurrencyRepositoryInterface::class); |
||
305 | $result = $repository->searchCurrency($query)->toArray(); |
||
306 | foreach ($result as $index => $item) { |
||
307 | $result[$index]['name'] = sprintf('%s (%s)', $item['name'], $item['code']); |
||
308 | } |
||
309 | |||
310 | return response()->json($result); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * An auto-complete specifically for expense accounts, used when mass updating mostly. |
||
315 | * |
||
316 | * @param Request $request |
||
317 | * |
||
318 | * @return JsonResponse |
||
319 | */ |
||
320 | public function expenseAccounts(Request $request): JsonResponse |
||
321 | { |
||
322 | $search = $request->get('search'); |
||
323 | /** @var AccountRepositoryInterface $repository */ |
||
324 | $repository = app(AccountRepositoryInterface::class); |
||
325 | |||
326 | // filter the account types: |
||
327 | $allowedAccountTypes = [AccountType::EXPENSE]; |
||
328 | Log::debug(sprintf('Now in expenseAccounts(%s). Filtering results.', $search), $allowedAccountTypes); |
||
329 | |||
330 | $return = []; |
||
331 | $result = $repository->searchAccount((string) $search, $allowedAccountTypes); |
||
332 | |||
333 | /** @var Account $account */ |
||
334 | foreach ($result as $account) { |
||
335 | $return[] = [ |
||
336 | 'id' => $account->id, |
||
337 | 'name' => $account->name, |
||
338 | 'type' => $account->accountType->type, |
||
339 | ]; |
||
340 | } |
||
341 | |||
342 | return response()->json($return); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return JsonResponse |
||
347 | * @codeCoverageIgnore |
||
348 | */ |
||
349 | public function piggyBanks(): JsonResponse |
||
350 | { |
||
351 | /** @var PiggyBankRepositoryInterface $repository */ |
||
352 | $repository = app(PiggyBankRepositoryInterface::class); |
||
353 | |||
354 | /** @var AccountRepositoryInterface $accountRepos */ |
||
355 | $accountRepos = app(AccountRepositoryInterface::class); |
||
356 | |||
357 | $piggies = $repository->getPiggyBanks(); |
||
358 | $defaultCurrency = Amount::getDefaultCurrency(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
359 | $response = []; |
||
360 | /** @var PiggyBank $piggy */ |
||
361 | foreach ($piggies as $piggy) { |
||
362 | $currency = $accountRepos->getAccountCurrency($piggy->account) ?? $defaultCurrency; |
||
363 | $currentAmount = $repository->getRepetition($piggy)->currentamount ?? '0'; |
||
364 | $piggy->name_with_amount = sprintf( |
||
365 | '%s (%s / %s)', |
||
366 | $piggy->name, |
||
367 | app('amount')->formatAnything($currency, $currentAmount, false), |
||
368 | app('amount')->formatAnything($currency, $piggy->targetamount, false), |
||
369 | ); |
||
370 | $response[] = $piggy->toArray(); |
||
371 | } |
||
372 | |||
373 | return response()->json($response); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * An auto-complete specifically for revenue accounts, used when converting transactions mostly. |
||
378 | * |
||
379 | * @param Request $request |
||
380 | * |
||
381 | * @return JsonResponse |
||
382 | */ |
||
383 | public function revenueAccounts(Request $request): JsonResponse |
||
384 | { |
||
385 | $search = $request->get('search'); |
||
386 | /** @var AccountRepositoryInterface $repository */ |
||
387 | $repository = app(AccountRepositoryInterface::class); |
||
388 | |||
389 | // filter the account types: |
||
390 | $allowedAccountTypes = [AccountType::REVENUE]; |
||
391 | Log::debug('Now in revenueAccounts(). Filtering results.', $allowedAccountTypes); |
||
392 | |||
393 | $return = []; |
||
394 | $result = $repository->searchAccount((string) $search, $allowedAccountTypes); |
||
395 | |||
396 | /** @var Account $account */ |
||
397 | foreach ($result as $account) { |
||
398 | $return[] = [ |
||
399 | 'id' => $account->id, |
||
400 | 'name' => $account->name, |
||
401 | 'type' => $account->accountType->type, |
||
402 | ]; |
||
403 | } |
||
404 | |||
405 | return response()->json($return); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param Request $request |
||
410 | * |
||
411 | * @return JsonResponse |
||
412 | * @codeCoverageIgnore |
||
413 | */ |
||
414 | public function tags(Request $request): JsonResponse |
||
415 | { |
||
416 | $search = (string) $request->get('search'); |
||
417 | /** @var TagRepositoryInterface $repository */ |
||
418 | $repository = app(TagRepositoryInterface::class); |
||
419 | $result = $repository->searchTags($search); |
||
420 | $array = $result->toArray(); |
||
421 | foreach ($array as $index => $item) { |
||
422 | // rename field for consistency. |
||
423 | $array[$index]['name'] = $item['tag']; |
||
424 | } |
||
425 | |||
426 | return response()->json($array); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @param Request $request |
||
431 | * |
||
432 | * @return JsonResponse |
||
433 | * @codeCoverageIgnore |
||
434 | */ |
||
435 | public function transactionTypes(Request $request): JsonResponse |
||
436 | { |
||
437 | $query = (string) $request->get('search'); |
||
438 | /** @var TransactionTypeRepositoryInterface $repository */ |
||
439 | $repository = app(TransactionTypeRepositoryInterface::class); |
||
440 | $array = $repository->searchTypes($query)->toArray(); |
||
441 | |||
442 | foreach ($array as $index => $item) { |
||
443 | // different key for consistency. |
||
444 | $array[$index]['name'] = $item['type']; |
||
445 | } |
||
446 | |||
447 | return response()->json($array); |
||
448 | } |
||
449 | } |
||
450 |