FabioFratini 11 месяцев назад
Родитель
Сommit
95851b07ff

+ 116 - 14
app/Http/Livewire/Report.php

@@ -23,6 +23,10 @@ use App\Models\ReportDataVeicoli;
 use App\Models\Vehicle;
 use \Illuminate\Support\Facades\DB;
 use \Illuminate\Support\Facades\Log;
+use setasign\Fpdi\Fpdi;
+use Smalot\PdfParser\Parser;
+use Illuminate\Http\Request;
+
 
 class Report extends Component
 {
@@ -1227,8 +1231,7 @@ class Report extends Component
 
             $targa = $this->vei_targa;
 
-            try
-            {
+            try {
                 Log::info("veicolo con targa: " . $targa);
 
                 $wd = "dettaglioAutoveicoloBase";
@@ -1296,14 +1299,10 @@ class Report extends Component
                     $this->vei_carta_circolazione_rilasciata_il = @$obj->DatiAmministrativiVeicolo[0]->DataEmissioneDocumento;
                     $this->vei_cilindrata = @$obj->DatiTecniciVeicolo->CilindrataInCentimetriCubi;
                     $this->vei_peso_complessivo = @$obj->DatiTecniciVeicolo->TaraInKG;
-                }
-                catch(\SoapFault $fault)
-                {
+                } catch (\SoapFault $fault) {
                     $this->loadTarga = $fault->getMessage();
                 }
-            }
-            catch(\Exception $ex)
-            {
+            } catch (\Exception $ex) {
                 Log::error($ex->getMessage());
             }
 
@@ -1388,16 +1387,12 @@ class Report extends Component
                     '</xmp><br/><br/> Error Message : <br/>',
                     $fault->getMessage();
                 }
-            }
-            catch(\SoapFault $ex)
-            {
+            } catch (\SoapFault $ex) {
                 Log::error($ex->getMessage());
             }
 
             $this->emit('hideLoading', 'btPatente');
-
         }
-
     }
 
     protected function handleMCTCError($response)
@@ -1408,7 +1403,8 @@ class Report extends Component
                 session()->flash('error', 'Errore di validazione numero di caratteri errato');
             } else {
                 session()->flash('error', $response->errore->descrizioneErrore);
-            }            return false;
+            }
+            return false;
         }
         return true;
     }
@@ -1499,4 +1495,110 @@ class Report extends Component
             $this->addError('verificatosi_in_data', 'Il campo Verificatosi in data deve essere nel formato corretto.');
         }
     }
