Explorar o código

refactor classi e polizze v1

FabioFratini hai 1 ano
pai
achega
d45c926785

+ 4 - 9
app/Helpers/DateHelper.php

@@ -1,21 +1,16 @@
 <?php
 namespace App\Helpers;
-
-class DateHelper
-{
-    public static function filterDate($date)
-    {
+class DateHelper{
+    public static function filterDate($date){
         return (bool)strtotime($date);
     }
 
-    public static function calculateAge($birthDate, $currentDate)
-    {
+    public static function calculateAge($birthDate, $currentDate){
         $birth = new \DateTime($birthDate);
         $current = new \DateTime($currentDate);
-        return $current->diff($birth)->y ?: 1; // Return 1 if age is 0
+        return $current->diff($birth)->y ?: 1;
     }
 
-
     public static function formatDate($date) {
         if (!$date) return '';
         return date('d/m/Y', strtotime($date));

+ 1 - 2
app/Helpers/PoliziaUtils.php

@@ -1,8 +1,7 @@
 <?php
 namespace App\Helpers;
 
-class PoliziaUtils
-{
+class PoliziaUtils{
     public static function numberToLetter($number) {
         return chr(65 + $number);
     }

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 100 - 488
app/Http/Livewire/Report.php


+ 104 - 0
app/Http/Livewire/Traits/HasAccertatore.php

@@ -0,0 +1,104 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+
+trait HasAccertatore{
+    public $currentAccertatore = 0;
+    public $currentAccertatoreNumero = 0;
+    public $accertatore_nome = '';
+    public $accertatore_cognome = '';
+    public $accertatore_grado = 0;
+    public $accertatore_username = '';
+    public $accertatore_email = '';
+    public $accertatore_password = '';
+    public $accertatore_password_conferma = '';
+    public $accertatore_1;
+    public $accertatore_2;
+    public $accertatore_3;
+    public $accertatore_4;
+    public $accertatore_5;
+
+    public function accertatoreSave(){
+        if ($this->currentAccertatore > 0) {
+            User::whereId($this->currentAccertatore)->update([
+                'firstname' => $this->accertatore_nome,
+                'lastname' => $this->accertatore_cognome,
+                'username' => $this->accertatore_username,
+                'email' => $this->accertatore_email,
+                'password' => $this->accertatore_password,
+            ]);
+        } else {
+            $accertatore = User::create([
+                'firstname' => $this->accertatore_nome,
+                'lastname' => $this->accertatore_cognome,
+                'username' => $this->accertatore_username,
+                'email' => $this->accertatore_email,
+                'password' => bcrypt($this->accertatore_password),
+            ]);
+
+            switch ($this->currentAccertatoreNumero) {
+                case 1:
+                    $this->accertatore_1 = $accertatore->id;
+                    break;
+                case 2:
+                    $this->accertatore_2 = $accertatore->id;
+                    break;
+                case 3:
+                    $this->accertatore_3 = $accertatore->id;
+                    break;
+                case 4:
+                    $this->accertatore_4 = $accertatore->id;
+                    break;
+                case 5:
+                    $this->accertatore_5 = $accertatore->id;
+                    break;
+            }
+        }
+
+        $this->resetAccertatore();
+        $this->emit('close-modal');
+    }
+
+    public function resetAccertatore() {
+        $this->currentAccertatore = 0;
+        $this->currentAccertatoreNumero = 0;
+        $this->accertatore_nome = '';
+        $this->accertatore_cognome = '';
+        $this->accertatore_grado = 0;
+        $this->accertatore_username = '';
+        $this->accertatore_email = '';
+        $this->accertatore_password = '';
+        $this->accertatore_password_conferma = '';
+    }
+
+    public function getAccertatore($accertatore){
+        if ($accertatore > 0) {
+            $ret = DB::table('fcf_users')->where('id', $accertatore)->first();
+            return @$ret->lastname . " " . @$ret->firstname;
+        }
+        return "";
+    }
+
+    public function editAccertatore($utente){
+        $this->resetAccertatore();
+        $acc = User::where('id', $utente)->first();
+
+        if ($acc != null) {
+            $this->currentAccertatore = $utente;
+            $this->accertatore_nome = $acc->firstname;
+            $this->accertatore_cognome = $acc->lastname;
+            $this->accertatore_username = $acc->username;
+            $this->accertatore_email = $acc->email;
+            $this->accertatore_password = '';
+            $this->accertatore_password_conferma = '';
+        }
+    }
+
+    public function addAccertatore($numero){
+        $this->resetAccertatore();
+        $this->currentAccertatoreNumero = $numero;  // Fixed typo in original code
+    }
+}

+ 185 - 0
app/Http/Livewire/Traits/HasParteCoinvolta.php

@@ -0,0 +1,185 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+
+use App\Models\ReportDataPartiCoinvolte;
+use App\Models\ReportDataVeicoli;
+use App\Models\Vehicle;
+
+trait HasParteCoinvolta{
+    public $anag_type;
+    public $parti_coinvolte;
+
+    public function changeAnagType($type) {
+        $this->anag_type = $type;
+    }
+
+    private function recalculateProgressives() {
+        $parti = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->orderBy('progressive')
+            ->get();
+
+        foreach ($parti as $index => $parte) {
+            if ($parte->progressive != $index) {
+                $parte->progressive = $index;
+                $parte->save();
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $parte->progressive)
+                    ->update(['progressive' => $index]);
+            }
+        }
+    }
+
+    public function updateConducentePedoneParteCoinvolta($conducente_o_pedone, $progressive) {
+        ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->update(['conducente_o_pedone' => $conducente_o_pedone]);
+
+        if ($conducente_o_pedone === 2) {
+            ReportDataVeicoli::updateOrCreate(
+                [
+                    'report_id' => $this->dataId,
+                    'progressive' => $progressive,
+                ],
+                [
+                    'conducente_uguale_proprietario' => false
+                ]
+            );
+        } else {
+            ReportDataVeicoli::where('report_id', $this->dataId)
+                ->where('progressive', $progressive)
+                ->delete();
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function updateAnagraficaParteCoinvolta($anagrafica, $progressive) {
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['anagrafica' => $anagrafica]);
+
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::updateOrCreate(
+                    [
+                        'report_id' => $this->dataId,
+                        'progressive' => $progressive,
+                    ],
+                    [
+                        'conducenti' => $anagrafica
+                    ]
+                );
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function removeAnagraficaParteCoinvolta($progressive){
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['anagrafica' => 0]);
+
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $progressive)
+                    ->update([
+                        'conducenti' => null,
+                        'conducente_uguale_proprietario' => null
+                    ]);
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function updateVeicoloParteCoinvolta($veicolo, $progressive){
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte && $parte->conducente_o_pedone == 2) {
+            $parte->update(['veicolo' => $veicolo]);
+
+            $vehicleInfo = Vehicle::find($veicolo);
+            if ($vehicleInfo) {
+                ReportDataVeicoli::updateOrCreate(
+                    [
+                        'report_id' => $this->dataId,
+                        'progressive' => $progressive,
+                    ],
+                    [
+                        'veicoli' => $veicolo,
+                    ]
+                );
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function removeVeicoloParteCoinvolta($progressive) {
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['veicolo' => 0]);
+
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $progressive)
+                    ->update([
+                        'veicoli' => null,
+                        'conducenti' => null,
+                        'conducente_uguale_proprietario' => null
+                    ]);
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function removeParteCoinvolta($progressive){
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->delete();
+            ReportDataVeicoli::where('report_id', $this->dataId)
+                ->where('progressive', $progressive)
+                ->delete();
+            $this->recalculateProgressives();
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function addParteCoinvolta($type = 'pedone'){
+        $count = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
+        $progressive = $count == 0 ? 0 : $count;
+
+        ReportDataPartiCoinvolte::create([
+            'report_id' => $this->dataId,
+            'progressive' => $progressive,
+            'progressive_type' => $progressive,
+            'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
+        ]);
+
+        if ($type === 'veicolo') {
+            ReportDataVeicoli::create([
+                'report_id' => $this->dataId,
+                'progressive' => $progressive
+            ]);
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+}

+ 146 - 0
app/Http/Livewire/Traits/HasPolizza.php

@@ -0,0 +1,146 @@
+<?php
+
+namespace App\Http\Livewire\Traits;
+
+use App\Models\Polizza;
+use Carbon\Carbon;
+
+trait HasPolizza{
+    public $currentPolizza;
+    public $data_polizze;
+    public $compagnia_id;
+    public $agenzia;
+    public $polizza_num;
+    public $valida_dal;
+    public $valida_al;
+    public $anagrafica_id;
+    public $modalType;
+
+    protected function getPolizzaRules(){
+        return [
+            'compagnia_id' => 'required',
+            'polizza_num' => 'required',
+            'valida_dal' => 'required',
+            'valida_al' => 'required',
+            'anagrafica_id' => 'required'
+        ];
+    }
+
+    public function getPolizza($polizza){
+        if ($polizza > 0) {
+            $ret = Polizza::with('compagnia')->with('anagrafica')->findOrFail($polizza);
+            return $ret->agenzia . " " .
+                   ($ret->compagnia ? $ret->compagnia->name : '') . " " .
+                   ($ret->anagrafica ? ($ret->anagrafica->lastname . " " . $ret->anagrafica->firstname) : '');
+        }
+        return "";
+    }
+
+    public function addPolizza(){
+        $this->resetPolizza();
+        $this->currentPolizza = 99;
+        $this->emit('load-polizza-modal');
+    }
+
+    public function removePolizza(){
+        $this->data_polizze = 0;
+        $this->emit('add-default-value', $this->data_polizze, '', 'data_polizze');
+    }
+
+    public function editPolizza(){
+        $this->currentPolizza = $this->data_polizze;
+        $this->editPolizzaData($this->data_polizze);
+        $this->emit('load-polizza-modal');
+    }
+
+    public function savePolizza(){
+        $this->validate($this->getPolizzaRules());
+
+        try {
+            if ($this->currentPolizza) {
+                $polizza = Polizza::find($this->currentPolizza);
+            } else {
+                $polizza = new Polizza();
+            }
+
+            $polizza->compagnia_id = $this->compagnia_id;
+            $polizza->agenzia = $this->agenzia;
+            $polizza->polizza_num = $this->polizza_num;
+            $polizza->valida_dal = Carbon::createFromFormat('d/m/Y', $this->valida_dal)->format('Y-m-d');
+            $polizza->valida_al = Carbon::createFromFormat('d/m/Y', $this->valida_al)->format('Y-m-d');
+            $polizza->anagrafica_id = $this->anagrafica_id;
+            $polizza->save();
+
+            $this->emit('polizzaSaved', [
+                'id' => $polizza->id,
+                'text' => $this->getPolizza($polizza->id)
+            ]);
+            $this->emit('closePolizzaModal');
+            $this->dispatchBrowserEvent('notify', ['message' => 'Polizza salvata con successo']);
+        } catch (\Exception $e) {
+            $this->dispatchBrowserEvent('notify', [
+                'type' => 'error',
+                'message' => 'Errore durante il salvataggio della polizza: ' . $e->getMessage()
+            ]);
+        }
+    }
+
+    private function resetPolizza(){
+        $this->compagnia_id = null;
+        $this->agenzia = null;
+        $this->polizza_num = null;
+        $this->valida_dal = null;
+        $this->valida_al = null;
+        $this->anagrafica_id = null;
+    }
+
+    public function cleanPolizzaData(){
+        $this->resetPolizza();
+        $this->currentPolizza = null;
+    }
+
+    private function editPolizzaData($id){
+        if ($id) {
+            $polizza = Polizza::with(['anagrafica', 'compagnia'])->find($id);
+            if ($polizza) {
+                $this->compagnia_id = $polizza->compagnia_id;
+                $this->agenzia = $polizza->agenzia;
+                $this->polizza_num = $polizza->polizza_num;
+                $this->valida_dal = $polizza->valida_dal;
+                $this->valida_al = $polizza->valida_al;
+                $this->anagrafica_id = $polizza->anagrafica_id;
+
+                $this->emit('updatePolizzaSelects', [
+                    'anagrafica' => [
+                        'id' => $polizza->anagrafica_id,
+                        'text' => $this->getAnagrafica($polizza->anagrafica_id)
+                    ],
+                    'compagnia' => [
+                        'id' => $polizza->compagnia_id,
+                        'text' => $polizza->compagnia ? $polizza->compagnia->name : ''
+                    ],
+                    'agenzia' => $polizza->agenzia
+                ]);
+            }
+        }
+    }
+
+    public function addAnagraficaPolizza(){
+        $this->resetAnagrafica();
+        $this->currentAnagrafica = 99;
+        $this->modalType = 'polizza';
+        $this->emit('load-anagrafica-modal');
+    }
+
+    public function removeAnagraficaPolizza(){
+        $this->anagrafica_id = 0;
+        $this->emit('add-default-value', $this->anagrafica_id, '', 'anagrafica_id');
+    }
+
+    public function editAnagraficaPolizza(){
+        $this->currentAnagrafica = $this->anagrafica_id;
+        $this->modalType = 'polizza';
+        $this->editAnagrafica($this->anagrafica_id);
+        $this->emit('load-anagrafica-modal');
+    }
+}

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 365 - 195
resources/views/livewire/report.blade.php


+ 37 - 0
routes/web.php

@@ -315,3 +315,40 @@ Route::get('/countries', function() {
         'results' => $countries
     ]);
 });
+
+Route::get('/compagnie', function(){
+    if (isset($_GET["q"]))
+        $compagnie = \App\Models\Compagnia::where('name', 'like', '%' . $_GET["q"] . '%')->orderBy('name')->get();
+    else
+        $compagnie = \App\Models\Compagnia::orderBy('name')->get();
+
+    $data = array();
+    foreach($compagnie as $c)
+    {
+        $data[] = array("id" => $c->id, "text" => $c->name);
+    }
+    return array("results" => $data);
+});
+
+Route::get('/polizze/agenzie', function(Request $request) {
+    $query = \App\Models\Polizza::query();
+
+    if ($request->has('q')) {
+        $query->where('agenzia', 'like', '%' . $request->q . '%');
+    }
+
+    // Get unique agenzie
+    $agenzie = $query->distinct()
+        ->whereNotNull('agenzia')
+        ->where('agenzia', '!=', '')
+        ->pluck('agenzia');
+
+    return [
+        'results' => $agenzie->map(function($agenzia) {
+            return [
+                'id' => $agenzia,
+                'text' => $agenzia
+            ];
+        })
+    ];
+});

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio