116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Investment\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Modules\Investment\Entities\UserInvestmentLimit;
|
|
use Modules\Investment\Repositories\Contracts\UserInvestmentLimitRepositoryInterface;
|
|
|
|
class UserInvestmentLimitRepository implements UserInvestmentLimitRepositoryInterface
|
|
{
|
|
protected function baseQuery()
|
|
{
|
|
return UserInvestmentLimit::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',
|
|
'effective_date',
|
|
'expiry_date',
|
|
'max_investment_amount',
|
|
'max_transaction_amount',
|
|
'created_at',
|
|
];
|
|
$sortBy = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'created_at';
|
|
$sortOrder = strtolower($sortOrder) === 'asc' ? 'asc' : 'desc';
|
|
|
|
$query = UserInvestmentLimit::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 ($investmentTypeId = request('investment_type_id')) {
|
|
$query->where('investment_type_id', $investmentTypeId);
|
|
}
|
|
|
|
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): UserInvestmentLimit
|
|
{
|
|
return UserInvestmentLimit::create($data);
|
|
}
|
|
|
|
public function findById(string $id): ?UserInvestmentLimit
|
|
{
|
|
return UserInvestmentLimit::query()
|
|
->with(['user:id,name,email', 'investmentType:id,code,name'])
|
|
->find($id);
|
|
}
|
|
|
|
public function delete(string $id): bool
|
|
{
|
|
$limit = UserInvestmentLimit::query()->find($id);
|
|
|
|
if ($limit) {
|
|
return (bool) $limit->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function all(string $search = ''): Collection
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->get();
|
|
}
|
|
}
|