HasParteCoinvolta.php 7.5 KB

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