доработка компоненты вывода клиентов: исправлена пагинация

This commit is contained in:
developer 2026-04-23 16:56:09 +08:00
parent 0b33aaaf9f
commit 42bc369227
2 changed files with 58 additions and 46 deletions

View File

@ -25,6 +25,8 @@ class ClientsTable extends Component
public $count; public $count;
public $mode;//short || full public $mode;//short || full
private $filter = []; private $filter = [];
public $clientsCount;
public $currentPage;
public function mount($status = null, $count = 10, $mode = 'full') public function mount($status = null, $count = 10, $mode = 'full')
{ {
@ -34,8 +36,13 @@ public function mount($status = null, $count = 10, $mode = 'full')
$this->filter = []; $this->filter = [];
} }
public function goToPage($page) {
$this->currentPage = $page;
}
#[On('clientsTableFilterUpdated')] #[On('clientsTableFilterUpdated')]
public function appendFilter($filter) { public function appendFilter($filter)
{
if ($filter) { if ($filter) {
$this->filter = $filter; $this->filter = $filter;
} else { } else {
@ -50,32 +57,27 @@ public function getDeals()
$clients = false; $clients = false;
$user = auth()->user(); $user = auth()->user();
if($cityManager = CityManager::where('user_id', auth()->id())->first()) { if ($cityManager = CityManager::where('user_id', auth()->id())->first()) {
$companies = $cityManager->city->companies->pluck('id'); $companies = $cityManager->city->companies->pluck('id');
$deals = Deal::whereIn('agent_id', function ($query) use ($companies) $deals = Deal::whereIn('agent_id', function ($query) use ($companies) {
{
$query->select('id'); $query->select('id');
$query->from('agents'); $query->from('agents');
$query->whereIn('company_id', $companies); $query->whereIn('company_id', $companies);
}); });
} } else if ($company = AdminCompanyOfUser()) {
else if ($company = AdminCompanyOfUser()) { $deals = Deal::whereIn('agent_id', function ($query) use ($company) {
$deals = Deal::whereIn('agent_id', function ($query) use ($company)
{
$query->select('id'); $query->select('id');
$query->from('agents'); $query->from('agents');
$query->where('company_id', $company->id); $query->where('company_id', $company->id);
}); });
} } elseif ($agent = Agent::where('user_id', $user->id)->first()) {
elseif ($agent = Agent::where('user_id', $user->id)->first())
{
$deals = Deal::where('agent_id', $agent->id); $deals = Deal::where('agent_id', $agent->id);
} }
if (array_key_exists('status', $this->filter)) { if (array_key_exists('status', $this->filter)) {
$deals->where('status', $this->filter['status']); $deals->where('status', $this->filter['status']);
} }
if (!$deals) { if (!$deals) {
return Client::where('id',0); return Client::where('id', 0);
} }
return $deals; return $deals;
} }
@ -84,15 +86,13 @@ function getClients()
{ {
$deals = $this->getDeals(); $deals = $this->getDeals();
$clients = Client::join('deal_clients', 'users.id', '=', 'deal_clients.client_id') $clients = Client::join('deal_clients', 'users.id', '=', 'deal_clients.client_id')
->whereIn('users.id', function ($query) use ($deals) ->whereIn('users.id', function ($query) use ($deals) {
{
$query->select('client_id') $query->select('client_id')
->from('deal_clients') ->from('deal_clients')
->whereIn('deal_id', $deals->get()->pluck('id')); ->whereIn('deal_id', $deals->get()->pluck('id'));
}) })
->orderBy('deal_clients.id', 'desc') ->orderBy('deal_clients.id', 'desc')
->with('deals'); ->with('deals');
;
$clients = $clients->select('users.*'); $clients = $clients->select('users.*');
$clients = $clients->orderBy('name'); $clients = $clients->orderBy('name');
@ -100,33 +100,31 @@ function getClients()
$clients->whereFullText(['name', 'phone', 'email'], $this->filter['search']); $clients->whereFullText(['name', 'phone', 'email'], $this->filter['search']);
} }
if (array_key_exists('status', $this->filter) && $this->status == DealStatus::UNIQUE) if (array_key_exists('status', $this->filter) && $this->status == DealStatus::UNIQUE) {
{ $clients = $clients->whereHas('deals', function ($query) {
$clients = $clients->whereHas('deals', function ($query)
{
$query->where('status', DealStatus::UNIQUE); $query->where('status', DealStatus::UNIQUE);
})->paginate($this->count, ['*'], 'clients'); });
} } elseif (array_key_exists('status', $this->filter) && $this->status == DealStatus::NOT_UNIQUE) {
elseif (array_key_exists('status', $this->filter) && $this->status == DealStatus::NOT_UNIQUE) $clients = $clients->whereHas('deals', function ($query) {
{
$clients = $clients->whereHas('deals', function ($query)
{
$query->whereIn('status', [DealStatus::MODERATION, DealStatus::NEW , DealStatus::NOT_UNIQUE]); $query->whereIn('status', [DealStatus::MODERATION, DealStatus::NEW , DealStatus::NOT_UNIQUE]);
})->paginate($this->count, ['*'], 'clients'); });
}
else
{
$clients = $clients->paginate($this->count, ['*'], 'clients');
} }
//$clients = $clients->skip($this->currentPage)->take(10)->get();
return $clients; return $clients;
} }
public function render() public function render()
{ {
$clients = $this->getClients();
if ($this->clientsCount != $clients->count()) {
$this->clientsCount = $clients->count();
$this->currentPage = 1;
}
$clients = $clients->skip(($this->currentPage - 1) * $this->count)->take($this->count)->get();
return view( return view(
'livewire.clients-table', 'livewire.clients-table',
[ [
'clients' => $this->getClients(), 'clients' => $clients,
'statuses' => DealStatus::class 'statuses' => DealStatus::class
] ]
); );

View File

@ -1,7 +1,9 @@
<div class="position-relative"> <div class="position-relative">
<div wire:loading.class.remove="d-none" class="d-none d-flex position-absolute w-100 h-100 top-0 start-0 align-items-center justify-content-center rounded-4" style="background-color:#ffffffb5"> <div wire:loading.class.remove="d-none"
class="d-none d-flex position-absolute w-100 h-100 top-0 start-0 align-items-center justify-content-center rounded-4"
style="background-color:#ffffffb5">
<div class="spinner-border text-secondary" style="width: 3rem; height: 3rem;" role="status"> <div class="spinner-border text-secondary" style="width: 3rem; height: 3rem;" role="status">
<span class="visually-hidden">Loading...</span> <span class="visually-hidden">Loading...</span>
</div> </div>
</div> </div>
<div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4"> <div class="fs-5 bg-light p-0 m-0 border border-1 rounded-4">
@ -21,9 +23,9 @@
//echo 'dId:' . $deal->id; //echo 'dId:' . $deal->id;
$filterRow[] = 'filter[deal_id][]=' . $deal->id; $filterRow[] = 'filter[deal_id][]=' . $deal->id;
$complexesNames[] = '<span>' $complexesNames[] = '<span>'
. $deal->complex?->name . $deal->complex?->name
. (($deal->unique_until) ? '<span class="ms-1" title="Срок уникальности истечет ' . \Carbon\Carbon::parse($deal->unique_until)->format('d.m.Y') . '">'. '<i class="bi bi-info-circle-fill"></i>' . '</span>' : '' ) . (($deal->unique_until) ? '<span class="ms-1" title="Срок уникальности истечет ' . \Carbon\Carbon::parse($deal->unique_until)->format('d.m.Y') . '">' . '<i class="bi bi-info-circle-fill"></i>' . '</span>' : '')
. '</span>'; . '</span>';
$companiesNames[] = $deal->agent?->company?->name; $companiesNames[] = $deal->agent?->company?->name;
$agentsNames[] = $deal->agent?->user?->name; $agentsNames[] = $deal->agent?->user?->name;
} }
@ -35,7 +37,7 @@
$agentsNames = array_unique($agentsNames); $agentsNames = array_unique($agentsNames);
$agentsNames = implode('<br>', $agentsNames); $agentsNames = implode('<br>', $agentsNames);
$dealsWithContracts = $client->deals()->whereHas('contract'); $dealsWithContracts = $client->deals()->whereHas('contract');
?> ?>
@if ($mode == 'full') @if ($mode == 'full')
<div class="d-flex flex-row m-0 my-2 px-2 client-row"> <div class="d-flex flex-row m-0 my-2 px-2 client-row">
<div class="d-flex flex-column flex-md-row w-100"> <div class="d-flex flex-column flex-md-row w-100">
@ -43,7 +45,7 @@
{{ $client->name }} {{ $client->name }}
@if (env('APP_DEBUG')) @if (env('APP_DEBUG'))
@foreach($client->deals as $deal) @foreach($client->deals as $deal)
<div class="text-secondary fs-6 fw-light">Bitrix ID: {{ $deal->bitrixId() }} </div> <div class="text-secondary fs-6 fw-light">Bitrix ID: {{ $deal->bitrixId() }} </div>
@endforeach @endforeach
@endif @endif
</div> </div>
@ -114,7 +116,7 @@ class="bi bi-file-earmark-text ms-auto" viewBox="0 0 16 16">
$filterRow[] = 'filter[deal_id][]=' . $deal->id; $filterRow[] = 'filter[deal_id][]=' . $deal->id;
} }
$filterRow = implode('&', $filterRow); $filterRow = implode('&', $filterRow);
?> ?>
<a href="{{ route('contracts', [$filterRow]) }}" <a href="{{ route('contracts', [$filterRow]) }}"
class="icon-link icon-link-hover w-100 hstack gap-2 text-decoration-none"> class="icon-link icon-link-hover w-100 hstack gap-2 text-decoration-none">
<span class="col-8 text-dark">{{ $client->name }}</span> <span class="col-8 text-dark">{{ $client->name }}</span>
@ -144,10 +146,22 @@ class="bi bi-arrow-right ms-auto" viewBox="0 0 16 16">
@endforeach @endforeach
</div> </div>
</div> </div>
@if ($mode == 'full') @if($mode == 'full')
<div class="mt-3"> @if($clientsCount > $count)
{{ $clients->links('vendor.pagination.bootstrap-5', ['scrollTo' => false]) }} <div class="d-flex justify-content-end mt-2">
</div> <ul class="pagination">
<li class="d-none page-item {{ ($currentPage == 1) ? 'disabled' : ''}}" aria-label="« Назад">
<span class="page-link" aria-hidden="true"></span>
</li>
@for($pageNum = 1; $pageNum <= $clientsCount / $count; $pageNum++)
<li wire:click="goToPage({{ $pageNum }})" class="page-item {{ ($pageNum == $currentPage) ? 'active' : '' }}"
aria-current="page" style="cursor:pointer"><span class="page-link">{{ $pageNum }}</span></li>
@endfor
<li class="d-none page-item">
<a class="page-link" rel="next" aria-label="Вперед »"></a>
</li>
</ul>
</div>
@endif
@endif @endif
</div> </div>