|
|
@@ -7,6 +7,7 @@ use App\Enums\UserStatusEnum;
|
|
|
use App\Enums\UserTypeEnum;
|
|
|
use App\Models\PartnerAgreement;
|
|
|
use App\Models\User;
|
|
|
+use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
@@ -28,19 +29,52 @@ class PartnerAgreementService
|
|
|
|
|
|
public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
|
|
|
{
|
|
|
- $query = PartnerAgreement::with(['category', 'city', 'logo'])
|
|
|
- ->orderBy('company_name');
|
|
|
+ $query = $this->baseQuery($filters)
|
|
|
+ ->with(['category', 'city', 'logo']);
|
|
|
+
|
|
|
+ if (!empty($filters['expires_in_days'])) {
|
|
|
+ $query->orderBy('contract_end');
|
|
|
+ } else {
|
|
|
+ $query->orderBy('company_name');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($filters['status'])) {
|
|
|
+ $query->where('status', $filters['status']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Status existentes no conjunto filtrado, desconsiderando o próprio filtro de status.
|
|
|
+ *
|
|
|
+ * @return array<int, string>
|
|
|
+ */
|
|
|
+ public function getStatusOptions(array $filters = []): array
|
|
|
+ {
|
|
|
+ return $this->baseQuery($filters)
|
|
|
+ ->toBase()
|
|
|
+ ->distinct()
|
|
|
+ ->pluck('status')
|
|
|
+ ->filter()
|
|
|
+ ->values()
|
|
|
+ ->all();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function baseQuery(array $filters): Builder
|
|
|
+ {
|
|
|
+ $query = PartnerAgreement::query();
|
|
|
|
|
|
if (!empty($filters['type'])) {
|
|
|
$query->where('type', $filters['type']);
|
|
|
}
|
|
|
|
|
|
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}%");
|
|
|
+ $term = '%' . mb_strtolower($filters['search']) . '%';
|
|
|
+ $query->where(function ($q) use ($term) {
|
|
|
+ $q->whereRaw('UNACCENT(LOWER(company_name)) LIKE UNACCENT(?)', [$term])
|
|
|
+ ->orWhereRaw('UNACCENT(LOWER(COALESCE(responsible, \'\'))) LIKE UNACCENT(?)', [$term])
|
|
|
+ ->orWhereRaw('UNACCENT(LOWER(COALESCE(cnpj, \'\'))) LIKE UNACCENT(?)', [$term]);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -54,7 +88,7 @@ class PartnerAgreementService
|
|
|
->whereYear('created_at', now()->year);
|
|
|
}
|
|
|
|
|
|
- return $query->paginate($perPage);
|
|
|
+ return $query;
|
|
|
}
|
|
|
|
|
|
public function getExpiringPaginated(int $days = 30, int $perPage = 10): LengthAwarePaginator
|