Bladeren bron

refactoring classi

FabioFratini 1 jaar geleden
bovenliggende
commit
4106935f02

+ 7 - 204
app/Http/Livewire/Report.php

@@ -1,33 +1,30 @@
 <?php
-
 namespace App\Http\Livewire;
-
 use App\Http\Livewire\Traits\HasAccertatore;
+use App\Http\Livewire\Traits\HasNote;
 use App\Http\Livewire\Traits\HasParteCoinvolta;
 use App\Http\Livewire\Traits\HasPolizza;
+use App\Http\Livewire\Traits\HasAllegato;
+use App\Http\Livewire\Traits\HasRichieste;
 use Livewire\Component;
-
 use Livewire\WithPagination;
-
 use Livewire\WithFileUploads;
-
 use Barryvdh\DomPDF\Facade\Pdf;
 use App\Models\ReportDataPartiCoinvolte;
 use App\Models\ReportDataVeicoli;
 use App\Models\Vehicle;
-
 use \Illuminate\Support\Facades\DB;
 use \Illuminate\Support\Facades\Log;
-
-
-class Report extends Component
-{
+class Report extends Component{
 
     use WithPagination;
     use WithFileUploads;
     use HasParteCoinvolta;
     use HasPolizza;
     use HasAccertatore;
+    use HasAllegato;
+    use HasRichieste;
+    use HasNote;
     protected $paginationTheme = 'bootstrap';
     public $title = 'Verbali';
     public $loadPatente;
@@ -415,25 +412,6 @@ class Report extends Component
     public $verbale_violazione_numero;
     public $articolo_violato;
     public $cds;
-
-    public $noteText;
-    public $notes;
-
-    public $richiestaId;
-    public $richiestaAnagrafica;
-    public $richiestaData;
-    public $richiestaConsegna;
-    public $richiestaText;
-    public $richieste;
-    public $allegatoId;
-    public $allegatoType;
-    public $allegatoGallery;
-    public $allegatoName;
-    public $allegatoVisible;
-    public $allegatoFiles;
-    public $allegatiFiles;
-    public $allegatiImmagini;
-    public $allegatiDocumenti;
     public $segnalazione_pervenuta_da;
     public $segnaletica_verticale_1;
     public $segnaletica_verticale_2;
@@ -451,20 +429,6 @@ class Report extends Component
     public $loadAnagrafica;
     public $currentInfortunato;
     public $cds_2;
-    public $allegati = [];
-
-    public function updatedAllegati()
-    {
-        foreach ($this->allegati as $allegato)
-        {
-            $name = $allegato->getClientOriginalName();
-            $allegato->storeAs('public', $name);
-            $this->allegatiFiles[] = $name;
-        }
-        $this->emit('attachments', implode("|", $this->allegatiFiles));
-        $this->allegati = [];
-    }
-
     protected $rules = [
         //'name' => 'required'
     ];
@@ -1507,162 +1471,12 @@ class Report extends Component
         }
     }
 
