ProviderRequest.php 6.5 KB

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