ProviderRequest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 'recipient_name' => 'sometimes|string|max:255',
  65. 'recipient_email' => 'sometimes|email|max:255',
  66. 'recipient_description' => 'sometimes|nullable|string',
  67. 'recipient_document' => 'sometimes|string|max:20',
  68. 'recipient_type' => ['sometimes', Rule::in(['individual', 'company'])],
  69. 'recipient_code' => 'sometimes|string|max:255',
  70. 'recipient_payment_mode' => ['sometimes', Rule::in(['bank_transfer'])],
  71. 'recipient_metadata' => 'sometimes|array',
  72. ];
  73. if ($this->isMethod('post')) {
  74. $rules['document'] = [
  75. 'required',
  76. 'string',
  77. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  78. function ($attribute, $value, $fail) {
  79. if (!$this->isValidCpfCnpj($value)) {
  80. $fail(__('validation.custom.document.invalid'));
  81. }
  82. },
  83. Rule::unique('providers', 'document')->whereNull('deleted_at'),
  84. ];
  85. $rules['recipient_name'] = 'required|string|max:255';
  86. $rules['recipient_email'] = 'required|email|max:255';
  87. $rules['recipient_description'] = 'required|string';
  88. $rules['recipient_document'] = 'required|string|max:20';
  89. $rules['recipient_type'] = ['required', Rule::in(['individual', 'company'])];
  90. $rules['recipient_code'] = 'required|string|max:255';
  91. $rules['recipient_payment_mode'] = ['required', Rule::in(['bank_transfer'])];
  92. $rules['recipient_default_bank_account'] = 'required|array';
  93. $rules['recipient_default_bank_account.holder_name'] = 'required|string|max:255';
  94. $rules['recipient_default_bank_account.holder_type'] = ['required', Rule::in(['individual', 'company'])];
  95. $rules['recipient_default_bank_account.holder_document'] = 'required|string|max:20';
  96. $rules['recipient_default_bank_account.bank'] = 'required|string|max:20';
  97. $rules['recipient_default_bank_account.branch_number'] = 'required|string|max:20';
  98. $rules['recipient_default_bank_account.branch_check_digit'] = 'sometimes|nullable|string|max:10';
  99. $rules['recipient_default_bank_account.account_number'] = 'required|string|max:20';
  100. $rules['recipient_default_bank_account.account_check_digit'] = 'required|string|max:10';
  101. $rules['recipient_default_bank_account.type'] = ['required', Rule::in(['checking', 'savings'])];
  102. $rules['recipient_default_bank_account.metadata'] = 'required|array';
  103. $rules['recipient_metadata'] = 'required|array';
  104. $rules['user_id'] = [
  105. 'required',
  106. 'exists:users,id',
  107. Rule::unique('providers', 'user_id')->whereNull('deleted_at'),
  108. function ($attribute, $value, $fail) {
  109. $clientExists = \DB::table('clients')
  110. ->where('user_id', $value)
  111. ->whereNull('deleted_at')
  112. ->exists();
  113. if ($clientExists) {
  114. $fail(__('validation.custom.user_id.already_linked_to_client'));
  115. }
  116. $providerExists = \DB::table('providers')
  117. ->where('user_id', $value)
  118. ->whereNull('deleted_at')
  119. ->exists();
  120. if ($providerExists) {
  121. $fail(__('validation.custom.user_id.already_linked_to_provider'));
  122. }
  123. },
  124. ];
  125. }
  126. return $rules;
  127. }
  128. /**
  129. * Valida CPF ou CNPJ
  130. */
  131. private function isValidCpfCnpj(string $value): bool
  132. {
  133. $value = preg_replace('/[^0-9]/', '', $value);
  134. if (strlen($value) === 11) {
  135. return $this->isValidCpf($value);
  136. } elseif (strlen($value) === 14) {
  137. return $this->isValidCnpj($value);
  138. }
  139. return false;
  140. }
  141. /**
  142. * Valida CPF
  143. */
  144. private function isValidCpf(string $cpf): bool
  145. {
  146. // Elimina CPFs inválidos conhecidos
  147. if (preg_match('/(\d)\1{10}/', $cpf)) {
  148. return false;
  149. }
  150. // Valida primeiro dígito verificador
  151. for ($t = 9; $t < 11; $t++) {
  152. for ($d = 0, $c = 0; $c < $t; $c++) {
  153. $d += $cpf[$c] * (($t + 1) - $c);
  154. }
  155. $d = ((10 * $d) % 11) % 10;
  156. if ($cpf[$c] != $d) {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. /**
  163. * Valida CNPJ
  164. */
  165. private function isValidCnpj(string $cnpj): bool
  166. {
  167. // Elimina CNPJs inválidos conhecidos
  168. if (preg_match('/(\d)\1{13}/', $cnpj)) {
  169. return false;
  170. }
  171. // Valida primeiro dígito verificador
  172. $length = strlen($cnpj) - 2;
  173. $numbers = substr($cnpj, 0, $length);
  174. $digits = substr($cnpj, $length);
  175. $sum = 0;
  176. $pos = $length - 7;
  177. for ($i = $length; $i >= 1; $i--) {
  178. $sum += $numbers[$length - $i] * $pos--;
  179. if ($pos < 2) {
  180. $pos = 9;
  181. }
  182. }
  183. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  184. if ($result != $digits[0]) {
  185. return false;
  186. }
  187. // Valida segundo dígito verificador
  188. $length = $length + 1;
  189. $numbers = substr($cnpj, 0, $length);
  190. $sum = 0;
  191. $pos = $length - 7;
  192. for ($i = $length; $i >= 1; $i--) {
  193. $sum += $numbers[$length - $i] * $pos--;
  194. if ($pos < 2) {
  195. $pos = 9;
  196. }
  197. }
  198. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  199. if ($result != $digits[1]) {
  200. return false;
  201. }
  202. return true;
  203. }
  204. }