-    public function addNote(){
-        \App\Models\ReportProtocolloNote::create([
-            'report_id' => $this->dataId,
-            'text' => $this->noteText,
-            'created' => date("Y-m-d H:i:s"),
-            'created_by' => 0 // * * *
-        ]);
-        $this->noteText = '';
-        $this->notes = \App\Models\ReportProtocolloNote::where('report_id', $this->dataId)->get();
-    }
-
-    public function removeNote($id) {
-        \App\Models\ReportProtocolloNote::findOrFail($id)->delete();
-        $this->notes = \App\Models\ReportProtocolloNote::where('report_id', $this->dataId)->get();
-    }
-
-    public function saveRichiesta(){
-        $rules = [
-            'richiestaAnagrafica' => 'required|min:1',
-            'richiestaData' => 'required|date',
-            'richiestaConsegna' => 'required|date',
-        ];
-        $messages = [
-            'richiestaData.required' => 'Il campo data richiesta è obbligatorio.',
-            'richiestaData.date' => 'Il campo data richiesta deve essere una data valida.',
-            'richiestaConsegna.required' => 'Il campo data consegna è obbligatorio.',
-            'richiestaConsegna.date' => 'Il campo data consegna deve essere una data valida.',
-        ];
-        $this->validate($rules);
-
-        if ($this->richiestaId > 0){
-            \App\Models\ReportRichiesta::where('id', $this->richiestaId)->update([
-                'anagrafica_id' => $this->richiestaAnagrafica,
-                'data_richiesta' => $this->richiestaData,
-                'consegna_richiesta' => $this->richiestaConsegna,
-                'description' => $this->richiestaText,
-                'state' => 0
-            ]);
-        }else{
-            \App\Models\ReportRichiesta::create([
-                'report_id' => $this->dataId,
-                'anagrafica_id' => $this->richiestaAnagrafica,
-                'data_richiesta' => $this->richiestaData,
-                'consegna_richiesta' => $this->richiestaConsegna,
-                'description' => $this->richiestaText,
-                'state' => 0,
-                'created' => date("Y-m-d H:i:s"),
-                'created_by' => 0 // * * *
-            ]);
-        }
-        $this->richiestaId = 0;
-        $this->richiestaAnagrafica = 0;
-        $this->richiestaData = null;
-        $this->richiestaConsegna = null;
-        $this->richiestaText = '';
-        $this->richieste = \App\Models\ReportRichiesta::where('report_id', $this->dataId)->get();
-    }
-
-    public function editRichiesta($id){
-        $r = \App\Models\ReportRichiesta::where('id', $id)->first();
-
-        if ($r != null){
-            $this->richiestaId = $id;
-            $this->richiestaAnagrafica = $r->anagrafica_id;
-            $this->richiestaData = $r->data_richiesta;
-            $this->richiestaConsegna = $r->consegna_richiesta;
-            $this->richiestaText = $r->description;
-        }
-
-    }
-
-    public function removeRichiesta($id){
-        \App\Models\ReportRichiesta::findOrFail($id)->delete();
-        $this->richieste = \App\Models\ReportRichiesta::where('report_id', $this->dataId)->get();
-    }
-
-    public function addAllegato($type){
-        $this->allegatoId = 0;
-        $this->allegatoType = $type;
-        $this->allegatoGallery = 0;
-        $this->allegatoName = '';
-        $this->allegatoVisible = false;
-        $this->allegatoFiles = '';
-        $this->allegatiFiles = [];
-        $this->emit('attachments', "");
-    }
-
     public function closeAndResetModal(){
         logger('closeAndResetModal called');
         $this->resetVeicoloData();
         $this->emit('close-modal-parte-comune');
 
     }
-
-    public function saveAllegato() {
-        $files = implode("|", $this->allegatiFiles);
-
-        if ($this->allegatoId > 0){
-            \App\Models\ReportAllegatiGallery::where('id', $this->allegatoId)->update([
-                'file_type' => $this->allegatoType,
-                'gallery_type' => $this->allegatoGallery,
-                'name' => $this->allegatoName,
-                'is_visible' => $this->allegatoVisible,
-                'files' => $files,
-                'state' => 0
-            ]);
-        }else {
-            \App\Models\ReportAllegatiGallery::create([
-                'report_id' => $this->dataId,
-                'file_type' => $this->allegatoType,
-                'gallery_type' => $this->allegatoGallery,
-                'name' => $this->allegatoName,
-                'is_visible' => $this->allegatoVisible,
-                'files' => $files,
-                'state' => 0,
-                'created' => date("Y-m-d H:i:s"),
-                'created_by' => 0 // * * *
-            ]);
-        }
-
-        $this->allegatoId = 0;
-        $this->allegatoGallery = 0;
-        $this->allegatoName = '';
-        $this->allegatoVisible = false;
-        $this->allegatoFiles = '';
-        $this->allegatiFiles = [];
-
-        if ($this->allegatoType == 0)
-            $this->allegatiImmagini = \App\Models\ReportAllegatiGallery::where('report_id',  $this->dataId)->where('file_type', 0)->orderBy('name')->get();
-        else
-            $this->allegatiDocumenti = \App\Models\ReportAllegatiGallery::where('report_id',  $this->dataId)->where('file_type', 1)->orderBy('name')->get();
-
-        $this->allegatoType = 0;
-        $this->emit('close-modal');
-    }
-
-    public function editAllegato($id){
-        $this->emit('attachments', "");
-        $a = \App\Models\ReportAllegatiGallery::where('id', $id)->first();
-
-        if ($a != null){
-            $this->allegatoId = $id;
-            $this->allegatoType = $a->file_type;
-            $this->allegatoGallery = $a->gallery_type;
-            $this->allegatoName = $a->name;
-            $this->allegatoFiles = $a->files;
-            $this->allegatiFiles = explode("|", $this->allegatoFiles);
-            $this->emit('attachments', $this->allegatoFiles);
-        }
-    }
-    public function removeAllegato($id){
-        \App\Models\ReportAllegatiGallery::findOrFail($id)->delete();
-        $this->allegatiImmagini = \App\Models\ReportAllegatiGallery::where('report_id',  $this->dataId)->where('file_type', 0)->orderBy('name')->get();
-        $this->allegatiDocumenti = \App\Models\ReportAllegatiGallery::where('report_id',  $this->dataId)->where('file_type', 1)->orderBy('name')->get();
-    }
-
     public function editAnagraficaParte($progressive) {
         $parte_comune = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->first();
         if ($parte_comune != null)
@@ -2649,17 +2463,6 @@ class Report extends Component
         }
         return "";
     }
