113 lines
3.3 KiB
PHP
113 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\ClientLocation;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
class ClientLocationRepository extends BaseRepository implements ClientLocationRepositoryInterface
|
|
{
|
|
public function __construct(ClientLocation $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function getByClientId(int $client_id)
|
|
{
|
|
$query = $this->model->newQuery();
|
|
$query->where('client_id', $client_id);
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* Get paginated client locations with optional filters
|
|
*/
|
|
public function getPaginated(array $filters = [], int $perPage = 10)
|
|
{
|
|
$query = $this->model->newQuery();
|
|
|
|
// Filter by client_id
|
|
if (isset($filters['client_id'])) {
|
|
$query->where('client_id', $filters['client_id']);
|
|
}
|
|
|
|
// Filter by is_default
|
|
if (isset($filters['is_default'])) {
|
|
$query->where('is_default', $filters['is_default']);
|
|
}
|
|
|
|
// Search filter
|
|
if (!empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'LIKE', "%{$search}%")
|
|
->orWhere('address', 'LIKE', "%{$search}%")
|
|
->orWhere('city', 'LIKE', "%{$search}%")
|
|
->orWhere('postal_code', 'LIKE', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// Order by
|
|
$query->orderBy('is_default', 'desc')
|
|
->orderBy('created_at', 'desc');
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Get paginated locations for a specific client
|
|
*/
|
|
public function getPaginatedByClientId(int $clientId, array $filters = [], int $perPage = 10): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery()->where('client_id', $clientId);
|
|
|
|
if (isset($filters['is_default'])) {
|
|
$query->where('is_default', $filters['is_default']);
|
|
}
|
|
|
|
if (!empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'LIKE', "%{$search}%")
|
|
->orWhere('address', 'LIKE', "%{$search}%")
|
|
->orWhere('city', 'LIKE', "%{$search}%")
|
|
->orWhere('postal_code', 'LIKE', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
$query->orderBy('is_default', 'desc')
|
|
->orderBy('created_at', 'desc');
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Get default location for a client
|
|
*/
|
|
public function getDefaultByClientId(int $clientId): ?ClientLocation
|
|
{
|
|
return $this->model->where('client_id', $clientId)
|
|
->where('is_default', true)
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* Set a location as default and update others
|
|
*/
|
|
public function setAsDefault(int $locationId): ClientLocation
|
|
{
|
|
$location = $this->find($locationId);
|
|
|
|
$this->model->where('client_id', $location->client_id)
|
|
->where('id', '!=', $locationId)
|
|
->update(['is_default' => false]);
|
|
|
|
// Set this location as default
|
|
$location->update(['is_default' => true]);
|
|
|
|
return $location->fresh();
|
|
}
|
|
}
|