diff --git a/app/CustomFuncPao.php b/app/CustomFuncPao.php index e683422b..db21cc0b 100644 --- a/app/CustomFuncPao.php +++ b/app/CustomFuncPao.php @@ -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; -} diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index 4e861af0..899be2da 100755 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -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); + } +} } \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 913a75fa..0388ea88 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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']);