ProviderRequest.php 9.3 KB

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