2018-02-10 09:57:31 +01:00
|
|
|
<?php
|
2018-05-11 10:08:34 +02:00
|
|
|
|
2018-02-10 09:57:31 +01:00
|
|
|
/**
|
2020-10-27 16:26:10 +01:00
|
|
|
* BillStoreRequest.php
|
2020-01-23 19:44:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-10 09:57:31 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-10 09:57:31 +01: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-02-10 09:57:31 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-10 09:57:31 +01: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-02-10 09:57:31 +01: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-02-10 09:57:31 +01:00
|
|
|
*/
|
|
|
|
|
2018-05-11 10:08:34 +02:00
|
|
|
declare(strict_types=1);
|
2018-02-10 09:57:31 +01:00
|
|
|
|
2021-03-06 12:45:49 +01:00
|
|
|
namespace FireflyIII\Api\V1\Requests\Models\Bill;
|
2018-02-10 09:57:31 +01:00
|
|
|
|
2025-05-29 15:01:06 +02:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2025-05-27 16:53:48 +02:00
|
|
|
use ValueError;
|
|
|
|
use TypeError;
|
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-10-27 16:26:10 +01:00
|
|
|
use FireflyIII\Support\Request\ChecksLogin;
|
2020-07-18 08:34:00 +02:00
|
|
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
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;
|
2018-02-18 10:31:15 +01:00
|
|
|
|
2018-02-10 09:57:31 +01:00
|
|
|
/**
|
2021-03-06 12:45:49 +01:00
|
|
|
* Class StoreRequest
|
2018-02-10 09:57:31 +01:00
|
|
|
*/
|
2021-03-06 12:45:49 +01:00
|
|
|
class StoreRequest extends FormRequest
|
2018-02-10 09:57:31 +01:00
|
|
|
{
|
2022-10-30 14:23:00 +01:00
|
|
|
use ChecksLogin;
|
2023-11-04 14:18:49 +01:00
|
|
|
use ConvertsDataTypes;
|
2018-02-10 09:57:31 +01:00
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Get all data from the request.
|
2018-02-10 09:57:31 +01:00
|
|
|
*/
|
|
|
|
public function getAll(): array
|
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Raw fields in Bill StoreRequest', $this->all());
|
2021-03-13 16:47:29 +01:00
|
|
|
$fields = [
|
2022-05-04 05:53:47 +02:00
|
|
|
'name' => ['name', 'convertString'],
|
|
|
|
'amount_min' => ['amount_min', 'convertString'],
|
|
|
|
'amount_max' => ['amount_max', 'convertString'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'currency_id' => ['currency_id', 'convertInteger'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'currency_code' => ['currency_code', 'convertString'],
|
2021-03-13 16:47:29 +01:00
|
|
|
'date' => ['date', 'date'],
|
2021-07-25 19:39:35 +02:00
|
|
|
'end_date' => ['end_date', 'date'],
|
|
|
|
'extension_date' => ['extension_date', 'date'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'repeat_freq' => ['repeat_freq', 'convertString'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'skip' => ['skip', 'convertInteger'],
|
2021-03-13 16:47:29 +01:00
|
|
|
'active' => ['active', 'boolean'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'order' => ['order', 'convertInteger'],
|
2021-04-06 13:30:09 +02:00
|
|
|
'notes' => ['notes', 'stringWithNewlines'],
|
2022-10-01 05:29:42 +02:00
|
|
|
'object_group_id' => ['object_group_id', 'convertInteger'],
|
2022-05-04 05:53:47 +02:00
|
|
|
'object_group_title' => ['object_group_title', 'convertString'],
|
2018-02-10 09:57:31 +01:00
|
|
|
];
|
2021-03-13 16:47:29 +01:00
|
|
|
|
|
|
|
return $this->getAllData($fields);
|
2018-02-10 09:57:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* The rules that the incoming request must be matched against.
|
2018-02-10 09:57:31 +01:00
|
|
|
*/
|
2018-02-16 16:42:23 +01:00
|
|
|
public function rules(): array
|
2018-02-10 09:57:31 +01:00
|
|
|
{
|
2020-10-27 16:26:10 +01:00
|
|
|
return [
|
2024-01-05 09:48:59 +01:00
|
|
|
'name' => 'min:1|max:255|uniqueObjectForUser:bills,name',
|
2024-01-02 14:33:17 +01:00
|
|
|
'amount_min' => ['required', new IsValidPositiveAmount()],
|
|
|
|
'amount_max' => ['required', new IsValidPositiveAmount()],
|
2021-07-25 19:39:35 +02:00
|
|
|
'currency_id' => 'numeric|exists:transaction_currencies,id',
|
2023-04-26 06:17:04 +02:00
|
|
|
'currency_code' => 'min:3|max:51|exists:transaction_currencies,code',
|
2025-01-25 04:49:28 +01:00
|
|
|
'date' => 'date|required|after:1900-01-01|before:2099-12-31',
|
|
|
|
'end_date' => 'nullable|date|after:date|after:1900-01-01|before:2099-12-31',
|
|
|
|
'extension_date' => 'nullable|date|after:date|after:1900-01-01|before:2099-12-31',
|
2021-07-25 19:39:35 +02:00
|
|
|
'repeat_freq' => 'in:weekly,monthly,quarterly,half-year,yearly|required',
|
2024-01-05 09:48:59 +01:00
|
|
|
'skip' => 'min:0|max:31|numeric',
|
2022-10-30 14:23:00 +01:00
|
|
|
'active' => [new IsBoolean()],
|
2024-10-05 09:41:07 +02:00
|
|
|
'notes' => 'nullable|min:1|max:32768',
|
2018-02-10 09:57:31 +01:00
|
|
|
];
|
|
|
|
}
|
2018-02-18 10:31:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the validator instance.
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
$validator->after(
|
2023-12-21 04:59:23 +01:00
|
|
|
static function (Validator $validator): void {
|
2025-01-26 07:44:41 +01:00
|
|
|
$data = $validator->getData();
|
|
|
|
$min = $data['amount_min'] ?? '0';
|
|
|
|
$max = $data['amount_max'] ?? '0';
|
2022-12-24 05:06:39 +01:00
|
|
|
|
2025-01-26 14:33:19 +01:00
|
|
|
if (is_array($min) || is_array($max)) {
|
2025-01-26 07:44:41 +01:00
|
|
|
$validator->errors()->add('amount_min', (string) trans('validation.generic_invalid'));
|
|
|
|
$validator->errors()->add('amount_max', (string) trans('validation.generic_invalid'));
|
2025-01-26 14:33:19 +01:00
|
|
|
$min = '0';
|
2025-01-26 07:44:41 +01:00
|
|
|
$max = '0';
|
|
|
|
}
|
|
|
|
$result = false;
|
2025-01-26 14:33:19 +01:00
|
|
|
|
2025-01-26 07:44:41 +01:00
|
|
|
try {
|
|
|
|
$result = bccomp($min, $max);
|
2025-05-27 16:53:48 +02:00
|
|
|
} catch (ValueError $e) {
|
2025-01-26 07:44:41 +01:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
$validator->errors()->add('amount_min', (string) trans('validation.generic_invalid'));
|
|
|
|
$validator->errors()->add('amount_max', (string) trans('validation.generic_invalid'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 === $result) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$validator->errors()->add('amount_min', (string) trans('validation.amount_min_over_max'));
|
2018-02-18 10:31:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2025-01-26 07:44:41 +01:00
|
|
|
$failed = false;
|
2025-01-26 14:33:19 +01:00
|
|
|
|
2025-01-26 07:44:41 +01:00
|
|
|
try {
|
|
|
|
$failed = $validator->fails();
|
2025-05-27 16:53:48 +02:00
|
|
|
} catch (TypeError $e) {
|
2025-01-26 07:44:41 +01:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
$failed = false;
|
|
|
|
}
|
|
|
|
if ($failed) {
|
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
|
|
|
}
|
2018-02-18 10:31:15 +01:00
|
|
|
}
|
2018-03-05 19:35:58 +01:00
|
|
|
}
|