From 16cbec01a0f8af47e884795fc3193785af9bb1b6 Mon Sep 17 00:00:00 2001 From: michele Date: Mon, 13 Apr 2026 15:54:47 +0000 Subject: [PATCH] Fix water Blade parse errors and common meter moves --- .../Condomini/LettureServiziArchivio.php | 114 ++++++++++++++++++ .../supporto/ticket-acqua-generale.blade.php | 26 ++-- .../pages/supporto/ticket-acqua.blade.php | 68 +++++++---- 3 files changed, 179 insertions(+), 29 deletions(-) diff --git a/app/Filament/Pages/Condomini/LettureServiziArchivio.php b/app/Filament/Pages/Condomini/LettureServiziArchivio.php index f5ab3d2..d78e5c1 100644 --- a/app/Filament/Pages/Condomini/LettureServiziArchivio.php +++ b/app/Filament/Pages/Condomini/LettureServiziArchivio.php @@ -631,6 +631,88 @@ public function table(Table $table): Table ->send(); }), + Action::make('spostaContatoreComune') + ->label('Sposta a contatore comune') + ->icon('heroicon-o-arrow-path-rounded-square') + ->color('warning') + ->form([ + Select::make('stabile_servizio_id') + ->label('Contatore / servizio comune di destinazione') + ->options(fn(StabileServizioLettura $record) => $this->getCommonWaterServiceOptions((int) $record->stabile_id)) + ->searchable() + ->required(), + Select::make('rilevatore_tipo') + ->label('Tipo rilevatore') + ->options($this->getReaderTypeOptions()), + TextInput::make('rilevatore_nome')->label('Nominativo rilevatore')->maxLength(191), + Textarea::make('motivo')->label('Motivo spostamento')->rows(3)->maxLength(1000), + ]) + ->fillForm(fn(StabileServizioLettura $record): array => [ + 'stabile_servizio_id' => $record->stabile_servizio_id, + 'rilevatore_tipo' => (string) ($record->rilevatore_tipo ?? ''), + 'rilevatore_nome' => (string) ($record->rilevatore_nome ?? ''), + 'motivo' => '', + ]) + ->visible(fn(StabileServizioLettura $record): bool => strtolower(trim((string) ($record->servizio?->tipo ?? ''))) === 'acqua') + ->action(function (StabileServizioLettura $record, array $data): void { + $targetServiceId = (int) ($data['stabile_servizio_id'] ?? 0); + $targetService = StabileServizio::query() + ->where('id', $targetServiceId) + ->where('stabile_id', (int) $record->stabile_id) + ->where('tipo', 'acqua') + ->first(); + + if (! $targetService instanceof StabileServizio) { + Notification::make()->title('Servizio comune non valido')->danger()->send(); + return; + } + + $raw = is_array($record->raw ?? null) ? $record->raw : []; + $movements = is_array($raw['common_meter_movements'] ?? null) ? $raw['common_meter_movements'] : []; + $movements[] = [ + 'from_stabile_servizio_id' => (int) ($record->getOriginal('stabile_servizio_id') ?? 0) ?: null, + 'to_stabile_servizio_id' => (int) $targetService->id, + 'from_unita_immobiliare_id'=> (int) ($record->getOriginal('unita_immobiliare_id') ?? 0) ?: null, + 'to_unita_immobiliare_id' => null, + 'note' => trim((string) ($data['motivo'] ?? '')), + 'changed_by' => Auth::id(), + 'changed_at' => now()->toIso8601String(), + ]; + + $raw['common_meter_movements'] = $movements; + $raw['reader_assignment'] = [ + 'user_id' => null, + 'name' => trim((string) ($data['rilevatore_nome'] ?? '')) ?: (trim((string) ($record->rilevatore_nome ?? '')) ?: null), + 'type' => trim((string) ($data['rilevatore_tipo'] ?? '')) ?: (trim((string) ($record->rilevatore_tipo ?? '')) ?: null), + ]; + $raw['moved_to_common_meter'] = [ + 'service_id' => (int) $targetService->id, + 'service_name' => (string) ($targetService->nome ?? ''), + 'common_asset' => (string) data_get($targetService->meta, 'common_asset_label', ''), + 'counter_scope' => (string) data_get($targetService->meta, 'counter_scope', ''), + ]; + + $record->fill([ + 'stabile_servizio_id' => (int) $targetService->id, + 'unita_immobiliare_id' => null, + 'fornitore_id' => $targetService->fornitore_id, + 'rilevatore_tipo' => trim((string) ($data['rilevatore_tipo'] ?? '')) ?: $record->rilevatore_tipo, + 'rilevatore_nome' => trim((string) ($data['rilevatore_nome'] ?? '')) ?: $record->rilevatore_nome, + 'tipologia_lettura' => 'autolettura_contatore_generale', + 'lettura_precedente_valore' => null, + 'lettura_precedente_foto_path' => null, + 'raw' => $raw, + ]); + $record->save(); + $this->hydrateReadingWithPreviousData($record); + + Notification::make() + ->title('Lettura spostata su contatore comune') + ->body('La lettura รจ ora agganciata al servizio corretto del contatore comune.') + ->success() + ->send(); + }), + Action::make('edit') ->label('Modifica') ->icon('heroicon-o-pencil-square') @@ -977,6 +1059,38 @@ private function getServiziOptionsByTipo(string $tipo): array ->all(); } + /** + * @return array + */ + private function getCommonWaterServiceOptions(int $stabileId): array + { + return StabileServizio::query() + ->where('stabile_id', $stabileId) + ->where('tipo', 'acqua') + ->where('attivo', true) + ->orderBy('nome') + ->orderBy('contatore_matricola') + ->get(['id', 'nome', 'contatore_matricola', 'meta']) + ->mapWithKeys(function (StabileServizio $service): array { + $name = trim((string) ($service->nome ?? '')) ?: ('Servizio #' . (int) $service->id); + $asset = trim((string) data_get($service->meta, 'common_asset_label', '')); + $servedArea = trim((string) data_get($service->meta, 'served_area_label', '')); + $counterScope = trim((string) data_get($service->meta, 'counter_scope', '')); + $matricola = trim((string) ($service->contatore_matricola ?? '')); + + $parts = array_filter([ + $name, + $asset, + $servedArea, + $counterScope !== '' ? 'Contatore ' . $counterScope : null, + $matricola !== '' ? 'Matricola ' . $matricola : null, + ]); + + return [(int) $service->id => implode(' - ', $parts)]; + }) + ->all(); + } + private function scheduleWaterCampaign(array $data): int { $user = Auth::user(); diff --git a/resources/views/filament/pages/supporto/ticket-acqua-generale.blade.php b/resources/views/filament/pages/supporto/ticket-acqua-generale.blade.php index d9653db..cef499e 100644 --- a/resources/views/filament/pages/supporto/ticket-acqua-generale.blade.php +++ b/resources/views/filament/pages/supporto/ticket-acqua-generale.blade.php @@ -316,15 +316,27 @@ $gpsLng = data_get($metadata, 'gps_decimal.lng'); $hasGps = is_numeric($gpsLat) && is_numeric($gpsLng); $device = trim((string) data_get($metadata, 'device', '')); + $detailRows = []; + + if ($hasGps) { + $detailRows[] = [ + 'label' => 'Coordinate', + 'value' => number_format((float) $gpsLat, 5, '.', '') . ', ' . number_format((float) $gpsLng, 5, '.', ''), + ]; + } + + if ($device !== '') { + $detailRows[] = [ + 'label' => 'Dispositivo', + 'value' => $device, + ]; + } @endphp - @if($hasGps || $device !== '') + @if($detailRows !== [])
- @if($hasGps) -
Coordinate: {{ number_format((float) $gpsLat, 5, '.', '') }}, {{ number_format((float) $gpsLng, 5, '.', '') }}
- @endif - @if($device !== '') -
Dispositivo: {{ $device }}
- @endif + @foreach($detailRows as $detail) +
{{ $detail['label'] }}: {{ $detail['value'] }}
+ @endforeach
@endif