119 lines
3.4 KiB
PHP
119 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Investment\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Modules\Investment\Entities\SalaryContribution;
|
|
use Modules\Investment\Repositories\Contracts\SalaryContributionRepositoryInterface;
|
|
|
|
class SalaryContributionRepository implements SalaryContributionRepositoryInterface
|
|
{
|
|
protected function baseQuery()
|
|
{
|
|
return SalaryContribution::query()
|
|
->with(['user:id,name,email', 'investmentType:id,code,name'])
|
|
->orderByDesc('created_at');
|
|
}
|
|
|
|
protected function applySearch($query, string $search)
|
|
{
|
|
if ($search !== '') {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->whereHas('user', function ($userQuery) use ($search) {
|
|
$userQuery->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('email', 'ILIKE', "%{$search}%")
|
|
->orWhere('ic_number', 'ILIKE', "%{$search}%");
|
|
})->orWhereHas('investmentType', function ($typeQuery) use ($search) {
|
|
$typeQuery->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('code', 'ILIKE', "%{$search}%");
|
|
});
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function getAllPaginated(int $perPage = 10, string $search = '')
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelationsPaginated(
|
|
int $perPage = 10,
|
|
string $search = '',
|
|
string $sortBy = 'created_at',
|
|
string $sortOrder = 'desc'
|
|
) {
|
|
$allowedSortColumns = [
|
|
'id',
|
|
'monthly_amount',
|
|
'start_date',
|
|
'end_date',
|
|
'status',
|
|
'created_at',
|
|
];
|
|
$sortBy = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'created_at';
|
|
$sortOrder = strtolower($sortOrder) === 'asc' ? 'asc' : 'desc';
|
|
|
|
$query = SalaryContribution::query()
|
|
->with(['user:id,name,email', 'investmentType:id,code,name'])
|
|
->orderBy($sortBy, $sortOrder);
|
|
|
|
$this->applySearch($query, $search);
|
|
|
|
if ($userId = request('user_id')) {
|
|
$query->where('user_id', $userId);
|
|
}
|
|
|
|
if ($status = request('status')) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelations(string $search = ''): Collection
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function create(array $data): SalaryContribution
|
|
{
|
|
$contribution = SalaryContribution::create($data);
|
|
|
|
return $contribution->load(['user:id,name,email', 'investmentType:id,code,name']);
|
|
}
|
|
|
|
public function findById(string $id): ?SalaryContribution
|
|
{
|
|
return SalaryContribution::query()
|
|
->with(['user:id,name,email', 'investmentType:id,code,name'])
|
|
->find($id);
|
|
}
|
|
|
|
public function delete(string $id): bool
|
|
{
|
|
$item = SalaryContribution::query()->find($id);
|
|
|
|
if ($item) {
|
|
return (bool) $item->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function all(string $search = ''): Collection
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->get();
|
|
}
|
|
}
|