60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php
|
|
namespace App\Services\Affitti;
|
|
|
|
use App\Models\IstatIndiceMensile;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class IstatRivalutazioneService
|
|
{
|
|
public function getIndice(int $anno, int $mese, string $serie = 'FOI'): ?float
|
|
{
|
|
if ($anno <= 0 || $mese < 1 || $mese > 12) {
|
|
return null;
|
|
}
|
|
|
|
$row = IstatIndiceMensile::query()
|
|
->where('codice_serie', strtoupper($serie))
|
|
->where('anno', $anno)
|
|
->where('mese', $mese)
|
|
->first(['indice']);
|
|
|
|
if (! $row || ! is_numeric($row->indice)) {
|
|
return null;
|
|
}
|
|
|
|
return (float) $row->indice;
|
|
}
|
|
|
|
public function calcolaAdeguamentoAnnuale(float $canoneBase, Carbon $meseRiferimento, string $serie = 'FOI'): ?array
|
|
{
|
|
if ($canoneBase <= 0) {
|
|
return null;
|
|
}
|
|
|
|
$annoCorrente = (int) $meseRiferimento->format('Y');
|
|
$mese = (int) $meseRiferimento->format('m');
|
|
$annoPrecedente = $annoCorrente - 1;
|
|
|
|
$indiceCorrente = $this->getIndice($annoCorrente, $mese, $serie);
|
|
$indicePrecedente = $this->getIndice($annoPrecedente, $mese, $serie);
|
|
|
|
if ($indiceCorrente === null || $indicePrecedente === null || $indicePrecedente <= 0) {
|
|
return null;
|
|
}
|
|
|
|
$variazionePercentuale = (($indiceCorrente - $indicePrecedente) / $indicePrecedente) * 100;
|
|
$importoAdeguamento = round($canoneBase * ($variazionePercentuale / 100), 2);
|
|
|
|
return [
|
|
'serie' => strtoupper($serie),
|
|
'anno_corrente' => $annoCorrente,
|
|
'anno_precedente' => $annoPrecedente,
|
|
'mese' => $mese,
|
|
'indice_corrente' => $indiceCorrente,
|
|
'indice_precedente' => $indicePrecedente,
|
|
'variazione_percentuale' => round($variazionePercentuale, 4),
|
|
'importo_adeguamento' => $importoAdeguamento,
|
|
];
|
|
}
|
|
}
|