PartnerAgreementService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\PartnerAgreement;
  5. use App\Models\User;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Pagination\LengthAwarePaginator;
  8. use Illuminate\Support\Facades\Hash;
  9. class PartnerAgreementService
  10. {
  11. public function getAll(): Collection
  12. {
  13. return PartnerAgreement::with(['category', 'city', 'logo'])
  14. ->orderBy('company_name')
  15. ->get();
  16. }
  17. public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  18. {
  19. $query = PartnerAgreement::with(['category', 'city', 'logo'])
  20. ->orderBy('company_name');
  21. if (!empty($filters['search'])) {
  22. $search = $filters['search'];
  23. $query->where(function ($q) use ($search) {
  24. $q->where('company_name', 'like', "%{$search}%")
  25. ->orWhere('responsible', 'like', "%{$search}%")
  26. ->orWhere('cnpj', 'like', "%{$search}%");
  27. });
  28. }
  29. if (!empty($filters['expires_in_days'])) {
  30. $days = (int) $filters['expires_in_days'];
  31. $query->whereBetween('contract_end', [now()->startOfDay(), now()->addDays($days)->endOfDay()]);
  32. }
  33. if (!empty($filters['created_month']) && $filters['created_month'] === 'current') {
  34. $query->whereMonth('created_at', now()->month)
  35. ->whereYear('created_at', now()->year);
  36. }
  37. return $query->paginate($perPage);
  38. }
  39. public function getExpiringPaginated(int $days = 30, int $perPage = 10): LengthAwarePaginator
  40. {
  41. return PartnerAgreement::with(['category', 'city', 'logo'])
  42. ->whereNotNull('contract_end')
  43. ->whereBetween('contract_end', [now()->startOfDay(), now()->addDays($days)->endOfDay()])
  44. ->orderBy('contract_end')
  45. ->paginate($perPage);
  46. }
  47. public function findById(int $id): ?PartnerAgreement
  48. {
  49. return PartnerAgreement::with(['category', 'city', 'state', 'services', 'logo', 'media', 'user'])->find($id);
  50. }
  51. public function findByUserId(int $userId): ?PartnerAgreement
  52. {
  53. $partner = PartnerAgreement::with(['category', 'city', 'state', 'services', 'logo', 'media'])
  54. ->where('user_id', $userId)
  55. ->first();
  56. if(!$partner) {
  57. $partner = new PartnerAgreement();
  58. $partner->company_name = 'Novo Parceiro';
  59. $partner->user_id = $userId;
  60. $partner->save();
  61. $partner->fresh(['category', 'city', 'state', 'services', 'logo', 'media']);
  62. }
  63. return $partner;
  64. }
  65. public function findDados(int $id): ?PartnerAgreement
  66. {
  67. return PartnerAgreement::with(['category', 'logo'])->find($id);
  68. }
  69. public function findContato(int $id): ?PartnerAgreement
  70. {
  71. return PartnerAgreement::find($id);
  72. }
  73. public function findEndereco(int $id): ?PartnerAgreement
  74. {
  75. return PartnerAgreement::with(['city', 'state'])->find($id);
  76. }
  77. public function findContrato(int $id): ?PartnerAgreement
  78. {
  79. return PartnerAgreement::with(['media'])->find($id);
  80. }
  81. public function create(array $data): PartnerAgreement
  82. {
  83. $user = User::create([
  84. 'name' => $data['user_name'],
  85. 'email' => $data['user_email'],
  86. 'password' => Hash::make($data['user_password']),
  87. 'type' => UserTypeEnum::PARCEIRO,
  88. ]);
  89. $partnerData = collect($data)
  90. ->except(['user_name', 'user_email', 'user_password'])
  91. ->put('user_id', $user->id)
  92. ->all();
  93. $model = PartnerAgreement::create($partnerData);
  94. return $model->fresh(['category', 'logo', 'user']);
  95. }
  96. public function update(int $id, array $data): ?PartnerAgreement
  97. {
  98. $model = PartnerAgreement::find($id);
  99. if (!$model) {
  100. return null;
  101. }
  102. $model->update($data);
  103. return $model->fresh(['category', 'city', 'state', 'logo', 'media']);
  104. }
  105. public function delete(int $id): bool
  106. {
  107. $model = PartnerAgreement::find($id);
  108. if (!$model) {
  109. return false;
  110. }
  111. return $model->delete();
  112. }
  113. }