Browse Source

nuova segnalazione

Roberto Santini 1 month ago
parent
commit
dbf90f6f06

+ 34
- 31
app/Http/Controllers/SegnalazioneController.php View File

@@ -346,7 +346,7 @@ class SegnalazioneController extends Controller implements HasMiddleware
346 346
       'ordine_lavoro_id' => 'nullable|exists:ordine_lavoro,id',
347 347
       'contratto_servizio_id' => 'nullable|exists:contratto_servizio,id',
348 348
       'titolo' => 'required|string|max:255',
349
-      'descrizione' => 'required|string',
349
+      'descrizione' => 'nullable|string',
350 350
       'allegato' => 'nullable|array',
351 351
       'allegato.*' => 'file|max:10240', // max 10MB per file
352 352
       'cc' => 'nullable|string',
@@ -390,38 +390,41 @@ class SegnalazioneController extends Controller implements HasMiddleware
390 390
       }
391 391
       $segnalazione->save();
392 392
 
393
-      // creo la prima annotazione con il testo della segnalazione
394
-      $annotazione = new Annotazione();
395
-      $annotazione->text = $request->descrizione;
396
-      $annotazione->segnalazione_id = $segnalazione->id;
397
-      $annotazione->riservata = false;
398
-      $annotazione->user_id = Auth::user()->id;
399
-      $annotazione->riferimento = null;
400
-      $annotazione->email = null;
401
-      $annotazione->telefono = null;
402
-      $annotazione->save();
403
-
404
-
405
-      // Se è stato caricato un file, crea l'allegato usando il sistema esistente
406
-      if ($request->hasFile('allegato')) {
407
-        // Assicurati che la directory esista
408
-        $directory = storage_path('app/public/allegatiAnnotazioni/' . $segnalazione->id);
409
-        if (!\Illuminate\Support\Facades\File::exists($directory)) {
410
-          \Illuminate\Support\Facades\File::makeDirectory($directory, 0755, true);
411
-        }
393
+      $descrizioneHtml = (string) $request->input('descrizione', '');
394
+      $descrizioneVuota = trim(html_entity_decode(strip_tags(str_replace(' ', ' ', $descrizioneHtml)))) === '';
395
+      $haAllegati = $request->hasFile('allegato');
396
+
397
+      // La descrizione è facoltativa: l'annotazione nasce solo se c'è testo o un allegato.
398
+      if (! $descrizioneVuota || $haAllegati) {
399
+        $annotazione = new Annotazione();
400
+        $annotazione->text = $descrizioneVuota ? '' : $descrizioneHtml;
401
+        $annotazione->segnalazione_id = $segnalazione->id;
402
+        $annotazione->riservata = false;
403
+        $annotazione->user_id = Auth::user()->id;
404
+        $annotazione->riferimento = null;
405
+        $annotazione->email = null;
406
+        $annotazione->telefono = null;
407
+        $annotazione->save();
408
+
409
+        if ($haAllegati) {
410
+          $directory = storage_path('app/public/allegatiAnnotazioni/' . $segnalazione->id);
411
+          if (!\Illuminate\Support\Facades\File::exists($directory)) {
412
+            \Illuminate\Support\Facades\File::makeDirectory($directory, 0755, true);
413
+          }
412 414
 
413
-        foreach (array_values($request->file('allegato')) as $index => $file) {
414
-          if (!$file) {
415
-            continue;
415
+          foreach (array_values($request->file('allegato')) as $index => $file) {
416
+            if (!$file) {
417
+              continue;
418
+            }
419
+            $filename = time() . '_' . $index . '_' . $file->getClientOriginalName();
420
+            $path = $file->storeAs($segnalazione->id, $filename, 'allegatiAnnotazioni');
421
+
422
+            $allegato = new AllegatoAnnotazione();
423
+            $allegato->annotazione()->associate($annotazione);
424
+            $allegato->nome = $file->getClientOriginalName();
425
+            $allegato->path_file = $path;
426
+            $allegato->save();
416 427
           }
417
-          $filename = time() . '_' . $index . '_' . $file->getClientOriginalName();
418
-          $path = $file->storeAs($segnalazione->id, $filename, 'allegatiAnnotazioni');
419
-
420
-          $allegato = new AllegatoAnnotazione();
421
-          $allegato->annotazione()->associate($annotazione);
422
-          $allegato->nome = $file->getClientOriginalName();
423
-          $allegato->path_file = $path;
424
-          $allegato->save();
425 428
         }
426 429
       }
427 430
 

+ 60
- 6
resources/views/segnalazione/admin/nuova.blade.php View File

@@ -73,11 +73,23 @@ $selectedContrattoId = old('contratto_servizio_id');
73 73
                 <label for="azienda_id" class="form-label text-primary fw-semibold">
74 74
                   <i class="bx bx-buildings me-1"></i>Azienda
75 75
                 </label>
76
-                <select class="form-select" id="azienda_id" name="azienda_id">
76
+                <select class="form-select" id="azienda_id" name="azienda_id" data-placeholder="Cerca azienda…">
77 77
                   <option value="">-- Nessuna azienda --</option>
78
-                  @foreach(\App\Models\Azienda::orderBy('nome')->get() as $azienda)
79
-                  <option value="{{ $azienda->id }}" {{ (string) $selectedAziendaId === (string) $azienda->id ? 'selected' : '' }}>
80
-                    {{ $azienda->full_name }}
78
+                  @foreach(\App\Models\Azienda::orderBy('ragione_sociale')->orderBy('nome')->get() as $azienda)
79
+                  @php
80
+                    $ragioneSociale = trim((string) $azienda->ragione_sociale);
81
+                    $nomeCommerciale = trim((string) $azienda->nome);
82
+                    if ($ragioneSociale !== '' && $nomeCommerciale !== '' && strcasecmp($ragioneSociale, $nomeCommerciale) !== 0) {
83
+                      $aziendaLabel = $ragioneSociale.' — '.$nomeCommerciale;
84
+                    } else {
85
+                      $aziendaLabel = $azienda->full_name;
86
+                    }
87
+                  @endphp
88
+                  <option value="{{ $azienda->id }}"
89
+                    data-ragione-sociale="{{ $ragioneSociale }}"
90
+                    data-nome-commerciale="{{ $nomeCommerciale }}"
91
+                    {{ (string) $selectedAziendaId === (string) $azienda->id ? 'selected' : '' }}>
92
+                    {{ $aziendaLabel }}
81 93
                   </option>
82 94
                   @endforeach
83 95
                 </select>
@@ -233,7 +245,7 @@ $selectedContrattoId = old('contratto_servizio_id');
233 245
         <div class="row g-4">
234 246
 
235 247
           <div class="mb-3">
236
-            <label for="descrizione" class="form-label">Descrizione <span class="text-danger">*</span></label>
248
+            <label for="descrizione" class="form-label">Descrizione</label>
237 249
             <input type="hidden" id="descrizione" name="descrizione" value="{{ old('descrizione') }}">
238 250
             <div id="quill_descrizione_toolbar">
239 251
               @include('content.form-elements.quill_toolbar')
@@ -342,7 +354,8 @@ $selectedContrattoId = old('contratto_servizio_id');
342 354
     margin-top: 0;
343 355
     margin-bottom: 0.25rem;
344 356
   }
345
-  #user_id + .select2-container {
357
+  #user_id + .select2-container,
358
+  #azienda_id + .select2-container {
346 359
     width: 100% !important;
347 360
   }
348 361
 </style>
@@ -390,6 +403,47 @@ $selectedContrattoId = old('contratto_servizio_id');
390 403
 
391 404
     const quill_descrizione = initQuillEditor('quill_descrizione', 'quill_descrizione_toolbar', 'descrizione');
392 405
 
406
+    function testoRicerca(value) {
407
+      return String(value || '')
408
+        .toLowerCase()
409
+        .normalize('NFD')
410
+        .replace(/[\u0300-\u036f]/g, '');
411
+    }
412
+
413
+    function matcherAzienda(params, data) {
414
+      if ($.trim(params.term) === '') {
415
+        return data;
416
+      }
417
+      if (!data.id) {
418
+        return null;
419
+      }
420
+      const term = testoRicerca(params.term);
421
+      const el = data.element;
422
+      const campi = [
423
+        data.text,
424
+        el ? el.getAttribute('data-ragione-sociale') : '',
425
+        el ? el.getAttribute('data-nome-commerciale') : ''
426
+      ];
427
+      const trovato = campi.some(function (campo) {
428
+        return testoRicerca(campo).indexOf(term) !== -1;
429
+      });
430
+      return trovato ? data : null;
431
+    }
432
+
433
+    const $aziendaSelect = $('#azienda_id');
434
+    if ($aziendaSelect.length && typeof $aziendaSelect.select2 === 'function') {
435
+      $aziendaSelect.select2({
436
+        placeholder: $aziendaSelect.data('placeholder') || 'Cerca azienda…',
437
+        width: '100%',
438
+        dropdownParent: $aziendaSelect.parent(),
439
+        matcher: matcherAzienda,
440
+        language: {
441
+          noResults: function () { return 'Nessuna azienda trovata'; },
442
+          searching: function () { return 'Ricerca…'; }
443
+        }
444
+      });
445
+    }
446
+
393 447
     const $userSelect = $('#user_id');
394 448
     if ($userSelect.length && typeof $userSelect.select2 === 'function') {
395 449
       $userSelect.select2({

Loading…
Cancel
Save