| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Services;
- use App\Models\PartnerAgreement;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Pagination\LengthAwarePaginator;
- class PartnerAgreementService
- {
- public function getAll(): Collection
- {
- return PartnerAgreement::with(['category', 'city', 'state', 'logo'])
- ->orderBy('company_name')
- ->get();
- }
- public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
- {
- $query = PartnerAgreement::with(['category', 'city', 'state', 'logo'])
- ->orderBy('company_name');
- if (!empty($filters['search'])) {
- $search = $filters['search'];
- $query->where(function ($q) use ($search) {
- $q->where('company_name', 'like', "%{$search}%")
- ->orWhere('responsible', 'like', "%{$search}%")
- ->orWhere('cnpj', 'like', "%{$search}%");
- });
- }
- if (!empty($filters['expires_in_days'])) {
- $days = (int) $filters['expires_in_days'];
- $query->whereBetween('contract_end', [now()->startOfDay(), now()->addDays($days)->endOfDay()]);
- }
- if (!empty($filters['created_month']) && $filters['created_month'] === 'current') {
- $query->whereMonth('created_at', now()->month)
- ->whereYear('created_at', now()->year);
- }
- return $query->paginate($perPage);
- }
- public function findById(int $id): ?PartnerAgreement
- {
- return PartnerAgreement::with(['category', 'city', 'state', 'services', 'logo', 'media'])->find($id);
- }
- public function create(array $data): PartnerAgreement
- {
- return PartnerAgreement::create($data);
- }
- public function update(int $id, array $data): ?PartnerAgreement
- {
- $model = PartnerAgreement::find($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh(['category', 'city', 'state', 'logo', 'media']);
- }
- public function delete(int $id): bool
- {
- $model = PartnerAgreement::find($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- }
|