156 lines
4.7 KiB
PHP
156 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Contabilita;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Contabilita\Models\PianoConti;
|
|
use App\Support\StabileContext;
|
|
use Filament\Actions\Action;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class ContoMastrino extends Page
|
|
{
|
|
protected static ?string $title = 'Mastrino conto';
|
|
|
|
protected static ?string $slug = 'contabilita/mastrino/conti/{record}';
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected string $view = 'filament.pages.contabilita.conto-mastrino';
|
|
|
|
public PianoConti $conto;
|
|
|
|
/** @var array<int, array{data: string, descrizione: string, dare: float, avere: float, saldo: float, registrazione_id: int}> */
|
|
public array $righe = [];
|
|
|
|
public float $totaleDare = 0.0;
|
|
|
|
public float $totaleAvere = 0.0;
|
|
|
|
public float $saldoFinale = 0.0;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->hasAnyRole(['super-admin', 'admin'])) {
|
|
return true;
|
|
}
|
|
|
|
return $user->can('contabilita.prima-nota');
|
|
}
|
|
|
|
public function mount(int|string $record): void
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$this->conto = PianoConti::query()->findOrFail((int) $record);
|
|
|
|
$stabileId = StabileContext::resolveActiveStabileId($user);
|
|
if (! $stabileId) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! Schema::hasTable('contabilita_movimenti') || ! Schema::hasTable('contabilita_registrazioni')) {
|
|
return;
|
|
}
|
|
|
|
$select = [
|
|
'm.id',
|
|
'm.registrazione_id',
|
|
'm.tipo',
|
|
'm.importo',
|
|
'm.movement_description',
|
|
'r.data_registrazione',
|
|
];
|
|
if (Schema::hasColumn('contabilita_registrazioni', 'description')) {
|
|
$select[] = 'r.description';
|
|
}
|
|
if (Schema::hasColumn('contabilita_registrazioni', 'descrizione')) {
|
|
$select[] = 'r.descrizione';
|
|
}
|
|
|
|
$rows = DB::table('contabilita_movimenti as m')
|
|
->leftJoin('contabilita_registrazioni as r', 'r.id', '=', 'm.registrazione_id')
|
|
->where('m.conto_id', (int) $this->conto->id)
|
|
->select($select)
|
|
->orderBy('r.data_registrazione')
|
|
->orderBy('m.registrazione_id')
|
|
->orderBy('m.id')
|
|
->get();
|
|
|
|
$saldo = 0.0;
|
|
foreach ($rows as $row) {
|
|
$tipo = (string) ($row->tipo ?? '');
|
|
$importo = (float) ($row->importo ?? 0);
|
|
$dare = $tipo === 'dare' ? $importo : 0.0;
|
|
$avere = $tipo === 'avere' ? $importo : 0.0;
|
|
$saldo += $dare - $avere;
|
|
|
|
$this->totaleDare += $dare;
|
|
$this->totaleAvere += $avere;
|
|
|
|
$descr = trim((string) ($row->movement_description ?? ''));
|
|
if ($descr === '') {
|
|
$descr = trim((string) ($row->description ?? ''));
|
|
}
|
|
if ($descr === '' && Schema::hasColumn('contabilita_registrazioni', 'descrizione')) {
|
|
$descr = trim((string) ($row->descrizione ?? ''));
|
|
}
|
|
|
|
$dataRaw = $row->data_registrazione ?? null;
|
|
$data = '—';
|
|
if (is_string($dataRaw) && $dataRaw !== '') {
|
|
try {
|
|
$data = Carbon::parse($dataRaw)->format('d/m/Y');
|
|
} catch (\Throwable) {
|
|
$data = $dataRaw;
|
|
}
|
|
}
|
|
|
|
$this->righe[] = [
|
|
'data' => $data,
|
|
'descrizione' => $descr !== '' ? $descr : '—',
|
|
'dare' => $dare,
|
|
'avere' => $avere,
|
|
'saldo' => $saldo,
|
|
'registrazione_id' => (int) ($row->registrazione_id ?? 0),
|
|
];
|
|
}
|
|
|
|
$this->saldoFinale = $saldo;
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('torna')
|
|
->label('Torna alla prima nota')
|
|
->icon('heroicon-o-arrow-left')
|
|
->url(fn (): string => PrimaNotaArchivio::getUrl(panel: 'admin-filament')),
|
|
];
|
|
}
|
|
|
|
public function getTitle(): string
|
|
{
|
|
$codice = trim((string) ($this->conto->codice ?? ''));
|
|
$desc = trim((string) ($this->conto->descrizione ?? ''));
|
|
$label = $codice !== '' ? $codice : ('Conto #' . $this->conto->id);
|
|
if ($desc !== '') {
|
|
$label .= ' - ' . $desc;
|
|
}
|
|
|
|
return 'Mastrino · ' . $label;
|
|
}
|
|
}
|