2018-06-24 14:54:06 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-06-24 14:54:06 +02:00
|
|
|
/**
|
2020-03-14 07:30:55 +01:00
|
|
|
* BudgetUpdateRequest.php
|
2020-01-23 19:44:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-06-24 14:54:06 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-06-24 14:54:06 +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.
|
2018-06-24 14:54:06 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-06-24 14:54:06 +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.
|
2018-06-24 14:54:06 +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/>.
|
2018-06-24 14:54:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-03-06 08:40:51 +01:00
|
|
|
namespace FireflyIII\Api\V1\Requests\Models\Budget;
|
2018-06-24 14:54:06 +02:00
|
|
|
|
2022-10-30 14:44:49 +01:00
|
|
|
use FireflyIII\Models\Budget;
|
2018-12-14 18:33:07 +01:00
|
|
|
use FireflyIII\Rules\IsBoolean;
|
2024-01-02 14:33:17 +01:00
|
|
|
use FireflyIII\Rules\IsValidPositiveAmount;
|
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;
|
2024-01-09 20:48:17 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-03-14 07:43:49 +01:00
|
|
|
use Illuminate\Validation\Validator;
|
2018-06-24 14:54:06 +02:00
|
|
|
|
|
|
|
/**
|
2021-03-06 08:40:51 +01:00
|
|
|
* Class UpdateRequest
|
2018-06-24 14:54:06 +02:00
|
|
|
*/
|
2021-03-06 08:40:51 +01:00
|
|
|
class UpdateRequest extends FormRequest
|
2018-06-24 14:54:06 +02:00
|
|
|
{
|
2023-11-04 14:18:49 +01:00
|
|
|
use ChecksLogin;
|
2022-10-30 14:23:00 +01:00
|
|
|
use ConvertsDataTypes;
|
|
|
|
use ValidatesAutoBudgetRequest;
|
2018-06-24 14:54:06 +02:00
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Get all data from the request.
|
2018-06-24 14:54:06 +02:00
|
|
|
*/
|
|
|
|
public function getAll(): array
|
|
|
|
{
|
2021-03-13 19:03:08 +01:00
|
|
|
// this is the way:
|
2021-03-31 19:36:08 +02:00
|
|
|
$fields = [
|
2022-05-04 05:53:47 +02:00
|
|
|
'name' => ['name', 'convertString'],
|
2021-03-13 19:03:08 +01:00
|
|
|
'active' => ['active', 'boolean'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'order' => ['order', 'convertInteger'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'notes' => ['notes', 'convertString'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'currency_id' => ['auto_budget_currency_id', 'convertInteger'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'currency_code' => ['auto_budget_currency_code', 'convertString'],
|
|
|
|
'auto_budget_type' => ['auto_budget_type', 'convertString'],
|
|
|
|
'auto_budget_amount' => ['auto_budget_amount', 'convertString'],
|
|
|
|
'auto_budget_period' => ['auto_budget_period', 'convertString'],
|
2018-06-24 14:54:06 +02:00
|
|
|
];
|
2021-03-31 19:36:08 +02:00
|
|
|
$allData = $this->getAllData($fields);
|
|
|
|
if (array_key_exists('auto_budget_type', $allData)) {
|
|
|
|
$types = [
|
|
|
|
'none' => 0,
|
|
|
|
'reset' => 1,
|
|
|
|
'rollover' => 2,
|
2023-05-15 06:18:02 +02:00
|
|
|
'adjusted' => 3,
|
2021-03-31 19:36:08 +02:00
|
|
|
];
|
|
|
|
$allData['auto_budget_type'] = $types[$allData['auto_budget_type']] ?? 0;
|
|
|
|
}
|
2021-03-13 19:03:08 +01:00
|
|
|
|
2021-03-31 19:36:08 +02:00
|
|
|
return $allData;
|
2018-06-24 14:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* The rules that the incoming request must be matched against.
|
2018-06-24 14:54:06 +02:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2022-10-30 14:44:49 +01:00
|
|
|
/** @var Budget $budget */
|
2020-03-14 07:30:55 +01:00
|
|
|
$budget = $this->route()->parameter('budget');
|
2021-09-18 10:26:12 +02:00
|
|
|
|
2020-03-14 07:30:55 +01:00
|
|
|
return [
|
2024-01-05 09:48:59 +01:00
|
|
|
'name' => sprintf('min:1|max:100|uniqueObjectForUser:budgets,name,%d', $budget->id),
|
2022-10-30 14:23:00 +01:00
|
|
|
'active' => [new IsBoolean()],
|
2024-01-04 11:41:03 +01:00
|
|
|
'notes' => 'nullable|min:1|max:32768',
|
2023-05-15 06:18:02 +02:00
|
|
|
'auto_budget_type' => 'in:reset,rollover,adjusted,none',
|
2020-03-14 07:43:49 +01:00
|
|
|
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
|
|
|
|
'auto_budget_currency_code' => 'exists:transaction_currencies,code',
|
2024-01-02 14:33:17 +01:00
|
|
|
'auto_budget_amount' => ['nullable', new IsValidPositiveAmount()],
|
2020-03-14 07:43:49 +01:00
|
|
|
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
|
2018-06-24 14:54:06 +02:00
|
|
|
];
|
|
|
|
}
|
2020-03-14 07:43:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the validator instance with special rules for after the basic validation rules.
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
$validator->after(
|
2023-12-21 05:07:26 +01:00
|
|
|
function (Validator $validator): void {
|
2020-03-14 07:43:49 +01:00
|
|
|
// validate all account info
|
|
|
|
$this->validateAutoBudgetAmount($validator);
|
|
|
|
}
|
|
|
|
);
|
2024-02-22 01:29:01 +01:00
|
|
|
if ($validator->fails()) {
|
2025-05-04 12:57:14 +02:00
|
|
|
Log::channel('audit')->error(sprintf('Validation errors in %s', self::class), $validator->errors()->toArray());
|
2024-01-09 20:48:17 +01:00
|
|
|
}
|
2020-03-14 07:43:49 +01:00
|
|
|
}
|
2018-07-22 20:32:02 +02:00
|
|
|
}
|