ProviderRequest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. use App\Enums\UserTypeEnum;
  6. class ProviderRequest extends FormRequest
  7. {
  8. public function rules(): array
  9. {
  10. $providerId = $this->route('id');
  11. $rules = [
  12. 'document' => [
  13. 'sometimes',
  14. 'string',
  15. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  16. function ($attribute, $value, $fail) use ($providerId) {
  17. if (!$this->isValidCpfCnpj($value)) {
  18. $fail(__('validation.custom.document.invalid'));
  19. }
  20. },
  21. Rule::unique('providers', 'document')
  22. ->ignore($providerId)
  23. ->whereNull('deleted_at'),
  24. ],
  25. 'rg' => 'sometimes|nullable|string|max:20',
  26. 'user_id' => [
  27. 'sometimes',
  28. 'exists:users,id',
  29. Rule::unique('providers', 'user_id')
  30. ->ignore($providerId)
  31. ->whereNull('deleted_at'),
  32. function ($attribute, $value, $fail) use ($providerId) {
  33. $clientExists = \DB::table('clients')
  34. ->where('user_id', $value)
  35. ->whereNull('deleted_at')
  36. ->exists();
  37. if ($clientExists) {
  38. $fail(__('validation.custom.user_id.already_linked_to_client'));
  39. }
  40. $providerExists = \DB::table('providers')
  41. ->where('user_id', $value)
  42. ->whereNull('deleted_at')
  43. ->when($providerId, function ($query) use ($providerId) {
  44. $query->where('id', '!=', $providerId);
  45. })
  46. ->exists();
  47. if ($providerExists) {
  48. $fail(__('validation.custom.user_id.already_linked_to_provider'));
  49. }
  50. },
  51. ],
  52. 'average_rating' => 'sometimes|nullable|numeric|min:0|max:5',
  53. 'total_services' => 'sometimes|integer|min:0',
  54. 'birth_date' => 'sometimes|nullable|date|before:today',
  55. 'selfie_verified' => 'sometimes|boolean',
  56. 'document_verified' => 'sometimes|boolean',
  57. 'is_approved' => 'sometimes|boolean',
  58. 'daily_price_8h' => 'sometimes|nullable|numeric|min:100|max:500',
  59. 'daily_price_6h' => 'sometimes|nullable|numeric',
  60. 'daily_price_4h' => 'sometimes|nullable|numeric',
  61. 'daily_price_2h' => 'sometimes|nullable|numeric',
  62. 'profile_media_id' => 'sometimes|nullable|exists:media,id',
  63. ];
  64. if ($this->isMethod('post')) {
  65. $rules['document'] = [
  66. 'required',
  67. 'string',
  68. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  69. function ($attribute, $value, $fail) {
  70. if (!$this->isValidCpfCnpj($value)) {
  71. $fail(__('validation.custom.document.invalid'));
  72. }
  73. },
  74. Rule::unique('providers', 'document')->whereNull('deleted_at'),
  75. ];
  76. $rules['user_id'] = [
  77. 'required',
  78. 'exists:users,id',
  79. Rule::unique('providers', 'user_id')->whereNull('deleted_at'),
  80. function ($attribute, $value, $fail) {
  81. $clientExists = \DB::table('clients')
  82. ->where('user_id', $value)
  83. ->whereNull('deleted_at')
  84. ->exists();
  85. if ($clientExists) {
  86. $fail(__('validation.custom.user_id.already_linked_to_client'));
  87. }
  88. $providerExists = \DB::table('providers')
  89. ->where('user_id', $value)
  90. ->whereNull('deleted_at')
  91. ->exists();
  92. if ($providerExists) {
  93. $fail(__('validation.custom.user_id.already_linked_to_provider'));
  94. }
  95. },
  96. ];
  97. }
  98. return $rules;
  99. }
  100. /**
  101. * Valida CPF ou CNPJ
  102. */
  103. private function isValidCpfCnpj(string $value): bool
  104. {
  105. $value = preg_replace('/[^0-9]/', '', $value);
  106. if (strlen($value) === 11) {
  107. return $this->isValidCpf($value);
  108. } elseif (strlen($value) === 14) {
  109. return $this->isValidCnpj($value);
  110. }
  111. return false;
  112. }
  113. /**
  114. * Valida CPF
  115. */
  116. private function isValidCpf(string $cpf): bool
  117. {
  118. // Elimina CPFs inválidos conhecidos
  119. if (preg_match('/(\d)\1{10}/', $cpf)) {
  120. return false;
  121. }
  122. // Valida primeiro dígito verificador
  123. for ($t = 9; $t < 11; $t++) {
  124. for ($d = 0, $c = 0; $c < $t; $c++) {
  125. $d += $cpf[$c] * (($t + 1) - $c);
  126. }
  127. $d = ((10 * $d) % 11) % 10;
  128. if ($cpf[$c] != $d) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * Valida CNPJ
  136. */
  137. private function isValidCnpj(string $cnpj): bool
  138. {
  139. // Elimina CNPJs inválidos conhecidos
  140. if (preg_match('/(\d)\1{13}/', $cnpj)) {
  141. return false;
  142. }
  143. // Valida primeiro dígito verificador
  144. $length = strlen($cnpj) - 2;
  145. $numbers = substr($cnpj, 0, $length);
  146. $digits = substr($cnpj, $length);
  147. $sum = 0;
  148. $pos = $length - 7;
  149. for ($i = $length; $i >= 1; $i--) {
  150. $sum += $numbers[$length - $i] * $pos--;
  151. if ($pos < 2) {
  152. $pos = 9;
  153. }
  154. }
  155. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  156. if ($result != $digits[0]) {
  157. return false;
  158. }
  159. // Valida segundo dígito verificador
  160. $length = $length + 1;
  161. $numbers = substr($cnpj, 0, $length);
  162. $sum = 0;
  163. $pos = $length - 7;
  164. for ($i = $length; $i >= 1; $i--) {
  165. $sum += $numbers[$length - $i] * $pos--;
  166. if ($pos < 2) {
  167. $pos = 9;
  168. }
  169. }
  170. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  171. if ($result != $digits[1]) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. }