StudentContractRequest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Models\StudentContract;
  4. use Carbon\CarbonImmutable;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Validation\Validator;
  7. class StudentContractRequest extends FormRequest
  8. {
  9. public function rules(): array
  10. {
  11. // aluno e pacote sao obrigatorios apenas no cadastro
  12. $required = $this->isMethod('POST') ? 'required' : 'sometimes';
  13. return [
  14. 'student_id' => "$required|exists:students,id",
  15. 'protocol' => 'sometimes|nullable|string|max:255',
  16. 'signature_date' => 'sometimes|nullable|date_format:Y-m-d',
  17. 'end_date' => 'sometimes|nullable|date_format:Y-m-d',
  18. 'class_package_unit_id' => "$required|exists:class_package_units,id",
  19. 'pricing_mode' => 'sometimes|nullable|string|in:pavao,irrecusavel',
  20. 'class_quantity' => 'sometimes|nullable|integer|min:1',
  21. 'weekday' => 'sometimes|nullable|integer|min:0|max:6',
  22. 'start_time' => 'sometimes|nullable|date_format:H:i',
  23. 'end_time' => 'sometimes|nullable|date_format:H:i',
  24. 'second_weekday' => 'sometimes|nullable|integer|min:0|max:6',
  25. 'second_start_time' => 'sometimes|nullable|date_format:H:i',
  26. 'second_end_time' => 'sometimes|nullable|date_format:H:i',
  27. 'due_day' => 'sometimes|nullable|integer|min:1|max:31',
  28. 'tax_register' => 'sometimes|nullable|numeric|min:0',
  29. 'installments' => 'sometimes|nullable|integer|min:1|max:12',
  30. 'enrollment_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  31. 'package_value' => 'sometimes|nullable|numeric|min:0',
  32. 'package_installments' => 'sometimes|nullable|integer|min:1|max:13',
  33. 'package_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  34. 'materials_value' => 'sometimes|nullable|numeric|min:0',
  35. 'materials_installments' => 'sometimes|nullable|integer|min:1|max:13',
  36. 'materials_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  37. 'total_value' => 'sometimes|nullable|numeric|min:0',
  38. 'total_installments' => 'sometimes|nullable|integer|min:1|max:13',
  39. 'total_due_date' => 'sometimes|nullable|date_format:Y-m-d',
  40. 'payment_method' => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
  41. 'fine_cancelled' => 'sometimes|nullable|numeric|min:0|max:100',
  42. ];
  43. }
  44. public function after(): array
  45. {
  46. return [
  47. function (Validator $validator): void {
  48. // o cadastro e a renovacao possuem limites de periodo diferentes
  49. if ($this->isMethod('POST')) {
  50. $this->validateCreationPeriod($validator);
  51. return;
  52. }
  53. $this->validateRenewalPeriod($validator);
  54. },
  55. ];
  56. }
  57. private function validateCreationPeriod(Validator $validator): void
  58. {
  59. // na criacao o contrato pode durar no maximo um ano desde a assinatura
  60. if (
  61. ! $this->filled(['signature_date', 'end_date'])
  62. || $validator->errors()->hasAny(['signature_date', 'end_date'])
  63. ) {
  64. return;
  65. }
  66. $signatureDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('signature_date')->toString());
  67. $endDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('end_date')->toString());
  68. if ($endDate->greaterThan($signatureDate->addYearNoOverflow())) {
  69. $validator->errors()->add(
  70. 'end_date',
  71. __('validation.student_contract_max_duration'),
  72. );
  73. }
  74. }
  75. private function validateRenewalPeriod(Validator $validator): void
  76. {
  77. // a renovacao sempre considera os dados da versao atual do contrato
  78. $contract = $this->contractBeingUpdated();
  79. if (!$contract) {
  80. return;
  81. }
  82. // a data inicial fica imutavel depois que o contrato e criado
  83. if (
  84. $this->exists('signature_date')
  85. && !$validator->errors()->has('signature_date')
  86. && $this->input('signature_date') !== $contract->signature_date?->format('Y-m-d')
  87. ) {
  88. $validator->errors()->add(
  89. 'signature_date',
  90. __('validation.student_contract_signature_date_immutable'),
  91. );
  92. }
  93. // sem uma nova data final valida nao existe periodo para conferir
  94. if (!$this->exists('end_date') || $validator->errors()->has('end_date')) {
  95. return;
  96. }
  97. $currentEndDate = $contract->end_date?->toImmutable();
  98. $newEndDate = $this->filled('end_date')
  99. ? CarbonImmutable::createFromFormat('!Y-m-d', $this->string('end_date')->toString())
  100. : null;
  101. // a data final nunca pode diminuir nem ser removida
  102. if ($currentEndDate && (!$newEndDate || $newEndDate->lessThan($currentEndDate))) {
  103. $validator->errors()->add(
  104. 'end_date',
  105. __('validation.student_contract_end_date_cannot_decrease'),
  106. );
  107. return;
  108. }
  109. // reenviar a mesma data nao caracteriza uma renovacao
  110. if (
  111. (!$currentEndDate && !$newEndDate)
  112. || ($currentEndDate && $newEndDate && $newEndDate->equalTo($currentEndDate))
  113. ) {
  114. return;
  115. }
  116. // a renovacao pode avancar no maximo um ano a partir da data atual
  117. $maximumEndDate = CarbonImmutable::today()->addYearNoOverflow();
  118. if ($newEndDate?->greaterThan($maximumEndDate)) {
  119. $validator->errors()->add(
  120. 'end_date',
  121. __('validation.student_contract_renewal_max_end_date', [
  122. 'date' => $maximumEndDate->format('d/m/Y'),
  123. ]),
  124. );
  125. }
  126. }
  127. protected function contractBeingUpdated(): ?StudentContract
  128. {
  129. // o id da rota identifica a versao usada como base para a edicao
  130. $contractId = $this->route('id');
  131. return is_numeric($contractId) ? StudentContract::find((int) $contractId) : null;
  132. }
  133. }