Files
firefly-iii/app/Http/Requests/ReportFormRequest.php

234 lines
6.5 KiB
PHP
Raw Normal View History

<?php
/**
* ReportFormRequest.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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.
2017-10-21 08:40:00 +02: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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2017-02-15 20:07:10 +01:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2020-10-24 07:55:09 +02:00
use FireflyIII\Support\Request\ChecksLogin;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class CategoryFormRequest.
*/
2020-10-24 07:55:09 +02:00
class ReportFormRequest extends FormRequest
{
2020-10-24 07:55:09 +02:00
use ChecksLogin;
/**
2018-07-22 08:10:16 +02:00
* Validate list of accounts.
*
* @return Collection
*/
2016-12-14 18:59:12 +01:00
public function getAccountList(): Collection
{
2017-09-12 21:44:31 +02:00
// fixed
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('accounts');
$collection = new Collection;
if (is_array($set)) {
foreach ($set as $accountId) {
2020-10-24 07:55:09 +02:00
$account = $repository->findNull((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
}
}
return $collection;
}
/**
2018-07-22 08:10:16 +02:00
* Validate list of budgets.
*
* @return Collection
*/
2016-12-14 18:59:12 +01:00
public function getBudgetList(): Collection
{
2016-12-14 18:59:12 +01:00
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$set = $this->get('budget');
$collection = new Collection;
if (is_array($set)) {
2016-12-14 18:59:12 +01:00
foreach ($set as $budgetId) {
2020-10-24 07:55:09 +02:00
$budget = $repository->findNull((int)$budgetId);
if (null !== $budget) {
2016-12-14 18:59:12 +01:00
$collection->push($budget);
}
}
}
return $collection;
}
/**
2018-07-22 08:10:16 +02:00
* Validate list of categories.
*
* @return Collection
*/
2016-12-14 18:59:12 +01:00
public function getCategoryList(): Collection
{
2016-12-14 18:59:12 +01:00
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$set = $this->get('category');
$collection = new Collection;
if (is_array($set)) {
2016-12-14 18:59:12 +01:00
foreach ($set as $categoryId) {
2020-10-24 07:55:09 +02:00
$category = $repository->findNull((int)$categoryId);
if (null !== $category) {
2016-12-14 18:59:12 +01:00
$collection->push($category);
}
}
}
return $collection;
}
/**
* Validate list of accounts which exist twice in system.
*
* @return Collection
*/
public function getDoubleList(): Collection
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('double');
$collection = new Collection;
if (is_array($set)) {
foreach ($set as $accountId) {
2020-10-24 07:55:09 +02:00
$account = $repository->findNull((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
}
}
return $collection;
}
2016-11-18 20:06:08 +01:00
/**
2018-07-22 08:10:16 +02:00
* Validate end date.
*
2016-11-18 20:06:08 +01:00
* @return Carbon
2017-11-15 12:25:49 +01:00
*
2020-10-24 07:55:09 +02:00
* @throws FireflyException
2016-11-18 20:06:08 +01:00
*/
public function getEndDate(): Carbon
{
2020-09-11 07:12:33 +02:00
$date = today(config('app.timezone'));
$range = $this->get('daterange');
2020-10-24 07:55:09 +02:00
$parts = explode(' - ', (string)$range);
if (2 === count($parts)) {
try {
$date = new Carbon($parts[1]);
2021-04-07 07:28:43 +02:00
} catch (Exception $e) {
2018-07-08 07:59:58 +02:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
2021-04-07 07:28:43 +02:00
throw new FireflyException($error, 0, $e);
}
2018-03-03 17:16:47 +01:00
}
return $date;
}
/**
2018-07-22 08:10:16 +02:00
* Validate start date.
*
* @return Carbon
2017-11-15 12:25:49 +01:00
*
2020-10-24 07:55:09 +02:00
* @throws FireflyException
*/
public function getStartDate(): Carbon
{
2020-09-11 07:12:33 +02:00
$date = today(config('app.timezone'));
$range = $this->get('daterange');
2020-10-24 07:55:09 +02:00
$parts = explode(' - ', (string)$range);
if (2 === count($parts)) {
try {
$date = new Carbon($parts[0]);
2021-04-07 07:28:43 +02:00
} catch (Exception $e) {
2018-07-08 07:59:58 +02:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
2021-04-07 07:28:43 +02:00
throw new FireflyException($error, 0, $e);
}
}
return $date;
}
2017-02-15 20:07:10 +01:00
/**
2018-07-22 08:10:16 +02:00
* Validate list of tags.
*
2017-02-15 20:07:10 +01:00
* @return Collection
*/
public function getTagList(): Collection
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$set = $this->get('tag');
$collection = new Collection;
2018-09-15 13:44:36 +02:00
Log::debug('Set is:', $set ?? []);
if (is_array($set)) {
2017-02-15 20:07:10 +01:00
foreach ($set as $tagTag) {
2018-09-15 13:44:36 +02:00
Log::debug(sprintf('Now searching for "%s"', $tagTag));
2017-02-15 20:07:10 +01:00
$tag = $repository->findByTag($tagTag);
2018-07-25 06:45:25 +02:00
if (null !== $tag) {
2017-02-15 20:07:10 +01:00
$collection->push($tag);
2018-08-17 05:54:29 +02:00
continue;
}
2020-10-24 07:55:09 +02:00
$tag = $repository->findNull((int)$tagTag);
2018-08-17 05:54:29 +02:00
if (null !== $tag) {
$collection->push($tag);
2017-02-15 20:07:10 +01:00
}
}
}
return $collection;
}
/**
2018-07-22 08:10:16 +02:00
* Rules for this request.
*
* @return array
*/
public function rules(): array
{
return [
2019-09-03 22:35:41 +02:00
'report_type' => 'in:audit,default,category,budget,tag,double',
];
}
}