+
+    public function downloadTemplate()
+    {
+        $pdf = new Fpdi();
+        $pdf->AddPage();
+        $pdf->SetFont('Arial', 'B', 16);
+
+        $pdf->Cell(0, 10, 'Verbale Template', 0, 1, 'C');
+        $pdf->Ln(10);
+
+        $pdf->SetFont('Arial', '', 12);
+
+        $fields = [
+            'Localita' => '',
+            'Indirizzo' => '',
+            'Verificatosi in data' => '',
+            'Ora incidente' => '',
+            'Data segnalazione' => '',
+            'Ora segnalazione' => '',
+            'Ora intervento' => '',
+            'Tipo di rilievo' => '',
+            'Localizzazione incidente' => '',
+            'Nomenclatura strada' => '',
+            'Condizioni atmosferiche' => '',
+            'Visibilita' => '',
+            'Pavimentazione strada' => '',
+            'Fondo stradale' => '',
+            'Particolarita strada' => '',
+            'Condizione strada' => ''
+        ];
+
+        foreach ($fields as $label => $value) {
+            $pdf->Cell(80, 10, $label . ':', 0);
+            $pdf->Cell(0, 10, $value, 0);
+            $pdf->Ln(15);
+        }
+
+        $pdf->SetFont('Arial', 'I', 10);
+        $pdf->Ln(10);
+        $pdf->MultiCell(0, 5, 'Istruzioni: Compilare tutti i campi. Per le caselle di controllo, segnare con una X l\'opzione appropriata.');
+
+        $pdf->Output('D', 'verbale_template.pdf');
+    }
+
+
+    public function uploadVerbale(Request $request)
+    {
+        try {
+            $file = $request->file('verbale_file');
+            $parser = new Parser();
+            $pdf = $parser->parseFile($file->getPathname());
+
+            $text = $pdf->getText();
+            $lines = explode("\n", $text);
+            $data = [];
+
+            foreach ($lines as $line) {
+                if (strpos($line, ':') !== false) {
+                    list($key, $value) = explode(':', $line, 2);
+                    $data[trim($key)] = trim($value);
+                }
+            }
+
+            $formData = $this->mapPdfDataToForm($data);
+
+            return response()->json([
+                'success' => true,
+                'data' => $formData
+            ]);
+        } catch (\Exception $e) {
+            return response()->json([
+                'success' => false,
+                'message' => 'Error processing PDF: ' . $e->getMessage()
+            ], 422);
+        }
+    }
+
+    private function mapPdfDataToForm($data)
+    {
+        return [
+            'localita_uno' => $data['Localita'] ?? '',
+            'localita_due' => $data['Indirizzo'] ?? '',
+            'verificatosi_in_data' => $this->formatDate($data['Verificatosi in data'] ?? ''),
+            'verificatosi_in_data_ora' => $this->extractHours($data['Ora incidente'] ?? ''),
+            'verificatosi_in_data_minuti' => $this->extractMinutes($data['Ora incidente'] ?? ''),
+            'segnalazione_data' => $this->formatDate($data['Data segnalazione'] ?? ''),
+            'segnalazione_ora' => $this->extractHours($data['Ora segnalazione'] ?? ''),
+            'segnalazione_minuti' => $this->extractMinutes($data['Ora segnalazione'] ?? ''),
+            'sinistro_ora' => $this->extractHours($data['Ora intervento'] ?? ''),
+            'sinistro_minuti' => $this->extractMinutes($data['Ora intervento'] ?? ''),
+            'rilievi_id' => $data['Tipo di rilievo'] ?? '',
+            'localizzazione_incidente' => $this->mapLocalizzazione($data['Localizzazione incidente'] ?? ''),
+            'nomenclatura_strada' => $data['Nomenclatura strada'] ?? '',
+            'condizioni_atmosferiche' => $data['Condizioni atmosferiche'] ?? '',
+            'visibilita' => $this->mapVisibilita($data['Visibilita'] ?? ''),
+            'pavimentazione' => $data['Pavimentazione strada'] ?? '',
+            'fondo_stradale' => $data['Fondo stradale'] ?? '',
+            'particolarita_strada' => $data['Particolarita strada'] ?? '',
+            'condizione_strada' => $data['Condizione strada'] ?? ''
+        ];
+    }
+
+    public function showUploadForm()
+    {
+        return view('livewire/upload-verbale');
+    }
 }

+ 25 - 0
app/Http/Livewire/UploadVerbale.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Livewire;
+
+use Livewire\Component;
+
+class UploadVerbale extends Component
+{
+    public $showModal = false;
+
+    public function render()
+    {
+        return view('livewire.upload-verbale');
+    }
+
+    public function openModal()
+    {
+        $this->showModal = true;
+    }
+
+    public function closeModal()
+    {
+        $this->showModal = false;
+    }
+}

+ 4 - 1
composer.json

@@ -11,7 +11,10 @@
         "laravel/framework": "^10.10",
         "laravel/sanctum": "^3.2",
         "laravel/tinker": "^2.8",
