|
|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
use App\Models\StudentContract;
|
|
|
+use App\Rules\StudentContractVariationConsistency;
|
|
|
use Carbon\CarbonImmutable;
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
use Illuminate\Validation\Validator;
|
|
|
@@ -16,29 +17,33 @@ public function rules(): array
|
|
|
$required = $this->isMethod('POST') ? 'required' : 'sometimes';
|
|
|
|
|
|
return [
|
|
|
- 'student_id' => "$required|exists:students,id",
|
|
|
- 'protocol' => 'sometimes|nullable|string|max:255',
|
|
|
- 'signature_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
- 'end_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
- 'class_package_unit_id' => "$required|exists:class_package_units,id",
|
|
|
- 'class_quantity' => 'sometimes|nullable|integer|min:1',
|
|
|
- 'weekday' => 'sometimes|nullable|integer|min:0|max:6',
|
|
|
- 'start_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
- 'end_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
- 'second_weekday' => 'sometimes|nullable|integer|min:0|max:6',
|
|
|
- 'second_start_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
- 'second_end_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
- 'due_day' => 'sometimes|nullable|integer|min:1|max:31',
|
|
|
- 'tax_register' => 'sometimes|nullable|numeric|min:0',
|
|
|
- 'installments' => 'sometimes|nullable|integer|min:1|max:12',
|
|
|
- 'enrollment_due_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
- 'package_value' => 'sometimes|nullable|numeric|min:0',
|
|
|
- 'package_installments' => 'sometimes|nullable|integer|min:1|max:13',
|
|
|
- 'package_due_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
- 'early_payment_discount' => 'sometimes|nullable|numeric|min:0|max:100',
|
|
|
- 'interest_rate' => 'sometimes|nullable|numeric|min:0',
|
|
|
- 'payment_method' => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
|
|
|
- 'fine_cancelled' => 'sometimes|nullable|numeric|min:0|max:100',
|
|
|
+ 'student_id' => "$required|exists:students,id",
|
|
|
+ 'protocol' => 'sometimes|nullable|string|max:255',
|
|
|
+ 'signature_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
+ 'end_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
+ 'class_package_unit_id' => "$required|exists:class_package_units,id",
|
|
|
+ 'class_package_unit_variation_id' => 'sometimes|nullable|integer|min:1',
|
|
|
+ 'class_quantity' => 'sometimes|nullable|integer|min:1',
|
|
|
+ 'weekday' => 'sometimes|nullable|integer|min:0|max:6',
|
|
|
+ 'start_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
+ 'end_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
+ 'second_weekday' => 'sometimes|nullable|integer|min:0|max:6',
|
|
|
+ 'second_start_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
+ 'second_end_time' => 'sometimes|nullable|date_format:H:i',
|
|
|
+ 'due_day' => 'sometimes|nullable|integer|min:1|max:31',
|
|
|
+ 'tax_register' => 'sometimes|nullable|numeric|min:0',
|
|
|
+ 'installments' => 'sometimes|nullable|integer|min:1|max:60',
|
|
|
+ 'enrollment_due_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
+ 'package_value' => 'sometimes|nullable|numeric|min:0',
|
|
|
+ 'package_installments' => 'sometimes|nullable|integer|min:1|max:60',
|
|
|
+ 'package_due_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
+ 'material_value' => 'sometimes|nullable|numeric|min:0',
|
|
|
+ 'material_installments' => 'sometimes|nullable|integer|min:1|max:60',
|
|
|
+ 'material_due_date' => 'sometimes|nullable|date_format:Y-m-d',
|
|
|
+ 'early_payment_discount' => 'sometimes|nullable|numeric|min:0|max:100',
|
|
|
+ 'interest_rate' => 'sometimes|nullable|numeric|min:0',
|
|
|
+ 'payment_method' => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
|
|
|
+ 'fine_cancelled' => 'sometimes|nullable|numeric|min:0|max:100',
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -50,11 +55,11 @@ function (Validator $validator): void {
|
|
|
|
|
|
if ($this->isMethod('POST')) {
|
|
|
$this->validateCreationPeriod($validator);
|
|
|
-
|
|
|
- return;
|
|
|
+ } else {
|
|
|
+ $this->validateRenewalPeriod($validator);
|
|
|
}
|
|
|
|
|
|
- $this->validateRenewalPeriod($validator);
|
|
|
+ $this->validateVariation($validator);
|
|
|
},
|
|
|
];
|
|
|
}
|
|
|
@@ -151,6 +156,41 @@ private function validateRenewalPeriod(Validator $validator): void
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function validateVariation(Validator $validator): void
|
|
|
+ {
|
|
|
+ // campos reprovados nas regras basicas nao entram na validacao cruzada
|
|
|
+
|
|
|
+ $fields = [
|
|
|
+ 'class_package_unit_id', 'class_package_unit_variation_id',
|
|
|
+ 'tax_register', 'installments',
|
|
|
+ 'package_value', 'package_installments',
|
|
|
+ 'material_value', 'material_installments',
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($validator->errors()->hasAny($fields)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // na edicao os campos ausentes mantem o valor da versao atual
|
|
|
+
|
|
|
+ $contract = $this->isMethod('POST') ? null : $this->contractBeingUpdated();
|
|
|
+
|
|
|
+ $data = array_merge($contract?->getAttributes() ?? [], $this->all());
|
|
|
+
|
|
|
+ $errors = $this->variationConsistencyRule()->errors($data);
|
|
|
+
|
|
|
+ foreach ($errors as $field => $messages) {
|
|
|
+ foreach ($messages as $message) {
|
|
|
+ $validator->errors()->add($field, $message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function variationConsistencyRule(): StudentContractVariationConsistency
|
|
|
+ {
|
|
|
+ return new StudentContractVariationConsistency;
|
|
|
+ }
|
|
|
+
|
|
|
protected function contractBeingUpdated(): ?StudentContract
|
|
|
{
|
|
|
// o id da rota identifica a versao usada como base para a edicao
|