This commit is contained in:
paoloar77
2024-11-27 17:35:32 +01:00
parent d0fcb0baa3
commit 135690c2a0
2 changed files with 57 additions and 46 deletions

View File

@@ -46,6 +46,10 @@ class ArticleController extends Controller
// ->take(50) // ->take(50)
->get(); ->get();
if ($articoliVenduti->isEmpty()) {
return response()->json(['message' => 'Nessun articolo trovato.'], 404);
}
} catch (\Exception $e) { } catch (\Exception $e) {
// Registrazione dell'errore // Registrazione dell'errore
return response()->json(['error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e->getMessage()], 500); return response()->json(['error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e->getMessage()], 500);
@@ -97,38 +101,38 @@ class ArticleController extends Controller
} }
public function exportArticlesSalesByJSON(Request $request): Response public function exportArticlesSalesByJSON(Request $request): Response
{ {
try { try {
// Recupera gli articoli venduti // Recupera gli articoli venduti
$articoliVenduti = $this->queryArticlesSales(); $articoliVenduti = $this->queryArticlesSales();
// Mappa i risultati nella struttura JSON desiderata // Mappa i risultati nella struttura JSON desiderata
$result = $articoliVenduti->map(function ($articoloVenduto) { $result = $articoliVenduti->map(function ($articoloVenduto) {
return [ return [
'id' => $articoloVenduto->idArticolo, 'id' => $articoloVenduto->idArticolo,
'title' => $articoloVenduto->Titolo, 'title' => $articoloVenduto->Titolo,
'DataPubblicazione' => $articoloVenduto->DataPubblicazione, 'DataPubblicazione' => $articoloVenduto->DataPubblicazione,
'totaleVenduti' => $articoloVenduto->totaleVenduto, 'totaleVenduti' => $articoloVenduto->totaleVenduto,
'rank3M' => $articoloVenduto->rank3M, 'rank3M' => $articoloVenduto->rank3M,
'rank6M' => $articoloVenduto->rank6M, 'rank6M' => $articoloVenduto->rank6M,
'rank1Y' => $articoloVenduto->rank1Y, 'rank1Y' => $articoloVenduto->rank1Y,
'venditeLastM' => $articoloVenduto->totaleVendutoUltimoMese, 'venditeLastM' => $articoloVenduto->totaleVendutoUltimoMese,
'venditeLast6M' => $articoloVenduto->totaleVendutoUltimi6Mesi, 'venditeLast6M' => $articoloVenduto->totaleVendutoUltimi6Mesi,
'venditeLastY' => $articoloVenduto->totaleVendutoUltimoAnno, 'venditeLastY' => $articoloVenduto->totaleVendutoUltimoAnno,
'venditeLast2Y' => $articoloVenduto->totaleVendutoUltimi2Anni, 'venditeLast2Y' => $articoloVenduto->totaleVendutoUltimi2Anni,
'dataUltimoOrdine' => $articoloVenduto->ultimoOrdine, 'dataUltimoOrdine' => $articoloVenduto->ultimoOrdine,
]; ];
}); });
// Imposta il contenuto della risposta come JSON // Imposta il contenuto della risposta come JSON
$response = new Response($result->toJson(), 200); $response = new Response($result->toJson(), 200);
$response->headers->set('Content-Type', 'application/json'); $response->headers->set('Content-Type', 'application/json');
$response->headers->set('Content-Disposition', 'attachment; filename="articoli_venduti_' . date('Y-m-d') . '.json"'); $response->headers->set('Content-Disposition', 'attachment; filename="articoli_venduti_' . date('Y-m-d') . '.json"');
return $response; return $response;
} catch (\Exception $e) { } catch (\Exception $e) {
return new Response('Error exporting articles: ' . $e->getMessage(), 500); return new Response('Error exporting articles: ' . $e->getMessage(), 500);
}
} }
} }
}

View File

@@ -1,11 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="it"> <html lang="it">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Articoli Venduti</title> <title>Articoli Venduti</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head> </head>
<body> <body>
<div class="container mt-4"> <div class="container mt-4">
<h1 class="mb-4">Articoli Venduti</h1> <h1 class="mb-4">Articoli Venduti</h1>
@@ -28,30 +30,35 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<pre>{{ print_r($articoliVenduti, true) }}</pre>
@if ($articoliVenduti->isEmpty()) @if ($articoliVenduti->isEmpty())
<tr> <tr>
<td colspan="6" class="text-center">Nessun articolo trovato.</td> <td colspan="6" class="text-center">Nessun articolo trovato.</td>
</tr> </tr>
@else @else
@foreach ($articoliVenduti as $articolo) @foreach ($articoliVenduti as $articolo)
<tr> @if (isset($articolo->idArticolo))
<td>{{ $articolo->idArticolo }}</td> <tr>
<td>{{ $articolo->Titolo }}</td> <td>{{ $articolo->idArticolo }}</td>
<td>{{ $articolo->DataPubblicazione }}</td> <td>{{ $articolo->Titolo }}</td>
<td>{{ $articolo->rank3M }}</td> <td>{{ $articolo->DataPubblicazione }}</td>
<td>{{ $articolo->rank6M }}</td> <td>{{ $articolo->rank3M }}</td>
<td>{{ $articolo->rank1Y }}</td> <td>{{ $articolo->rank6M }}</td>
<td>{{ \Carbon\Carbon::parse($articolo->ultimoOrdine)->format('d/m/Y') }}</td> <td>{{ $articolo->rank1Y }}</td>
<td>{{ $articolo->totaleVenduto }}</td> <td>{{ \Carbon\Carbon::parse($articolo->ultimoOrdine)->format('d/m/Y') }}</td>
<td>{{ $articolo->totaleVendutoUltimoMese }}</td> <td>{{ $articolo->totaleVenduto }}</td>
<td>{{ $articolo->totaleVendutoUltimi6Mesi }}</td> <td>{{ $articolo->totaleVendutoUltimoMese }}</td>
<td>{{ $articolo->totaleVendutoUltimoAnno }}</td> <td>{{ $articolo->totaleVendutoUltimi6Mesi }}</td>
<td>{{ $articolo->totaleVendutoUltimi2Anni }}</td> <td>{{ $articolo->totaleVendutoUltimoAnno }}</td>
</tr> <td>{{ $articolo->totaleVendutoUltimi2Anni }}</td>
</tr>
@endif
@endforeach @endforeach
@endif @endif
</tbody> </tbody>
</table> </table>
</div> </div>
</body> </body>
</html> </html>