2016-11-09 14:36:54 -06:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-11-09 14:36:54 -06:00
|
|
|
/**
|
|
|
|
* ReportFormRequest.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-11-09 14:36:54 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-11-09 14:36:54 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -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.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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/>.
|
2016-11-09 14:36:54 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-11-09 14:36:54 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Exception;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-12-08 14:50:20 -06:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2016-11-09 14:36:54 -06:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2017-02-15 13:07:10 -06:00
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
2020-10-24 00:55:09 -05:00
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2016-11-09 14:36:54 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-03-28 12:37:59 -05:00
|
|
|
use Log;
|
2016-11-09 14:36:54 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class CategoryFormRequest.
|
2016-11-09 14:36:54 -06:00
|
|
|
*/
|
2020-10-24 00:55:09 -05:00
|
|
|
class ReportFormRequest extends FormRequest
|
2016-11-09 14:36:54 -06:00
|
|
|
{
|
2020-10-24 00:55:09 -05:00
|
|
|
use ChecksLogin;
|
|
|
|
|
2016-11-09 14:36:54 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate list of accounts.
|
|
|
|
*
|
2016-11-09 14:36:54 -06:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-12-14 11:59:12 -06:00
|
|
|
public function getAccountList(): Collection
|
2016-11-09 14:36:54 -06:00
|
|
|
{
|
2017-09-12 14:44:31 -05:00
|
|
|
// fixed
|
2016-11-09 14:36:54 -06:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$set = $this->get('accounts');
|
2022-10-30 08:24:28 -05:00
|
|
|
$collection = new Collection();
|
2019-06-22 06:09:25 -05:00
|
|
|
if (is_array($set)) {
|
2016-11-09 14:36:54 -06:00
|
|
|
foreach ($set as $accountId) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$account = $repository->find((int)$accountId);
|
2018-02-28 13:23:45 -06:00
|
|
|
if (null !== $account) {
|
2016-11-09 14:36:54 -06:00
|
|
|
$collection->push($account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate list of budgets.
|
|
|
|
*
|
2016-11-09 14:36:54 -06:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-12-14 11:59:12 -06:00
|
|
|
public function getBudgetList(): Collection
|
2016-11-09 14:36:54 -06:00
|
|
|
{
|
2016-12-14 11:59:12 -06:00
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
|
|
|
$set = $this->get('budget');
|
2022-10-30 08:24:28 -05:00
|
|
|
$collection = new Collection();
|
2019-06-22 06:09:25 -05:00
|
|
|
if (is_array($set)) {
|
2016-12-14 11:59:12 -06:00
|
|
|
foreach ($set as $budgetId) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$budget = $repository->find((int)$budgetId);
|
2018-02-28 13:23:45 -06:00
|
|
|
if (null !== $budget) {
|
2016-12-14 11:59:12 -06:00
|
|
|
$collection->push($budget);
|
2016-11-09 14:36:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2016-12-08 14:50:20 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate list of categories.
|
|
|
|
*
|
2016-12-08 14:50:20 -06:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-12-14 11:59:12 -06:00
|
|
|
public function getCategoryList(): Collection
|
2016-12-08 14:50:20 -06:00
|
|
|
{
|
2016-12-14 11:59:12 -06:00
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$set = $this->get('category');
|
2022-10-30 08:24:28 -05:00
|
|
|
$collection = new Collection();
|
2019-06-22 06:09:25 -05:00
|
|
|
if (is_array($set)) {
|
2016-12-14 11:59:12 -06:00
|
|
|
foreach ($set as $categoryId) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$category = $repository->find((int)$categoryId);
|
2018-02-28 13:23:45 -06:00
|
|
|
if (null !== $category) {
|
2016-12-14 11:59:12 -06:00
|
|
|
$collection->push($category);
|
2016-12-08 14:50:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:02:57 -05:00
|
|
|
/**
|
|
|
|
* 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');
|
2022-10-30 08:24:28 -05:00
|
|
|
$collection = new Collection();
|
2020-03-17 09:02:57 -05:00
|
|
|
if (is_array($set)) {
|
|
|
|
foreach ($set as $accountId) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$account = $repository->find((int)$accountId);
|
2020-03-17 09:02:57 -05:00
|
|
|
if (null !== $account) {
|
|
|
|
$collection->push($account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2016-11-18 13:06:08 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate end date.
|
|
|
|
*
|
2016-11-18 13:06:08 -06:00
|
|
|
* @return Carbon
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2020-10-24 00:55:09 -05:00
|
|
|
* @throws FireflyException
|
2016-11-18 13:06:08 -06:00
|
|
|
*/
|
2016-11-09 14:36:54 -06:00
|
|
|
public function getEndDate(): Carbon
|
|
|
|
{
|
2020-09-11 00:12:33 -05:00
|
|
|
$date = today(config('app.timezone'));
|
2016-11-09 14:36:54 -06:00
|
|
|
$range = $this->get('daterange');
|
2022-12-29 12:42:26 -06:00
|
|
|
$parts = explode(' - ', (string)$range);
|
2019-05-30 05:31:19 -05:00
|
|
|
if (2 === count($parts)) {
|
2022-01-26 11:43:14 -06:00
|
|
|
$string = $parts[1];
|
|
|
|
// validate as date
|
|
|
|
// if regex for YYYY-MM-DD:
|
|
|
|
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][\d]|3[01])$/';
|
|
|
|
if (preg_match($pattern, $string)) {
|
|
|
|
try {
|
|
|
|
$date = new Carbon($parts[1]);
|
2022-12-30 13:25:04 -06:00
|
|
|
} catch (Exception $e) { // intentional generic exception
|
2022-01-26 11:43:14 -06:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
|
|
|
|
Log::error($error);
|
|
|
|
throw new FireflyException($error, 0, $e);
|
|
|
|
}
|
|
|
|
return $date;
|
2016-11-09 14:36:54 -06:00
|
|
|
}
|
2022-01-26 11:43:14 -06:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
|
|
|
|
Log::error($error);
|
|
|
|
throw new FireflyException($error, 0);
|
2016-11-09 14:36:54 -06:00
|
|
|
}
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate start date.
|
|
|
|
*
|
2016-11-09 14:36:54 -06:00
|
|
|
* @return Carbon
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2020-10-24 00:55:09 -05:00
|
|
|
* @throws FireflyException
|
2016-11-09 14:36:54 -06:00
|
|
|
*/
|
|
|
|
public function getStartDate(): Carbon
|
|
|
|
{
|
2020-09-11 00:12:33 -05:00
|
|
|
$date = today(config('app.timezone'));
|
2016-11-09 14:36:54 -06:00
|
|
|
$range = $this->get('daterange');
|
2022-12-29 12:42:26 -06:00
|
|
|
$parts = explode(' - ', (string)$range);
|
2019-05-30 05:31:19 -05:00
|
|
|
if (2 === count($parts)) {
|
2022-01-26 11:43:14 -06:00
|
|
|
$string = $parts[0];
|
|
|
|
// validate as date
|
|
|
|
// if regex for YYYY-MM-DD:
|
|
|
|
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][\d]|3[01])$/';
|
|
|
|
if (preg_match($pattern, $string)) {
|
|
|
|
try {
|
2022-01-30 10:37:44 -06:00
|
|
|
$date = new Carbon($parts[0]);
|
2022-12-30 13:25:04 -06:00
|
|
|
} catch (Exception $e) { // intentional generic exception
|
2022-01-26 11:43:14 -06:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
|
|
|
|
Log::error($error);
|
|
|
|
throw new FireflyException($error, 0, $e);
|
|
|
|
}
|
|
|
|
return $date;
|
2016-11-09 14:36:54 -06:00
|
|
|
}
|
2022-01-26 11:43:14 -06:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
|
|
|
|
Log::error($error);
|
|
|
|
throw new FireflyException($error, 0);
|
2016-11-09 14:36:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
2017-02-15 13:07:10 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Validate list of tags.
|
|
|
|
*
|
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');
|
2022-10-30 08:24:28 -05:00
|
|
|
$collection = new Collection();
|
2023-01-20 15:08:18 -06:00
|
|
|
if (is_array($set)) {
|
|
|
|
Log::debug('Set is:', $set);
|
|
|
|
}
|
|
|
|
if (!is_array($set)) {
|
|
|
|
Log::error(sprintf('Set is not an array! "%s"', $set));
|
|
|
|
}
|
2019-06-22 06:09:25 -05:00
|
|
|
if (is_array($set)) {
|
2017-02-15 13:07:10 -06:00
|
|
|
foreach ($set as $tagTag) {
|
2018-09-15 06:44:36 -05:00
|
|
|
Log::debug(sprintf('Now searching for "%s"', $tagTag));
|
2017-02-15 13:07:10 -06:00
|
|
|
$tag = $repository->findByTag($tagTag);
|
2018-07-24 23:45:25 -05:00
|
|
|
if (null !== $tag) {
|
2017-02-15 13:07:10 -06:00
|
|
|
$collection->push($tag);
|
2018-08-16 22:54:29 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-29 12:42:26 -06:00
|
|
|
$tag = $repository->find((int)$tagTag);
|
2018-08-16 22:54:29 -05:00
|
|
|
if (null !== $tag) {
|
|
|
|
$collection->push($tag);
|
2017-02-15 13:07:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2016-11-09 14:36:54 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Rules for this request.
|
|
|
|
*
|
2016-11-09 14:36:54 -06:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2019-09-03 15:35:41 -05:00
|
|
|
'report_type' => 'in:audit,default,category,budget,tag,double',
|
2016-11-09 14:36:54 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|