Files
firefly-iii/app/Api/V1/Controllers/Autocomplete/PiggyBankController.php

130 lines
5.1 KiB
PHP
Raw Normal View History

2020-07-21 06:09:24 +02:00
<?php
2020-07-21 06:09:24 +02:00
/**
* PiggyBankController.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
2020-07-22 20:17:20 +02:00
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
2020-07-21 06:09:24 +02:00
/**
* Class PiggyBankController
*/
class PiggyBankController extends Controller
{
2021-09-18 05:26:31 +02:00
private AccountRepositoryInterface $accountRepository;
2020-07-22 20:17:20 +02:00
private PiggyBankRepositoryInterface $piggyRepository;
2020-07-31 09:42:00 +02:00
2020-07-22 20:17:20 +02:00
/**
* PiggyBankController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->piggyRepository = app(PiggyBankRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->piggyRepository->setUser($user);
$this->accountRepository->setUser($user);
return $next($request);
}
);
}
/**
2021-09-18 05:26:31 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/autocomplete/getPiggiesAC
2020-07-22 20:17:20 +02:00
*/
public function piggyBanks(AutocompleteRequest $request): JsonResponse
{
2024-12-14 05:45:54 +01:00
$data = $request->getData();
$piggies = $this->piggyRepository->searchPiggyBank($data['query'], $this->parameters->get('limit'));
$response = [];
2020-07-23 06:19:34 +02:00
2020-07-22 20:17:20 +02:00
/** @var PiggyBank $piggy */
foreach ($piggies as $piggy) {
2024-12-01 18:16:48 +01:00
$currency = $piggy->transactionCurrency;
2023-02-12 06:53:36 +01:00
$objectGroup = $piggy->objectGroups()->first();
$response[] = [
2024-12-22 08:43:12 +01:00
'id' => (string) $piggy->id,
2020-07-22 20:17:20 +02:00
'name' => $piggy->name,
2024-12-22 08:43:12 +01:00
'currency_id' => (string) $currency->id,
2020-07-22 20:17:20 +02:00
'currency_name' => $currency->name,
'currency_code' => $currency->code,
2021-09-18 05:26:31 +02:00
'currency_symbol' => $currency->symbol,
2020-07-22 20:17:20 +02:00
'currency_decimal_places' => $currency->decimal_places,
2024-12-22 08:43:12 +01:00
'object_group_id' => null === $objectGroup ? null : (string) $objectGroup->id,
2023-02-12 06:53:36 +01:00
'object_group_title' => $objectGroup?->title,
2020-07-22 20:17:20 +02:00
];
}
return response()->json($response);
}
/**
2021-09-18 05:26:31 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/autocomplete/getPiggiesBalanceAC
2020-07-22 20:17:20 +02:00
*/
public function piggyBanksWithBalance(AutocompleteRequest $request): JsonResponse
{
2024-12-14 05:45:54 +01:00
$data = $request->getData();
$piggies = $this->piggyRepository->searchPiggyBank($data['query'], $this->parameters->get('limit'));
$response = [];
2023-12-20 19:35:52 +01:00
2020-07-22 20:17:20 +02:00
/** @var PiggyBank $piggy */
foreach ($piggies as $piggy) {
2024-12-01 18:16:48 +01:00
$currency = $piggy->transactionCurrency;
2024-12-15 08:50:57 +01:00
$currentAmount = $this->piggyRepository->getCurrentAmount($piggy);
2023-02-12 06:53:36 +01:00
$objectGroup = $piggy->objectGroups()->first();
2021-03-22 06:45:13 +01:00
$response[] = [
2024-12-22 08:43:12 +01:00
'id' => (string) $piggy->id,
'name' => $piggy->name,
'name_with_balance' => sprintf(
2022-10-30 14:23:00 +01:00
'%s (%s / %s)',
$piggy->name,
app('amount')->formatAnything($currency, $currentAmount, false),
2024-11-30 16:02:30 +01:00
app('amount')->formatAnything($currency, $piggy->target_amount, false),
2020-07-22 20:17:20 +02:00
),
2024-12-22 08:43:12 +01:00
'currency_id' => (string) $currency->id,
2020-07-22 20:17:20 +02:00
'currency_name' => $currency->name,
'currency_code' => $currency->code,
2021-09-18 05:26:31 +02:00
'currency_symbol' => $currency->symbol,
2020-07-22 20:17:20 +02:00
'currency_decimal_places' => $currency->decimal_places,
2024-12-22 08:43:12 +01:00
'object_group_id' => null === $objectGroup ? null : (string) $objectGroup->id,
2023-02-12 06:53:36 +01:00
'object_group_title' => $objectGroup?->title,
2020-07-22 20:17:20 +02:00
];
}
return response()->json($response);
}
2020-07-21 06:09:24 +02:00
}