netgescon-day0/resources/views/admin/stabili/tabs/rate-emesse.blade.php

207 lines
8.8 KiB
PHP

@php
/** @var \App\Models\Stabile $stabile */
$pianoIds = \App\Models\PianoRateizzazione::query()
->where('stabile_id', $stabile->id)
->pluck('id')
->map(fn ($v) => (int) $v)
->all();
$tot = [
'n' => 0,
'addebitato' => 0.0,
'pagato' => 0.0,
'residuo' => 0.0,
];
$perGestione = [];
$perPiano = [];
$extractGestioneLabel = function (string $descrizione): string {
$s = trim($descrizione);
if ($s === '') {
return '—';
}
// Cerca pattern tipo "2024-25" o "2024-2025" (anche con spazi)
if (preg_match('/\b(\d{4})\s*[-\/]\s*(\d{2,4})\b/', $s, $m)) {
$a = $m[1];
$b = $m[2];
if (strlen($b) === 2) {
return $a . '-' . $b;
}
return $a . '-' . substr($b, -2);
}
// Fallback: anno singolo
if (preg_match('/\b(\d{4})\b/', $s, $m)) {
return $m[1];
}
return '—';
};
if (! empty($pianoIds) && \Illuminate\Support\Facades\Schema::hasTable('rate_emesse')) {
$rows = \App\Models\RataEmessaNg::query()
->whereIn('piano_rateizzazione_id', $pianoIds)
->selectRaw('piano_rateizzazione_id, COUNT(*) as n, SUM(importo_addebitato_soggetto) as addebitato, SUM(importo_pagato) as pagato')
->groupBy('piano_rateizzazione_id')
->get();
$piani = \App\Models\PianoRateizzazione::query()
->whereIn('id', $pianoIds)
->get(['id', 'descrizione'])
->keyBy('id');
foreach ($rows as $r) {
$pid = (int) $r->piano_rateizzazione_id;
$addebitato = (float) ($r->addebitato ?? 0);
$pagato = (float) ($r->pagato ?? 0);
$descr = (string) ($piani[$pid]->descrizione ?? ('Piano #' . $pid));
$gestioneLabel = $extractGestioneLabel($descr);
if (!isset($perGestione[$gestioneLabel])) {
$perGestione[$gestioneLabel] = [
'gestione' => $gestioneLabel,
'n' => 0,
'addebitato' => 0.0,
'pagato' => 0.0,
'residuo' => 0.0,
];
}
$perGestione[$gestioneLabel]['n'] += (int) ($r->n ?? 0);
$perGestione[$gestioneLabel]['addebitato'] += $addebitato;
$perGestione[$gestioneLabel]['pagato'] += $pagato;
$perPiano[] = [
'piano_id' => $pid,
'descrizione' => $descr,
'n' => (int) ($r->n ?? 0),
'addebitato' => $addebitato,
'pagato' => $pagato,
'residuo' => $addebitato - $pagato,
];
$tot['n'] += (int) ($r->n ?? 0);
$tot['addebitato'] += $addebitato;
$tot['pagato'] += $pagato;
}
$tot['residuo'] = $tot['addebitato'] - $tot['pagato'];
$perGestione = array_values(array_map(function (array $g) {
$g['residuo'] = (float) $g['addebitato'] - (float) $g['pagato'];
return $g;
}, $perGestione));
usort($perGestione, function ($a, $b) {
$parse = function (string $label): int {
if (preg_match('/^(\d{4})\b/', $label, $m)) {
return (int) $m[1];
}
return -1;
};
return $parse((string) $b['gestione']) <=> $parse((string) $a['gestione']);
});
usort($perPiano, fn ($a, $b) => ($b['addebitato'] <=> $a['addebitato']) ?: ($b['n'] <=> $a['n']));
}
@endphp
<div class="space-y-4">
<div class="rounded-2xl border bg-white p-4">
<div class="flex flex-wrap items-end justify-between gap-3">
<div>
<div class="text-sm font-semibold text-gray-900">Rate emesse</div>
<div class="text-xs text-gray-500">Riepilogo per stabile</div>
</div>
<a class="fi-btn fi-btn-color-gray fi-btn-size-sm" href="{{ \App\Filament\Pages\Contabilita\RateEmesseArchivio::getUrl(panel: 'admin-filament') }}">
<span class="fi-btn-label">Apri archivio</span>
</a>
</div>
<div class="mt-3 grid gap-3 sm:grid-cols-4">
<div class="rounded-lg border bg-gray-50 p-3">
<div class="text-xs text-gray-500">N. rate</div>
<div class="text-lg font-semibold text-gray-900">{{ $tot['n'] }}</div>
</div>
<div class="rounded-lg border bg-gray-50 p-3">
<div class="text-xs text-gray-500">Totale addebitato</div>
<div class="text-lg font-semibold text-gray-900">{{ number_format($tot['addebitato'], 2, ',', '.') }} </div>
</div>
<div class="rounded-lg border bg-gray-50 p-3">
<div class="text-xs text-gray-500">Totale pagato</div>
<div class="text-lg font-semibold text-gray-900">{{ number_format($tot['pagato'], 2, ',', '.') }} </div>
</div>
<div class="rounded-lg border bg-gray-50 p-3">
<div class="text-xs text-gray-500">Residuo</div>
<div class="text-lg font-semibold text-gray-900">{{ number_format($tot['residuo'], 2, ',', '.') }} </div>
</div>
</div>
</div>
<div class="rounded-2xl border bg-white p-4">
<div class="text-sm font-semibold text-gray-900 mb-2">Per gestione (da piano rateizzazione)</div>
@if(empty($perGestione))
<div class="text-sm text-gray-500">Nessun dato per gestione disponibile.</div>
@else
<div class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead>
<tr class="text-gray-500 border-b">
<th class="text-left py-2 pr-4">Gestione</th>
<th class="text-right py-2 pr-4">N.</th>
<th class="text-right py-2 pr-4">Addebitato</th>
<th class="text-right py-2 pr-4">Pagato</th>
<th class="text-right py-2 pr-0">Residuo</th>
</tr>
</thead>
<tbody class="divide-y">
@foreach($perGestione as $a)
<tr>
<td class="py-2 pr-4 font-semibold text-gray-900">{{ $a['gestione'] ?: '—' }}</td>
<td class="py-2 pr-4 text-right text-gray-900">{{ $a['n'] }}</td>
<td class="py-2 pr-4 text-right text-gray-900">{{ number_format($a['addebitato'], 2, ',', '.') }} </td>
<td class="py-2 pr-4 text-right text-gray-900">{{ number_format($a['pagato'], 2, ',', '.') }} </td>
<td class="py-2 pr-0 text-right text-gray-900">{{ number_format($a['residuo'], 2, ',', '.') }} </td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
</div>
<div class="rounded-2xl border bg-white p-4">
<div class="text-sm font-semibold text-gray-900 mb-2">Per piano rateizzazione</div>
@if(empty($perPiano))
<div class="text-sm text-gray-500">Nessuna rata trovata per questo stabile.</div>
@else
<div class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead>
<tr class="text-gray-500 border-b">
<th class="text-left py-2 pr-4">Piano</th>
<th class="text-right py-2 pr-4">N.</th>
<th class="text-right py-2 pr-4">Addebitato</th>
<th class="text-right py-2 pr-4">Pagato</th>
<th class="text-right py-2 pr-0">Residuo</th>
</tr>
</thead>
<tbody class="divide-y">
@foreach($perPiano as $p)
<tr>
<td class="py-2 pr-4 font-semibold text-gray-900">{{ $p['descrizione'] }}</td>
<td class="py-2 pr-4 text-right text-gray-900">{{ $p['n'] }}</td>
<td class="py-2 pr-4 text-right text-gray-900">{{ number_format($p['addebitato'], 2, ',', '.') }} </td>
<td class="py-2 pr-4 text-right text-gray-900">{{ number_format($p['pagato'], 2, ',', '.') }} </td>
<td class="py-2 pr-0 text-right text-gray-900">{{ number_format($p['residuo'], 2, ',', '.') }} </td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
</div>
</div>