-
-    public function getAllegatoType($type)
-    {
-        if ($type > 0)
-        {
-            $ret = \App\Models\AllegatiGalleryType::findOrFail($type);
-            return $ret->name;
-        }
-        return "";
-    }
-
     public function getVeicolo($veicolo)
     {
         if ($veicolo > 0)

+ 132 - 0
app/Http/Livewire/Traits/HasAllegato.php

@@ -0,0 +1,132 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+
+use App\Models\AllegatiGalleryType;
+use App\Models\ReportAllegatiGallery;
+use Livewire\WithFileUploads;
+
+trait HasAllegato
+{
+    public $allegatoId = 0;
+    public $allegatoType = 0;
+    public $allegatoGallery = 0;
+    public $allegatoName = '';
+    public $allegatoVisible = false;
+    public $allegatoFiles = '';
+    public $allegatiFiles = [];
+    public $allegatiImmagini;
+    public $allegatiDocumenti;
+    public $allegati = [];
+
+
+    public function getAllegatoType($type)
+    {
+        if ($type > 0) {
+            $ret = AllegatiGalleryType::findOrFail($type);
+            return $ret->name;
+        }
+        return "";
+    }
+
+    public function addAllegato($type)
+    {
+        $this->allegatoId = 0;
+        $this->allegatoType = $type;
+        $this->allegatoGallery = 0;
+        $this->allegatoName = '';
+        $this->allegatoVisible = false;
+        $this->allegatoFiles = '';
+        $this->allegatiFiles = [];
+        $this->emit('attachments', "");
+    }
+
+    public function saveAllegato()
+    {
+        $files = implode("|", $this->allegatiFiles);
+
+        if ($this->allegatoId > 0) {
+            ReportAllegatiGallery::where('id', $this->allegatoId)->update([
+                'file_type' => $this->allegatoType,
+                'gallery_type' => $this->allegatoGallery,
+                'name' => $this->allegatoName,
+                'is_visible' => $this->allegatoVisible,
+                'files' => $files,
+                'state' => 0
+            ]);
+        } else {
+            ReportAllegatiGallery::create([
+                'report_id' => $this->dataId,
+                'file_type' => $this->allegatoType,
+                'gallery_type' => $this->allegatoGallery,
+                'name' => $this->allegatoName,
+                'is_visible' => $this->allegatoVisible,
+                'files' => $files,
+                'state' => 0,
+                'created' => date("Y-m-d H:i:s"),
+                'created_by' => 0
+            ]);
+        }
+
+        $this->resetAllegatoFields();
+        $this->refreshAllegatiCollections();
+        $this->emit('close-modal');
+    }
+
+    private function resetAllegatoFields()
+    {
+        $this->allegatoId = 0;
+        $this->allegatoGallery = 0;
+        $this->allegatoName = '';
+        $this->allegatoVisible = false;
+        $this->allegatoFiles = '';
+        $this->allegatiFiles = [];
+        $this->allegatoType = 0;
+    }
+
+    private function refreshAllegatiCollections()
+    {
+        $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
+            ->where('file_type', 0)
+            ->orderBy('name')
+            ->get();
+
+        $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
+            ->where('file_type', 1)
+            ->orderBy('name')
+            ->get();
+    }
+
+    public function editAllegato($id)
+    {
+        $this->emit('attachments', "");
+        $a = ReportAllegatiGallery::where('id', $id)->first();
+
+        if ($a != null) {
+            $this->allegatoId = $id;
+            $this->allegatoType = $a->file_type;
+            $this->allegatoGallery = $a->gallery_type;
+            $this->allegatoName = $a->name;
+            $this->allegatoFiles = $a->files;
+            $this->allegatiFiles = explode("|", $this->allegatoFiles);
+            $this->emit('attachments', $this->allegatoFiles);
+        }
+    }
+
+    public function removeAllegato($id)
+    {
+        ReportAllegatiGallery::findOrFail($id)->delete();
+        $this->refreshAllegatiCollections();
+    }
+
+    public function updatedAllegati()
+    {
+        foreach ($this->allegati as $allegato) {
+            $name = $allegato->getClientOriginalName();
+            $allegato->storeAs('public', $name);
+            $this->allegatiFiles[] = $name;
+        }
+        $this->emit('attachments', implode("|", $this->allegatiFiles));
+        $this->allegati = [];
+    }
+}

+ 33 - 0
app/Http/Livewire/Traits/HasNote.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+
+use App\Models\ReportProtocolloNote;
+
+trait HasNote{
+    public $noteText;
+    public $notes;
+    public function addNote()
+    {
+        ReportProtocolloNote::create([
+            'report_id' => $this->dataId,
+            'text' => $this->noteText,
+            'created' => now(),
+            'created_by' => auth()->id() ?? 0,
+        ]);
+
+        $this->noteText = '';
+        $this->refreshNotes();
+    }
+
+    public function removeNote($id)
+    {
+        ReportProtocolloNote::findOrFail($id)->delete();
+        $this->refreshNotes();
+    }
+
+    private function refreshNotes()
+    {
+        $this->notes = ReportProtocolloNote::where('report_id', $this->dataId)->get();
+    }
+}

+ 96 - 0
app/Http/Livewire/Traits/HasRichieste.php

@@ -0,0 +1,96 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+use App\Models\ReportRichiesta;
+use Illuminate\Support\Facades\Validator;
+
+trait HasRichieste{
+    public $richiestaId = 0;
+    public $richiestaAnagrafica = 0;
+    public $richiestaData = null;
+    public $richiestaConsegna = null;
+    public $richiestaText = '';
+    public $richieste;
+
+    protected function getRichiestaRules(){
+        return [
+            'richiestaAnagrafica' => 'required|min:1',
+            'richiestaData' => 'required|date',
+            'richiestaConsegna' => 'required|date',
+        ];
+    }
+
+    protected function getRichiestaMessages(){
+        return [
+            'richiestaData.required' => 'Il campo data richiesta è obbligatorio.',
+            'richiestaData.date' => 'Il campo data richiesta deve essere una data valida.',
+            'richiestaConsegna.required' => 'Il campo data consegna è obbligatorio.',
+            'richiestaConsegna.date' => 'Il campo data consegna deve essere una data valida.',
+        ];
+    }
+
+    public function saveRichiesta(){
+        $this->validate($this->getRichiestaRules(), $this->getRichiestaMessages());
+
+        if ($this->richiestaId > 0) {
+            $this->updateExistingRichiesta();
+        } else {
+            $this->createNewRichiesta();
+        }
+
+        $this->resetRichiestaFields();
+        $this->refreshRichieste();
+    }
+
+    protected function updateExistingRichiesta(){
+        ReportRichiesta::where('id', $this->richiestaId)->update([
+            'anagrafica_id' => $this->richiestaAnagrafica,
+            'data_richiesta' => $this->richiestaData,
+            'consegna_richiesta' => $this->richiestaConsegna,
+            'description' => $this->richiestaText,
+            'state' => 0
+        ]);
+    }
+
+    protected function createNewRichiesta(){
+        ReportRichiesta::create([
+            'report_id' => $this->dataId,
+            'anagrafica_id' => $this->richiestaAnagrafica,
+            'data_richiesta' => $this->richiestaData,
+            'consegna_richiesta' => $this->richiestaConsegna,
+            'description' => $this->richiestaText,
+            'state' => 0,
+            'created' => date("Y-m-d H:i:s"),
+            'created_by' => 0
+        ]);
+    }
+
+    protected function resetRichiestaFields(){
+        $this->richiestaId = 0;
+        $this->richiestaAnagrafica = 0;
+        $this->richiestaData = null;
+        $this->richiestaConsegna = null;
+        $this->richiestaText = '';
+    }
+
+    protected function refreshRichieste(){
+        $this->richieste = ReportRichiesta::where('report_id', $this->dataId)->get();
+    }
+
+    public function editRichiesta($id){
+        $richiesta = ReportRichiesta::where('id', $id)->first();
+
+        if ($richiesta) {
+            $this->richiestaId = $id;
+            $this->richiestaAnagrafica = $richiesta->anagrafica_id;
+            $this->richiestaData = $richiesta->data_richiesta;
+            $this->richiestaConsegna = $richiesta->consegna_richiesta;
+            $this->richiestaText = $richiesta->description;
+        }
+    }
+
+    public function removeRichiesta($id){
+        ReportRichiesta::findOrFail($id)->delete();
+        $this->refreshRichieste();
+    }
+}