@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
Totale addebitato
{{ number_format($tot['addebitato'], 2, ',', '.') }} €
Totale pagato
{{ number_format($tot['pagato'], 2, ',', '.') }} €
Residuo
{{ number_format($tot['residuo'], 2, ',', '.') }} €
Per gestione (da piano rateizzazione)
@if(empty($perGestione))
Nessun dato per gestione disponibile.
@else
| Gestione |
N. |
Addebitato |
Pagato |
Residuo |
@foreach($perGestione as $a)
| {{ $a['gestione'] ?: '—' }} |
{{ $a['n'] }} |
{{ number_format($a['addebitato'], 2, ',', '.') }} € |
{{ number_format($a['pagato'], 2, ',', '.') }} € |
{{ number_format($a['residuo'], 2, ',', '.') }} € |
@endforeach
@endif
Per piano rateizzazione
@if(empty($perPiano))
Nessuna rata trovata per questo stabile.
@else
| Piano |
N. |
Addebitato |
Pagato |
Residuo |
@foreach($perPiano as $p)
| {{ $p['descrizione'] }} |
{{ $p['n'] }} |
{{ number_format($p['addebitato'], 2, ',', '.') }} € |
{{ number_format($p['pagato'], 2, ',', '.') }} € |
{{ number_format($p['residuo'], 2, ',', '.') }} € |
@endforeach
@endif