2020-03-14 07:30:55 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BudgetStoreRequest.php
|
|
|
|
* Copyright (c) 2019 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-03-06 08:40:51 +01:00
|
|
|
namespace FireflyIII\Api\V1\Requests\Models\Budget;
|
2020-03-14 07:30:55 +01:00
|
|
|
|
|
|
|
use FireflyIII\Rules\IsBoolean;
|
2020-11-08 13:36:13 +01:00
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
2020-07-18 08:34:00 +02:00
|
|
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
|
|
|
use FireflyIII\Validation\AutoBudget\ValidatesAutoBudgetRequest;
|
2020-07-18 08:25:25 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2020-03-14 07:30:55 +01:00
|
|
|
use Illuminate\Validation\Validator;
|
|
|
|
|
|
|
|
/**
|
2021-03-06 08:40:51 +01:00
|
|
|
* Class StoreRequest
|
2020-03-14 07:30:55 +01:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2021-03-06 08:40:51 +01:00
|
|
|
class StoreRequest extends FormRequest
|
2020-03-14 07:30:55 +01:00
|
|
|
{
|
2020-11-08 13:36:13 +01:00
|
|
|
use ConvertsDataTypes, ValidatesAutoBudgetRequest, ChecksLogin;
|
2020-03-14 07:30:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all data from the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAll(): array
|
|
|
|
{
|
2021-03-13 19:03:08 +01:00
|
|
|
$fields = [
|
|
|
|
'name' => ['name', 'string'],
|
|
|
|
'active' => ['active', 'boolean'],
|
|
|
|
'order' => ['active', 'integer'],
|
2020-03-14 07:30:55 +01:00
|
|
|
|
2021-03-13 19:03:08 +01:00
|
|
|
// auto budget currency:
|
|
|
|
'currency_id' => ['auto_budget_currency_id', 'integer'],
|
|
|
|
'currency_code' => ['auto_budget_currency_code', 'string'],
|
|
|
|
'auto_budget_type' => ['auto_budget_type', 'string'],
|
|
|
|
'auto_budget_amount' => ['auto_budget_amount', 'string'],
|
|
|
|
'auto_budget_period' => ['auto_budget_period', 'string'],
|
2020-03-14 07:30:55 +01:00
|
|
|
];
|
2021-03-13 19:03:08 +01:00
|
|
|
|
|
|
|
return $this->getAllData($fields);
|
2020-03-14 07:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The rules that the incoming request must be matched against.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-03-13 19:03:08 +01:00
|
|
|
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
|
|
|
|
'active' => [new IsBoolean],
|
|
|
|
'currency_id' => 'exists:transaction_currencies,id',
|
|
|
|
'currency_code' => 'exists:transaction_currencies,code',
|
2021-02-15 06:39:55 +01:00
|
|
|
// auto budget info
|
2021-03-13 19:03:08 +01:00
|
|
|
'auto_budget_type' => 'in:reset,rollover,none',
|
|
|
|
'auto_budget_amount' => 'min:0|max:1000000000',
|
|
|
|
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
2020-03-14 07:30:55 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the validator instance with special rules for after the basic validation rules.
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
$validator->after(
|
|
|
|
function (Validator $validator) {
|
|
|
|
// validate all account info
|
|
|
|
$this->validateAutoBudgetAmount($validator);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|