-        "livewire/livewire": "^2.12"
+        "livewire/livewire": "^2.12",
+        "setasign/fpdf": "^1.8",
+        "setasign/fpdi": "^2.6",
+        "smalot/pdfparser": "^2.11"
     },
     "require-dev": {
         "fakerphp/faker": "^1.9.1",

+ 173 - 4
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "6028c7a30294b8ee4597779e5541615d",
+    "content-hash": "57a877224808b699d940c5610f6b3359",
     "packages": [
         {
             "name": "barryvdh/laravel-dompdf",
@@ -3464,6 +3464,175 @@
             },
             "time": "2021-12-11T13:40:54+00:00"
         },
+        {
+            "name": "setasign/fpdf",
+            "version": "1.8.6",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/Setasign/FPDF.git",
+                "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/Setasign/FPDF/zipball/0838e0ee4925716fcbbc50ad9e1799b5edfae0a0",
+                "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0",
+                "shasum": ""
+            },
+            "require": {
+                "ext-gd": "*",
+                "ext-zlib": "*"
+            },
+            "type": "library",
+            "autoload": {
+                "classmap": [
+                    "fpdf.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Olivier Plathey",
+                    "email": "oliver@fpdf.org",
+                    "homepage": "http://fpdf.org/"
+                }
+            ],
+            "description": "FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.",
+            "homepage": "http://www.fpdf.org",
+            "keywords": [
+                "fpdf",
+                "pdf"
+            ],
+            "support": {
+                "source": "https://github.com/Setasign/FPDF/tree/1.8.6"
+            },
+            "time": "2023-06-26T14:44:25+00:00"
+        },
+        {
+            "name": "setasign/fpdi",
+            "version": "v2.6.3",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/Setasign/FPDI.git",
+                "reference": "67c31f5e50c93c20579ca9e23035d8c540b51941"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/Setasign/FPDI/zipball/67c31f5e50c93c20579ca9e23035d8c540b51941",
+                "reference": "67c31f5e50c93c20579ca9e23035d8c540b51941",
+                "shasum": ""
+            },
+            "require": {
+                "ext-zlib": "*",
+                "php": "^7.1 || ^8.0"
+            },
+            "conflict": {
+                "setasign/tfpdf": "<1.31"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^7",
+                "setasign/fpdf": "~1.8.6",
+                "setasign/tfpdf": "~1.33",
+                "squizlabs/php_codesniffer": "^3.5",
+                "tecnickcom/tcpdf": "^6.2"
+            },
+            "suggest": {
+                "setasign/fpdf": "FPDI will extend this class but as it is also possible to use TCPDF or tFPDF as an alternative. There's no fixed dependency configured."
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "setasign\\Fpdi\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Jan Slabon",
+                    "email": "jan.slabon@setasign.com",
+                    "homepage": "https://www.setasign.com"
+                },
+                {
+                    "name": "Maximilian Kresse",
+                    "email": "maximilian.kresse@setasign.com",
+                    "homepage": "https://www.setasign.com"
+                }
+            ],
+            "description": "FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF documents and use them as templates in FPDF. Because it is also possible to use FPDI with TCPDF, there are no fixed dependencies defined. Please see suggestions for packages which evaluates the dependencies automatically.",
+            "homepage": "https://www.setasign.com/fpdi",
+            "keywords": [
+                "fpdf",
+                "fpdi",
+                "pdf"
+            ],
+            "support": {
+                "issues": "https://github.com/Setasign/FPDI/issues",
+                "source": "https://github.com/Setasign/FPDI/tree/v2.6.3"
+            },
+            "funding": [
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/setasign/fpdi",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2025-02-05T13:22:35+00:00"
+        },
+        {
+            "name": "smalot/pdfparser",
+            "version": "v2.11.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/smalot/pdfparser.git",
+                "reference": "ac8e6678b0940e4b2ccd5caadd3fb18e68093be6"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/smalot/pdfparser/zipball/ac8e6678b0940e4b2ccd5caadd3fb18e68093be6",
+                "reference": "ac8e6678b0940e4b2ccd5caadd3fb18e68093be6",
+                "shasum": ""
+            },
+            "require": {
+                "ext-iconv": "*",
+                "ext-zlib": "*",
+                "php": ">=7.1",
+                "symfony/polyfill-mbstring": "^1.18"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-0": {
+                    "Smalot\\PdfParser\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL-3.0"
+            ],
+            "authors": [
+                {
+                    "name": "Sebastien MALOT",
+                    "email": "sebastien@malot.fr"
+                }
+            ],
+            "description": "Pdf parser library. Can read and extract information from pdf file.",
+            "homepage": "https://www.pdfparser.org",
+            "keywords": [
+                "extract",
+                "parse",
+                "parser",
+                "pdf",
+                "text"
+            ],
+            "support": {
+                "issues": "https://github.com/smalot/pdfparser/issues",
+                "source": "https://github.com/smalot/pdfparser/tree/v2.11.0"
+            },
+            "time": "2024-08-16T06:48:03+00:00"
+        },
         {
             "name": "symfony/console",
             "version": "v6.3.2",
@@ -8416,12 +8585,12 @@
     ],
     "aliases": [],
     "minimum-stability": "stable",
