2017-08-15 17:34:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AutoCompleteController.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-08-15 17:34:34 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Json;
|
|
|
|
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2019-05-04 20:58:43 +02:00
|
|
|
use FireflyIII\Models\Account;
|
2017-08-15 17:34:34 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
2019-05-04 20:58:43 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
2018-06-01 22:04:52 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-09-23 06:57:00 +02:00
|
|
|
use Illuminate\Http\Request;
|
2019-05-04 20:58:43 +02:00
|
|
|
use Log;
|
2017-08-15 17:34:34 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class AutoCompleteController.
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-06-29 19:47:31 +02:00
|
|
|
* TODO autocomplete for transaction types.
|
|
|
|
*
|
2018-07-20 14:34:56 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2017-08-15 17:34:34 +02:00
|
|
|
*/
|
|
|
|
class AutoCompleteController extends Controller
|
|
|
|
{
|
2018-04-07 06:19:40 +02:00
|
|
|
|
2019-05-04 20:58:43 +02:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function accounts(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
$accountTypes = explode(',', $request->get('types') ?? '');
|
|
|
|
$search = $request->get('query');
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
|
|
|
|
// filter the account types:
|
|
|
|
$allowedAccountTypes = [AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,];
|
|
|
|
$filteredAccountTypes = [];
|
|
|
|
foreach ($accountTypes as $type) {
|
|
|
|
if (in_array($type, $allowedAccountTypes, true)) {
|
|
|
|
$filteredAccountTypes[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log::debug('Now in accounts(). Filtering results.', $filteredAccountTypes);
|
|
|
|
|
|
|
|
$return = [];
|
|
|
|
$result = $repository->searchAccount((string)$search, $filteredAccountTypes);
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
|
|
|
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($result as $account) {
|
|
|
|
$currency = $repository->getAccountCurrency($account);
|
|
|
|
$currency = $currency ?? $defaultCurrency;
|
|
|
|
$return[] = [
|
|
|
|
'id' => $account->id,
|
|
|
|
'name' => $account->name,
|
|
|
|
'type' => $account->accountType->type,
|
|
|
|
'currency_id' => $currency->id,
|
|
|
|
'currency_name' => $currency->name,
|
|
|
|
'currency_code' => $currency->code,
|
|
|
|
'currency_decimal_places' => $currency->decimal_places,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response()->json($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return JsonResponse
|
2019-06-29 19:47:31 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-05-04 20:58:43 +02:00
|
|
|
*/
|
|
|
|
public function budgets(): JsonResponse
|
|
|
|
{
|
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
return response()->json($repository->getActiveBudgets()->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2019-06-29 19:47:31 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-05-04 20:58:43 +02:00
|
|
|
*/
|
|
|
|
public function categories(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
$query = (string)$request->get('query');
|
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$result = $repository->searchCategory($query);
|
|
|
|
|
|
|
|
return response()->json($result->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return JsonResponse
|
2019-06-29 19:47:31 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-05-04 20:58:43 +02:00
|
|
|
*/
|
2019-06-29 19:47:31 +02:00
|
|
|
public function currencies(): JsonResponse
|
2019-05-04 20:58:43 +02:00
|
|
|
{
|
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$return = [];
|
|
|
|
$collection = $repository->getAll();
|
|
|
|
|
|
|
|
/** @var TransactionCurrency $currency */
|
|
|
|
foreach ($collection as $currency) {
|
|
|
|
$return[] = [
|
|
|
|
'id' => $currency->id,
|
|
|
|
'name' => $currency->name,
|
|
|
|
'code' => $currency->code,
|
|
|
|
'symbol' => $currency->symbol,
|
|
|
|
'enabled' => $currency->enabled,
|
|
|
|
'decimal_places' => $currency->decimal_places,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return JsonResponse
|
2019-06-29 19:47:31 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-05-04 20:58:43 +02:00
|
|
|
*/
|
|
|
|
public function piggyBanks(): JsonResponse
|
|
|
|
{
|
|
|
|
/** @var PiggyBankRepositoryInterface $repository */
|
|
|
|
$repository = app(PiggyBankRepositoryInterface::class);
|
|
|
|
|
|
|
|
return response()->json($repository->getPiggyBanks()->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-29 19:47:31 +02:00
|
|
|
* @param Request $request
|
2019-05-04 20:58:43 +02:00
|
|
|
* @return JsonResponse
|
2019-06-29 19:47:31 +02:00
|
|
|
* @codeCoverageIgnore
|
2019-05-04 20:58:43 +02:00
|
|
|
*/
|
|
|
|
public function tags(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
$query = (string)$request->get('query');
|
|
|
|
/** @var TagRepositoryInterface $repository */
|
|
|
|
$repository = app(TagRepositoryInterface::class);
|
|
|
|
$result = $repository->searchTags($query);
|
|
|
|
|
|
|
|
|
|
|
|
return response()->json($result->toArray());
|
|
|
|
}
|
|
|
|
|
2017-08-31 06:47:18 +02:00
|
|
|
}
|