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