Files
firefly-iii/app/Api/V1/Controllers/CurrencyController.php

698 lines
26 KiB
PHP
Raw Normal View History

2018-04-13 17:28:11 +02:00
<?php
/**
* CurrencyController.php
* Copyright (c) 2019 james@firefly-iii.org
2018-04-13 17:28:11 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-04-13 17:28:11 +02:00
*
* 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.
2018-04-13 17:28:11 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-04-13 17:28:11 +02:00
* 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.
2018-04-13 17:28:11 +02:00
*
* 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/>.
2018-04-13 17:28:11 +02:00
*/
declare(strict_types=1);
2018-04-15 19:20:24 +02:00
2018-04-13 17:28:11 +02:00
namespace FireflyIII\Api\V1\Controllers;
2020-11-08 13:31:54 +01:00
use FireflyIII\Api\V1\Requests\CurrencyUpdateRequest;
use FireflyIII\Api\V1\Requests\CurrencyStoreRequest;
2018-04-13 17:28:11 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-03-25 15:14:09 +01:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-12-08 21:26:20 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleTrigger;
2018-04-13 17:28:11 +02:00
use FireflyIII\Models\TransactionCurrency;
2018-12-08 21:26:20 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2019-08-30 08:02:11 +02:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
2019-08-30 07:54:09 +02:00
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
2018-04-13 17:28:11 +02:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-12-08 21:26:20 +01:00
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
2018-04-13 17:28:11 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-12-08 21:26:20 +01:00
use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\AvailableBudgetTransformer;
use FireflyIII\Transformers\BillTransformer;
use FireflyIII\Transformers\BudgetLimitTransformer;
use FireflyIII\Transformers\CurrencyExchangeRateTransformer;
2018-04-13 17:28:11 +02:00
use FireflyIII\Transformers\CurrencyTransformer;
2018-12-08 21:26:20 +01:00
use FireflyIII\Transformers\RecurrenceTransformer;
use FireflyIII\Transformers\RuleTransformer;
2019-03-25 15:14:09 +01:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2018-07-05 06:10:35 +02:00
use FireflyIII\User;
2018-04-15 19:20:24 +02:00
use Illuminate\Http\JsonResponse;
2018-04-13 17:28:11 +02:00
use Illuminate\Http\Request;
2018-04-15 19:20:24 +02:00
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2018-04-13 17:28:11 +02:00
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
/**
* Class CurrencyController.
2018-04-13 17:28:11 +02:00
*/
class CurrencyController extends Controller
{
2018-12-08 21:26:20 +01:00
use AccountFilter, TransactionFilter;
2020-07-31 09:42:00 +02:00
2020-10-13 06:28:07 +02:00
private CurrencyRepositoryInterface $repository;
private UserRepositoryInterface $userRepository;
2018-04-13 17:28:11 +02:00
2020-07-31 09:24:08 +02:00
2018-04-13 17:28:11 +02:00
/**
* CurrencyRepository constructor.
2019-08-30 07:54:09 +02:00
*
* @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-07-05 06:10:35 +02:00
/** @var User $admin */
$admin = auth()->user();
2018-04-13 17:28:11 +02:00
/** @var CurrencyRepositoryInterface repository */
$this->repository = app(CurrencyRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
2018-07-05 06:10:35 +02:00
$this->repository->setUser($admin);
2018-04-13 17:28:11 +02:00
return $next($request);
}
);
}
2018-12-08 21:26:20 +01:00
/**
* Display a list of accounts.
*
2019-08-30 07:54:09 +02:00
* @param Request $request
2018-12-08 21:26:20 +01:00
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
public function accounts(Request $request, TransactionCurrency $currency): JsonResponse
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
// read type from URI
$type = $request->get('type') ?? 'all';
$this->parameters->set('type', $type);
// types to get, page size:
$types = $this->mapAccountTypes($this->parameters->get('type'));
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-08 21:26:20 +01:00
// get list of accounts. Count it and split it.
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$unfiltered = $accountRepository->getAccountsByType($types);
// filter list on currency preference:
$collection = $unfiltered->filter(
2019-06-10 20:14:00 +02:00
static function (Account $account) use ($currency, $accountRepository) {
$currencyId = (int) $accountRepository->getMetaValue($account, 'currency_id');
2018-12-08 21:26:20 +01:00
return $currencyId === $currency->id;
}
);
$count = $collection->count();
$accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.accounts', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($accounts, $transformer, 'accounts');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
2018-12-10 21:45:44 +01:00
* Display a listing of the resource.
2018-12-08 21:26:20 +01:00
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function availableBudgets(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
// types to get, page size:
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-08 21:26:20 +01:00
// get list of available budgets. Count it and split it.
2019-08-30 08:02:11 +02:00
/** @var AvailableBudgetRepositoryInterface $abRepository */
$abRepository = app(AvailableBudgetRepositoryInterface::class);
$collection = $abRepository->getAvailableBudgetsByCurrency($currency);
2018-12-08 21:26:20 +01:00
$count = $collection->count();
$availableBudgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($availableBudgets, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.available_budgets', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var AvailableBudgetTransformer $transformer */
$transformer = app(AvailableBudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($availableBudgets, $transformer, 'available_budgets');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* List all bills
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function bills(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
2020-10-13 06:28:07 +02:00
/** @var BillRepositoryInterface $billRepos */
2020-10-18 08:01:10 +02:00
$billRepos = app(BillRepositoryInterface::class);
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2020-10-13 06:28:07 +02:00
$unfiltered = $billRepos->getBills();
2018-12-08 21:26:20 +01:00
// filter and paginate list:
$collection = $unfiltered->filter(
2019-06-10 20:14:00 +02:00
static function (Bill $bill) use ($currency) {
2018-12-08 21:26:20 +01:00
return $bill->transaction_currency_id === $currency->id;
}
);
$count = $collection->count();
$bills = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.bills', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($bills, $transformer, 'bills');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* List all budget limits
*
2019-02-13 17:38:41 +01:00
* @param TransactionCurrency $currency
2018-12-08 21:26:20 +01:00
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function budgetLimits(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
2019-08-30 07:54:09 +02:00
/** @var BudgetLimitRepositoryInterface $blRepository */
$blRepository = app(BudgetLimitRepositoryInterface::class);
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2019-08-30 07:54:09 +02:00
$collection = $blRepository->getAllBudgetLimitsByCurrency($currency, $this->parameters->get('start'), $this->parameters->get('end'));
2018-12-08 21:26:20 +01:00
$count = $collection->count();
$budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
$paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.budget_limits', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var BudgetLimitTransformer $transformer */
$transformer = app(BudgetLimitTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* Show a list of known exchange rates
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function cer(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
// create some objects:
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-08 21:26:20 +01:00
$collection = $this->repository->getExchangeRates($currency);
$count = $collection->count();
$exchangeRates = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
$paginator = new LengthAwarePaginator($exchangeRates, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.cer', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var CurrencyExchangeRateTransformer $transformer */
$transformer = app(CurrencyExchangeRateTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($exchangeRates, $transformer, 'currency_exchange_rates');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
2018-04-13 17:28:11 +02:00
/**
* Remove the specified resource from storage.
*
* @param TransactionCurrency $currency
2018-04-13 17:28:11 +02:00
*
2020-10-13 06:28:07 +02:00
* @return JsonResponse
2018-04-13 17:28:11 +02:00
* @throws FireflyException
* @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
*/
2018-04-15 19:20:24 +02:00
public function delete(TransactionCurrency $currency): JsonResponse
2018-04-13 17:28:11 +02:00
{
2018-07-05 06:10:35 +02:00
/** @var User $admin */
$admin = auth()->user();
if (!$this->userRepository->hasRole($admin, 'owner')) {
2018-04-13 17:28:11 +02:00
// access denied:
2019-10-30 20:02:21 +01:00
throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
}
if ($this->repository->currencyInUse($currency)) {
2019-10-30 20:02:21 +01:00
throw new FireflyException('200006: Currency in use.'); // @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
}
2020-02-13 20:09:27 +01:00
if ($this->repository->isFallbackCurrency($currency)) {
throw new FireflyException('200026: Currency is fallback.'); // @codeCoverageIgnore
}
2018-04-13 17:28:11 +02:00
$this->repository->destroy($currency);
return response()->json([], 204);
}
2018-12-08 21:26:20 +01:00
/**
* Disable a currency.
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function disable(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
// must be unused.
if ($this->repository->currencyInUse($currency)) {
return response()->json([], 409);
}
$this->repository->disable($currency);
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2018-12-08 21:26:20 +01:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* Enable a currency.
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function enable(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
$this->repository->enable($currency);
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2018-12-08 21:26:20 +01:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
2018-04-13 17:28:11 +02:00
/**
* Display a listing of the resource.
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
* @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
*/
2019-09-04 17:39:39 +02:00
public function index(): JsonResponse
2018-04-13 17:28:11 +02:00
{
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$collection = $this->repository->getAll();
2018-04-15 19:20:24 +02:00
$count = $collection->count();
// slice them:
$currencies = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
$paginator = new LengthAwarePaginator($currencies, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.index') . $this->buildParams());
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-04-13 17:28:11 +02:00
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($currencies, $transformer, 'currencies');
2018-04-15 19:20:24 +02:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2018-04-13 17:28:11 +02:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-04-13 17:28:11 +02:00
}
2018-12-08 21:26:20 +01:00
/**
* Make the currency a default currency.
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function makeDefault(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
$this->repository->enable($currency);
app('preferences')->set('currencyPreference', $currency->code);
app('preferences')->mark();
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
$this->parameters->set('defaultCurrency', $currency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2018-12-08 21:26:20 +01:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* List all recurring transactions.
*
2019-02-13 17:38:41 +01:00
* @param TransactionCurrency $currency
2018-12-08 21:26:20 +01:00
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function recurrences(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-08 21:26:20 +01:00
// types to get, page size:
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-08 21:26:20 +01:00
// get list of budgets. Count it and split it.
2020-10-13 06:28:07 +02:00
/** @var RecurringRepositoryInterface $recurringRepos */
$recurringRepos = app(RecurringRepositoryInterface::class);
2020-10-18 08:01:10 +02:00
$unfiltered = $recurringRepos->getAll();
2018-12-08 21:26:20 +01:00
// filter selection
$collection = $unfiltered->filter(
2019-06-10 20:14:00 +02:00
static function (Recurrence $recurrence) use ($currency) {
2018-12-08 21:26:20 +01:00
/** @var RecurrenceTransaction $transaction */
foreach ($recurrence->recurrenceTransactions as $transaction) {
if ($transaction->transaction_currency_id === $currency->id || $transaction->foreign_currency_id === $currency->id) {
return $recurrence;
}
}
return null;
}
);
$count = $collection->count();
$piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.currencies.recurrences', [$currency->code]) . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($piggyBanks, $transformer, 'recurrences');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
/**
* List all of them.
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-08 21:26:20 +01:00
*/
2019-09-04 17:39:39 +02:00
public function rules(TransactionCurrency $currency): JsonResponse
2018-12-08 21:26:20 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-08 21:26:20 +01:00
// get list of budgets. Count it and split it.
2020-10-13 06:28:07 +02:00
/** @var RuleRepositoryInterface $ruleRepos */
2020-10-18 08:01:10 +02:00
$ruleRepos = app(RuleRepositoryInterface::class);
2020-10-13 06:28:07 +02:00
$unfiltered = $ruleRepos->getAll();
2018-12-08 21:26:20 +01:00
$collection = $unfiltered->filter(
2019-06-10 20:14:00 +02:00
static function (Rule $rule) use ($currency) {
2018-12-08 21:26:20 +01:00
/** @var RuleTrigger $trigger */
foreach ($rule->ruleTriggers as $trigger) {
if ('currency_is' === $trigger->trigger_type && $currency->name === $trigger->trigger_value) {
return $rule;
}
}
return null;
}
);
$count = $collection->count();
$rules = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.rules.index') . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var RuleTransformer $transformer */
$transformer = app(RuleTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($rules, $transformer, 'rules');
2018-12-08 21:26:20 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-08 21:26:20 +01:00
}
2018-04-13 17:28:11 +02:00
/**
* Show a currency.
*
2018-04-13 17:28:11 +02:00
* @param TransactionCurrency $currency
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
* @codeCoverageIgnore
2018-04-13 17:28:11 +02:00
*/
2019-09-04 17:39:39 +02:00
public function show(TransactionCurrency $currency): JsonResponse
2018-04-13 17:28:11 +02:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-04-13 17:28:11 +02:00
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2018-04-13 17:28:11 +02:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-04-13 17:28:11 +02:00
}
2020-03-20 04:42:19 +01:00
/**
* Show a currency.
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function showDefault(): JsonResponse
{
$manager = $this->getManager();
$currency = app('amount')->getDefaultCurrencyByUser(auth()->user());
2020-06-27 13:41:27 +02:00
$this->parameters->set('defaultCurrency', $currency);
2020-07-31 09:24:08 +02:00
2020-03-20 04:42:19 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2020-03-20 04:42:19 +01:00
}
2018-04-13 17:28:11 +02:00
/**
* Store new currency.
*
2020-11-08 13:31:54 +01:00
* @param CurrencyStoreRequest $request
2018-04-13 17:28:11 +02:00
*
* @return JsonResponse
2020-10-13 06:28:07 +02:00
* @throws FireflyException
2018-04-13 17:28:11 +02:00
*/
2020-11-08 13:31:54 +01:00
public function store(CurrencyStoreRequest $request): JsonResponse
2018-04-13 17:28:11 +02:00
{
$currency = $this->repository->store($request->getAll());
2019-10-30 20:02:21 +01:00
if (true === $request->boolean('default')) {
app('preferences')->set('currencyPreference', $currency->code);
app('preferences')->mark();
}
$manager = $this->getManager();
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-04-13 17:28:11 +02:00
2019-10-30 20:02:21 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
2018-04-13 17:28:11 +02:00
2019-10-30 20:02:21 +01:00
$resource = new Item($currency, $transformer, 'currencies');
2018-04-13 17:28:11 +02:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-04-13 17:28:11 +02:00
}
2018-12-10 21:45:44 +01:00
/**
* Show all transactions.
*
2019-08-30 07:54:09 +02:00
* @param Request $request
2018-12-10 21:45:44 +01:00
*
* @param TransactionCurrency $currency
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-10 21:45:44 +01:00
*/
public function transactions(Request $request, TransactionCurrency $currency): JsonResponse
{
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-10 21:45:44 +01:00
$type = $request->get('type') ?? 'default';
$this->parameters->set('type', $type);
$types = $this->mapTransactionTypes($this->parameters->get('type'));
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-10 21:45:44 +01:00
/** @var User $admin */
$admin = auth()->user();
2019-03-25 15:14:09 +01:00
// use new group collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector
->setUser($admin)
// filter on currency.
->setCurrency($currency)
// all info needed for the API:
->withAPIInformation()
// set page size:
->setLimit($pageSize)
// set page to retrieve
->setPage($this->parameters->get('page'))
// set types of transactions to return.
->setTypes($types);
2018-12-10 21:45:44 +01:00
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
}
2019-03-25 15:14:09 +01:00
$paginator = $collector->getPaginatedGroups();
2018-12-10 21:45:44 +01:00
$paginator->setPath(route('api.v1.currencies.transactions', [$currency->code]) . $this->buildParams());
$transactions = $paginator->getCollection();
2019-03-25 15:14:09 +01:00
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
2018-12-15 22:03:05 +01:00
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($transactions, $transformer, 'transactions');
2018-12-10 21:45:44 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-10 21:45:44 +01:00
}
2018-04-13 17:28:11 +02:00
/**
* Update a currency.
*
2020-11-08 13:31:54 +01:00
* @param CurrencyUpdateRequest $request
2018-04-13 17:28:11 +02:00
* @param TransactionCurrency $currency
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
2018-04-13 17:28:11 +02:00
*/
2020-11-08 13:31:54 +01:00
public function update(CurrencyUpdateRequest $request, TransactionCurrency $currency): JsonResponse
2018-04-13 17:28:11 +02:00
{
$data = $request->getAll();
$currency = $this->repository->update($currency, $data);
2018-07-05 21:18:53 +02:00
if (true === $request->boolean('default')) {
2018-06-20 21:17:16 +02:00
app('preferences')->set('currencyPreference', $currency->code);
app('preferences')->mark();
2018-04-13 17:28:11 +02:00
}
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-04-13 17:28:11 +02:00
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency);
2018-12-15 22:03:05 +01:00
/** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($currency, $transformer, 'currencies');
2018-04-13 17:28:11 +02:00
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-04-13 17:28:11 +02:00
}
}