| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Http\Livewire\Traits;
- use App\Models\ReportDataPartiCoinvolte;
- use App\Models\ReportDataPedoni;
- use App\Models\ReportDataVeicoli;
- use App\Models\Vehicle;
- use Illuminate\Support\Facades\Log;
- 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) {
- $proprietarioRelation = ReportDataVeicoli::where('progressive', $progressive)
- ->where('report_id', $this->dataId)
- ->first();
- if ($proprietarioRelation) {
- $proprietario = $proprietarioRelation->proprietario()->first();
- if ($proprietario) {
- $conducente_uguale_proprietario = ($anagrafica == $proprietario->id);
- } else {
- $conducente_uguale_proprietario = false;
- }
- } else {
- $conducente_uguale_proprietario = false;
- }
- ReportDataVeicoli::updateOrCreate(
- [
- 'report_id' => $this->dataId,
- 'progressive' => $progressive,
- ],
- [
- 'conducenti' => $anagrafica,
- 'conducente_uguale_proprietario'=> $conducente_uguale_proprietario,
- ]
- );
- }else{
- ReportDataPedoni::updateOrCreate(
- [
- 'report_id' => $this->dataId,
- 'progressive' => $progressive,
- ],
- [
- 'pedoni' => $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
- ]);
- }else{
- ReportDataPedoni::create([
- 'report_id' => $this->dataId,
- 'progressive' => $progressive
- ]);
- }
- $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
- }
- }
|