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-12-20 11:31:03 +01:00
use Illuminate\Support\Facades\Schema ;
2024-11-27 14:50:35 +01:00
use App\Article ;
2024-12-08 15:56:49 +01:00
use DateTime ;
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-08 16:14:33 +01:00
private function convDate ( $data_italiana )
{
2024-12-08 15:56:14 +01:00
return DateTime :: createFromFormat ( 'Y-m-d' , $data_italiana ) -> format ( 'Y-d-m' );
}
2024-12-08 12:00:30 +01:00
private function articoliByDataStart ( $data_start )
2024-12-07 22:59:14 +01:00
{
2024-12-08 15:56:14 +01:00
$data_start = $this -> convDate ( $data_start );
$data_start = $data_start . ' 00:00:00' ;
2024-12-07 23:29:57 +01:00
ini_set ( " memory_limit " , " 512M " );
2024-12-07 22:59:14 +01:00
try {
2024-12-08 14:35:03 +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-19 10:59:33 +01:00
-> leftJoin ( DB :: raw ( '(SELECT CodArticoloGM, SUM(Qta) as totVen, MAX(DataOra) as ultimaDataOra FROM T_WEB_Ordini WHERE DataOra > \'' . $data_start . '\' GROUP BY CodArticoloGM) O' ), 'T_WEB_Articoli.IdArticolo' , '=' , 'O.CodArticoloGM' )
// ->leftJoin(DB::raw('(SELECT CodArticolo, SUM(Qta) as totVen, MAX(DataOra) as ultimaDataOra FROM T_WEB_ArticoliFatturati WHERE DataOra > \'' . $data_start . '\' GROUP BY CodArticolo) O'), 'T_WEB_Articoli.IdArticolo', '=', 'O.CodArticolo')
2024-12-08 14:35:03 +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-08 12:44:30 +01:00
$join -> on ( 'T_WEB_Articoli.IdTipologia' , '=' , 'i.IdTipologia' );
2024-12-08 12:48:30 +01:00
})
2024-12-13 21:15:18 +01:00
-> select ( 'T_WEB_Articoli.*' , 'ultimaDataOra' , DB :: raw ( 'COALESCE(O.totVen, 0) as totVen' ))
-> where ( 'totVen' , '>' , 0 )
2024-12-08 12:44:30 +01:00
-> where ( 'DescrizioneTipologia' , 'Libri' )
2024-12-13 21:15:18 +01:00
-> orderBy ( 'totVen' , 'desc' )
2024-12-08 12:44:30 +01:00
-> get ();
2024-12-07 22:59:14 +01:00
2024-12-08 16:14:33 +01:00
// var_dump($articoli->toArray());
2024-12-08 15:46:54 +01:00
2024-12-07 22:59:14 +01:00
if ( $articoli -> isEmpty ()) {
2024-12-08 12:55:44 +01:00
return [ 'error' => 'Nessun articolo' ];
2024-12-07 22:59:14 +01:00
}
} catch ( \Exception $e ) {
// Registrazione dell'errore
2024-12-08 12:11:17 +01:00
return [ 'error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e -> getMessage ()];
2024-12-07 22:59:14 +01:00
}
return $articoli ;
2024-12-07 20:57:15 +01:00
2024-12-07 23:24:49 +01:00
}
2024-12-08 11:41:57 +01:00
private function queryOrdini ( $numrec )
2024-12-07 23:24:49 +01:00
{
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 12:01:41 +01:00
-> leftJoin ( DB :: raw ( ' (
2024-12-08 11:34:21 +01:00
SELECT
2024-12-08 11:36:07 +01:00
IdArticolo ,
2024-12-08 11:34:21 +01:00
MAX ( DataOra ) as data
FROM T_WEB_Articoli
GROUP BY IdArticolo
2024-12-08 11:36:07 +01:00
) b '), ' O . CodArticoloGM ', ' = ', ' b . IdArticolo ' )
2024-12-08 12:01:41 +01:00
-> join ( 'T_WEB_Articoli as A' , function ( $join ) {
$join -> on ( 'O.CodArticoloGM' , '=' , 'A.IdArticolo' )
-> on ( 'b.data' , '=' , 'A.DataOra' );
})
-> select (
DB :: raw ( 'ROW_NUMBER() OVER (ORDER BY O.DataOra DESC) AS progressivo' ),
'O.DataOra' ,
'O.Qta' ,
'A.Titolo' ,
2024-12-19 11:25:55 +01:00
'O.CodArticoloGM' ,
2024-12-08 12:01:41 +01:00
'A.Ean13'
)
-> orderBy ( 'O.DataOra' , 'desc' )
-> take ( $numrec )
-> get ();
2024-12-07 23:24:49 +01:00
} 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-08 16:42:48 +01:00
}
private function queryOrdiniByIdArticolo ( $idArticolo )
{
ini_set ( " memory_limit " , " 512M " );
try {
$ordini = DB :: table ( 'T_WEB_Ordini as O' )
-> leftJoin ( DB :: raw ( ' (
SELECT
IdArticolo ,
MAX ( DataOra ) as data
FROM T_WEB_Articoli
GROUP BY IdArticolo
) b '), ' O . CodArticoloGM ', ' = ', ' b . IdArticolo ' )
-> join ( 'T_WEB_Articoli as A' , function ( $join ) {
$join -> on ( 'O.CodArticoloGM' , '=' , 'A.IdArticolo' )
-> on ( 'b.data' , '=' , 'A.DataOra' );
})
-> select (
DB :: raw ( 'ROW_NUMBER() OVER (ORDER BY O.DataOra DESC) AS progressivo' ),
'O.DataOra' ,
'O.Qta' ,
'A.Titolo' ,
2024-12-19 11:25:55 +01:00
'O.CodArticoloGM' ,
2024-12-08 16:42:48 +01:00
'A.Ean13'
)
-> orderBy ( 'O.DataOra' , 'desc' )
2024-12-08 16:59:10 +01:00
-> where ( 'O.CodArticoloGM' , '=' , $idArticolo )
2024-12-08 16:42:48 +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-18 09:14:02 +01:00
}
private function queryFatturatiByIdArticolo ( $idArticolo )
{
ini_set ( " memory_limit " , " 512M " );
try {
$ordini = DB :: table ( 'T_WEB_ArticoliFatturati as O' )
-> leftJoin ( DB :: raw ( ' (
SELECT
IdArticolo ,
MAX ( DataOra ) as data
FROM T_WEB_Articoli
GROUP BY IdArticolo
) b '), ' O . CodArticolo ', ' = ', ' b . IdArticolo ' )
-> join ( 'T_WEB_Articoli as A' , function ( $join ) {
$join -> on ( 'O.CodArticolo' , '=' , 'A.IdArticolo' )
-> on ( 'b.data' , '=' , 'A.DataOra' );
})
-> select (
DB :: raw ( 'ROW_NUMBER() OVER (ORDER BY O.DataOra DESC) AS progressivo' ),
'O.DataOra' ,
'O.Qta' ,
'A.Titolo' ,
2024-12-19 11:25:55 +01:00
'O.CodArticolo' ,
2024-12-18 09:14:02 +01:00
'A.Ean13'
)
-> orderBy ( 'O.DataOra' , 'desc' )
-> where ( 'O.CodArticolo' , '=' , $idArticolo )
-> 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-12-08 16:14:33 +01:00
private function queryshowInfoArticolo ( $idArticolo )
{
try {
ini_set ( " memory_limit " , " 512M " );
$articoli = Article :: where ( 'IdArticolo' , $idArticolo )
-> get (); // Usa
2024-12-08 16:28:34 +01:00
if ( $articoli -> isEmpty ()) {
return [ 'error' => 'Nessun articolo' ];
}
2024-12-08 16:14:33 +01:00
} catch ( \Exception $e ) {
// Registrazione dell'errore
2024-12-08 16:28:34 +01:00
return [ 'error' => 'Si è verificato un errore durante il recupero dei dati: ' . $e -> getMessage ()];
2024-12-08 16:14:33 +01:00
}
return $articoli ;
}
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-13 21:15:18 +01:00
-> leftJoin ( DB :: raw ( '(SELECT CodArticoloGM, SUM(Qta) as totVen 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-20 11:31:03 +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' );
})
2024-12-19 11:07:03 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticolo , SUM ( TRY_CAST ( Qta AS INT )) as fat3mesi , RANK () OVER ( ORDER BY SUM ( TRY_CAST ( Qta AS INT )) DESC ) as fatrank3M
FROM T_WEB_ArticoliFatturati
WHERE DataOra >= DATEADD ( MONTH , - 3 , GETDATE ()) AND ISNUMERIC ( Qta ) = 1
GROUP BY CodArticolo ) t ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 't.CodArticolo' );
})
-> leftJoin ( DB :: raw ( '(SELECT CodArticolo, SUM(TRY_CAST(Qta AS INT)) as totFat FROM T_WEB_ArticoliFatturati WHERE ISNUMERIC(Qta) = 1 GROUP BY CodArticolo) u' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'u.CodArticolo' );
})
2024-12-20 12:38:21 +01:00
-> leftJoin ( DB :: raw ( ' (
SELECT v . IdCollana , v . Descrizione as DescrizioneCollana
FROM T_WEB_Collane v
INNER JOIN (
SELECT IdCollana , MAX ( ID ) as MaxID
FROM T_WEB_Collane
GROUP BY IdCollana
) x ON v . IdCollana = x . IdCollana AND v . ID = x . MaxID
) y ' ), function ( $join ) {
2024-12-20 11:15:52 +01:00
$join -> on ( 'T_WEB_Articoli.IdCollana' , '=' , 'y.IdCollana' );
})
2025-02-12 10:04:00 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT g2 . IdArgomento , g2 . Descrizione as DescrArgomento FROM T_WEB_Argomenti g2
JOIN ( SELECT IdArgomento , MAX ( DataOra ) as data12 from T_WEB_Argomenti GROUP BY IdArgomento ) h2
2025-02-12 10:05:07 +01:00
ON g2 . IdArgomento = h2 . IdArgomento AND g2 . DataOra = h2 . data12 ) i2 ' ), function ( $join ) {
2025-02-12 09:33:53 +01:00
$join -> on ( 'T_WEB_Articoli.ListaArgomenti' , '=' , 'i2.IdArgomento' );
})
2024-12-07 20:30:37 +01:00
-> select (
'T_WEB_Articoli.*' ,
'f.DescrizioneStatoProdotto' ,
'i.DescrizioneTipologia' ,
'n.DescrizioneFormato' ,
2024-12-20 11:15:52 +01:00
'y.DescrizioneCollana' ,
2025-02-12 09:33:53 +01:00
'i2.DescrArgomento' ,
2024-12-13 21:15:18 +01:00
DB :: raw ( 'COALESCE(o.totVen, 0) as totVen' ),
2024-12-19 11:07:03 +01:00
DB :: raw ( 'COALESCE(u.totFat, 0) as totFat' ),
2024-12-07 20:30:37 +01:00
DB :: raw ( 'COALESCE(p.venduti3mesi, 0) as venduti3mesi' ),
2024-12-19 10:59:33 +01:00
DB :: raw ( 'COALESCE(t.fat3mesi, 0) as fat3mesi' ),
2024-12-07 20:30:37 +01:00
DB :: raw ( 'COALESCE(p.rank3M, 0) as rank3M' ),
2024-12-19 11:07:03 +01:00
DB :: raw ( 'COALESCE(t.fatrank3M, 0) as fatrank3M' ),
2024-12-07 20:30:37 +01:00
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')
2024-12-13 21:15:18 +01:00
-> orderBy ( 'totVen' , '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-12-18 09:28:40 +01:00
}
private function queryArticlesFatturati ()
{
try {
ini_set ( " memory_limit " , " 512M " );
$articoliVenduti = 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' );
})
-> 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' );
})
-> 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' );
})
-> 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-19 10:59:33 +01:00
-> leftJoin ( DB :: raw ( '(SELECT CodArticolo, SUM(TRY_CAST(Qta AS INT)) AS totFat FROM T_WEB_ArticoliFatturati WHERE ISNUMERIC(Qta) = 1 GROUP BY CodArticolo) o' ), function ( $join ) {
2024-12-18 09:28:40 +01:00
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'o.CodArticolo' );
})
2024-12-19 10:21:31 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticolo , SUM ( TRY_CAST ( Qta AS INT )) as venduti3mesi , RANK () OVER ( ORDER BY SUM ( TRY_CAST ( Qta AS INT )) DESC ) as rank3M
2024-12-18 09:28:40 +01:00
FROM T_WEB_ArticoliFatturati
2024-12-19 10:13:07 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 3 , GETDATE ()) AND ISNUMERIC ( Qta ) = 1
2024-12-18 09:28:40 +01:00
GROUP BY CodArticolo ) p ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'p.CodArticolo' );
})
2024-12-19 10:21:31 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticolo , SUM ( TRY_CAST ( Qta AS INT )) as venduti6mesi , RANK () OVER ( ORDER BY SUM ( TRY_CAST ( Qta AS INT )) DESC ) as rank6M
2024-12-18 09:28:40 +01:00
FROM T_WEB_ArticoliFatturati
2024-12-19 10:13:07 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 6 , GETDATE ()) AND ISNUMERIC ( Qta ) = 1
2024-12-18 09:28:40 +01:00
GROUP BY CodArticolo ) q ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'q.CodArticolo' );
})
2024-12-19 10:21:31 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticolo , SUM ( TRY_CAST ( Qta AS INT )) as venduti1anno , RANK () OVER ( ORDER BY SUM ( TRY_CAST ( Qta AS INT )) DESC ) as rank1Y
2024-12-18 09:28:40 +01:00
FROM T_WEB_ArticoliFatturati
2024-12-19 10:13:07 +01:00
WHERE DataOra >= DATEADD ( MONTH , - 12 , GETDATE ()) AND ISNUMERIC ( Qta ) = 1
2024-12-18 09:28:40 +01:00
GROUP BY CodArticolo ) r ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 'r.CodArticolo' );
})
-> leftJoin ( DB :: raw ( ' ( SELECT CodArticolo , MAX ( DataOra ) as ultimoOrdine
2024-12-19 10:14:29 +01:00
FROM T_WEB_ArticoliFatturati
WHERE ISNUMERIC ( Qta ) = 1
2024-12-18 09:28:40 +01:00
GROUP BY CodArticolo ) s ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdArticolo' , '=' , 's.CodArticolo' );
})
2025-02-12 10:02:27 +01:00
-> leftJoin ( DB :: raw ( ' (
SELECT v . IdCollana , v . Descrizione as DescrizioneCollana
FROM T_WEB_Collane v
INNER JOIN (
SELECT IdCollana , MAX ( ID ) as MaxID
FROM T_WEB_Collane
GROUP BY IdCollana
) x ON v . IdCollana = x . IdCollana AND v . ID = x . MaxID
) y ' ), function ( $join ) {
$join -> on ( 'T_WEB_Articoli.IdCollana' , '=' , 'y.IdCollana' );
})
2025-02-12 10:04:00 +01:00
-> leftJoin ( DB :: raw ( ' ( SELECT g2 . IdArgomento , g2 . Descrizione as DescrArgomento FROM T_WEB_Argomenti g2
JOIN ( SELECT IdArgomento , MAX ( DataOra ) as data12 from T_WEB_Argomenti GROUP BY IdArgomento ) h2
2025-02-12 10:05:07 +01:00
ON g2 . IdArgomento = h2 . IdArgomento AND g2 . DataOra = h2 . data12 ) i2 ' ), function ( $join ) {
2025-02-12 10:02:27 +01:00
$join -> on ( 'T_WEB_Articoli.ListaArgomenti' , '=' , 'i2.IdArgomento' );
})
2024-12-18 09:28:40 +01:00
-> select (
'T_WEB_Articoli.*' ,
'f.DescrizioneStatoProdotto' ,
'i.DescrizioneTipologia' ,
'n.DescrizioneFormato' ,
2025-02-12 10:02:27 +01:00
'y.DescrizioneCollana' ,
'i2.DescrArgomento' ,
2024-12-19 10:59:33 +01:00
DB :: raw ( 'COALESCE(o.totFat, 0) as totFat' ),
2024-12-18 09:28:40 +01:00
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' )
//->orderBy('rank1Y', 'asc')
2024-12-19 10:59:33 +01:00
-> orderBy ( 'totFat' , 'desc' )
2024-12-18 09:28:40 +01:00
-> get ();
if ( $articoliVenduti -> isEmpty ()) {
return response () -> json ([ 'message' => 'Nessun articolo fatturato 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 $articoliVenduti ;
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
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showArticlesSales: ' . $e -> getMessage (), 500 );
2024-11-27 14:56:15 +01:00
}
}
2024-12-18 09:28:40 +01:00
public function showArticlesFatturati ( Request $request )
{
try {
$articoliVenduti = $this -> queryArticlesFatturati ();
return view ( 'export_articles_fatturati' , [ 'articoliVenduti' => $articoliVenduti ]);
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:55:13 +01:00
return new Response ( 'Error exporting showArticlesFatturati: ' . $e -> getMessage () .
2024-12-20 11:31:03 +01:00
'Articoli Fatturati ' . json_encode ( $articoliVenduti ), 500 );
2024-12-18 09:28:40 +01:00
}
}
2024-12-08 12:00:30 +01:00
public function showArticoliByDataStart ( $data_start )
2024-12-07 20:57:15 +01:00
{
try {
2024-12-08 12:44:30 +01:00
$this -> validateDate ( $data_start );
2024-12-07 20:57:15 +01:00
2024-12-08 12:00:30 +01:00
$articoliVenduti = $this -> articoliByDataStart ( $data_start );
2024-12-07 20:57:15 +01:00
2024-12-08 12:55:44 +01:00
if ( $articoliVenduti && isset ( $articoliVenduti [ 'error' ])) {
2024-12-08 12:09:42 +01:00
return view ( 'error' , [ 'message' => $articoliVenduti [ 'error' ]]);
}
2024-12-08 16:14:33 +01:00
return view ( 'view-lista-ordini-totale' , [ 'articoli' => $articoliVenduti ]);
2024-12-07 20:57:15 +01:00
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showArticoliByDataStart: ' . $e -> getMessage (), 500 );
2024-12-07 20:57:15 +01:00
2024-12-08 12:44:30 +01:00
2024-12-07 20:57:15 +01:00
}
}
2024-12-08 12:44:30 +01:00
protected function validateDate ( $date )
{
$validator = \Validator :: make ([ 'data_start' => $date ], [
'data_start' => 'required|date_format:Y-m-d'
]);
if ( $validator -> fails ()) {
throw new ValidationException ( $validator );
}
}
2024-12-08 11:41:57 +01:00
public function showOrdini ( $numrec )
2024-12-07 23:24:49 +01:00
{
try {
2024-12-08 11:41:57 +01:00
$ordini = $this -> queryOrdini ( $numrec );
2024-12-07 23:24:49 +01:00
return view ( 'ordini_test' , [ 'ordini' => $ordini ]);
2024-12-08 16:42:48 +01:00
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showOrdini: ' . $e -> getMessage (), 500 );
2024-12-08 16:42:48 +01:00
}
}
public function showOrdiniByIdArticolo ( $idArticolo )
{
try {
$ordini = $this -> queryOrdiniByIdArticolo ( $idArticolo );
return view ( 'ordini_test' , [ 'ordini' => $ordini ]);
2024-12-07 23:24:49 +01:00
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showOrdiniByIdArticolo: ' . $e -> getMessage (), 500 );
2024-12-07 23:24:49 +01:00
}
}
2024-12-18 09:14:02 +01:00
public function showFatturatiByIdArticolo ( $idArticolo )
{
try {
$ordini = $this -> queryFatturatiByIdArticolo ( $idArticolo );
return view ( 'fatturati' , [ 'ordini' => $ordini ]);
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showFatturatiByIdArticolo: ' . $e -> getMessage (), 500 );
2024-12-18 09:14:02 +01:00
}
}
2024-12-08 16:25:39 +01:00
public function showInfoArticolo ( $idArticolo )
2024-12-08 16:14:33 +01:00
{
try {
2024-12-08 16:25:39 +01:00
$articoli = $this -> queryshowInfoArticolo ( $idArticolo );
2024-12-08 16:14:33 +01:00
2024-12-08 16:28:34 +01:00
if ( $articoli && isset ( $articoli [ 'error' ])) {
return view ( 'error' , [ 'message' => $articoli [ 'error' ]]);
}
2024-12-08 16:14:33 +01:00
return view ( 'info-articolo' , [ 'articoli' => $articoli ]);
} catch ( \Exception $e ) {
// Potresti considerare di registrare l'errore per debugging
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting showInfoArticolo: ' . $e -> getMessage (), 500 );
2024-12-08 16:14:33 +01:00
}
}
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-13 21:15:18 +01:00
$csvContent = " IdArticolo,Titolo,DataPubblicazione,Ean13,rank3M,rank6M,rank1Y,totVen,totVenUltimoMese,totVenUltimi6Mesi,totVenUltimoAnno,totVenUltimi2Anni,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-17 21:23:13 +01:00
$csvContent .= " { $articoloVenduto -> idArticolo } , { $articoloVenduto -> Titolo } , { $articoloVenduto -> DataPubblicazione } , { $articoloVenduto -> rank3M } , { $articoloVenduto -> rank6M } , { $articoloVenduto -> rank1Y } , { $articoloVenduto -> totVen } , { $articoloVenduto -> venduti3mesi } , { $articoloVenduto -> venduti6mesi } , { $articoloVenduto -> venduti1anno } , { $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-12-19 09:58:05 +01:00
return new Response ( 'Error exporting exportArticlesSales: ' . $e -> getMessage (), 500 );
2024-11-27 14:27:16 +01:00
}
}
2024-11-27 17:07:02 +01:00
2024-12-18 09:28:40 +01:00
public function exportArticlesSalesByJSON_Base ( $cosa , Request $request ) : Response
2024-11-27 17:35:32 +01:00
{
try {
2024-12-18 09:28:40 +01:00
if ( $cosa === 'fatturati' ) {
$articoliVenduti = $this -> queryArticlesFatturati ();
} else {
$articoliVenduti = $this -> queryArticlesSales ();
}
2024-11-27 17:35:32 +01:00
2024-12-19 10:09:38 +01:00
if ( $articoliVenduti ) {
// Mappa i risultati nella struttura JSON desiderata
$result = $articoliVenduti -> map ( function ( $articoloVenduto ) {
return [
'IdArticolo' => $articoloVenduto -> IdArticolo ,
'title' => $articoloVenduto -> Titolo ,
'DataPubblicazione' => $articoloVenduto -> DataPubblicazione ,
'isbn' => $articoloVenduto -> Ean13 ,
2024-12-20 11:15:52 +01:00
'IdCollana' => $articoloVenduto -> IdCollana ,
2025-02-03 11:08:32 +01:00
'DescrizioneCollana' => $articoloVenduto -> DescrizioneCollana ,
2025-02-12 09:33:53 +01:00
'DescrArgomento' => $articoloVenduto -> DescrArgomento ,
2025-02-12 10:29:15 +01:00
'ListaArgomenti' => $articoloVenduto -> ListaArgomenti ,
2024-12-19 10:09:38 +01:00
'Pagine' => $articoloVenduto -> Pagine ,
'IdTipoFormato' => $articoloVenduto -> IdTipoFormato ,
2025-01-16 08:39:54 +01:00
'Misure' => $articoloVenduto -> Misure ,
2024-12-19 10:09:38 +01:00
'totVen' => $articoloVenduto -> totVen ,
2024-12-19 10:59:33 +01:00
'totFat' => $articoloVenduto -> totFat ,
2024-12-19 10:09:38 +01:00
'rank3M' => $articoloVenduto -> rank3M ,
2024-12-19 10:59:33 +01:00
'fatrank3M' => $articoloVenduto -> fatrank3M ,
2024-12-19 10:09:38 +01:00
'rank6M' => $articoloVenduto -> rank6M ,
'rank1Y' => $articoloVenduto -> rank1Y ,
2024-12-19 10:59:33 +01:00
'fatLast3M' => $articoloVenduto -> fat3mesi ,
2024-12-19 10:09:38 +01:00
'vLast3M' => $articoloVenduto -> venduti3mesi ,
'vLast6M' => $articoloVenduto -> venduti6mesi ,
'vLastY' => $articoloVenduto -> venduti1anno ,
'dataUltimoOrdine' => $articoloVenduto -> ultimoOrdine ,
];
});
// Imposta il contenuto della risposta come JSON
$response = new Response ( $result -> toJson (), 200 );
$response -> headers -> set ( 'Content-Type' , 'application/json' );
2024-12-19 10:59:33 +01:00
$response -> headers -> set ( 'Content-Disposition' , 'attachment; filename="ranking_' . $cosa . '_' . date ( 'Y-m-d' ) . '.json"' );
2024-12-19 10:09:38 +01:00
return $response ;
} else {
return new Response ( 'Nessun articolo' , 500 );
}
2024-11-27 17:35:32 +01:00
} catch ( \Exception $e ) {
2024-12-19 09:58:05 +01:00
return new Response ( 'Error exporting exportArticlesSalesByJSON_Base: ' . $e -> getMessage () . 'Articoli: ' . json_decode ( $articoliVenduti ), 500 );
2024-11-27 17:35:32 +01:00
}
2024-11-27 17:07:02 +01:00
}
2024-11-28 17:30:10 +01:00
2024-12-18 09:28:40 +01:00
public function exportArticlesSalesByJSON ( Request $request ) : Response
{
return $this -> exportArticlesSalesByJSON_Base ( '' , $request );
}
public function exportArticlesFatturatiByJSON ( Request $request ) : Response
{
return $this -> exportArticlesSalesByJSON_Base ( 'fatturati' , $request );
}
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-12-20 11:31:03 +01:00
function showTableContent ( $tableName , $recordCount )
{
// Verifica se la tabella esiste
if ( ! Schema :: hasTable ( $tableName )) {
return " La tabella ' $tableName ' non esiste. " ;
}
// Ottieni tutti i campi della tabella
$columns = Schema :: getColumnListing ( $tableName );
// Recupera i record dalla tabella
$records = DB :: table ( $tableName ) -> take ( $recordCount ) -> get ();
// Se non ci sono record, restituisci un messaggio
if ( $records -> isEmpty ()) {
return " Nessun record trovato nella tabella ' $tableName '. " ;
}
// Prepara l'output in formato tabellare
$output = " <table border='1'><thead><tr> " ;
// Intestazioni della tabella
foreach ( $columns as $column ) {
$output .= " <th> $column </th> " ;
}
$output .= " </tr></thead><tbody> " ;
// Righe dei dati
foreach ( $records as $record ) {
$output .= " <tr> " ;
foreach ( $columns as $column ) {
$output .= " <td> " . ( $record -> $column ? ? 'NULL' ) . " </td> " ;
}
$output .= " </tr> " ;
}
$output .= " </tbody></table> " ;
return $output ;
}
public function showTableByName ( $tableName , $numrec )
{
2024-12-20 12:38:21 +01:00
2024-12-20 11:31:03 +01:00
// Usa la funzione showTableContent per ottenere i dati in formato tabellare
$tableContent = $this -> showTableContent ( $tableName , $numrec );
// Passa i dati alla vista
return view ( 'article.info' , [
'tableName' => $tableName ,
'tableContent' => $tableContent
]);
}
2024-11-27 14:27:16 +01:00
}