diff --git a/app/CustomFuncPao.php b/app/CustomFuncPao.php index db21cc0b..e683422b 100644 --- a/app/CustomFuncPao.php +++ b/app/CustomFuncPao.php @@ -2216,3 +2216,30 @@ 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 new file mode 100755 index 00000000..bc09902f --- /dev/null +++ b/app/Http/Controllers/ArticleController.php @@ -0,0 +1,45 @@ +leftJoin('StatoProdotto as sp', function ($join) { + $join->on('Article.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('Article.idArticolo, SUM(Ordine.qta) as totaleVenduto') + ->groupBy('Article.idArticolo') + ->take(10) + ->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; + + } 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 f00f0390..63c8f416 100644 --- a/routes/web.php +++ b/routes/web.php @@ -7,6 +7,7 @@ use App\Authornimaia; use App\Clientegm; use App\Clientegmdest; use App\Gm_product; +use App\Http\Controllers\ArticleController; use App\Http\Controllers\TestPaoController; use App\WpPostMeta; use App\Newproduct; @@ -6401,3 +6402,5 @@ 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']);