From 93f2463aac2ca57d19c76feb9e729f9f082c0b78 Mon Sep 17 00:00:00 2001
From: Jonathan Foucher <jfoucher@gmail.com>
Date: Mon, 29 Aug 2022 13:23:12 +0200
Subject: [PATCH] Can print pdf. Fixes
 https://app.asana.com/0/1202404842967175/1202869349405283

---
 app/Http/Controllers/PdfController.php        | 65 ++++++++++---------
 resources/js/components/Evaluations/List.vue  | 12 ++--
 .../Evaluations/Single/View/index.vue         |  8 ++-
 routes/web.php                                |  2 +-
 4 files changed, 48 insertions(+), 39 deletions(-)

diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php
index d94f58e2..efba771e 100644
--- a/app/Http/Controllers/PdfController.php
+++ b/app/Http/Controllers/PdfController.php
@@ -34,15 +34,7 @@ public function ActionPlanPdf()
             ->orderBy('updated_at', 'DESC')
             ->first();
 
-        if (!$evaluation) {
-            abort(404);
-        }
-
-        $collectionAction = collect($evaluation->measureLevels)->filter(function (MeasureLevel $ml) {
-            return null !== $ml->expected_level && $ml->actual_level < 3 && $ml->actual_level !== $ml->expected_level;
-        });
-        $actionSorted = $collectionAction->sortBy('end_date');
-        $actionSorted = $actionSorted->values()->all();
+        $actionSorted = $this->getActionSorted($evaluation);
 
         $data = [
             'organization' => $organization,
@@ -152,35 +144,36 @@ public function PolitiqueSecuritePdf()
         return Storage::download('politique_securite/' . $filename, 'Politique de sécurité - ' . date('Y-m-d') . '.pdf');
     }
 
-    public function DossierCyberSecuritePdf(GraphDataRepository $repository)
+    public function DossierCyberSecuritePdf(GraphDataRepository $repository, $organization_id, $evaluation_id = null)
     {
-        $organization_id = request()->route()->parameters['organization_id'];
-
         $authUser = Auth::user();
         $isAdmin = $authUser && (User::ROLE_ADMIN === $authUser->role);
 
         $organization = Organization::with('referent')->where('id', $organization_id)
             ->first();
 
-        $evaluation = Evaluation::where('organization_id', $organization_id)
-            ->where('status', Evaluation::STATUS_DONE)
-            ->where('reference', env('REFERENTIEL_VERSION'))
-            ->with('dangerLevels')
-            ->with('measureLevels')
-            ->with('measureLevels.measure')
-            ->with('organization')
-            ->orderBy('updated_at', 'DESC')
-            ->first();
-
-        if (!$evaluation) {
-            abort(404);
+        if ($evaluation_id) {
+            $evaluation = Evaluation::where('organization_id', $organization_id)
+                ->where('id', $evaluation_id)
+//                ->where('status', Evaluation::STATUS_DONE)
+                ->with('dangerLevels')
+                ->with('measureLevels')
+                ->with('measureLevels.measure')
+                ->with('organization')
+                ->first();
+        } else {
+            $evaluation = Evaluation::where('organization_id', $organization_id)
+                ->where('status', Evaluation::STATUS_DONE)
+                ->where('reference', env('REFERENTIEL_VERSION'))
+                ->with('dangerLevels')
+                ->with('measureLevels')
+                ->with('measureLevels.measure')
+                ->with('organization')
+                ->orderBy('updated_at', 'DESC')
+                ->first();
         }
 
-        $collectionAction = collect($evaluation->measureLevels)->filter(function (MeasureLevel $ml) {
-            return null !== $ml->expected_level && $ml->actual_level < 3 && $ml->actual_level !== $ml->expected_level;
-        });
-        $actionSorted = $collectionAction->sortBy('end_date');
-        $actionSorted = $actionSorted->values()->all();
+        $actionSorted = $this->getActionSorted($evaluation);
 
         $dangerLevels = DangerLevel::all();
 
@@ -229,4 +222,18 @@ public function DossierCyberSecuritePdf(GraphDataRepository $repository)
 
         return Storage::download('dossier_cybersecurite/' . $filename, 'Dossier cybersécurité - ' . date('Y-m-d') . '.pdf');
     }
+
+    public function getActionSorted(Evaluation|null $evaluation): array
+    {
+        if (!$evaluation) {
+            abort(404);
+        }
+
+        $collectionAction = collect($evaluation->measureLevels)->filter(function (MeasureLevel $ml) {
+            return null !== $ml->expected_level && $ml->actual_level < 3 && $ml->actual_level !== $ml->expected_level;
+        });
+        $actionSorted = $collectionAction->sortBy('end_date');
+
+        return $actionSorted->values()->all();
+    }
 }
