2016-11-09 21:36:54 +01:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
/**
|
|
|
|
* ReportFormRequest.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-11-09 21:36:54 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-11-09 21:36:54 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Exception;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-12-08 21:50:20 +01:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2016-11-09 21:36:54 +01:00
|
|
|
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;
|
2016-11-09 21:36:54 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-09 21:03:26 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Validation\Validator;
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class CategoryFormRequest.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
2020-10-24 07:55:09 +02:00
|
|
|
class ReportFormRequest extends FormRequest
|
2016-11-09 21:36:54 +01:00
|
|
|
{
|
2020-10-24 07:55:09 +02:00
|
|
|
use ChecksLogin;
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Validate list of accounts.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
2016-12-14 18:59:12 +01:00
|
|
|
public function getAccountList(): Collection
|
2016-11-09 21:36:54 +01:00
|
|
|
{
|
2017-09-12 21:44:31 +02:00
|
|
|
// fixed
|
2016-11-09 21:36:54 +01:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$set = $this->get('accounts');
|
2022-10-30 14:24:28 +01:00
|
|
|
$collection = new Collection();
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($set)) {
|
2016-11-09 21:36:54 +01:00
|
|
|
foreach ($set as $accountId) {
|
2023-12-31 06:20:29 +01:00
|
|
|
$account = $repository->find((int) $accountId);
|
2018-02-28 20:23:45 +01:00
|
|
|
if (null !== $account) {
|
2016-11-09 21:36:54 +01:00
|
|
|
$collection->push($account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Validate list of budgets.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
2016-12-14 18:59:12 +01:00
|
|
|
public function getBudgetList(): Collection
|
2016-11-09 21:36:54 +01:00
|
|
|
{
|
2016-12-14 18:59:12 +01:00
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
|
|
|
$set = $this->get('budget');
|
2022-10-30 14:24:28 +01:00
|
|
|
$collection = new Collection();
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($set)) {
|
2016-12-14 18:59:12 +01:00
|
|
|
foreach ($set as $budgetId) {
|
2023-12-31 06:20:29 +01:00
|
|
|
$budget = $repository->find((int) $budgetId);
|
2018-02-28 20:23:45 +01:00
|
|
|
if (null !== $budget) {
|
2016-12-14 18:59:12 +01:00
|
|
|
$collection->push($budget);
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2016-12-08 21:50:20 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Validate list of categories.
|
2016-12-08 21:50:20 +01:00
|
|
|
*/
|
2016-12-14 18:59:12 +01:00
|
|
|
public function getCategoryList(): Collection
|
2016-12-08 21:50:20 +01:00
|
|
|
{
|
2016-12-14 18:59:12 +01:00
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$set = $this->get('category');
|
2022-10-30 14:24:28 +01:00
|
|
|
$collection = new Collection();
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($set)) {
|
2016-12-14 18:59:12 +01:00
|
|
|
foreach ($set as $categoryId) {
|
2023-12-31 06:20:29 +01:00
|
|
|
$category = $repository->find((int) $categoryId);
|
2018-02-28 20:23:45 +01:00
|
|
|
if (null !== $category) {
|
2016-12-14 18:59:12 +01:00
|
|
|
$collection->push($category);
|
2016-12-08 21:50:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:02:57 +01:00
|
|
|
/**
|
|
|
|
* Validate list of accounts which exist twice in system.
|
|
|
|
*/
|
|
|
|
public function getDoubleList(): Collection
|
|
|
|
{
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$set = $this->get('double');
|
2022-10-30 14:24:28 +01:00
|
|
|
$collection = new Collection();
|
2020-03-17 15:02:57 +01:00
|
|
|
if (is_array($set)) {
|
|
|
|
foreach ($set as $accountId) {
|
2023-12-31 06:20:29 +01:00
|
|
|
$account = $repository->find((int) $accountId);
|
2020-03-17 15:02:57 +01:00
|
|
|
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.
|
|
|
|
*
|
2020-10-24 07:55:09 +02:00
|
|
|
* @throws FireflyException
|
2016-11-18 20:06:08 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
public function getEndDate(): Carbon
|
|
|
|
{
|
2020-09-11 07:12:33 +02:00
|
|
|
$date = today(config('app.timezone'));
|
2016-11-09 21:36:54 +01:00
|
|
|
$range = $this->get('daterange');
|
2023-12-31 06:20:29 +01:00
|
|
|
$parts = explode(' - ', (string) $range);
|
2019-05-30 12:31:19 +02:00
|
|
|
if (2 === count($parts)) {
|
2024-01-01 14:43:56 +01:00
|
|
|
$string = $parts[1];
|
2022-01-26 18:43:14 +01:00
|
|
|
// 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])$/';
|
2023-12-31 06:20:29 +01:00
|
|
|
$result = preg_match($pattern, $string);
|
2023-12-21 05:07:26 +01:00
|
|
|
if (false !== $result && 0 !== $result) {
|
2022-01-26 18:43:14 +01:00
|
|
|
try {
|
|
|
|
$date = new Carbon($parts[1]);
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // intentional generic exception
|
2022-01-26 18:43:14 +01:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($error);
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
throw new FireflyException($error, 0, $e);
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
return $date;
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($error);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
throw new FireflyException($error, 0);
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Validate start date.
|
|
|
|
*
|
2020-10-24 07:55:09 +02:00
|
|
|
* @throws FireflyException
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
public function getStartDate(): Carbon
|
|
|
|
{
|
2020-09-11 07:12:33 +02:00
|
|
|
$date = today(config('app.timezone'));
|
2016-11-09 21:36:54 +01:00
|
|
|
$range = $this->get('daterange');
|
2023-12-31 06:20:29 +01:00
|
|
|
$parts = explode(' - ', (string) $range);
|
2019-05-30 12:31:19 +02:00
|
|
|
if (2 === count($parts)) {
|
2024-01-01 14:43:56 +01:00
|
|
|
$string = $parts[0];
|
2022-01-26 18:43:14 +01:00
|
|
|
// 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])$/';
|
2023-12-31 06:20:29 +01:00
|
|
|
$result = preg_match($pattern, $string);
|
2023-12-21 05:07:26 +01:00
|
|
|
if (false !== $result && 0 !== $result) {
|
2022-01-26 18:43:14 +01:00
|
|
|
try {
|
2022-01-30 17:37:44 +01:00
|
|
|
$date = new Carbon($parts[0]);
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Exception $e) { // intentional generic exception
|
2022-01-26 18:43:14 +01:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($error);
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
throw new FireflyException($error, 0, $e);
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
return $date;
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($error);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-01-26 18:43:14 +01:00
|
|
|
throw new FireflyException($error, 0);
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function getTagList(): Collection
|
|
|
|
{
|
|
|
|
/** @var TagRepositoryInterface $repository */
|
|
|
|
$repository = app(TagRepositoryInterface::class);
|
|
|
|
$set = $this->get('tag');
|
2022-10-30 14:24:28 +01:00
|
|
|
$collection = new Collection();
|
2023-01-20 22:08:18 +01:00
|
|
|
if (is_array($set)) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Set is:', $set);
|
2023-01-20 22:08:18 +01:00
|
|
|
}
|
|
|
|
if (!is_array($set)) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Set is not an array! "%s"', $set));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-08-30 15:57:59 +02:00
|
|
|
return $collection;
|
2023-01-20 22:08:18 +01:00
|
|
|
}
|
2023-08-30 15:57:59 +02:00
|
|
|
foreach ($set as $tagTag) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now searching for "%s"', $tagTag));
|
2023-08-30 15:57:59 +02:00
|
|
|
$tag = $repository->findByTag($tagTag);
|
|
|
|
if (null !== $tag) {
|
|
|
|
$collection->push($tag);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-08-30 15:57:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-31 06:20:29 +01:00
|
|
|
$tag = $repository->find((int) $tagTag);
|
2023-08-30 15:57:59 +02:00
|
|
|
if (null !== $tag) {
|
|
|
|
$collection->push($tag);
|
2017-02-15 20:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Rules for this request.
|
2016-11-09 21:36:54 +01:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2019-09-03 22:35:41 +02:00
|
|
|
'report_type' => 'in:audit,default,category,budget,tag,double',
|
2016-11-09 21:36:54 +01:00
|
|
|
];
|
|
|
|
}
|
2024-01-09 21:03:26 +01:00
|
|
|
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
if($validator->fails()) {
|
|
|
|
Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray());
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|