2015-02-22 15:40:13 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BudgetFormRequest.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02: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-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
2018-08-07 19:24:07 +02:00
|
|
|
use FireflyIII\Models\Budget;
|
2020-10-24 07:55:09 +02:00
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
2020-07-18 08:42:13 +02:00
|
|
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
|
|
|
use FireflyIII\Validation\AutoBudget\ValidatesAutoBudgetRequest;
|
2020-10-24 07:55:09 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2020-03-14 07:01:31 +01:00
|
|
|
use Illuminate\Validation\Validator;
|
2015-02-22 15:40:13 +01:00
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* @codeCoverageIgnore
|
2020-03-13 21:35:22 +01:00
|
|
|
* Class BudgetFormUpdateRequest
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2020-10-24 07:55:09 +02:00
|
|
|
class BudgetFormUpdateRequest extends FormRequest
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2020-10-24 07:55:09 +02:00
|
|
|
use ConvertsDataTypes, ValidatesAutoBudgetRequest, ChecksLogin;
|
2015-02-22 15:40:13 +01:00
|
|
|
|
2016-10-22 22:03:00 +02:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Returns the data required by the controller.
|
|
|
|
*
|
2016-10-22 22:03:00 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBudgetData(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-03-15 07:45:46 +01:00
|
|
|
'name' => $this->string('name'),
|
|
|
|
'active' => $this->boolean('active'),
|
|
|
|
'auto_budget_type' => $this->integer('auto_budget_type'),
|
|
|
|
'currency_id' => $this->integer('auto_budget_currency_id'),
|
|
|
|
'auto_budget_amount' => $this->string('auto_budget_amount'),
|
|
|
|
'auto_budget_period' => $this->string('auto_budget_period'),
|
2016-10-22 22:03:00 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-02-22 15:40:13 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Rules for this request.
|
|
|
|
*
|
2015-02-22 15:40:13 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function rules(): array
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2018-08-07 19:24:07 +02:00
|
|
|
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name';
|
|
|
|
|
|
|
|
/** @var Budget $budget */
|
|
|
|
$budget = $this->route()->parameter('budget');
|
|
|
|
|
|
|
|
if (null !== $budget) {
|
|
|
|
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,' . $budget->id;
|
2015-02-22 15:40:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2020-03-14 07:01:31 +01:00
|
|
|
'name' => $nameRule,
|
|
|
|
'active' => 'numeric|between:0,1',
|
2021-11-05 07:48:22 +01:00
|
|
|
'auto_budget_type' => 'numeric|integer|gte:0|lte:31',
|
2020-04-10 21:16:46 +02:00
|
|
|
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
|
2021-04-13 06:26:51 +02:00
|
|
|
'auto_budget_amount' => 'min:0|max:1000000000|required_if:auto_budget_type,1|required_if:auto_budget_type,2',
|
2020-03-14 07:01:31 +01:00
|
|
|
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
2015-02-22 15:40:13 +01:00
|
|
|
];
|
|
|
|
}
|
2020-03-14 07:01:31 +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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-03-29 08:14:32 +02:00
|
|
|
}
|