diff --git a/resources/js/components/Evaluations/List.vue b/resources/js/components/Evaluations/List.vue
index b454c098..28d8f192 100644
--- a/resources/js/components/Evaluations/List.vue
+++ b/resources/js/components/Evaluations/List.vue
@@ -137,13 +137,14 @@
                         ><i class="fas fa-pen" /> Modifier</button>
                     </span>
 
-                    <button
-                        v-else
+                    <a
+                        v-else-if="props.row.status === 2"
                         class="btn btn-light btn-xs"
-                        @click.prevent="downloadPdf(props.row)"
+                        :href="`/pdf/dossierCyberSecurite/${props.row.organization_id}/${props.row.id}`"
+                        target="_blank"
                     >
                         <i class="fas fa-clipboard-list" /> Rapport
-                    </button>
+                    </a>
                     <button
                         v-if="!isUser()"
                         class="btn btn-light btn-xs"
@@ -396,9 +397,6 @@ export default {
             if (confirm(`Souhaitez-vous supprimer l'évaluation du ${item.updated_at.format('DD/MM/YYYY')} ?`)) {
                 this.$store.dispatch('deleteEvaluation', item)
             }
-        },
-        downloadPdf(item) {
-            alert('Fonctionnalité en attente');
         }
     }
 }
diff --git a/resources/js/components/Evaluations/Single/View/index.vue b/resources/js/components/Evaluations/Single/View/index.vue
index 29b7dd03..1404e3dc 100644
--- a/resources/js/components/Evaluations/Single/View/index.vue
+++ b/resources/js/components/Evaluations/Single/View/index.vue
@@ -3,9 +3,13 @@
         <button class="btn btn-outline-primary" @click.prevent="$router.push('/evaluations')">
             <i class="fas fa-arrow-left"></i> Retour à la liste des évaluations
         </button>
-        <button class="btn btn-outline-primary" @click.prevent="alert('Pas encore implémenté')">
+        <a
+            class="btn btn-outline-primary"
+            :href="`/pdf/dossierCyberSecurite/${evaluation.organization_id}/${evaluation.id}`"
+            target="_blank"
+        >
             <i class="fas fa-print"></i> Imprimer l'évaluation
-        </button>
+        </a>
         <button
             v-if="user && user.data && user.data.role <= $store.state.user.roles.ROLE_MANAGER"
             class="btn btn-outline-primary"
diff --git a/routes/web.php b/routes/web.php
index e88a88c2..409fba56 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -31,7 +31,7 @@
 Route::middleware('auth:sanctum')->prefix('/pdf')->group(function () {
     Route::get('/planAction/{organization_id}', [PdfController::class, 'ActionPlanPdf'])->name('pdf.plan_action');
     Route::get('/politiqueSecurite/{organization_id}', [PdfController::class, 'PolitiqueSecuritePdf'])->name('pdf.politique_securite');
-    Route::get('/dossierCyberSecurite/{organization_id}', [PdfController::class, 'DossierCyberSecuritePdf'])->name('pdf.dossier_cyber_securite');
+    Route::get('/dossierCyberSecurite/{organization_id}/{evaluation_id?}', [PdfController::class, 'DossierCyberSecuritePdf'])->name('pdf.dossier_cyber_securite');
 });
 
 Route::middleware('auth:sanctum')->get('/{any}', function () {
-- 
GitLab