query totalevenduti
This commit is contained in:
45
app/Http/Controllers/ArticleController.php
Executable file
45
app/Http/Controllers/ArticleController.php
Executable file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Illuminate\Support\Facades\DB; // Added use statement
|
||||
use App\Article;
|
||||
|
||||
class ArticleController extends AbstractController // Should extend AbstractController
|
||||
{
|
||||
public function exportArticlesSales(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$articoliVenduti = Article::join('Ordine', 'Article.idArticolo', '=', 'Ordine.codArticoloGM')
|
||||
->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user