173 lines
7.7 KiB
PHP
Executable File
173 lines
7.7 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.Titolo,
|
|
MIN(T_WEB_Articoli.DataPubblicazione) as DataPubblicazione,
|
|
T_WEB_Articoli.Ean13,
|
|
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
|
|
')
|
|
// ->groupBy('T_WEB_Articoli.idArticolo', 'T_WEB_Articoli.Titolo', 'T_WEB_Articoli.Ean13')
|
|
->groupBy('T_WEB_Articoli.Ean13', 'T_WEB_Articoli.Titolo') // Raggruppa solo per il codice Ean13
|
|
->orderBy('rank1Y', 'asc')
|
|
// ->take(50)
|
|
->get();
|
|
|
|
if ($articoliVenduti->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 $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,DataPubblicazione,Ean13,rank3M,rank6M,rank1Y,TotaleVenduto,TotaleVendutoUltimoMese,TotaleVendutoUltimi6Mesi,TotaleVendutoUltimoAnno,totaleVendutoUltimi2Anni,UltimoOrdine\n";
|
|
|
|
foreach ($articoliVenduti as $articoloVenduto) {
|
|
$csvContent .= "{$articoloVenduto->idArticolo},{$articoloVenduto->Titolo},{$articoloVenduto->DataPubblicazione},{$articoloVenduto->rank3M},{$articoloVenduto->rank6M},{$articoloVenduto->rank1Y},{$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);
|
|
}
|
|
}
|
|
|
|
public function exportArticlesSalesByJSON(Request $request): Response
|
|
{
|
|
try {
|
|
// Recupera gli articoli venduti
|
|
$articoliVenduti = $this->queryArticlesSales();
|
|
|
|
// Mappa i risultati nella struttura JSON desiderata
|
|
$result = $articoliVenduti->map(function ($articoloVenduto) {
|
|
return [
|
|
'id' => $articoloVenduto->idArticolo,
|
|
'title' => $articoloVenduto->Titolo,
|
|
'DataPubblicazione' => $articoloVenduto->DataPubblicazione,
|
|
'isbn' => $articoloVenduto->Ean13,
|
|
'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);
|
|
}
|
|
}
|
|
|
|
public function test(Request $request)
|
|
{
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
} |