|
|
@@ -2,8 +2,10 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Enums\UserDependentStatusEnum;
|
|
|
use App\Models\UserDependent;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
+use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
|
|
class UserDependentService
|
|
|
{
|
|
|
@@ -14,6 +16,28 @@ class UserDependentService
|
|
|
->get();
|
|
|
}
|
|
|
|
|
|
+ public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
|
|
|
+ {
|
|
|
+ $query = UserDependent::with(['responsibleUser.position', 'responsibleUser.sector'])
|
|
|
+ ->orderBy('created_at', 'asc');
|
|
|
+
|
|
|
+ if (!empty($filters['status'])) {
|
|
|
+ $query->where('status', $filters['status']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($filters['search'])) {
|
|
|
+ $term = '%' . mb_strtolower($filters['search']) . '%';
|
|
|
+ $query->where(function ($q) use ($term) {
|
|
|
+ $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
|
|
|
+ ->orWhereHas('responsibleUser', function ($q) use ($term) {
|
|
|
+ $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term]);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
public function findById(int $id): ?UserDependent
|
|
|
{
|
|
|
return UserDependent::find($id);
|
|
|
@@ -21,9 +45,35 @@ class UserDependentService
|
|
|
|
|
|
public function create(array $data): UserDependent
|
|
|
{
|
|
|
+ $data['status'] = UserDependentStatusEnum::PENDING->value;
|
|
|
+
|
|
|
return UserDependent::create($data);
|
|
|
}
|
|
|
|
|
|
+ public function approve(int $id): ?UserDependent
|
|
|
+ {
|
|
|
+ $model = $this->findById($id);
|
|
|
+
|
|
|
+ if (!$model) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $model->update(['status' => UserDependentStatusEnum::APPROVED]);
|
|
|
+ return $model->fresh();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function refuse(int $id): ?UserDependent
|
|
|
+ {
|
|
|
+ $model = $this->findById($id);
|
|
|
+
|
|
|
+ if (!$model) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $model->update(['status' => UserDependentStatusEnum::REFUSED]);
|
|
|
+ return $model->fresh();
|
|
|
+ }
|
|
|
+
|
|
|
public function update(int $id, array $data): ?UserDependent
|
|
|
{
|
|
|
$model = $this->findById($id);
|