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

246 lines
9.7 KiB
PHP
Raw Normal View History

2018-02-11 08:08:08 +01:00
<?php
/**
* AccountController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2018-02-11 08:08:08 +01:00
namespace FireflyIII\Api\V1\Controllers;
2018-02-13 18:24:06 +01:00
use FireflyIII\Api\V1\Requests\AccountRequest;
2018-02-11 08:08:08 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-13 18:24:06 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-02-11 08:08:08 +01:00
use FireflyIII\Transformers\AccountTransformer;
2018-06-24 06:51:22 +02:00
use FireflyIII\User;
2018-06-21 18:56:44 +02:00
use Illuminate\Http\JsonResponse;
2018-02-11 08:08:08 +01:00
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
/**
* Class AccountController.
*
2018-07-05 18:02:02 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2018-02-11 08:08:08 +01:00
*/
class AccountController extends Controller
{
/** @var CurrencyRepositoryInterface The currency repository */
2018-02-13 18:24:06 +01:00
private $currencyRepository;
/** @var AccountRepositoryInterface The account repository */
2018-02-11 08:08:08 +01:00
private $repository;
/**
* AccountController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-06-24 06:51:22 +02:00
/** @var User $user */
$user = auth()->user();
2018-03-11 08:22:20 +01:00
// @var AccountRepositoryInterface repository
2018-02-11 08:08:08 +01:00
$this->repository = app(AccountRepositoryInterface::class);
2018-06-24 06:51:22 +02:00
$this->repository->setUser($user);
2018-02-11 08:08:08 +01:00
2018-02-13 18:24:06 +01:00
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
2018-06-24 06:51:22 +02:00
$this->currencyRepository->setUser($user);
2018-02-13 18:24:06 +01:00
2018-02-11 08:08:08 +01:00
return $next($request);
}
);
}
/**
* Remove the specified resource from storage.
*
2018-03-11 08:22:20 +01:00
* @param \FireflyIII\Models\Account $account
2018-02-11 08:08:08 +01:00
*
2018-06-21 18:56:44 +02:00
* @return JsonResponse
2018-02-11 08:08:08 +01:00
*/
2018-06-21 18:56:44 +02:00
public function delete(Account $account): JsonResponse
2018-02-11 08:08:08 +01:00
{
2018-02-21 18:42:15 +01:00
$this->repository->destroy($account, null);
2018-02-11 08:08:08 +01:00
return response()->json([], 204);
}
/**
* Display a listing of the resource.
*
* @param Request $request
*
2018-06-21 18:56:44 +02:00
* @return JsonResponse
2018-02-11 08:08:08 +01:00
*/
2018-06-21 18:56:44 +02:00
public function index(Request $request): JsonResponse
2018-02-11 08:08:08 +01:00
{
2018-02-13 21:04:15 +01:00
// create some objects:
2018-06-24 06:51:22 +02:00
$manager = new Manager;
2018-03-10 22:38:20 +01:00
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
2018-02-13 21:04:15 +01:00
// read type from URI
$type = $request->get('type') ?? 'all';
$this->parameters->set('type', $type);
// types to get, page size:
2018-03-10 22:38:20 +01:00
$types = $this->mapTypes($this->parameters->get('type'));
2018-06-20 21:17:16 +02:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-02-13 21:04:15 +01:00
// get list of accounts. Count it and split it.
2018-02-11 08:08:08 +01:00
$collection = $this->repository->getAccountsByType($types);
$count = $collection->count();
$accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
2018-02-11 08:08:08 +01:00
// make paginator:
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));
2018-02-16 22:14:53 +01:00
$paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());
2018-02-11 08:08:08 +01:00
2018-02-13 21:04:15 +01:00
// present to user.
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new FractalCollection($accounts, new AccountTransformer($this->parameters), 'accounts');
2018-02-11 08:08:08 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2018-02-13 21:04:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-11 08:08:08 +01:00
}
/**
* Show single instance.
*
2018-02-11 08:08:08 +01:00
* @param Request $request
* @param Account $account
*
* @return \Illuminate\Http\JsonResponse
*/
2018-06-21 18:56:44 +02:00
public function show(Request $request, Account $account): JsonResponse
2018-02-11 08:08:08 +01:00
{
2018-06-24 06:51:22 +02:00
$manager = new Manager;
2018-02-11 08:08:08 +01:00
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($account, new AccountTransformer($this->parameters), 'accounts');
2018-02-11 08:08:08 +01:00
2018-02-13 21:04:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-11 08:08:08 +01:00
}
/**
* Store a new instance.
*
2018-02-11 08:08:08 +01:00
* @param AccountRequest $request
*
* @return \Illuminate\Http\JsonResponse
*/
2018-06-21 18:56:44 +02:00
public function store(AccountRequest $request): JsonResponse
2018-02-11 08:08:08 +01:00
{
2018-02-13 18:24:06 +01:00
$data = $request->getAll();
// if currency ID is 0, find the currency by the code:
2018-03-11 08:22:20 +01:00
if (0 === $data['currency_id']) {
2018-02-17 19:56:45 +01:00
$currency = $this->currencyRepository->findByCodeNull($data['currency_code']);
2018-04-02 14:17:11 +02:00
$data['currency_id'] = null === $currency ? 0 : $currency->id;
2018-02-13 18:24:06 +01:00
}
$account = $this->repository->store($data);
2018-06-24 06:51:22 +02:00
$manager = new Manager;
2018-02-13 18:24:06 +01:00
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($account, new AccountTransformer($this->parameters), 'accounts');
2018-02-13 21:04:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-11 08:08:08 +01:00
}
/**
2018-03-04 08:22:32 +01:00
* Update account.
*
2018-02-11 08:08:08 +01:00
* @param AccountRequest $request
* @param Account $account
*
* @return \Illuminate\Http\JsonResponse
*/
2018-06-21 18:56:44 +02:00
public function update(AccountRequest $request, Account $account): JsonResponse
2018-02-11 08:08:08 +01:00
{
2018-02-13 18:24:06 +01:00
$data = $request->getAll();
// if currency ID is 0, find the currency by the code:
2018-03-11 08:22:20 +01:00
if (0 === $data['currency_id']) {
2018-02-17 19:56:45 +01:00
$currency = $this->currencyRepository->findByCodeNull($data['currency_code']);
2018-04-02 14:17:11 +02:00
$data['currency_id'] = null === $currency ? 0 : $currency->id;
2018-02-13 18:24:06 +01:00
}
// set correct type:
$data['type'] = config('firefly.shortNamesByFullName.' . $account->accountType->type);
$this->repository->update($account, $data);
2018-06-24 06:51:22 +02:00
$manager = new Manager;
2018-02-13 18:24:06 +01:00
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-02-11 08:08:08 +01:00
2018-02-13 18:24:06 +01:00
$resource = new Item($account, new AccountTransformer($this->parameters), 'accounts');
2018-02-11 08:08:08 +01:00
2018-02-13 21:04:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-11 08:08:08 +01:00
}
/**
* All the available types.
*
2018-02-11 08:08:08 +01:00
* @param string $type
*
* @return array
*/
private function mapTypes(string $type): array
{
$types = [
2018-07-05 18:02:02 +02:00
'all' => [AccountType::DEFAULT, AccountType::CASH, AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE,
AccountType::INITIAL_BALANCE, AccountType::BENEFICIARY, AccountType::IMPORT, AccountType::RECONCILIATION,
2018-09-26 20:21:25 +02:00
AccountType::LOAN,AccountType::DEBT, AccountType::MORTGAGE],
2018-07-05 18:02:02 +02:00
'asset' => [AccountType::DEFAULT, AccountType::ASSET,],
'cash' => [AccountType::CASH,],
'expense' => [AccountType::EXPENSE, AccountType::BENEFICIARY,],
'revenue' => [AccountType::REVENUE,],
2018-09-26 20:21:25 +02:00
'special' => [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION,],
'hidden' => [AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'liability' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
'liabilities' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
'cc' => [AccountType::CREDITCARD],
'creditcard' => [AccountType::CREDITCARD],
'credit_card' => [AccountType::CREDITCARD],
2018-02-11 08:08:08 +01:00
AccountType::DEFAULT => [AccountType::DEFAULT],
AccountType::CASH => [AccountType::CASH],
AccountType::ASSET => [AccountType::ASSET],
AccountType::EXPENSE => [AccountType::EXPENSE],
AccountType::REVENUE => [AccountType::REVENUE],
AccountType::INITIAL_BALANCE => [AccountType::INITIAL_BALANCE],
AccountType::BENEFICIARY => [AccountType::BENEFICIARY],
AccountType::IMPORT => [AccountType::IMPORT],
AccountType::RECONCILIATION => [AccountType::RECONCILIATION],
AccountType::LOAN => [AccountType::LOAN],
AccountType::MORTGAGE => [AccountType::MORTGAGE],
AccountType::DEBT => [AccountType::DEBT],
AccountType::CREDITCARD => [AccountType::CREDITCARD],
2018-02-11 08:08:08 +01:00
];
$return = $types['all'];
2018-02-11 08:08:08 +01:00
if (isset($types[$type])) {
$return = $types[$type];
2018-02-11 08:08:08 +01:00
}
return $return; // @codeCoverageIgnore
2018-02-11 08:08:08 +01:00
}
2018-03-05 19:35:58 +01:00
}