ProviderRequest.php 9.2 KB

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