118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Stampe;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\Preventivo;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class PreventivoLettera extends Page
|
|
{
|
|
protected static ?string $title = 'Preventivo (formato lettera)';
|
|
|
|
protected static ?string $slug = 'stampe/preventivi/{record}/lettera';
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected string $view = 'filament.pages.stampe.preventivo-lettera';
|
|
|
|
public Preventivo $preventivo;
|
|
|
|
public ?Amministratore $amministratore = null;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
public function mount(int|string $record): void
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
if (! $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$this->preventivo = Preventivo::query()
|
|
->with([
|
|
'stabile.amministratore',
|
|
'voci.tabellaMillesimale',
|
|
])
|
|
->findOrFail((int) $record);
|
|
|
|
$allowedStabili = StabileContext::accessibleStabili($user)
|
|
->pluck('id')
|
|
->map(fn($v) => (int) $v)
|
|
->all();
|
|
|
|
if (! in_array((int) $this->preventivo->stabile_id, $allowedStabili, true)) {
|
|
abort(403);
|
|
}
|
|
|
|
$this->amministratore = $this->preventivo->stabile?->amministratore;
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('stampa')
|
|
->label('Stampa')
|
|
->icon('heroicon-o-printer')
|
|
->extraAttributes([
|
|
'onclick' => 'window.print(); return false;',
|
|
]),
|
|
];
|
|
}
|
|
|
|
public function getSubtotaleVociProperty(): float
|
|
{
|
|
return (float) ($this->preventivo->voci->sum('importo_preventivato') ?? 0);
|
|
}
|
|
|
|
public function getCompensoBaseProperty(): float
|
|
{
|
|
return (float) ($this->amministratore?->impostazioni['compensi']['base'] ?? 0);
|
|
}
|
|
|
|
public function getCassaPercentoProperty(): float
|
|
{
|
|
return (float) ($this->amministratore?->impostazioni['compensi']['cassa_percento'] ?? 0);
|
|
}
|
|
|
|
public function getIvaPercentoProperty(): float
|
|
{
|
|
return (float) ($this->amministratore?->impostazioni['compensi']['iva_percento'] ?? 0);
|
|
}
|
|
|
|
public function getTotaleCompensiProperty(): float
|
|
{
|
|
$base = $this->compensoBase;
|
|
if ($base <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
$cassa = $base * ($this->cassaPercento / 100);
|
|
$imponibile = $base + $cassa;
|
|
$iva = $imponibile * ($this->ivaPercento / 100);
|
|
|
|
return $base + $cassa + $iva;
|
|
}
|
|
|
|
public function getTotaleComplessivoProperty(): float
|
|
{
|
|
return $this->subtotaleVoci + $this->totaleCompensi;
|
|
}
|
|
}
|