StudentContractRequest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Http\Requests;
  3. use Carbon\CarbonImmutable;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Validation\Validator;
  6. class StudentContractRequest extends FormRequest
  7. {
  8. public function rules(): array
  9. {
  10. return [
  11. 'student_id' => 'required|exists:students,id',
  12. 'protocol' => 'sometimes|nullable|string|max:255',
  13. 'signature_date' => 'sometimes|nullable|date_format:Y-m-d',
  14. 'end_date' => 'sometimes|nullable|date_format:Y-m-d',
  15. 'class_package_unit_id' => 'required|exists:class_package_units,id',
  16. 'class_quantity' => 'sometimes|nullable|integer|min:1',
  17. 'weekday' => 'sometimes|nullable|integer|min:0|max:6',
  18. 'start_time' => 'sometimes|nullable|date_format:H:i',
  19. 'end_time' => 'sometimes|nullable|date_format:H:i',
  20. 'second_weekday' => 'sometimes|nullable|integer|min:0|max:6',
  21. 'second_start_time' => 'sometimes|nullable|date_format:H:i',
  22. 'second_end_time' => 'sometimes|nullable|date_format:H:i',
  23. 'due_day' => 'sometimes|nullable|integer|min:1|max:31',
  24. 'tax_register' => 'sometimes|nullable|numeric|min:0',
  25. 'installments' => 'sometimes|nullable|integer|min:1|max:12',
  26. 'enrollment_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  27. 'package_value' => 'sometimes|nullable|numeric|min:0',
  28. 'package_installments' => 'sometimes|nullable|integer|min:1|max:13',
  29. 'package_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  30. 'early_payment_discount' => 'sometimes|nullable|numeric|min:0|max:100',
  31. 'interest_rate' => 'sometimes|nullable|numeric|min:0',
  32. 'payment_method' => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
  33. 'fine_cancelled' => 'sometimes|nullable|numeric|min:0|max:100',
  34. ];
  35. }
  36. public function after(): array
  37. {
  38. return [
  39. function (Validator $validator): void {
  40. if (
  41. ! $this->isMethod('POST')
  42. || ! $this->filled(['signature_date', 'end_date'])
  43. || $validator->errors()->hasAny(['signature_date', 'end_date'])
  44. ) {
  45. return;
  46. }
  47. $signatureDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('signature_date')->toString());
  48. $endDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('end_date')->toString());
  49. if ($endDate->greaterThan($signatureDate->addYearNoOverflow())) {
  50. $validator->errors()->add(
  51. 'end_date',
  52. __('validation.student_contract_max_duration'),
  53. );
  54. }
  55. },
  56. ];
  57. }
  58. }