ProviderRequest.php 11 KB

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