ProviderRequest.php 9.0 KB

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