-    "stability-flags": [],
+    "stability-flags": {},
     "prefer-stable": true,
     "prefer-lowest": false,
     "platform": {
         "php": "^8.1"
     },
-    "platform-dev": [],
-    "plugin-api-version": "2.3.0"
+    "platform-dev": {},
+    "plugin-api-version": "2.6.0"
 }

+ 36 - 3
resources/views/layouts/app.blade.php

@@ -27,6 +27,8 @@
   <!-- summernote -->
   <link rel="stylesheet" href="/plugins/summernote/summernote-bs4.min.css">
   <link rel="stylesheet" href="/dist/css/app.css">
+  <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.9.3/min/dropzone.min.css" rel="stylesheet">
+  <meta name="csrf-token" content="{{ csrf_token() }}">
 
 </head>
 <body class="hold-transition sidebar-mini layout-fixed">
@@ -238,7 +240,15 @@
                 </li>
             </ul>
           </li>
-
+          <li class="nav-item">
+            <a href="{{ route('download.template') }}" class="nav-link">
+                <i class="nav-icon fas fa-download"></i>
+                <p>Download Template</p>
+            </a>
+          </li>
+          <li class="nav-item">
+            @livewire('upload-verbale')
+        </li>
 
         </ul>
       </nav>
@@ -323,8 +333,31 @@
 <script src="/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
 <!-- AdminLTE App -->
 <script src="/dist/js/adminlte.js"></script>
-
-
+<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.9.3/min/dropzone.min.js"></script>
+<script>
+Dropzone.options.verbaleDropzone = {
+    url: "{{ route('upload.verbale') }}",
+    acceptedFiles: ".pdf",
+    maxFiles: 1,
+    clickable: true,
+    addRemoveLinks: true,
+    autoProcessQueue: true,
+    headers: {
+        'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
+    },
+    dictDefaultMessage: "Drop files here or click to upload",
+    init: function() {
+        this.on("success", function(file, response) {
+            if (response.success) {
+                window.livewire.emit('modalClosed');
+            }
+        });
+        this.on("error", function(file, message) {
+            console.error(message);
+        });
+    }
+};
+</script>
 @livewireScripts
 
 @stack('scripts')

+ 30 - 0
resources/views/livewire/upload-verbale.blade.php

@@ -0,0 +1,30 @@
+<div>
+    <button class="nav-link text-left w-100" style="background: none; border: none;" wire:click="openModal">
+        <i class="nav-icon fas fa-upload"></i>
+        <p>Upload Verbale</p>
+    </button>
+
+    @if($showModal)
+    <div class="modal fade show d-block" tabindex="-1" role="dialog">
+        <div class="modal-dialog modal-lg">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h5 class="modal-title">Upload Verbale</h5>
+                    <button type="button" class="close" wire:click="closeModal">
+                        <span>&times;</span>
+                    </button>
+                </div>
+                <div class="modal-body">
+                    <form action="{{ route('upload.verbale') }}" class="dropzone" id="verbaleDropzone" enctype="multipart/form-data">                        @csrf
+                        <div class="dz-message">
+                            Drop files here or click to upload<br>
+                            <span class="note">(Only PDF files)</span>
+                        </div>
+                    </form>
+                </div>
+            </div>
+        </div>
+    </div>
+    <div class="modal-backdrop fade show"></div>
+    @endif
+</div>

+ 3 - 0
routes/web.php

@@ -352,3 +352,6 @@ Route::get('/polizze/agenzie', function(Request $request) {
         })
     ];
 });
+
+Route::get('/download-template', [Report::class, 'downloadTemplate'])->name('download.template');
+Route::post('/upload-verbale', [Report::class, 'uploadVerbale'])->name('upload.verbale');