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

342 lines
12 KiB
PHP
Raw Normal View History

2018-02-04 13:41:42 +01:00
<?php
2018-05-11 10:08:34 +02:00
2018-02-04 13:41:42 +01:00
/**
* BillController.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-04 13:41:42 +01:00
namespace FireflyIII\Api\V1\Controllers;
2018-02-10 09:57:31 +01:00
use FireflyIII\Api\V1\Requests\BillRequest;
2018-04-03 19:12:59 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-03-25 15:14:09 +01:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-02-04 13:41:42 +01:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2018-12-09 13:09:43 +01:00
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\BillTransformer;
2018-12-09 13:09:43 +01:00
use FireflyIII\Transformers\RuleTransformer;
2019-03-25 15:14:09 +01:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2018-12-09 13:09:43 +01:00
use FireflyIII\Transformers\TransactionTransformer;
2018-07-05 06:10:35 +02:00
use FireflyIII\User;
2018-04-15 19:20:24 +02:00
use Illuminate\Http\JsonResponse;
2018-02-04 13:41:42 +01:00
use Illuminate\Http\Request;
2018-12-09 13:09:43 +01:00
use Illuminate\Pagination\LengthAwarePaginator;
2018-02-06 07:49:56 +01:00
use Illuminate\Support\Collection;
2018-02-04 15:57:35 +01:00
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
2018-02-04 15:57:35 +01:00
use League\Fractal\Serializer\JsonApiSerializer;
2018-02-04 13:41:42 +01:00
/**
* Class BillController.
2018-02-04 13:41:42 +01:00
*/
class BillController extends Controller
{
2018-12-09 13:09:43 +01:00
use TransactionFilter;
/** @var BillRepositoryInterface The bill repository */
private $repository;
/**
* BillController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
2018-02-09 14:47:37 +01:00
$this->middleware(
function ($request, $next) {
2018-07-05 06:10:35 +02:00
/** @var User $admin */
$admin = auth()->user();
2018-02-09 14:47:37 +01:00
/** @var BillRepositoryInterface repository */
$this->repository = app(BillRepositoryInterface::class);
2018-07-05 06:10:35 +02:00
$this->repository->setUser($admin);
2018-02-09 14:47:37 +01:00
return $next($request);
}
);
}
2018-12-09 13:09:43 +01:00
/**
* Display a listing of the resource.
*
* @param Request $request
* @param Bill $bill
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
public function attachments(Request $request, Bill $bill): JsonResponse
{
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$collection = $this->repository->getAttachments($bill);
$count = $collection->count();
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.bills.attachments', [$bill->id]) . $this->buildParams());
// present to user.
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-12-15 22:03:05 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments');
2018-12-09 13:09:43 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
2018-02-04 13:41:42 +01:00
/**
* Remove the specified resource from storage.
*
* @param Bill $bill
2018-02-04 13:41:42 +01:00
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
* @codeCoverageIgnore
2018-02-04 13:41:42 +01:00
*/
2018-04-15 19:20:24 +02:00
public function delete(Bill $bill): JsonResponse
2018-02-04 13:41:42 +01:00
{
$this->repository->destroy($bill);
2018-02-09 19:11:55 +01:00
2018-02-10 09:57:31 +01:00
return response()->json([], 204);
2018-02-04 13:41:42 +01:00
}
/**
* Display a listing of the resource.
*
* @param Request $request
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
* @codeCoverageIgnore
2018-02-04 13:41:42 +01:00
*/
2018-04-15 19:20:24 +02:00
public function index(Request $request): JsonResponse
2018-02-04 13:41:42 +01:00
{
2018-06-20 21:17:16 +02:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$paginator = $this->repository->getPaginator($pageSize);
2018-02-06 07:49:56 +01:00
/** @var Collection $bills */
$bills = $paginator->getCollection();
2018-02-04 15:57:35 +01:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
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-02-04 15:57:35 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-04 13:41:42 +01:00
}
2018-12-09 13:09:43 +01:00
/**
* List all of them.
*
* @param Request $request
* @param Bill $bill
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
public function rules(Request $request, Bill $bill): JsonResponse
{
// create some objects:
$manager = new Manager;
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
// types to get, page size:
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
// get list of budgets. Count it and split it.
$collection = $this->repository->getRulesForBill($bill);
$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.bills.rules', [$bill->id]) . $this->buildParams());
// present to user.
$manager->setSerializer(new JsonApiSerializer($baseUrl));
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-09 13:09:43 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
2018-02-09 14:47:37 +01:00
2018-02-04 13:41:42 +01:00
/**
* Show the specified bill.
*
2018-02-09 14:47:37 +01:00
* @param Request $request
* @param Bill $bill
2018-02-04 13:41:42 +01:00
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
* @codeCoverageIgnore
2018-02-04 13:41:42 +01:00
*/
2018-04-15 19:20:24 +02:00
public function show(Request $request, Bill $bill): JsonResponse
2018-02-04 13:41:42 +01:00
{
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-12-15 22:03:05 +01:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($bill, $transformer, 'bills');
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-04 13:41:42 +01:00
}
/**
* Store a bill.
*
* @param BillRequest $request
2018-02-04 13:41:42 +01:00
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
2018-04-03 19:12:59 +02:00
* @throws FireflyException
2018-02-04 13:41:42 +01:00
*/
2018-04-15 19:20:24 +02:00
public function store(BillRequest $request): JsonResponse
2018-02-04 13:41:42 +01:00
{
2018-04-15 19:20:24 +02:00
$bill = $this->repository->store($request->getAll());
if (null !== $bill) {
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-02-10 09:57:31 +01:00
2018-12-15 22:03:05 +01:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($bill, $transformer, 'bills');
2018-02-10 09:57:31 +01:00
2018-04-15 19:20:24 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-04-03 19:12:59 +02:00
}
2018-04-15 19:20:24 +02:00
throw new FireflyException('Could not store new bill.'); // @codeCoverageIgnore
2018-02-10 09:57:31 +01:00
2018-02-04 13:41:42 +01:00
}
2018-12-09 13:09:43 +01:00
/**
* Show all transactions.
*
* @param Request $request
*
* @param Bill $bill
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
public function transactions(Request $request, Bill $bill): JsonResponse
{
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$type = $request->get('type') ?? 'default';
$this->parameters->set('type', $type);
$types = $this->mapTransactionTypes($this->parameters->get('type'));
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
/** @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)
// include source + destination account name and type.
->setBill($bill)
// 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);
// do parameter stuff on new group collector.
2018-12-09 13:09:43 +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
// get paginator.
$paginator = $collector->getPaginatedGroups();
2018-12-09 13:09:43 +01:00
$paginator->setPath(route('api.v1.bills.transactions', [$bill->id]) . $this->buildParams());
$transactions = $paginator->getCollection();
2018-12-15 22:03:05 +01:00
/** @var TransactionTransformer $transformer */
2019-03-25 15:14:09 +01:00
$transformer = app(TransactionGroupTransformer::class);
2018-12-15 22:03:05 +01:00
$transformer->setParameters($this->parameters);
2018-12-21 16:38:10 +01:00
$resource = new FractalCollection($transactions, $transformer, 'transactions');
2018-12-09 13:09:43 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
2018-02-11 07:46:34 +01:00
2018-02-04 13:41:42 +01:00
/**
* Update a bill.
*
* @param BillRequest $request
* @param Bill $bill
2018-02-04 13:41:42 +01:00
*
2018-04-15 19:20:24 +02:00
* @return JsonResponse
2018-02-04 13:41:42 +01:00
*/
2018-04-15 19:20:24 +02:00
public function update(BillRequest $request, Bill $bill): JsonResponse
2018-02-04 13:41:42 +01:00
{
$data = $request->getAll();
$bill = $this->repository->update($bill, $data);
2018-02-10 09:57:31 +01:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
2018-12-15 22:03:05 +01:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($bill, $transformer, 'bills');
2018-02-10 09:57:31 +01:00
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-10 09:57:31 +01:00
2018-02-04 13:41:42 +01:00
}
}