JSON
This commit is contained in:
@@ -2216,30 +2216,3 @@ function getvalstr($mystr, $value, $separato = false)
|
||||
|
||||
return $my;
|
||||
}
|
||||
|
||||
function exportArticlesSalesAction(Request $request)
|
||||
{
|
||||
$articoliVenduti = Articolo::join('Ordine', 'Articolo.idArticolo', '=', 'Ordine.codArticoloGM')
|
||||
->leftJoin('StatoProdotto as sp', function ($join) {
|
||||
$join->on('Articolo.idStatoProdotto', '=', 'sp.idStatoProdotto')
|
||||
->where('sp.dataOra', '=', DB::raw('(SELECT MAX(dataOra) FROM StatoProdotto WHERE idStatoProdotto = sp.idStatoProdotto)'));
|
||||
})
|
||||
->whereIn('sp.descrizione', ['In commercio', 'In prevendita', 'Prossima uscita'])
|
||||
->selectRaw('Articolo.idArticolo, SUM(Ordine.qta) as totaleVenduto')
|
||||
->groupBy('Articolo.idArticolo')
|
||||
->get();
|
||||
|
||||
$filename = 'articoli_venduti_' . date('Y-m-d') . '.csv';
|
||||
$response = new Response();
|
||||
$response->headers->set('Content-Type', 'text/csv');
|
||||
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
||||
|
||||
$csvContent = "IdArticolo,TotaleVenduto\n";
|
||||
foreach ($articoliVenduti as $articoloVenduto) {
|
||||
$csvContent .= $articoloVenduto->idArticolo . ',' . $articoloVenduto->totaleVenduto . "\n";
|
||||
}
|
||||
|
||||
$response->setContent($csvContent);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6404,5 +6404,6 @@ Route::get('/handle-article-action-pao/{id}/{action}', function ($id, $action) {
|
||||
Route::get('/product/{sku}', [TestPaoController::class, 'getProductBySku']);
|
||||
|
||||
Route::get('/export-articles-sales', [ArticleController::class, 'exportArticlesSales']);
|
||||
Route::get('/export-articles-sales-json', [ArticleController::class, 'exportArticlesSalesByJSON']);
|
||||
|
||||
Route::get('/view-articles-sales', [ArticleController::class, 'showArticlesSales']);
|
||||
|
||||
Reference in New Issue
Block a user