diff --git a/app/Api/V1/Controllers/Autocomplete/AccountController.php b/app/Api/V1/Controllers/Autocomplete/AccountController.php new file mode 100644 index 0000000000..ffd80198d7 --- /dev/null +++ b/app/Api/V1/Controllers/Autocomplete/AccountController.php @@ -0,0 +1,109 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Api\V1\Controllers\Autocomplete; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Support\Http\Api\AccountFilter; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; + +/** + * Class AccountController + */ +class AccountController extends Controller +{ + use AccountFilter; + + private array $balanceTypes; + private AccountRepositoryInterface $repository; + + /** + * TransactionController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + $this->repository = app(AccountRepositoryInterface::class); + $this->repository->setUser($user); + + return $next($request); + } + ); + $this->balanceTypes = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,]; + } + + /** + * @param AutocompleteRequest $request + * + * @return JsonResponse + */ + public function accounts(AutocompleteRequest $request): JsonResponse + { + $data = $request->getData(); + $types = $data['types']; + $query = $data['query']; + $date = $data['date'] ?? today(config('app.timezone')); + + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $return = []; + $result = $repository->searchAccount((string) $query, $types); + $defaultCurrency = app('amount')->getDefaultCurrency(); + + /** @var Account $account */ + foreach ($result as $account) { + $nameWithBalance = $account->name; + $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; + + if (in_array($account->accountType->type, $this->balanceTypes, true)) { + $balance = app('steam')->balance($account, $date); + $nameWithBalance = sprintf('%s (%s)', $account->name, app('amount')->formatAnything($currency, $balance, false)); + } + + $return[] = [ + 'id' => $account->id, + 'name' => $account->name, + 'name_with_balance' => $nameWithBalance, + '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); + } +}