From ab7b804d084a6568351eeedbb5e57543f47ab591 Mon Sep 17 00:00:00 2001 From: michele Date: Sat, 4 Apr 2026 13:47:15 +0000 Subject: [PATCH] fix: separa lavagna chiamate e filtra interni --- .../Pages/Strumenti/PostItGestione.php | 104 ++++++++++----- .../strumenti/post-it-gestione.blade.php | 123 ++++++++++-------- 2 files changed, 139 insertions(+), 88 deletions(-) diff --git a/app/Filament/Pages/Strumenti/PostItGestione.php b/app/Filament/Pages/Strumenti/PostItGestione.php index 7f78e6d..ba22bcc 100644 --- a/app/Filament/Pages/Strumenti/PostItGestione.php +++ b/app/Filament/Pages/Strumenti/PostItGestione.php @@ -68,8 +68,8 @@ public function mount(): void $focus = (int) request()->query('focus_post_it', 0); $this->focusPostItId = $focus > 0 ? $focus : null; - if (request()->query('tab') === 'storico') { - $this->activeTab = 'storico'; + if (in_array((string) request()->query('tab'), ['storico', 'lavagna'], true)) { + $this->activeTab = (string) request()->query('tab'); } } @@ -232,45 +232,67 @@ public function getRecentiProperty() $query->orderByRaw('CASE WHEN id = ? THEN 0 ELSE 1 END', [$this->focusPostItId]); } - return $query->limit(50)->get(); + return $query->limit(80)->get(); } catch (QueryException) { return collect(); } } + public function getRecentiVisibiliProperty() + { + return $this->recenti + ->filter(fn(ChiamataPostIt $postIt): bool => ! $this->isInternalPostIt($postIt)) + ->values(); + } + public function getRecentiRaggruppatiProperty() { $groups = []; - foreach ($this->recenti as $postIt) { + foreach ($this->recentiVisibili as $postIt) { if ($this->isInternalPostIt($postIt)) { continue; } $groupKey = $this->buildGroupedPostItKey($postIt); - $lastIndex = count($groups) - 1; - - if ($lastIndex >= 0 && $groups[$lastIndex]['key'] === $groupKey) { - $groups[$lastIndex]['items'][] = $postIt; - $groups[$lastIndex]['count']++; - $groups[$lastIndex]['last_at'] = optional($postIt->chiamata_il)->format('d/m/Y H:i') ?: '-'; + if (isset($groups[$groupKey])) { + $groups[$groupKey]['items'][] = $postIt; + $groups[$groupKey]['count']++; + $groups[$groupKey]['last_at'] = optional($postIt->chiamata_il)->format('d/m/Y H:i') ?: '-'; + $groups[$groupKey]['latest'] = $this->resolveLatestGroupedPostIt($groups[$groupKey]['items']); + $groups[$groupKey]['caller_label'] = $this->resolveGroupedCallerLabel($groups[$groupKey]['items']); continue; } - $groups[] = [ + $groups[$groupKey] = [ 'key' => $groupKey, 'items' => [$postIt], 'count' => 1, 'latest' => $postIt, 'caller_label' => $postIt->nome_chiamante ?: 'Chiamante non identificato', 'phone' => (string) ($postIt->telefono ?? ''), - 'route_label' => $this->resolvePostItRouteLabel($postIt), 'first_at' => optional($postIt->chiamata_il)->format('d/m/Y H:i') ?: '-', 'last_at' => optional($postIt->chiamata_il)->format('d/m/Y H:i') ?: '-', ]; } - return collect($groups); + return collect(array_values($groups)) + ->map(function (array $group): array { + usort($group['items'], function (ChiamataPostIt $left, ChiamataPostIt $right): int { + return ($right->chiamata_il?->getTimestamp() ?? 0) <=> ($left->chiamata_il?->getTimestamp() ?? 0); + }); + + $group['latest'] = $this->resolveLatestGroupedPostIt($group['items']); + $oldest = collect($group['items']) + ->sortBy(fn(ChiamataPostIt $item) => $item->chiamata_il?->getTimestamp() ?? 0) + ->first(); + $group['first_at'] = optional($oldest?->chiamata_il)->format('d/m/Y H:i') ?: '-'; + $group['last_at'] = optional($group['latest']->chiamata_il)->format('d/m/Y H:i') ?: '-'; + + return $group; + }) + ->sortByDesc(fn(array $group) => $group['latest']->chiamata_il?->getTimestamp() ?? 0) + ->values(); } public function getChiamateTecnicheProperty() @@ -563,28 +585,12 @@ private function buildTicketDescriptionFromPostIt(int $postItId, ChiamataPostIt private function buildGroupedPostItKey(ChiamataPostIt $postIt): string { - $phone = preg_replace('/\D+/', '', (string) ($postIt->telefono ?? '')) ?: ''; - $caller = mb_strtolower(trim((string) ($postIt->nome_chiamante ?? ''))); - $route = mb_strtolower($this->resolvePostItRouteLabel($postIt)); - - return implode('|', [$phone, $caller, $route, (string) $postIt->direzione]); - } - - private function resolvePostItRouteLabel(ChiamataPostIt $postIt): string - { - $message = $this->resolveMessageFromPostIt($postIt); - if ($message instanceof CommunicationMessage) { - $line = trim((string) ($message->target_extension ?: data_get($message->metadata, 'smdr.co', ''))); - if ($line !== '') { - return $line; - } + $phone = $this->normalizeGroupedPhone((string) ($postIt->telefono ?? '')); + if ($phone !== '') { + return 'phone:' . $phone; } - if (preg_match('/linea\s+([^\n]+)/i', (string) ($postIt->oggetto ?? ''), $matches) === 1) { - return trim((string) $matches[1]); - } - - return '-'; + return 'caller:' . mb_strtolower(trim((string) ($postIt->nome_chiamante ?? 'sconosciuto'))); } private function isInternalPostIt(ChiamataPostIt $postIt): bool @@ -614,6 +620,38 @@ private function resolveMessageFromPostIt(ChiamataPostIt $postIt): ?Communicatio return $message; } + private function normalizeGroupedPhone(string $phone): string + { + return preg_replace('/\D+/', '', $phone) ?: ''; + } + + /** + * @param array $items + */ + private function resolveLatestGroupedPostIt(array $items): ChiamataPostIt + { + usort($items, function (ChiamataPostIt $left, ChiamataPostIt $right): int { + return ($right->chiamata_il?->getTimestamp() ?? 0) <=> ($left->chiamata_il?->getTimestamp() ?? 0); + }); + + return $items[0]; + } + + /** + * @param array $items + */ + private function resolveGroupedCallerLabel(array $items): string + { + foreach ($items as $item) { + $label = trim((string) ($item->nome_chiamante ?? '')); + if ($label !== '') { + return $label; + } + } + + return 'Chiamante non identificato'; + } + public function richiediClickToCallDaMessaggio(int $messageId): void { $user = Auth::user(); diff --git a/resources/views/filament/pages/strumenti/post-it-gestione.blade.php b/resources/views/filament/pages/strumenti/post-it-gestione.blade.php index 8d9f20a..4979b05 100644 --- a/resources/views/filament/pages/strumenti/post-it-gestione.blade.php +++ b/resources/views/filament/pages/strumenti/post-it-gestione.blade.php @@ -15,6 +15,9 @@ + @@ -33,62 +36,8 @@

Storico chiamate recenti

-
-
-
-
Box chiamate raggruppate
-
Le comunicazioni interne non compaiono qui. Le chiamate consecutive dello stesso numero restano nello stesso box finche non interviene un altro chiamante.
-
-
- -
- @forelse($this->recentiRaggruppati as $gruppo) - @php($box = $gruppo['latest']) -
-
-
-
{{ $gruppo['caller_label'] }}
-
- {{ $gruppo['phone'] !== '' ? $gruppo['phone'] : 'Numero non disponibile' }} - @if($gruppo['route_label'] !== '-') - · verso {{ $gruppo['route_label'] }} - @endif -
-
- {{ $gruppo['count'] }} tentativi -
- -
{{ $gruppo['last_at'] }}{{ $gruppo['count'] > 1 ? ' · gruppo consecutivo' : '' }}
-
{{ $box->oggetto ?: 'Senza oggetto' }}
- -
- {{ $box->stato }} - @if($box->assegnazione_tipo) - {{ ucfirst(str_replace('_', ' ', (string) $box->assegnazione_tipo)) }} - @endif - @if($box->assegnatoAUser) - {{ $box->assegnatoAUser->name }} - @endif - @if($box->assegnatoAFornitore) - {{ $box->assegnatoAFornitore->ragione_sociale ?: trim(($box->assegnatoAFornitore->nome ?? '') . ' ' . ($box->assegnatoAFornitore->cognome ?? '')) }} - @endif -
- -
- Apri Post-it - @if($box->ticket_id) - Apri ticket - @endif -
-
- @empty -
Nessuna chiamata esterna raggruppabile disponibile.
- @endforelse -
-
-
- @forelse($this->recenti as $riga) + @forelse($this->recentiVisibili as $riga)
(int) ($this->focusPostItId ?? 0) === (int) $riga->id, @@ -159,6 +108,70 @@ @endforelse
+ @elseif($activeTab === 'lavagna') +
+
+

Lavagna chiamate raggruppate

+

Qui vedi i Post-it raggruppati per numero chiamante. Le chiamate interne restano nel database ma non vengono visualizzate.

+
+ +
+ @forelse($this->recentiRaggruppati as $gruppo) + @php($box = $gruppo['latest']) +
+
+
+
{{ $box->oggetto ?: 'Post-it chiamate' }}
+
+ {{ $gruppo['caller_label'] }} + @if($gruppo['phone'] !== '') + - {{ $gruppo['phone'] }} + @endif +
+ @php($rubricaUrlGrouped = $this->getRubricaUrlByPhone((string) $gruppo['phone'])) + @if($rubricaUrlGrouped) + Apri scheda rubrica + @endif +
+ {{ $gruppo['count'] }} chiamate +
+ +
+ Aperto dal {{ $gruppo['first_at'] }} al {{ $gruppo['last_at'] }} +
+ +
+ @foreach($gruppo['items'] as $item) +
+
+
+
{{ $item->oggetto ?: 'Senza oggetto' }}
+
{{ optional($item->chiamata_il)->format('d/m/Y H:i') }}
+
+ {{ $item->stato }} +
+ + @if($item->stabile) +
{{ $item->stabile->denominazione ?: ('Stabile #' . $item->stabile->id) }}
+ @endif + +
{{ $item->nota }}
+
+ @endforeach +
+ +
+ Apri Post-it + @if($box->ticket_id) + Apri ticket + @endif +
+
+ @empty +
Nessuna chiamata esterna disponibile per la lavagna.
+ @endforelse +
+
@elseif(in_array($activeTab, ['smdr', 'csta'], true))
@php($isCstaTab = $activeTab === 'csta')