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

217 lines
6.0 KiB
PHP
Raw Normal View History

<?php
/**
* ReportFormRequest.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://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 13:07:10 -06:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class CategoryFormRequest.
*/
class ReportFormRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
return auth()->check();
}
/**
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getAccountList(): Collection
{
2017-09-12 14:44:31 -05:00
// fixed
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('accounts');
$collection = new Collection;
2018-04-27 23:23:13 -05:00
if (\is_array($set)) {
foreach ($set as $accountId) {
2018-04-02 08:10:40 -05:00
$account = $repository->findNull((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
}
}
return $collection;
}
/**
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getBudgetList(): Collection
{
2016-12-14 11:59:12 -06:00
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$set = $this->get('budget');
$collection = new Collection;
2018-04-27 23:23:13 -05:00
if (\is_array($set)) {
2016-12-14 11:59:12 -06:00
foreach ($set as $budgetId) {
2018-04-02 08:10:40 -05:00
$budget = $repository->findNull((int)$budgetId);
if (null !== $budget) {
2016-12-14 11:59:12 -06:00
$collection->push($budget);
}
}
}
return $collection;
}
/**
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getCategoryList(): Collection
{
2016-12-14 11:59:12 -06:00
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$set = $this->get('category');
$collection = new Collection;
2018-04-27 23:23:13 -05:00
if (\is_array($set)) {
2016-12-14 11:59:12 -06:00
foreach ($set as $categoryId) {
2018-04-02 08:10:40 -05:00
$category = $repository->findNull((int)$categoryId);
if (null !== $category) {
2016-12-14 11:59:12 -06:00
$collection->push($category);
}
}
}
return $collection;
}
2016-11-18 13:06:08 -06:00
/**
* @return Carbon
2017-11-15 05:25:49 -06:00
*
2016-11-18 13:06:08 -06:00
* @throws FireflyException
*/
public function getEndDate(): Carbon
{
$date = new Carbon;
$range = $this->get('daterange');
2018-04-02 08:10:40 -05:00
$parts = explode(' - ', (string)$range);
2018-04-27 23:23:13 -05:00
if (2 === \count($parts)) {
try {
$date = new Carbon($parts[1]);
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreStart
} catch (Exception $e) {
2018-07-08 00:59:58 -05:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
throw new FireflyException($error);
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreEnd
}
2018-03-03 10:16:47 -06:00
}
return $date;
}
2017-12-16 12:48:31 -06:00
/**
* @return Collection
*/
public function getExpenseList(): Collection
{
// fixed
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('exp_rev');
$collection = new Collection;
2018-04-27 23:23:13 -05:00
if (\is_array($set)) {
2017-12-16 12:48:31 -06:00
foreach ($set as $accountId) {
2018-04-02 08:10:40 -05:00
$account = $repository->findNull((int)$accountId);
if (null !== $account) {
2017-12-16 12:48:31 -06:00
$collection->push($account);
}
}
}
return $collection;
}
/**
* @return Carbon
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
*/
public function getStartDate(): Carbon
{
$date = new Carbon;
$range = $this->get('daterange');
2018-04-02 08:10:40 -05:00
$parts = explode(' - ', (string)$range);
2018-04-27 23:23:13 -05:00
if (2 === \count($parts)) {
try {
$date = new Carbon($parts[0]);
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreStart
} catch (Exception $e) {
2018-07-08 00:59:58 -05:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
throw new FireflyException($error);
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreEnd
}
}
return $date;
}
2017-02-15 13:07:10 -06:00
/**
* @return Collection
*/
public function getTagList(): Collection
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$set = $this->get('tag');
$collection = new Collection;
2018-04-27 23:23:13 -05:00
if (\is_array($set)) {
2017-02-15 13:07:10 -06:00
foreach ($set as $tagTag) {
$tag = $repository->findByTag($tagTag);
2017-11-15 05:25:49 -06:00
if (null !== $tag->id) {
2017-02-15 13:07:10 -06:00
$collection->push($tag);
}
}
}
return $collection;
}
/**
* @return array
*/
public function rules(): array
{
return [
2017-12-09 14:49:19 -06:00
'report_type' => 'in:audit,default,category,budget,tag,account',
];
}
}