ProviderRequest.php 9.2 KB

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