StudentContractRequest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 'early_payment_discount' => 'sometimes|nullable|numeric|min:0|max:100',
  41. 'interest_rate' => 'sometimes|nullable|numeric|min:0',
  42. 'payment_method' => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
  43. 'fine_cancelled' => 'sometimes|nullable|numeric|min:0|max:100',
  44. ];
  45. }
  46. public function after(): array
  47. {
  48. return [
  49. function (Validator $validator): void {
  50. // o cadastro e a renovacao possuem limites de periodo diferentes
  51. if ($this->isMethod('POST')) {
  52. $this->validateCreationPeriod($validator);
  53. return;
  54. }
  55. $this->validateRenewalPeriod($validator);
  56. },
  57. ];
  58. }
  59. private function validateCreationPeriod(Validator $validator): void
  60. {
  61. // na criacao o contrato pode durar no maximo um ano desde a assinatura
  62. if (
  63. ! $this->filled(['signature_date', 'end_date'])
  64. || $validator->errors()->hasAny(['signature_date', 'end_date'])
  65. ) {
  66. return;
  67. }
  68. $signatureDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('signature_date')->toString());
  69. $endDate = CarbonImmutable::createFromFormat('!Y-m-d', $this->string('end_date')->toString());
  70. if ($endDate->greaterThan($signatureDate->addYearNoOverflow())) {
  71. $validator->errors()->add(
  72. 'end_date',
  73. __('validation.student_contract_max_duration'),
  74. );
  75. }
  76. }
  77. private function validateRenewalPeriod(Validator $validator): void
  78. {
  79. // a renovacao sempre considera os dados da versao atual do contrato
  80. $contract = $this->contractBeingUpdated();
  81. if (!$contract) {
  82. return;
  83. }
  84. // a data inicial fica imutavel depois que o contrato e criado
  85. if (
  86. $this->exists('signature_date')
  87. && !$validator->errors()->has('signature_date')
  88. && $this->input('signature_date') !== $contract->signature_date?->format('Y-m-d')
  89. ) {
  90. $validator->errors()->add(
  91. 'signature_date',
  92. __('validation.student_contract_signature_date_immutable'),
  93. );
  94. }
  95. // sem uma nova data final valida nao existe periodo para conferir
  96. if (!$this->exists('end_date') || $validator->errors()->has('end_date')) {
  97. return;
  98. }
  99. $currentEndDate = $contract->end_date?->toImmutable();
  100. $newEndDate = $this->filled('end_date')
  101. ? CarbonImmutable::createFromFormat('!Y-m-d', $this->string('end_date')->toString())
  102. : null;
  103. // a data final nunca pode diminuir nem ser removida
  104. if ($currentEndDate && (!$newEndDate || $newEndDate->lessThan($currentEndDate))) {
  105. $validator->errors()->add(
  106. 'end_date',
  107. __('validation.student_contract_end_date_cannot_decrease'),
  108. );
  109. return;
  110. }
  111. // reenviar a mesma data nao caracteriza uma renovacao
  112. if (
  113. (!$currentEndDate && !$newEndDate)
  114. || ($currentEndDate && $newEndDate && $newEndDate->equalTo($currentEndDate))
  115. ) {
  116. return;
  117. }
  118. // a renovacao pode avancar no maximo um ano a partir da data atual
  119. $maximumEndDate = CarbonImmutable::today()->addYearNoOverflow();
  120. if ($newEndDate?->greaterThan($maximumEndDate)) {
  121. $validator->errors()->add(
  122. 'end_date',
  123. __('validation.student_contract_renewal_max_end_date', [
  124. 'date' => $maximumEndDate->format('d/m/Y'),
  125. ]),
  126. );
  127. }
  128. }
  129. protected function contractBeingUpdated(): ?StudentContract
  130. {
  131. // o id da rota identifica a versao usada como base para a edicao
  132. $contractId = $this->route('id');
  133. return is_numeric($contractId) ? StudentContract::find((int) $contractId) : null;
  134. }
  135. }