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-11-27 15:00:24 +01:00
private function queryArticlesSales ()
2024-11-27 14:27:16 +01:00
{
try {
2024-11-27 14:37:05 +01:00
$articoliVenduti = Article :: join ( 'T_WEB_Ordini' , 'T_WEB_Articoli.idArticolo' , '=' , 'T_WEB_Ordini.codArticoloGM' )
2024-11-27 14:56:15 +01:00
-> leftJoin (
DB :: raw ( ' ( SELECT e . IdStatoProdotto , e . Descrizione as DescrizioneStatoProdotto
2024-11-28 17:29:09 +01:00
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 ' ),
2024-11-27 14:56:15 +01:00
function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdStatoProdotto' , '=' , 'f.IdStatoProdotto' );
}
)
2024-11-27 15:16:03 +01:00
-> whereIn ( 'f.DescrizioneStatoProdotto' , [ 'In commercio' ])
2024-11-27 15:10:05 +01:00
-> selectRaw ( '
2024-11-29 09:04:14 +01:00
T_WEB_Articoli . idArticolo ,
2024-11-29 10:37:26 +01:00
T_WEB_Articoli . Titolo ,
2024-11-29 09:00:19 +01:00
T_WEB_Articoli . DataPubblicazione as DataPubblicazione ,
2024-11-27 19:00:28 +01:00
T_WEB_Articoli . Ean13 ,
2024-11-28 17:35:47 +01:00
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 ,
RANK () OVER ( ORDER BY SUM ( CASE WHEN T_WEB_Ordini . DataOra >= DATEADD ( month , - 3 , GETDATE ()) THEN T_WEB_Ordini . qta ELSE 0 END ) DESC ) as rank3M ,
RANK () OVER ( ORDER BY SUM ( CASE WHEN T_WEB_Ordini . DataOra >= DATEADD ( month , - 6 , GETDATE ()) THEN T_WEB_Ordini . qta ELSE 0 END ) DESC ) as rank6M ,
RANK () OVER ( ORDER BY SUM ( CASE WHEN T_WEB_Ordini . DataOra >= DATEADD ( year , - 1 , GETDATE ()) THEN T_WEB_Ordini . qta ELSE 0 END ) DESC ) as rank1Y
2024-11-27 15:10:05 +01:00
' )
2024-11-28 20:50:02 +01:00
// ->groupBy('T_WEB_Articoli.idArticolo', 'T_WEB_Articoli.Titolo', 'T_WEB_Articoli.Ean13')
2024-11-29 10:37:26 +01:00
-> groupBy ( 'T_WEB_Articoli.idArticolo' , 'T_WEB_Articoli.Titolo' , 'T_WEB_Articoli.Ean13' , 'T_WEB_Articoli.DataPubblicazione' ) // Raggruppa solo per il codice Ean13
2024-11-28 17:29:09 +01:00
-> orderBy ( 'rank1Y' , 'asc' )
// ->take(50)
2024-11-27 14:41:18 +01:00
-> get ();
2024-11-27 14:27:16 +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 );
}
}
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-11-27 19:00:28 +01:00
$csvContent = " IdArticolo,Titolo,DataPubblicazione,Ean13,rank3M,rank6M,rank1Y,TotaleVenduto,TotaleVendutoUltimoMese,TotaleVendutoUltimi6Mesi,TotaleVendutoUltimoAnno,totaleVendutoUltimi2Anni,UltimoOrdine \n " ;
2024-11-27 15:16:03 +01:00
2024-11-27 14:56:15 +01:00
foreach ( $articoliVenduti as $articoloVenduto ) {
2024-11-27 17:24:13 +01:00
$csvContent .= " { $articoloVenduto -> idArticolo } , { $articoloVenduto -> Titolo } , { $articoloVenduto -> DataPubblicazione } , { $articoloVenduto -> rank3M } , { $articoloVenduto -> rank6M } , { $articoloVenduto -> rank1Y } , { $articoloVenduto -> totaleVenduto } , { $articoloVenduto -> totaleVendutoUltimoMese } , { $articoloVenduto -> totaleVendutoUltimi6Mesi } , { $articoloVenduto -> totaleVendutoUltimoAnno } , { $articoloVenduto -> ultimoOrdine } \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-28 21:08:52 +01:00
// 'id' => $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-27 17:35:32 +01:00
'totaleVenduti' => $articoloVenduto -> totaleVenduto ,
'rank3M' => $articoloVenduto -> rank3M ,
'rank6M' => $articoloVenduto -> rank6M ,
'rank1Y' => $articoloVenduto -> rank1Y ,
'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 );
}
2024-11-27 17:07:02 +01:00
}
2024-11-28 17:30:10 +01:00
2024-11-28 17:35:47 +01:00
public function test ( Request $request )
2024-11-28 17:30:10 +01:00
{
try {
$articoli = $this -> queryTest ();
dd ( $articoli );
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
return new Response ( 'Erroe test: ' . $e -> getMessage (), 500 );
}
}
public function queryTest ()
{
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
}