HasParteCoinvolta.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Livewire\Traits;
  3. use App\Models\ReportDataPartiCoinvolte;
  4. use App\Models\ReportDataVeicoli;
  5. use App\Models\Vehicle;
  6. trait HasParteCoinvolta{
  7. public $anag_type;
  8. public $parti_coinvolte;
  9. public function changeAnagType($type) {
  10. $this->anag_type = $type;
  11. }
  12. private function recalculateProgressives() {
  13. $parti = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  14. ->orderBy('progressive')
  15. ->get();
  16. foreach ($parti as $index => $parte) {
  17. if ($parte->progressive != $index) {
  18. $parte->progressive = $index;
  19. $parte->save();
  20. ReportDataVeicoli::where('report_id', $this->dataId)
  21. ->where('progressive', $parte->progressive)
  22. ->update(['progressive' => $index]);
  23. }
  24. }
  25. }
  26. public function updateConducentePedoneParteCoinvolta($conducente_o_pedone, $progressive) {
  27. ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  28. ->where('progressive', $progressive)
  29. ->update(['conducente_o_pedone' => $conducente_o_pedone]);
  30. if ($conducente_o_pedone === 2) {
  31. ReportDataVeicoli::updateOrCreate(
  32. [
  33. 'report_id' => $this->dataId,
  34. 'progressive' => $progressive,
  35. ],
  36. [
  37. 'conducente_uguale_proprietario' => false
  38. ]
  39. );
  40. } else {
  41. ReportDataVeicoli::where('report_id', $this->dataId)
  42. ->where('progressive', $progressive)
  43. ->delete();
  44. }
  45. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  46. }
  47. public function updateAnagraficaParteCoinvolta($anagrafica, $progressive) {
  48. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  49. ->where('progressive', $progressive)
  50. ->first();
  51. if ($parte) {
  52. $parte->update(['anagrafica' => $anagrafica]);
  53. if ($parte->conducente_o_pedone == 2) {
  54. ReportDataVeicoli::updateOrCreate(
  55. [
  56. 'report_id' => $this->dataId,
  57. 'progressive' => $progressive,
  58. ],
  59. [
  60. 'conducenti' => $anagrafica
  61. ]
  62. );
  63. }
  64. }
  65. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  66. }
  67. public function removeAnagraficaParteCoinvolta($progressive){
  68. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  69. ->where('progressive', $progressive)
  70. ->first();
  71. if ($parte) {
  72. $parte->update(['anagrafica' => 0]);
  73. if ($parte->conducente_o_pedone == 2) {
  74. ReportDataVeicoli::where('report_id', $this->dataId)
  75. ->where('progressive', $progressive)
  76. ->update([
  77. 'conducenti' => null,
  78. 'conducente_uguale_proprietario' => null
  79. ]);
  80. }
  81. }
  82. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  83. }
  84. public function updateVeicoloParteCoinvolta($veicolo, $progressive){
  85. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  86. ->where('progressive', $progressive)
  87. ->first();
  88. if ($parte && $parte->conducente_o_pedone == 2) {
  89. $parte->update(['veicolo' => $veicolo]);
  90. $vehicleInfo = Vehicle::find($veicolo);
  91. if ($vehicleInfo) {
  92. ReportDataVeicoli::updateOrCreate(
  93. [
  94. 'report_id' => $this->dataId,
  95. 'progressive' => $progressive,
  96. ],
  97. [
  98. 'veicoli' => $veicolo,
  99. ]
  100. );
  101. }
  102. }
  103. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  104. }
  105. public function removeVeicoloParteCoinvolta($progressive) {
  106. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  107. ->where('progressive', $progressive)
  108. ->first();
  109. if ($parte) {
  110. $parte->update(['veicolo' => 0]);
  111. if ($parte->conducente_o_pedone == 2) {
  112. ReportDataVeicoli::where('report_id', $this->dataId)
  113. ->where('progressive', $progressive)
  114. ->update([
  115. 'veicoli' => null,
  116. 'conducenti' => null,
  117. 'conducente_uguale_proprietario' => null
  118. ]);
  119. }
  120. }
  121. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  122. }
  123. public function removeParteCoinvolta($progressive){
  124. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  125. ->where('progressive', $progressive)
  126. ->first();
  127. if ($parte) {
  128. $parte->delete();
  129. ReportDataVeicoli::where('report_id', $this->dataId)
  130. ->where('progressive', $progressive)
  131. ->delete();
  132. $this->recalculateProgressives();
  133. }
  134. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  135. }
  136. public function addParteCoinvolta($type = 'pedone'){
  137. $count = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
  138. $progressive = $count == 0 ? 0 : $count;
  139. ReportDataPartiCoinvolte::create([
  140. 'report_id' => $this->dataId,
  141. 'progressive' => $progressive,
  142. 'progressive_type' => $progressive,
  143. 'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
  144. ]);
  145. if ($type === 'veicolo') {
  146. ReportDataVeicoli::create([
  147. 'report_id' => $this->dataId,
  148. 'progressive' => $progressive
  149. ]);
  150. }
  151. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  152. }
  153. }