2024-11-27 14:27:16 +01:00
< ? php
namespace App\Http\Controllers ;
2024-11-27 14:56:15 +01:00
use Illuminate\Support\Facades\View ;
2024-11-27 14:27:16 +01:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
2024-11-27 14:49:39 +01:00
use Illuminate\Support\Facades\DB ;
2024-11-27 14:50:35 +01:00
use App\Article ;
2024-11-27 14:48:14 +01:00
2024-11-27 14:50:05 +01:00
class ArticleController extends Controller
2024-11-27 14:27:16 +01:00
{
2024-12-07 22:59:14 +01:00
private function queryTest ()
{
2024-12-07 23:29:57 +01:00
ini_set ( " memory_limit " , " 512M " );
2024-12-07 22:59:14 +01:00
try {
2024-12-07 23:10:34 +01:00
$articoli = Article :: join ( DB :: raw ( '(SELECT IdArticolo, MAX(DataOra) AS data FROM T_WEB_Articoli GROUP BY IdArticolo) b' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'b.IdArticolo' )
-> on ( 'T_WEB_Articoli.DataOra' , '=' , 'b.data' );
})
2024-12-07 23:40:10 +01:00
-> leftJoin ( DB :: raw ( '(SELECT CodArticoloGM, SUM(Qta) as totaleVenduti FROM T_WEB_Ordini GROUP BY CodArticoloGM) O' ), 'T_WEB_Articoli.IdArticolo' , '=' , 'O.CodArticoloGM' )
2024-12-07 23:12:59 +01:00
-> leftJoin ( DB :: raw ( '(SELECT g.IdTipologia, g.Descrizione as DescrizioneTipologia FROM T_WEB_Tipologie g JOIN (SELECT IdTipologia, MAX(DataOra) as data1 from T_WEB_Tipologie GROUP BY IdTipologia) h ON g.IdTipologia = h.IdTipologia AND g.DataOra = h.data1 ) i' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdTipologia' , '=' , 'i.IdTipologia' );
})
-> select ( 'T_WEB_Articoli.*' , 'b.data as ultimaDataOra' , DB :: raw ( 'COALESCE(O.totaleVenduti, 0) as totaleVenduti' ))
-> where ( 'DescrizioneTipologia' , 'Libri' )
-> orderBy ( 'totaleVenduti' , 'desc' )
-> get ();
2024-12-07 22:59:14 +01:00
if ( $articoli -> isEmpty ()) {
return response () -> json ([ 'message' => 'Nessun articolo trovato.' ], 404 );
}
} catch ( \Exception $e ) {
// Registrazione dell'errore
return response () -> json ([ 'error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e -> getMessage ()], 500 );
}
return $articoli ;
2024-12-07 20:57:15 +01:00
2024-12-07 23:24:49 +01:00
}
private function queryOrdini ()
{
2024-12-07 23:29:57 +01:00
ini_set ( " memory_limit " , " 512M " );
2024-12-07 23:24:49 +01:00
try {
2024-12-07 23:40:10 +01:00
$ordini = DB :: table ( 'T_WEB_Ordini as O' )
2024-12-08 11:22:45 +01:00
-> leftJoin ( DB :: raw ( '(SELECT IdArticolo, DataOra AS data FROM T_WEB_Articoli) b' ), function ( $join ) {
$join -> on ( 'O.IdArticolo' , '=' , 'b.CodArticoloGM' );
2024-12-07 23:45:53 +01:00
})
2024-12-07 23:49:00 +01:00
-> join ( 'T_WEB_Articoli as A' , 'O.CodArticoloGM' , '=' , 'A.IdArticolo' )
2024-12-07 23:24:49 +01:00
-> select (
2024-12-07 23:28:27 +01:00
DB :: raw ( 'ROW_NUMBER() OVER (ORDER BY O.DataOra DESC) AS progressivo' ),
2024-12-07 23:24:49 +01:00
'O.DataOra' ,
'O.Qta' ,
2024-12-07 23:40:10 +01:00
'A.Titolo' ,
'A.Ean13'
2024-12-07 23:24:49 +01:00
)
-> orderBy ( 'O.DataOra' , 'desc' )
2024-12-07 23:31:23 +01:00
-> take ( 100 )
2024-12-07 23:24:49 +01:00
-> get ();
} catch ( \Exception $e ) {
// Registrazione dell'errore
return response () -> json ([ 'error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e -> getMessage ()], 500 );
}
return $ordini ;
2024-12-07 20:57:15 +01:00
}
2024-11-27 15:00:24 +01:00
private function queryArticlesSales ()
2024-11-27 14:27:16 +01:00
{
try {
2024-12-07 23:29:57 +01:00
ini_set ( " memory_limit " , " 512M " );
2024-12-07 23:31:23 +01:00
2024-12-07 19:43:32 +01:00
$articoliVenduti = Article :: join ( DB :: raw ( '(SELECT IdArticolo, MAX(DataOra) AS data FROM T_WEB_Articoli GROUP BY IdArticolo) b' ), function ( $join ) {
2024-12-07 19:01:37 +01:00
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'b.IdArticolo' )
-> on ( 'T_WEB_Articoli.DataOra' , '=' , 'b.data' );
})
-> 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 ) {
2024-12-07 18:55:29 +01:00
$join -> on ( 'T_WEB_Articoli.IdStatoProdotto' , '=' , 'f.IdStatoProdotto' );
2024-12-07 18:57:33 +01:00
})
2024-12-07 19:01:37 +01:00
-> leftJoin ( DB :: raw ( '(SELECT g.IdTipologia, g.Descrizione as DescrizioneTipologia FROM T_WEB_Tipologie g JOIN (SELECT IdTipologia, MAX(DataOra) as data1 from T_WEB_Tipologie GROUP BY IdTipologia) h ON g.IdTipologia = h.IdTipologia AND g.DataOra = h.data1 ) i' ), function ( $join ) {
2024-12-07 18:52:08 +01:00
$join -> on ( 'T_WEB_Articoli.IdTipologia' , '=' , 'i.IdTipologia' );
2024-12-07 18:57:33 +01:00
})
2024-12-07 19:01:37 +01:00
-> leftJoin ( DB :: raw ( '(SELECT l.IdTipoFormato, l.Descrizione as DescrizioneFormato FROM T_WEB_TipiFormato l JOIN (SELECT IdTipoFormato, MAX(DataOra) as data1 from T_WEB_TipiFormato GROUP BY IdTipoFormato) m ON l.IdTipoFormato = m.IdTipoFormato AND l.DataOra = m.data1 ) n' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdTipoFormato' , '=' , 'n.IdTipoFormato' );
2024-12-07 19:33:13 +01:00
})
2024-12-07 23:40:10 +01:00
-> leftJoin ( DB :: raw ( '(SELECT CodArticoloGM, SUM(Qta) as totaleVenduti FROM T_WEB_Ordini GROUP BY CodArticoloGM) o' ), function ( $join ) {
2024-12-07 19:33:13 +01:00
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'o.CodArticoloGM' );
})
2024-12-07 20:30:37 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticoloGM , SUM ( Qta ) as venduti3mesi , RANK () OVER ( ORDER BY SUM ( Qta ) DESC ) as rank3M
2024-12-07 23:40:10 +01:00
FROM T_WEB_Ordini
2024-12-07 20:33:53 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 3 , GETDATE ())
2024-12-07 20:30:37 +01:00
GROUP BY CodArticoloGM ) p ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'p.CodArticoloGM' );
})
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticoloGM , SUM ( Qta ) as venduti6mesi , RANK () OVER ( ORDER BY SUM ( Qta ) DESC ) as rank6M
2024-12-07 23:40:10 +01:00
FROM T_WEB_Ordini
2024-12-07 20:33:53 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 6 , GETDATE ())
2024-12-07 20:30:37 +01:00
GROUP BY CodArticoloGM ) q ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'q.CodArticoloGM' );
})
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticoloGM , SUM ( Qta ) as venduti1anno , RANK () OVER ( ORDER BY SUM ( Qta ) DESC ) as rank1Y
2024-12-07 23:40:10 +01:00
FROM T_WEB_Ordini
2024-12-07 20:33:53 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 12 , GETDATE ())
2024-12-07 20:30:37 +01:00
GROUP BY CodArticoloGM ) r ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'r.CodArticoloGM' );
})
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticoloGM , MAX ( DataOra ) as ultimoOrdine
2024-12-07 23:40:10 +01:00
FROM T_WEB_Ordini
2024-12-07 20:30:37 +01:00
GROUP BY CodArticoloGM ) s ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 's.CodArticoloGM' );
})
-> select (
'T_WEB_Articoli.*' ,
'f.DescrizioneStatoProdotto' ,
'i.DescrizioneTipologia' ,
'n.DescrizioneFormato' ,
DB :: raw ( 'COALESCE(o.totaleVenduti, 0) as totaleVenduti' ),
DB :: raw ( 'COALESCE(p.venduti3mesi, 0) as venduti3mesi' ),
DB :: raw ( 'COALESCE(p.rank3M, 0) as rank3M' ),
DB :: raw ( 'COALESCE(q.venduti6mesi, 0) as venduti6mesi' ),
DB :: raw ( 'COALESCE(q.rank6M, 0) as rank6M' ),
DB :: raw ( 'COALESCE(r.venduti1anno, 0) as venduti1anno' ),
DB :: raw ( 'COALESCE(r.rank1Y, 0) as rank1Y' ),
DB :: raw ( 's.ultimoOrdine' )
)
-> where ( 'DescrizioneStatoProdotto' , 'In commercio' )
-> where ( 'DescrizioneTipologia' , 'Libri' )
2024-12-07 20:57:15 +01:00
//->orderBy('rank1Y', 'asc')
-> orderBy ( 'totaleVenduti' , 'desc' )
2024-12-07 19:43:32 +01:00
-> get ();
2024-12-07 19:33:54 +01:00
2024-11-27 17:35:32 +01:00
if ( $articoliVenduti -> isEmpty ()) {
return response () -> json ([ 'message' => 'Nessun articolo trovato.' ], 404 );
}
2024-11-27 14:56:15 +01:00
} catch ( \Exception $e ) {
2024-11-27 15:19:45 +01:00
// Registrazione dell'errore
2024-11-27 15:38:18 +01:00
return response () -> json ([ 'error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e -> getMessage ()], 500 );
2024-11-27 14:56:15 +01:00
}
2024-11-27 14:27:16 +01:00
2024-11-27 14:56:15 +01:00
return $articoliVenduti ;
2024-11-27 14:27:16 +01:00
2024-11-27 14:56:15 +01:00
}
2024-11-27 14:27:16 +01:00
2024-11-27 15:01:20 +01:00
public function showArticlesSales ( Request $request )
2024-11-27 14:56:15 +01:00
{
try {
2024-11-27 15:00:24 +01:00
$articoliVenduti = $this -> queryArticlesSales ();
2024-11-27 14:56:15 +01:00
return view ( 'export_articles_sales' , [ 'articoliVenduti' => $articoliVenduti ]);
2024-11-27 15:20:49 +01:00
2024-11-27 14:56:15 +01:00
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
return new Response ( 'Error exporting articles: ' . $e -> getMessage (), 500 );
}
}
2024-12-07 20:57:15 +01:00
public function showtest ( Request $request )
{
try {
$articoliVenduti = $this -> queryTest ();
return view ( 'export_articles_test' , [ 'articoli' => $articoliVenduti ]);
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
return new Response ( 'Error exporting articles: ' . $e -> getMessage (), 500 );
}
}
2024-12-07 23:24:49 +01:00
public function showOrdini ( Request $request )
{
try {
$ordini = $this -> queryOrdini ();
return view ( 'ordini_test' , [ 'ordini' => $ordini ]);
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
return new Response ( 'Error exporting articles: ' . $e -> getMessage (), 500 );
}
}
2024-11-27 14:56:15 +01:00
public function exportArticlesSales ( Request $request ) : Response
{
try {
2024-11-27 15:19:45 +01:00
$articoliVenduti = $this -> queryArticlesSales ();
2024-11-27 14:56:15 +01:00
$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 . '"' );
2024-11-27 15:16:03 +01:00
2024-12-07 17:06:17 +01:00
$csvContent = " IdArticolo,Titolo,DataPubblicazione,Ean13,rank3M,rank6M,rank1Y,TotaleVenduti,TotaleVendutiUltimoMese,TotaleVendutiUltimi6Mesi,TotaleVendutiUltimoAnno,totaleVendutiUltimi2Anni,UltimoOrdine,misure \n " ;
2024-11-27 15:16:03 +01:00
2024-11-27 14:56:15 +01:00
foreach ( $articoliVenduti as $articoloVenduto ) {
2024-12-07 17:06:17 +01:00
$csvContent .= " { $articoloVenduto -> idArticolo } , { $articoloVenduto -> Titolo } , { $articoloVenduto -> DataPubblicazione } , { $articoloVenduto -> rank3M } , { $articoloVenduto -> rank6M } , { $articoloVenduto -> rank1Y } , { $articoloVenduto -> totaleVenduti } , { $articoloVenduto -> totaleVendutiUltimoMese } , { $articoloVenduto -> totaleVendutiUltimi6Mesi } , { $articoloVenduto -> totaleVendutiUltimoAnno } , { $articoloVenduto -> ultimoOrdine } , { $articoloVenduto -> misure } \n " ;
2024-11-27 14:48:14 +01:00
}
2024-11-27 14:27:16 +01:00
2024-11-27 14:56:15 +01:00
$response -> setContent ( $csvContent );
return $response ;
2024-11-27 14:27:16 +01:00
} catch ( \Exception $e ) {
2024-11-27 14:49:39 +01:00
// Potresti considerare di registrare l'errore per debugging
2024-11-27 14:27:16 +01:00
return new Response ( 'Error exporting articles: ' . $e -> getMessage (), 500 );
}
}
2024-11-27 17:07:02 +01:00
public function exportArticlesSalesByJSON ( Request $request ) : Response
2024-11-27 17:35:32 +01:00
{
try {
// Recupera gli articoli venduti
$articoliVenduti = $this -> queryArticlesSales ();
// Mappa i risultati nella struttura JSON desiderata
$result = $articoliVenduti -> map ( function ( $articoloVenduto ) {
return [
2024-11-29 11:18:00 +01:00
'idArticolo' => $articoloVenduto -> idArticolo ,
'title' => $articoloVenduto -> Titolo ,
2024-11-27 17:35:32 +01:00
'DataPubblicazione' => $articoloVenduto -> DataPubblicazione ,
2024-11-27 19:30:13 +01:00
'isbn' => $articoloVenduto -> Ean13 ,
2024-11-29 13:28:23 +01:00
'Pagine' => $articoloVenduto -> Pagine ,
'IdTipoFormato' => $articoloVenduto -> IdTipoFormato ,
2024-12-02 11:23:33 +01:00
'misure' => $articoloVenduto -> misure ,
2024-12-07 17:06:17 +01:00
'totaleVenduti' => $articoloVenduto -> totaleVenduti ,
2024-11-27 17:35:32 +01:00
'rank3M' => $articoloVenduto -> rank3M ,
'rank6M' => $articoloVenduto -> rank6M ,
'rank1Y' => $articoloVenduto -> rank1Y ,
2024-12-07 17:06:17 +01:00
'venditeLastM' => $articoloVenduto -> totaleVendutiUltimoMese ,
'venditeLast6M' => $articoloVenduto -> totaleVendutiUltimi6Mesi ,
'venditeLastY' => $articoloVenduto -> totaleVendutiUltimoAnno ,
'venditeLast2Y' => $articoloVenduto -> totaleVendutiUltimi2Anni ,
2024-11-27 17:35:32 +01:00
'dataUltimoOrdine' => $articoloVenduto -> ultimoOrdine ,
];
});
// Imposta il contenuto della risposta come JSON
$response = new Response ( $result -> toJson (), 200 );
$response -> headers -> set ( 'Content-Type' , 'application/json' );
2024-11-29 13:28:23 +01:00
$response -> headers -> set ( 'Content-Disposition' , 'attachment; filename="ranking_' . date ( 'Y-m-d' ) . '.json"' );
2024-11-27 17:35:32 +01:00
return $response ;
} catch ( \Exception $e ) {
return new Response ( 'Error exporting articles: ' . $e -> getMessage (), 500 );
}
2024-11-27 17:07:02 +01:00
}
2024-11-28 17:30:10 +01:00
2024-12-07 15:28:56 +01:00
public function test ( Request $request )
2024-11-28 17:30:10 +01:00
{
try {
2024-12-07 20:58:25 +01:00
$articoli = $this -> queryTest2 ();
2024-11-28 17:30:10 +01:00
dd ( $articoli );
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
return new Response ( 'Erroe test: ' . $e -> getMessage (), 500 );
}
}
2024-12-07 20:58:25 +01:00
public function queryTest2 ()
2024-11-28 17:30:10 +01:00
{
try {
$duplicati = DB :: table ( 'T_WEB_Articoli' )
-> select ( 'idArticolo' , 'Titolo' , 'DataPubblicazione' , 'Ean13' , DB :: raw ( 'count(*) as total' ))
-> groupBy ( 'idArticolo' , 'Titolo' , 'DataPubblicazione' , 'Ean13' )
-> having ( 'total' , '>' , 0 ) // Filtra duplicati
-> get ();
return $duplicati ;
} catch ( \Exception $e ) {
return new Response ( 'Errore: ' . $e -> getMessage (), 500 );
}
}
2024-11-28 17:36:03 +01:00
2024-11-27 14:27:16 +01:00
}