netgescon-day0/app/Http/Controllers/SuperAdmin/DiagnosticaFattureElettronicheController.php

155 lines
6.2 KiB
PHP

<?php
namespace App\Http\Controllers\SuperAdmin;
use App\Filament\Pages\Contabilita\FatturaElettronicaScheda;
use App\Http\Controllers\Controller;
use App\Models\FatturaElettronica;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
class DiagnosticaFattureElettronicheController extends Controller
{
public function index(Request $request): View
{
$sampleLimit = 1500;
$maxRowsPerBucket = 200;
$totalFe = FatturaElettronica::query()->count();
$missingPdfPathCount = FatturaElettronica::query()
->where(function ($q) {
$q->whereNull('allegato_pdf_path')->orWhere('allegato_pdf_path', '');
})
->count();
$missingXmlPathCount = FatturaElettronica::query()
->where(function ($q) {
$q->whereNull('xml_path')->orWhere('xml_path', '');
})
->where(function ($q) {
$q->whereNull('xml_content')->orWhere('xml_content', '');
})
->count();
$duplicatePdfPaths = FatturaElettronica::query()
->select('allegato_pdf_path', DB::raw('COUNT(*) as cnt'))
->whereNotNull('allegato_pdf_path')
->where('allegato_pdf_path', '!=', '')
->groupBy('allegato_pdf_path')
->having('cnt', '>', 1)
->orderByDesc('cnt')
->limit(100)
->get();
$duplicatePdfPathSet = [];
foreach ($duplicatePdfPaths as $row) {
$p = (string) ($row->allegato_pdf_path ?? '');
if ($p !== '') {
$duplicatePdfPathSet[$p] = (int) ($row->cnt ?? 0);
}
}
$missingPdfPathRows = FatturaElettronica::query()
->where(function ($q) {
$q->whereNull('allegato_pdf_path')->orWhere('allegato_pdf_path', '');
})
->orderByDesc('id')
->limit($maxRowsPerBucket)
->get();
$duplicatePdfPathRows = empty($duplicatePdfPathSet)
? collect()
: FatturaElettronica::query()
->whereIn('allegato_pdf_path', array_keys($duplicatePdfPathSet))
->orderByDesc('id')
->limit($maxRowsPerBucket)
->get();
$missingPdfOnDiskRows = [];
$pdfExistenceChecked = 0;
$sampleWithPdfPath = FatturaElettronica::query()
->whereNotNull('allegato_pdf_path')
->where('allegato_pdf_path', '!=', '')
->orderByDesc('id')
->limit($sampleLimit)
->get();
foreach ($sampleWithPdfPath as $fe) {
$pdfExistenceChecked++;
$path = is_string($fe->allegato_pdf_path) ? $fe->allegato_pdf_path : '';
if ($path !== '' && ! Storage::disk('local')->exists($path)) {
$missingPdfOnDiskRows[] = $fe;
if (count($missingPdfOnDiskRows) >= $maxRowsPerBucket) {
break;
}
}
}
$problemRows = collect()
->merge($missingPdfPathRows)
->merge($duplicatePdfPathRows)
->merge($missingPdfOnDiskRows)
->unique('id')
->values();
$rows = $problemRows->map(function (FatturaElettronica $fe) use ($duplicatePdfPathSet) {
$pdfPath = is_string($fe->allegato_pdf_path) ? $fe->allegato_pdf_path : '';
$xmlPath = is_string($fe->xml_path) ? $fe->xml_path : '';
$pdfExists = $pdfPath !== '' ? Storage::disk('local')->exists($pdfPath) : false;
$xmlExists = $xmlPath !== '' ? Storage::disk('local')->exists($xmlPath) : false;
$issues = [];
if ($pdfPath === '') {
$issues[] = ['type' => 'danger', 'label' => 'PDF path mancante'];
} elseif (! $pdfExists) {
$issues[] = ['type' => 'danger', 'label' => 'PDF file mancante'];
}
if ($pdfPath !== '' && array_key_exists($pdfPath, $duplicatePdfPathSet)) {
$issues[] = ['type' => 'warning', 'label' => 'PDF path duplicato (' . (int) $duplicatePdfPathSet[$pdfPath] . ')'];
}
$hasXmlContent = is_string($fe->xml_content) && trim($fe->xml_content) !== '';
if ($xmlPath === '' && ! $hasXmlContent) {
$issues[] = ['type' => 'danger', 'label' => 'XML mancante'];
} elseif ($xmlPath !== '' && ! $xmlExists && ! $hasXmlContent) {
$issues[] = ['type' => 'warning', 'label' => 'XML file mancante'];
}
return [
'id' => (int) $fe->id,
'stabile_id' => (int) $fe->stabile_id,
'fornitore' => (string) ($fe->fornitore_denominazione ?: ''),
'data' => optional($fe->data_fattura)->format('Y-m-d'),
'numero' => (string) ($fe->numero_fattura ?: ''),
'totale' => (float) ($fe->totale ?: 0),
'sdi_file' => (string) ($fe->sdi_file ?: ''),
'pdf_path' => $pdfPath,
'xml_path' => $xmlPath,
'pdf_exists' => $pdfExists,
'xml_exists' => $xmlExists,
'issues' => $issues,
'filament_url' => FatturaElettronicaScheda::getUrl(['record' => (int) $fe->id], panel: 'admin-filament'),
'pdf_inline_url' => route('admin.fatture-elettroniche.download-pdf', ['fattura' => (int) $fe->id]) . '?inline=1',
'pdf_download_url' => route('admin.fatture-elettroniche.download-pdf', ['fattura' => (int) $fe->id]),
'xml_download_url' => route('admin.fatture-elettroniche.download-xml', ['fattura' => (int) $fe->id]),
];
})->all();
return view('superadmin.diagnostica_fatture_elettroniche', [
'totalFe' => $totalFe,
'missingPdfPathCount' => $missingPdfPathCount,
'missingXmlPathCount' => $missingXmlPathCount,
'duplicatePdfPathsCount' => count($duplicatePdfPathSet),
'pdfExistenceChecked' => $pdfExistenceChecked,
'missingPdfOnDiskCount' => count($missingPdfOnDiskRows),
'rows' => $rows,
]);
}
}