PartnerAgreementService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PartnerAgreement;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use Illuminate\Pagination\LengthAwarePaginator;
  6. class PartnerAgreementService
  7. {
  8. public function getAll(): Collection
  9. {
  10. return PartnerAgreement::with(['category', 'city', 'logo'])
  11. ->orderBy('company_name')
  12. ->get();
  13. }
  14. public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  15. {
  16. $query = PartnerAgreement::with(['category', 'city', 'logo'])
  17. ->orderBy('company_name');
  18. if (!empty($filters['search'])) {
  19. $search = $filters['search'];
  20. $query->where(function ($q) use ($search) {
  21. $q->where('company_name', 'like', "%{$search}%")
  22. ->orWhere('responsible', 'like', "%{$search}%")
  23. ->orWhere('cnpj', 'like', "%{$search}%");
  24. });
  25. }
  26. if (!empty($filters['expires_in_days'])) {
  27. $days = (int) $filters['expires_in_days'];
  28. $query->whereBetween('contract_end', [now()->startOfDay(), now()->addDays($days)->endOfDay()]);
  29. }
  30. if (!empty($filters['created_month']) && $filters['created_month'] === 'current') {
  31. $query->whereMonth('created_at', now()->month)
  32. ->whereYear('created_at', now()->year);
  33. }
  34. return $query->paginate($perPage);
  35. }
  36. public function findById(int $id): ?PartnerAgreement
  37. {
  38. return PartnerAgreement::with(['category', 'city', 'state', 'services', 'logo', 'media'])->find($id);
  39. }
  40. public function findDados(int $id): ?PartnerAgreement
  41. {
  42. return PartnerAgreement::with(['category', 'logo'])->find($id);
  43. }
  44. public function findContato(int $id): ?PartnerAgreement
  45. {
  46. return PartnerAgreement::find($id);
  47. }
  48. public function findEndereco(int $id): ?PartnerAgreement
  49. {
  50. return PartnerAgreement::with(['city', 'state'])->find($id);
  51. }
  52. public function findContrato(int $id): ?PartnerAgreement
  53. {
  54. return PartnerAgreement::with(['media'])->find($id);
  55. }
  56. public function create(array $data): PartnerAgreement
  57. {
  58. $model = PartnerAgreement::create($data);
  59. return $model->fresh(['category', 'logo']);
  60. }
  61. public function update(int $id, array $data): ?PartnerAgreement
  62. {
  63. $model = PartnerAgreement::find($id);
  64. if (!$model) {
  65. return null;
  66. }
  67. $model->update($data);
  68. return $model->fresh(['category', 'city', 'state', 'logo', 'media']);
  69. }
  70. public function delete(int $id): bool
  71. {
  72. $model = PartnerAgreement::find($id);
  73. if (!$model) {
  74. return false;
  75. }
  76. return $model->delete();
  77. }
  78. }