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)
->get();
if ($articoliVenduti->isEmpty()) {
return response()->json(['message' => 'Nessun articolo trovato.'], 404);
}
} catch (\Exception $e) {
// Registrazione dell'errore
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
{
try {
// Recupera gli articoli venduti
$articoliVenduti = $this->queryArticlesSales();
{
try {
// Recupera gli articoli venduti
$articoliVenduti = $this->queryArticlesSales();
// Mappa i risultati nella struttura JSON desiderata
$result = $articoliVenduti->map(function ($articoloVenduto) {
return [
'id' => $articoloVenduto->idArticolo,
'title' => $articoloVenduto->Titolo,
'DataPubblicazione' => $articoloVenduto->DataPubblicazione,
'totaleVenduti' => $articoloVenduto->totaleVenduto,
'rank3M' => $articoloVenduto->rank3M,
'rank6M' => $articoloVenduto->rank6M,
'rank1Y' => $articoloVenduto->rank1Y,
'venditeLastM' => $articoloVenduto->totaleVendutoUltimoMese,
'venditeLast6M' => $articoloVenduto->totaleVendutoUltimi6Mesi,
'venditeLastY' => $articoloVenduto->totaleVendutoUltimoAnno,
'venditeLast2Y' => $articoloVenduto->totaleVendutoUltimi2Anni,
'dataUltimoOrdine' => $articoloVenduto->ultimoOrdine,
];
});
// Mappa i risultati nella struttura JSON desiderata
$result = $articoliVenduti->map(function ($articoloVenduto) {
return [
'id' => $articoloVenduto->idArticolo,
'title' => $articoloVenduto->Titolo,
'DataPubblicazione' => $articoloVenduto->DataPubblicazione,
'totaleVenduti' => $articoloVenduto->totaleVenduto,
'rank3M' => $articoloVenduto->rank3M,
'rank6M' => $articoloVenduto->rank6M,
'rank1Y' => $articoloVenduto->rank1Y,
'venditeLastM' => $articoloVenduto->totaleVendutoUltimoMese,
'venditeLast6M' => $articoloVenduto->totaleVendutoUltimi6Mesi,
'venditeLastY' => $articoloVenduto->totaleVendutoUltimoAnno,
'venditeLast2Y' => $articoloVenduto->totaleVendutoUltimi2Anni,
'dataUltimoOrdine' => $articoloVenduto->ultimoOrdine,
];
});
// Imposta il contenuto della risposta come JSON
$response = new Response($result->toJson(), 200);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Content-Disposition', 'attachment; filename="articoli_venduti_' . date('Y-m-d') . '.json"');
// Imposta il contenuto della risposta come JSON
$response = new Response($result->toJson(), 200);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Content-Disposition', 'attachment; filename="articoli_venduti_' . date('Y-m-d') . '.json"');
return $response;
return $response;
} catch (\Exception $e) {
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
} catch (\Exception $e) {
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
}
}
}
}

View File

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