ProviderRequest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_metadata' => 'sometimes|array',
  71. ];
  72. if ($this->isMethod('post')) {
  73. $rules['document'] = [
  74. 'required',
  75. 'string',
  76. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  77. function ($attribute, $value, $fail) {
  78. if (! $this->isValidCpfCnpj($value)) {
  79. $fail(__('validation.custom.document.invalid'));
  80. }
  81. },
  82. Rule::unique('providers', 'document')->whereNull('deleted_at'),
  83. ];
  84. $rules['recipient_name'] = 'required|string|max:255';
  85. $rules['recipient_email'] = 'required|email|max:255';
  86. $rules['recipient_description'] = 'required|string';
  87. $rules['recipient_document'] = 'required|string|max:20';
  88. $rules['recipient_type'] = ['required', Rule::in(['individual', 'company'])];
  89. $rules['recipient_code'] = 'required|string|max:255';
  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. }