PartnerAgreementRequest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Enums\PartnerAgreementStatusEnum;
  4. use App\Enums\PartnerAgreementTypeEnum;
  5. use App\Models\PartnerAgreement;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Validation\Rule;
  8. class PartnerAgreementRequest extends FormRequest
  9. {
  10. public function rules(): array
  11. {
  12. $cnpjUnique = 'unique:partner_agreements,cnpj';
  13. if ($this->isMethod('put')) {
  14. $partnerId = (int) $this->route('id') ?: PartnerAgreement::where('user_id', auth()->id())->value('id');
  15. if ($partnerId) {
  16. $cnpjUnique = 'unique:partner_agreements,cnpj,' . $partnerId;
  17. }
  18. }
  19. $rules = [
  20. 'user_name' => 'sometimes|nullable|string|max:255',
  21. 'user_email' => 'sometimes|nullable|email|max:255',
  22. 'user_password' => 'sometimes|nullable|string|min:6',
  23. 'company_name' => 'sometimes|string|max:255',
  24. 'cnpj' => ['sometimes', 'nullable', 'string', 'max:18', $cnpjUnique],
  25. 'category_id' => 'sometimes|nullable|integer|exists:categories,id',
  26. 'responsible' => 'sometimes|nullable|string|max:255',
  27. 'discount_percentage' => 'sometimes|nullable|numeric|min:0|max:100',
  28. 'description' => 'sometimes|nullable|string',
  29. 'email' => 'sometimes|nullable|email|max:255',
  30. 'phone' => 'sometimes|nullable|string|max:20',
  31. 'whatsapp' => 'sometimes|nullable|string|max:20',
  32. 'website' => 'sometimes|nullable|string|max:255',
  33. 'address' => 'sometimes|nullable|string|max:255',
  34. 'neighborhood' => 'sometimes|nullable|string|max:255',
  35. 'city_id' => 'sometimes|nullable|integer|exists:cities,id',
  36. 'state_id' => 'sometimes|nullable|integer|exists:states,id',
  37. 'zip_code' => 'sometimes|nullable|string|max:9',
  38. 'working_hours' => 'sometimes|nullable|string|max:255',
  39. 'contract_start' => 'sometimes|nullable|date',
  40. 'contract_end' => 'sometimes|nullable|date|after_or_equal:contract_start',
  41. 'status' => ['sometimes', Rule::enum(PartnerAgreementStatusEnum::class)],
  42. 'type' => ['sometimes', Rule::enum(PartnerAgreementTypeEnum::class)],
  43. ];
  44. if ($this->isMethod('post')) {
  45. $rules['company_name'] = 'required|string|max:255';
  46. $rules['user_name'] = 'required|string|max:255';
  47. $rules['user_email'] = 'required|email|max:255|unique:users,email';
  48. $rules['user_password'] = 'required|string|min:6';
  49. }
  50. return $rules;
  51. }
  52. }