- Created ExampleTest and RoutesTest for basic application response and route testing. - Implemented IsolatedDatabaseTestCase for isolated database testing. - Added unit tests for various models and services including Contract, CPE, and LeadDetail. - Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest. - Introduced Vite configuration for asset management in the Laravel application.
84 lines
4.4 KiB
PHP
84 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class RenewContractRequest extends FormRequest
|
|
{
|
|
protected function localizedMessage(string $key, string $fallback): string
|
|
{
|
|
if (function_exists('app') && app()->bound('translator')) {
|
|
return __($key, [], app()->getLocale()) ?: $fallback;
|
|
}
|
|
|
|
return $fallback;
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
foreach (['next_due_date', 'contract_date', 'activation_date'] as $field) {
|
|
$value = $this->input($field);
|
|
|
|
if ($value && str_contains($value, '/')) {
|
|
try {
|
|
$this->merge([
|
|
$field => Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d'),
|
|
]);
|
|
} catch (\Throwable) {
|
|
// Mantém o valor original para o validator apresentar a mensagem adequada.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'next_due_date' => ['required', 'date_format:Y-m-d'],
|
|
'annual_consumption' => ['nullable', 'numeric'],
|
|
'contract_type_id' => ['required', 'exists:contract_types,id'],
|
|
'typology_id' => ['nullable', 'exists:typologies,id'],
|
|
'contract_date' => ['nullable', 'date_format:Y-m-d'],
|
|
'activation_date' => ['nullable', 'date_format:Y-m-d'],
|
|
'renewal_type' => ['required', Rule::exists('renewal_types', 'value')],
|
|
'new_supplier_id' => ['required_if:change_supplier,1', 'nullable', 'exists:suppliers,id'],
|
|
];
|
|
|
|
if ($this->user()?->can('view_contract_summary')) {
|
|
$rules['amount_paid'] = ['required', 'numeric'];
|
|
$rules['commission_value'] = ['nullable', 'numeric'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'amount_paid.required' => $this->localizedMessage('validation.custom.amount_paid.required', 'O valor do contrato é obrigatório.'),
|
|
'amount_paid.numeric' => $this->localizedMessage('validation.custom.amount_paid.numeric', 'O valor do contrato deve ser um número.'),
|
|
'commission_value.numeric' => $this->localizedMessage('validation.custom.commission_value.numeric', 'A comissão deve ser um número.'),
|
|
'next_due_date.required' => $this->localizedMessage('validation.custom.next_due_date.required', 'A próxima renovação é obrigatória.'),
|
|
'next_due_date.date_format' => $this->localizedMessage('validation.custom.next_due_date.date_format', 'A próxima renovação deve estar no formato DD/MM/AAAA.'),
|
|
'renewal_type.required' => $this->localizedMessage('validation.custom.renewal_type.required', 'O tipo de renovação é obrigatório.'),
|
|
'renewal_type.exists' => $this->localizedMessage('validation.custom.renewal_type.exists', 'O tipo de renovação selecionado não é válido.'),
|
|
'annual_consumption.numeric' => $this->localizedMessage('validation.custom.annual_consumption.numeric', 'O consumo anual deve ser numérico.'),
|
|
'contract_type_id.required' => $this->localizedMessage('validation.custom.contract_type_id.required', 'O tipo de contrato é obrigatório.'),
|
|
'contract_type_id.exists' => $this->localizedMessage('validation.custom.contract_type_id.exists', 'O tipo de contrato selecionado não é válido.'),
|
|
'typology_id.exists' => $this->localizedMessage('validation.custom.typology_id.exists', 'A tipologia selecionada não é válida.'),
|
|
'contract_date.date_format' => $this->localizedMessage('validation.custom.contract_date.date_format', 'A data do contrato deve estar no formato DD/MM/AAAA.'),
|
|
'activation_date.date_format' => $this->localizedMessage('validation.custom.activation_date.date_format', 'A data de ativação deve estar no formato DD/MM/AAAA.'),
|
|
'new_supplier_id.required_if' => $this->localizedMessage('validation.custom.new_supplier_id.required_if', 'O novo fornecedor é obrigatório quando a alteração está ativa.'),
|
|
'new_supplier_id.exists' => $this->localizedMessage('validation.custom.new_supplier_id.exists', 'O novo fornecedor selecionado não é válido.'),
|
|
];
|
|
}
|
|
}
|