Files
firefly-iii/app/Api/V1/Requests/Insight/GenericRequest.php

271 lines
7.5 KiB
PHP
Raw Normal View History

2021-03-05 16:28:59 +01:00
<?php
2021-03-28 11:39:26 +02:00
/*
* GenericRequest.php
* Copyright (c) 2021 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
2021-03-05 16:28:59 +01:00
2021-03-28 11:39:26 +02:00
declare(strict_types=1);
2021-03-05 16:28:59 +01:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Requests\Insight;
2021-03-05 16:28:59 +01:00
use Carbon\Carbon;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
2021-03-05 16:28:59 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2021-03-05 20:17:39 +01:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2021-03-05 16:28:59 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2021-03-05 20:17:39 +01:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2021-03-05 16:28:59 +01:00
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
/**
2021-03-05 20:17:39 +01:00
* Class GenericRequest
2021-03-05 16:28:59 +01:00
*/
2021-03-05 20:17:39 +01:00
class GenericRequest extends FormRequest
2021-03-05 16:28:59 +01:00
{
2022-10-30 14:23:00 +01:00
use ChecksLogin;
2023-11-04 14:18:49 +01:00
use ConvertsDataTypes;
2021-03-05 16:28:59 +01:00
private Collection $accounts;
2021-03-21 09:15:40 +01:00
private Collection $bills;
2021-03-05 16:28:59 +01:00
private Collection $budgets;
private Collection $categories;
2021-03-05 20:17:39 +01:00
private Collection $tags;
2021-03-05 16:28:59 +01:00
/**
2021-03-21 09:15:40 +01:00
* Get all data from the request.
2021-03-05 16:28:59 +01:00
*/
2021-03-21 09:15:40 +01:00
public function getAll(): array
2021-03-05 16:28:59 +01:00
{
2021-03-21 09:15:40 +01:00
return [
2021-12-28 20:42:50 +01:00
'start' => $this->getCarbonDate('start'),
'end' => $this->getCarbonDate('end'),
2021-03-21 09:15:40 +01:00
];
2021-03-05 16:28:59 +01:00
}
2021-03-21 09:15:40 +01:00
public function getAssetAccounts(): Collection
2021-03-05 16:28:59 +01:00
{
2021-03-21 09:15:40 +01:00
$this->parseAccounts();
2022-10-30 14:23:00 +01:00
$return = new Collection();
2023-12-20 19:35:52 +01:00
2021-03-21 09:15:40 +01:00
/** @var Account $account */
foreach ($this->accounts as $account) {
$type = $account->accountType->type;
2025-01-03 09:15:52 +01:00
if (in_array($type, [AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value], true)) {
2021-03-21 09:15:40 +01:00
$return->push($account);
}
}
2021-03-05 16:28:59 +01:00
2021-03-21 09:15:40 +01:00
return $return;
2021-03-05 16:28:59 +01:00
}
2023-06-21 12:34:58 +02:00
private function parseAccounts(): void
{
if (0 !== $this->accounts->count()) {
return;
}
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('accounts');
2023-06-21 12:34:58 +02:00
if (is_array($array)) {
foreach ($array as $accountId) {
2024-12-22 08:43:12 +01:00
$accountId = (int) $accountId;
2023-06-21 12:34:58 +02:00
$account = $repository->find($accountId);
if (null !== $account) {
$this->accounts->push($account);
}
}
}
}
public function getBills(): Collection
{
$this->parseBills();
return $this->bills;
}
2023-06-21 12:34:58 +02:00
private function parseBills(): void
{
if (0 !== $this->bills->count()) {
return;
}
$repository = app(BillRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('bills');
2023-06-21 12:34:58 +02:00
if (is_array($array)) {
foreach ($array as $billId) {
2024-12-22 08:43:12 +01:00
$billId = (int) $billId;
2023-06-21 12:34:58 +02:00
$bill = $repository->find($billId);
if (null !== $bill) {
$this->bills->push($bill);
}
}
}
}
public function getBudgets(): Collection
{
$this->parseBudgets();
return $this->budgets;
}
2023-06-21 12:34:58 +02:00
private function parseBudgets(): void
{
if (0 !== $this->budgets->count()) {
return;
}
$repository = app(BudgetRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('budgets');
2023-06-21 12:34:58 +02:00
if (is_array($array)) {
foreach ($array as $budgetId) {
2024-12-22 08:43:12 +01:00
$budgetId = (int) $budgetId;
2023-06-21 12:34:58 +02:00
$budget = $repository->find($budgetId);
if (null !== $budget) {
$this->budgets->push($budget);
}
}
}
}
public function getCategories(): Collection
{
$this->parseCategories();
return $this->categories;
}
2023-06-21 12:34:58 +02:00
private function parseCategories(): void
{
if (0 !== $this->categories->count()) {
return;
}
$repository = app(CategoryRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('categories');
2023-06-21 12:34:58 +02:00
if (is_array($array)) {
foreach ($array as $categoryId) {
2024-12-22 08:43:12 +01:00
$categoryId = (int) $categoryId;
2023-06-21 12:34:58 +02:00
$category = $repository->find($categoryId);
if (null !== $category) {
$this->categories->push($category);
}
}
}
}
public function getEnd(): Carbon
{
$date = $this->getCarbonDate('end');
$date->endOfDay();
return $date;
}
public function getExpenseAccounts(): Collection
{
$this->parseAccounts();
$return = new Collection();
/** @var Account $account */
foreach ($this->accounts as $account) {
$type = $account->accountType->type;
2025-01-03 09:15:52 +01:00
if (AccountTypeEnum::EXPENSE->value === $type) {
$return->push($account);
}
}
return $return;
}
public function getRevenueAccounts(): Collection
{
$this->parseAccounts();
$return = new Collection();
/** @var Account $account */
foreach ($this->accounts as $account) {
$type = $account->accountType->type;
2025-01-03 09:15:52 +01:00
if (AccountTypeEnum::REVENUE->value === $type) {
$return->push($account);
}
}
return $return;
}
public function getStart(): Carbon
{
$date = $this->getCarbonDate('start');
$date->startOfDay();
return $date;
}
public function getTags(): Collection
{
$this->parseTags();
return $this->tags;
}
2021-03-05 20:17:39 +01:00
private function parseTags(): void
{
if (0 !== $this->tags->count()) {
return;
}
$repository = app(TagRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('tags');
2021-03-05 20:17:39 +01:00
if (is_array($array)) {
foreach ($array as $tagId) {
2024-12-22 08:43:12 +01:00
$tagId = (int) $tagId;
2021-06-30 06:17:38 +02:00
$tag = $repository->find($tagId);
2022-03-30 20:09:19 +02:00
if (null !== $tag) {
2021-03-05 20:17:39 +01:00
$this->tags->push($tag);
}
}
}
}
/**
* The rules that the incoming request must be matched against.
*/
public function rules(): array
{
// this is cheating, but it works to initialize the collections.
$this->accounts = new Collection();
$this->budgets = new Collection();
$this->categories = new Collection();
$this->bills = new Collection();
$this->tags = new Collection();
return [
'start' => 'required|date',
'end' => 'required|date|after_or_equal:start',
];
}
2021-03-28 11:39:26 +02:00
}