Files
firefly-iii/app/Api/V1/Controllers/Models/Bill/ListController.php

205 lines
7.3 KiB
PHP
Raw Normal View History

2018-02-04 13:41:42 +01:00
<?php
2021-03-06 12:45:49 +01:00
/*
* ListController.php
* Copyright (c) 2021 james@firefly-iii.org
2018-02-04 13:41:42 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-04 13:41:42 +01: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-02-04 13:41:42 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-02-04 13:41:42 +01: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-02-04 13:41:42 +01: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-02-04 13:41:42 +01:00
*/
2021-03-28 11:39:26 +02:00
declare(strict_types=1);
2018-05-11 10:08:34 +02:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Models\Bill;
2018-02-04 13:41:42 +01:00
2021-03-06 12:45:49 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
2021-09-18 10:20:19 +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\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-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-04 15:57:35 +01:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
2018-02-04 13:41:42 +01:00
/**
2021-03-06 12:45:49 +01:00
* Class ListController
2018-02-04 13:41:42 +01:00
*/
2021-03-06 12:45:49 +01:00
class ListController extends Controller
2018-02-04 13:41:42 +01:00
{
2018-12-09 13:09:43 +01:00
use TransactionFilter;
2020-07-31 09:42:00 +02:00
2020-10-27 16:26:10 +01:00
private BillRepositoryInterface $repository;
/**
* BillController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
2018-02-09 14:47:37 +01:00
$this->middleware(
function ($request, $next) {
$this->repository = app(BillRepositoryInterface::class);
2021-04-05 21:52:55 +02:00
$this->repository->setUser(auth()->user());
2018-02-09 14:47:37 +01:00
return $next($request);
}
);
}
2018-12-09 13:09:43 +01:00
/**
2021-09-19 08:27:52 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/bills/listAttachmentByBill
*
2018-12-09 13:09:43 +01:00
* Display a listing of the resource.
*
2019-09-04 17:39:39 +02:00
* @param Bill $bill
2018-12-09 13:09:43 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
2019-09-04 17:39:39 +02:00
public function attachments(Bill $bill): JsonResponse
2018-12-09 13:09:43 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2022-03-29 14:56:27 +02:00
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-09 13:09:43 +01:00
$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());
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));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-09 13:09:43 +01:00
}
/**
2021-09-19 08:27:52 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/bills/listRuleByBill
*
2018-12-09 13:09:43 +01:00
* List all of them.
*
2019-09-04 17:39:39 +02:00
* @param Bill $bill
2018-12-09 13:09:43 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
2019-09-04 17:39:39 +02:00
public function rules(Bill $bill): JsonResponse
2018-12-09 13:09:43 +01:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-09 13:09:43 +01:00
// types to get, page size:
2022-03-29 14:56:27 +02:00
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-09 13:09:43 +01:00
// 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());
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));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-09 13:09:43 +01:00
}
2018-02-09 14:47:37 +01:00
2018-12-09 13:09:43 +01:00
/**
2021-09-19 08:27:52 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/bills/listTransactionByBill
*
2018-12-09 13:09:43 +01:00
* Show all transactions.
*
* @param Request $request
*
2019-08-23 21:54:47 +02:00
* @param Bill $bill
2018-12-09 13:09:43 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
* @codeCoverageIgnore
2018-12-09 13:09:43 +01:00
*/
public function transactions(Request $request, Bill $bill): JsonResponse
{
2022-03-29 14:56:27 +02:00
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-12-09 13:09:43 +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-09 13:09:43 +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)
// 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 TransactionGroupTransformer $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));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-12-09 13:09:43 +01:00
}
2018-02-11 07:46:34 +01:00
2021-03-28 11:39:26 +02:00
}