JSON
This commit is contained in:
@@ -91,4 +91,36 @@ class ArticleController extends Controller
|
||||
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function exportArticlesSalesByJSON(Request $request): Response
|
||||
{
|
||||
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,
|
||||
'totaleVenduti' => $articoloVenduto->totaleVenduto,
|
||||
'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"');
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user