HasParteCoinvolta.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ->proprietario();
  60. $proprietario = $proprietarioRelation->first();
  61. Log::info('Proprietario: '.$proprietario->id);
  62. Log::info('Anagrafica: '.$anagrafica);
  63. if($anagrafica == $proprietario->id){
  64. $conducente_uguale_proprietario = true;
  65. } else {
  66. $conducente_uguale_proprietario = false;
  67. }
  68. ReportDataVeicoli::updateOrCreate(
  69. [
  70. 'report_id' => $this->dataId,
  71. 'progressive' => $progressive,
  72. ],
  73. [
  74. 'conducenti' => $anagrafica,
  75. 'conducente_uguale_proprietario'=> $conducente_uguale_proprietario,
  76. ]
  77. );
  78. }else{
  79. ReportDataPedoni::updateOrCreate(
  80. [
  81. 'report_id' => $this->dataId,
  82. 'progressive' => $progressive,
  83. ],
  84. [
  85. 'pedoni' => $anagrafica
  86. ]
  87. );
  88. }
  89. }
  90. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  91. }
  92. public function removeAnagraficaParteCoinvolta($progressive){
  93. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  94. ->where('progressive', $progressive)
  95. ->first();
  96. if ($parte) {
  97. $parte->update(['anagrafica' => 0]);
  98. if ($parte->conducente_o_pedone == 2) {
  99. ReportDataVeicoli::where('report_id', $this->dataId)
  100. ->where('progressive', $progressive)
  101. ->update([
  102. 'conducenti' => null,
  103. 'conducente_uguale_proprietario' => null
  104. ]);
  105. }
  106. }
  107. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  108. }
  109. public function updateVeicoloParteCoinvolta($veicolo, $progressive){
  110. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  111. ->where('progressive', $progressive)
  112. ->first();
  113. if ($parte && $parte->conducente_o_pedone == 2) {
  114. $parte->update(['veicolo' => $veicolo]);
  115. $vehicleInfo = Vehicle::find($veicolo);
  116. if ($vehicleInfo) {
  117. ReportDataVeicoli::updateOrCreate(
  118. [
  119. 'report_id' => $this->dataId,
  120. 'progressive' => $progressive,
  121. ],
  122. [
  123. 'veicoli' => $veicolo,
  124. ]
  125. );
  126. }
  127. }
  128. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  129. }
  130. public function removeVeicoloParteCoinvolta($progressive) {
  131. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  132. ->where('progressive', $progressive)
  133. ->first();
  134. if ($parte) {
  135. $parte->update(['veicolo' => 0]);
  136. if ($parte->conducente_o_pedone == 2) {
  137. ReportDataVeicoli::where('report_id', $this->dataId)
  138. ->where('progressive', $progressive)
  139. ->update([
  140. 'veicoli' => null,
  141. 'conducenti' => null,
  142. 'conducente_uguale_proprietario' => null
  143. ]);
  144. }
  145. }
  146. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  147. }
  148. public function removeParteCoinvolta($progressive){
  149. $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
  150. ->where('progressive', $progressive)
  151. ->first();
  152. if ($parte) {
  153. $parte->delete();
  154. ReportDataVeicoli::where('report_id', $this->dataId)
  155. ->where('progressive', $progressive)
  156. ->delete();
  157. $this->recalculateProgressives();
  158. }
  159. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  160. }
  161. public function addParteCoinvolta($type = 'pedone'){
  162. $count = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
  163. $progressive = $count == 0 ? 0 : $count;
  164. ReportDataPartiCoinvolte::create([
  165. 'report_id' => $this->dataId,
  166. 'progressive' => $progressive,
  167. 'progressive_type' => $progressive,
  168. 'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
  169. ]);
  170. if ($type === 'veicolo') {
  171. ReportDataVeicoli::create([
  172. 'report_id' => $this->dataId,
  173. 'progressive' => $progressive
  174. ]);
  175. }else{
  176. ReportDataPedoni::create([
  177. 'report_id' => $this->dataId,
  178. 'progressive' => $progressive
  179. ]);
  180. }
  181. $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
  182. }
  183. }