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

275 lines
9.2 KiB
PHP
Raw Normal View History

2018-06-28 15:15:22 -05:00
<?php
/**
* RecurrenceController.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-28 15:15:22 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-28 15:15:22 -05: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-06-28 15:15:22 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-06-28 15:15:22 -05: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-06-28 15:15:22 -05: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-06-28 15:15:22 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\RecurrenceStoreRequest;
use FireflyIII\Api\V1\Requests\RecurrenceUpdateRequest;
2018-12-07 13:20:54 -06:00
use FireflyIII\Exceptions\FireflyException;
2019-03-25 09:14:09 -05:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-06-29 12:27:07 -05:00
use FireflyIII\Models\Recurrence;
2018-06-29 07:07:33 -05:00
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
2018-12-07 13:20:54 -06:00
use FireflyIII\Support\Cronjobs\RecurringCronjob;
2018-12-08 14:26:20 -06:00
use FireflyIII\Support\Http\Api\TransactionFilter;
2018-06-29 07:07:33 -05:00
use FireflyIII\Transformers\RecurrenceTransformer;
2019-03-25 09:14:09 -05:00
use FireflyIII\Transformers\TransactionGroupTransformer;
2018-06-28 15:15:22 -05:00
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2018-06-29 07:07:33 -05:00
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
2018-06-29 12:27:07 -05:00
use League\Fractal\Resource\Item;
2018-12-07 13:20:54 -06:00
use Log;
2018-06-28 15:15:22 -05:00
2018-06-29 07:07:33 -05:00
/**
* Class RecurrenceController
*/
class RecurrenceController extends Controller
2018-06-28 15:15:22 -05:00
{
2018-12-08 14:26:20 -06:00
use TransactionFilter;
/** @var RecurringRepositoryInterface The recurring transaction repository */
2018-06-29 07:07:33 -05:00
private $repository;
/**
* RecurrenceController constructor.
*
* @codeCoverageIgnore
*/
2018-06-28 15:15:22 -05:00
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
2018-06-29 07:07:33 -05:00
/** @var RecurringRepositoryInterface repository */
$this->repository = app(RecurringRepositoryInterface::class);
$this->repository->setUser($user);
2018-06-28 15:15:22 -05:00
return $next($request);
}
);
}
/**
* Delete the resource.
*
2018-07-02 13:02:20 -05:00
* @param Recurrence $recurrence
2018-06-28 15:15:22 -05:00
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 15:15:22 -05:00
*/
2018-06-29 12:27:07 -05:00
public function delete(Recurrence $recurrence): JsonResponse
2018-06-28 15:15:22 -05:00
{
2018-06-29 12:27:07 -05:00
$this->repository->destroy($recurrence);
2018-06-28 15:15:22 -05:00
return response()->json([], 204);
}
/**
* List all of them.
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 15:15:22 -05:00
*/
2019-09-04 10:39:39 -05:00
public function index(): JsonResponse
2018-06-28 15:15:22 -05:00
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-29 07:07:33 -05:00
// 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->getAll();
$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.recurrences.index') . $this->buildParams());
2018-12-15 15:03:05 -06:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($piggyBanks, $transformer, 'recurrences');
2018-06-29 07:07:33 -05:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-06-28 15:15:22 -05:00
}
/**
* List single resource.
*
2018-06-29 12:27:07 -05:00
* @param Recurrence $recurrence
2018-06-28 15:15:22 -05:00
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 15:15:22 -05:00
*/
2019-09-04 10:39:39 -05:00
public function show(Recurrence $recurrence): JsonResponse
2018-06-28 15:15:22 -05:00
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-29 12:27:07 -05:00
2018-12-15 15:03:05 -06:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($recurrence, $transformer, 'recurrences');
2018-06-29 12:27:07 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-06-28 15:15:22 -05:00
}
/**
* Store new object.
*
* @param RecurrenceStoreRequest $request
2018-06-28 15:15:22 -05:00
*
* @return JsonResponse
2019-09-04 10:39:39 -05:00
* @throws FireflyException
2018-06-28 15:15:22 -05:00
*/
public function store(RecurrenceStoreRequest $request): JsonResponse
2018-06-28 15:15:22 -05:00
{
$data = $request->getAll();
$recurrence = $this->repository->store($data);
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-12-15 15:03:05 -06:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($recurrence, $transformer, 'recurrences');
2018-06-28 15:15:22 -05:00
2018-06-29 12:27:07 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-06-28 15:15:22 -05:00
}
2018-12-07 13:20:54 -06:00
/**
* Show transactions for this recurrence.
*
* @param Request $request
2018-12-07 13:20:54 -06:00
* @param Recurrence $recurrence
*
* @return JsonResponse
* @codeCoverageIgnore
2018-12-07 13:20:54 -06:00
*/
public function transactions(Request $request, Recurrence $recurrence): JsonResponse
{
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$type = $request->get('type') ?? 'default';
$this->parameters->set('type', $type);
2018-12-08 14:26:20 -06:00
$types = $this->mapTransactionTypes($this->parameters->get('type'));
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-12-07 13:20:54 -06:00
// whatever is returned by the query, it must be part of these journals:
$journalIds = $this->repository->getJournalIds($recurrence);
/** @var User $admin */
$admin = auth()->user();
2019-03-25 09:14:09 -05:00
// use new group collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector
->setUser($admin)
// filter on journal IDs.
->setJournalIds($journalIds)
// 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-07 13:20:54 -06: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 09:14:09 -05:00
$paginator = $collector->getPaginatedGroups();
2018-12-07 13:20:54 -06:00
$paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
$transactions = $paginator->getCollection();
2018-12-15 15:03:05 -06:00
2019-03-25 09:14:09 -05:00
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
2018-12-15 15:03:05 -06:00
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($transactions, $transformer, 'transactions');
2018-12-07 13:20:54 -06:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* @return JsonResponse
* @throws FireflyException
* @codeCoverageIgnore
2018-12-07 13:20:54 -06:00
*/
public function trigger(): JsonResponse
{
2018-12-10 14:45:44 -06:00
/** @var RecurringCronjob $recurring */
$recurring = app(RecurringCronjob::class);
2018-12-07 13:20:54 -06:00
try {
$result = $recurring->fire();
} catch (FireflyException $e) {
Log::error($e->getMessage());
2019-11-02 02:19:50 -05:00
throw new FireflyException('200022: Error in cron job.');
2018-12-07 13:20:54 -06:00
}
if (false === $result) {
return response()->json([], 204);
}
if (true === $result) {
2019-02-13 10:38:41 -06:00
return response()->json();
2018-12-07 13:20:54 -06:00
}
2018-12-15 15:03:05 -06:00
2018-12-10 14:45:44 -06:00
return response()->json([], 418); // @codeCoverageIgnore
2018-12-07 13:20:54 -06:00
}
2018-06-28 15:15:22 -05:00
/**
* Update single recurrence.
*
* @param RecurrenceUpdateRequest $request
* @param Recurrence $recurrence
2018-06-28 15:15:22 -05:00
*
* @return JsonResponse
*/
public function update(RecurrenceUpdateRequest $request, Recurrence $recurrence): JsonResponse
2018-06-28 15:15:22 -05:00
{
$data = $request->getAll();
$category = $this->repository->update($recurrence, $data);
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-12-15 15:03:05 -06:00
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters($this->parameters);
2018-12-15 15:03:05 -06:00
$resource = new Item($category, $transformer, 'recurrences');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-06-28 15:15:22 -05:00
}
}