HasParteCoinvolta.php 6.7 KB

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