firefly-iii/app/Api/V1/Controllers/TransactionController.php

334 lines
12 KiB
PHP
Raw Normal View History

2018-02-13 14:04:15 -06:00
<?php
/**
* TransactionController.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
2018-02-16 09:42:13 -06:00
use FireflyIII\Api\V1\Requests\TransactionRequest;
2018-02-18 03:31:15 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2018-02-16 09:42:13 -06:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2018-02-16 09:42:13 -06:00
use FireflyIII\Models\TransactionType;
2018-02-13 14:04:15 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-02-21 11:42:15 -06:00
use FireflyIII\Services\Internal\Update\JournalUpdateService;
2018-02-16 09:42:13 -06:00
use FireflyIII\Transformers\TransactionTransformer;
2018-02-13 14:04:15 -06:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
2018-02-16 09:42:13 -06:00
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;
2018-02-13 14:04:15 -06:00
use Preferences;
/**
* Class TransactionController
*/
class TransactionController extends Controller
{
/** @var JournalRepositoryInterface */
private $repository;
2018-02-16 09:42:13 -06:00
2018-02-13 14:04:15 -06:00
/**
* TransactionController constructor.
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var JournalRepositoryInterface repository */
$this->repository = app(JournalRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* Remove the specified resource from storage.
*
2018-02-16 09:42:13 -06:00
* @param \FireflyIII\Models\Transaction $transaction
2018-02-13 14:04:15 -06:00
*
* @return \Illuminate\Http\Response
*/
2018-02-16 09:42:13 -06:00
public function delete(Transaction $transaction)
2018-02-13 14:04:15 -06:00
{
2018-02-16 09:42:13 -06:00
$journal = $transaction->transactionJournal;
$this->repository->delete($journal);
2018-02-13 14:04:15 -06:00
return response()->json([], 204);
}
/**
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
2018-02-16 09:42:13 -06:00
$pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data);
// read type from URI
$type = $request->get('type') ?? 'default';
$this->parameters->set('type', $type);
// types to get, page size:
$types = $this->mapTypes($this->parameters->get('type'));
2018-02-13 14:04:15 -06:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-02-16 09:42:13 -06:00
// collect transactions using the journal collector
2018-02-18 03:31:15 -06:00
$collector = app(JournalCollectorInterface::class);
2018-02-16 09:42:13 -06:00
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setAllAssetAccounts();
// remove internal transfer filter:
if (in_array(TransactionType::TRANSFER, $types)) {
$collector->removeFilter(InternalTransferFilter::class);
}
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
}
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
$collector->setTypes($types);
$paginator = $collector->getPaginatedJournals();
$paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
$transactions = $paginator->getCollection();
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
2018-02-13 14:04:15 -06:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2018-02-18 03:31:15 -06:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-13 14:04:15 -06:00
}
/**
2018-02-16 09:42:13 -06:00
* @param Request $request
* @param Transaction $transaction
2018-02-13 14:04:15 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
2018-02-16 09:42:13 -06:00
public function show(Request $request, Transaction $transaction)
2018-02-13 14:04:15 -06:00
{
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-02-16 09:42:13 -06:00
// add include parameter:
$include = $request->get('include') ?? '';
$manager->parseIncludes($include);
// collect transactions using the journal collector
2018-02-18 03:31:15 -06:00
$collector = app(JournalCollectorInterface::class);
2018-02-16 09:42:13 -06:00
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$transaction->transactionJournal]));
// add filter to remove transactions:
$transactionType = $transaction->transactionJournal->transactionType->type;
if ($transactionType === TransactionType::WITHDRAWAL) {
$collector->addFilter(PositiveAmountFilter::class);
}
if (!($transactionType === TransactionType::WITHDRAWAL)) {
$collector->addFilter(NegativeAmountFilter::class);
}
2018-02-18 03:31:15 -06:00
$transactions = $collector->getJournals();
$resource = new Item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
2018-02-16 09:42:13 -06:00
2018-02-18 03:31:15 -06:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-13 14:04:15 -06:00
}
/**
2018-02-16 09:42:13 -06:00
* @param TransactionRequest $request
2018-02-13 14:04:15 -06:00
*
* @return \Illuminate\Http\JsonResponse
2018-02-16 09:57:27 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2018-02-13 14:04:15 -06:00
*/
2018-02-23 09:59:21 -06:00
public function store(TransactionRequest $request, JournalRepositoryInterface $repository)
2018-02-13 14:04:15 -06:00
{
2018-02-16 09:42:13 -06:00
$data = $request->getAll();
$data['user'] = auth()->user()->id;
2018-02-23 09:59:21 -06:00
$journal = $repository->store($data);
2018-02-16 09:42:13 -06:00
2018-02-13 14:04:15 -06:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-02-16 09:57:27 -06:00
// add include parameter:
$include = $request->get('include') ?? '';
$manager->parseIncludes($include);
2018-02-13 14:04:15 -06:00
2018-02-16 09:57:27 -06:00
// collect transactions using the journal collector
2018-02-18 03:31:15 -06:00
$collector = app(JournalCollectorInterface::class);
2018-02-16 09:57:27 -06:00
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$journal]));
2018-02-13 14:04:15 -06:00
2018-02-16 09:57:27 -06:00
// add filter to remove transactions:
$transactionType = $journal->transactionType->type;
if ($transactionType === TransactionType::WITHDRAWAL) {
$collector->addFilter(PositiveAmountFilter::class);
}
if (!($transactionType === TransactionType::WITHDRAWAL)) {
$collector->addFilter(NegativeAmountFilter::class);
}
2018-02-18 03:31:15 -06:00
$transactions = $collector->getJournals();
2018-02-20 11:03:02 -06:00
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
2018-02-16 09:57:27 -06:00
2018-02-18 03:31:15 -06:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-13 14:04:15 -06:00
}
/**
* @param TransactionRequest $request
* @param TransactionJournal $journal
2018-02-13 14:04:15 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
public function update(TransactionRequest $request, Transaction $transaction)
2018-02-13 14:04:15 -06:00
{
$data = $request->getAll();
$data['user'] = auth()->user()->id;
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$journal = $service->update($transaction->transactionJournal, $data);
2018-02-13 14:04:15 -06:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
// add include parameter:
$include = $request->get('include') ?? '';
$manager->parseIncludes($include);
// needs a lot of extra data to match the journal collector. Or just expand that one.
// collect transactions using the journal collector
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$journal]));
// add filter to remove transactions:
$transactionType = $journal->transactionType->type;
if ($transactionType === TransactionType::WITHDRAWAL) {
$collector->addFilter(PositiveAmountFilter::class);
}
if (!($transactionType === TransactionType::WITHDRAWAL)) {
$collector->addFilter(NegativeAmountFilter::class);
}
$transactions = $collector->getJournals();
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
2018-02-13 14:04:15 -06:00
2018-02-18 03:31:15 -06:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-13 14:04:15 -06:00
}
2018-02-16 09:42:13 -06:00
/**
* @param string $type
*
* @return array
*/
private function mapTypes(string $type): array
{
$types = [
'all' => [
TransactionType::WITHDRAWAL,
TransactionType::DEPOSIT,
TransactionType::TRANSFER,
TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,
],
'withdrawal' => [
TransactionType::WITHDRAWAL,
],
'withdrawals' => [
TransactionType::WITHDRAWAL,
],
'expense' => [
TransactionType::WITHDRAWAL,
],
'income' => [
TransactionType::DEPOSIT,
],
'deposit' => [
TransactionType::DEPOSIT,
],
'deposits' => [
TransactionType::DEPOSIT,
],
'transfer' => [
TransactionType::TRANSFER,
],
'transfers' => [
TransactionType::TRANSFER,
],
'opening_balance' => [
TransactionType::OPENING_BALANCE,
],
'reconciliation' => [
TransactionType::RECONCILIATION,
],
'reconciliations' => [
TransactionType::RECONCILIATION,
],
'special' => [
TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,
],
'specials' => [
TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,
],
'default' => [
TransactionType::WITHDRAWAL,
TransactionType::DEPOSIT,
TransactionType::TRANSFER,
],
];
if (isset($types[$type])) {
return $types[$type];
}
2018-02-18 03:31:15 -06:00
return $types['default']; // @codeCoverageIgnore
2018-02-16 09:42:13 -06:00
}
2018-02-13 14:04:15 -06:00
}