94 lines
4.1 KiB
PHP
Executable File
94 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Support\Facades\View;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Article;
|
|
|
|
class ArticleController extends Controller
|
|
{
|
|
private function queryArticlesSales()
|
|
{
|
|
try {
|
|
$articoliVenduti = Article::join('T_WEB_Ordini', 'T_WEB_Articoli.idArticolo', '=', 'T_WEB_Ordini.codArticoloGM')
|
|
->leftJoin(
|
|
DB::raw('(SELECT e.IdStatoProdotto, e.Descrizione as DescrizioneStatoProdotto
|
|
FROM T_WEB_StatiProdotto e
|
|
JOIN (SELECT IdStatoProdotto, MAX(DataOra) as data1
|
|
FROM T_WEB_StatiProdotto
|
|
GROUP BY IdStatoProdotto) c
|
|
ON e.IdStatoProdotto = c.IdStatoProdotto AND e.DataOra = c.data1) f'),
|
|
function ($join) {
|
|
$join->on('T_WEB_Articoli.IdStatoProdotto', '=', 'f.IdStatoProdotto');
|
|
}
|
|
)
|
|
->whereIn('f.DescrizioneStatoProdotto', ['In commercio'])
|
|
->selectRaw('
|
|
T_WEB_Articoli.idArticolo,
|
|
T_WEB_Articoli.Titolo,
|
|
SUM(T_WEB_Ordini.qta) as totaleVenduto,
|
|
SUM(CASE WHEN T_WEB_Ordini.DataOra >= DATEADD(month, -1, GETDATE()) THEN T_WEB_Ordini.qta ELSE 0 END) as totaleVendutoUltimoMese,
|
|
SUM(CASE WHEN T_WEB_Ordini.DataOra >= DATEADD(month, -6, GETDATE()) THEN T_WEB_Ordini.qta ELSE 0 END) as totaleVendutoUltimi6Mesi,
|
|
SUM(CASE WHEN T_WEB_Ordini.DataOra >= DATEADD(year, -1, GETDATE()) THEN T_WEB_Ordini.qta ELSE 0 END) as totaleVendutoUltimoAnno,
|
|
SUM(CASE WHEN T_WEB_Ordini.DataOra >= DATEADD(year, -2, GETDATE()) THEN T_WEB_Ordini.qta ELSE 0 END) as totaleVendutoUltimi2Anni,
|
|
MAX(T_WEB_Ordini.DataOra) AS ultimoOrdine
|
|
')
|
|
->groupBy('T_WEB_Articoli.idArticolo', 'T_WEB_Articoli.Titolo')
|
|
->orderBy('totaleVendutoUltimi2Anni', 'desc')
|
|
->take(50)
|
|
->get();
|
|
|
|
} catch (\Exception $e) {
|
|
// Registrazione dell'errore
|
|
return response()->json(['error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e->getMessage()], 500);
|
|
}
|
|
|
|
return $articoliVenduti;
|
|
|
|
}
|
|
|
|
public function showArticlesSales(Request $request)
|
|
{
|
|
try {
|
|
|
|
$articoliVenduti = $this->queryArticlesSales();
|
|
|
|
return view('export_articles_sales', ['articoliVenduti' => $articoliVenduti]);
|
|
|
|
} catch (\Exception $e) {
|
|
// Potresti considerare di registrare l'errore per debugging
|
|
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
|
|
|
|
}
|
|
}
|
|
public function exportArticlesSales(Request $request): Response
|
|
{
|
|
try {
|
|
|
|
$articoliVenduti = $this->queryArticlesSales();
|
|
|
|
$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,Titolo,TotaleVenduto,TotaleVendutoUltimoMese,TotaleVendutoUltimi6Mesi,TotaleVendutoUltimoAnno,totaleVendutoUltimi2Anni,UltimoOrdine\n";
|
|
|
|
foreach ($articoliVenduti as $articoloVenduto) {
|
|
$csvContent .= "{$articoloVenduto->idArticolo},{$articoloVenduto->Titolo},{$articoloVenduto->totaleVenduto},{$articoloVenduto->totaleVendutoUltimoMese},{$articoloVenduto->totaleVendutoUltimi6Mesi},{$articoloVenduto->totaleVendutoUltimoAnno},{$articoloVenduto->UltimoOrdine}\n";
|
|
}
|
|
|
|
$response->setContent($csvContent);
|
|
return $response;
|
|
|
|
} catch (\Exception $e) {
|
|
// Potresti considerare di registrare l'errore per debugging
|
|
return new Response('Error exporting articles: ' . $e->getMessage(), 500);
|
|
}
|
|
}
|
|
} |