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

312 lines
7.9 KiB
PHP
Raw Normal View History

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