Expend API.

This commit is contained in:
James Cole
2021-03-05 20:17:39 +01:00
parent 0cb0a628e3
commit 0afebb26e0
17 changed files with 1026 additions and 195 deletions

View File

@@ -8,23 +8,27 @@ use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
/**
* Class ExpenseRequest
* Class GenericRequest
*/
class ExpenseRequest extends FormRequest
class GenericRequest extends FormRequest
{
use ConvertsDataTypes, ChecksLogin;
private Collection $accounts;
private Collection $budgets;
private Collection $categories;
private Collection $bills;
private Collection $tags;
/**
* @return Carbon
@@ -48,31 +52,6 @@ class ExpenseRequest extends FormRequest
return $date;
}
/**
*
*/
private function parseAccounts(): void
{
if (null === $this->accounts) {
$this->accounts = new Collection;
}
if (0 !== $this->accounts->count()) {
return;
}
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('accounts');
if (is_array($array)) {
foreach ($array as $accountId) {
$accountId = (int)$accountId;
$account = $repository->findNull($accountId);
if (null !== $account) {
$this->accounts->push($account);
}
}
}
}
/**
*
*/
@@ -118,6 +97,26 @@ class ExpenseRequest extends FormRequest
return $this->categories;
}
/**
* @return Collection
*/
public function getBills(): Collection
{
$this->parseBills();
return $this->bills;
}
/**
* @return Collection
*/
public function getTags(): Collection
{
$this->parseTags();
return $this->tags;
}
/**
* @return Collection
@@ -155,6 +154,24 @@ class ExpenseRequest extends FormRequest
return $return;
}
/**
* @return Collection
*/
public function getRevenueAccounts(): Collection
{
$this->parseAccounts();
$return = new Collection;
/** @var Account $account */
foreach ($this->accounts as $account) {
$type = $account->accountType->type;
if (in_array($type, [AccountType::REVENUE])) {
$return->push($account);
}
}
return $return;
}
/**
* Get all data from the request.
*
@@ -176,9 +193,11 @@ class ExpenseRequest extends FormRequest
public function rules(): array
{
// this is cheating but it works:
$this->accounts = new Collection;
$this->budgets = new Collection;
$this->accounts = new Collection;
$this->budgets = new Collection;
$this->categories = new Collection;
$this->bills = new Collection;
$this->tags = new Collection;
return [
'start' => 'required|date',
@@ -186,6 +205,56 @@ class ExpenseRequest extends FormRequest
];
}
/**
*
*/
private function parseAccounts(): void
{
if (null === $this->accounts) {
$this->accounts = new Collection;
}
if (0 !== $this->accounts->count()) {
return;
}
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
$array = $this->get('accounts');
if (is_array($array)) {
foreach ($array as $accountId) {
$accountId = (int)$accountId;
$account = $repository->findNull($accountId);
if (null !== $account) {
$this->accounts->push($account);
}
}
}
}
/**
*
*/
private function parseBills(): void
{
if (null === $this->bills) {
$this->bills = new Collection;
}
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->findNull($billId);
if (null !== $billId) {
$this->bills->push($bill);
}
}
}
}
/**
*
*/
@@ -210,4 +279,29 @@ class ExpenseRequest extends FormRequest
}
}
}
/**
*
*/
private function parseTags(): void
{
if (null === $this->tags) {
$this->tags = new Collection;
}
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;
$tag = $repository->findNull($tagId);
if (null !== $tagId) {
$this->tags->push($tag);
}
}
}
}
}