Files
apimacro/app/Http/Controllers/ArticleController.php

82 lines
3.2 KiB
PHP
Raw Normal View History

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-27 14:49:39 +01:00
FROM T_WEB_StatiProdotto e
JOIN (SELECT IdStatoProdotto, MAX(DataOra) as data1
FROM T_WEB_StatiProdotto
GROUP BY IdStatoProdotto) c
2024-11-27 14:56:15 +01:00
ON e.IdStatoProdotto = c.IdStatoProdotto AND e.DataOra = c.data1) f'),
function ($join) {
$join->on('T_WEB_Articoli.IdStatoProdotto', '=', 'f.IdStatoProdotto');
}
)
2024-11-27 14:41:18 +01:00
->whereIn('f.DescrizioneStatoProdotto', ['In commercio', 'In prevendita', 'Prossima uscita'])
2024-11-27 14:49:39 +01:00
->selectRaw('T_WEB_Articoli.idArticolo, T_WEB_Articoli.Titolo, SUM(T_WEB_Ordini.qta) as totaleVenduto')
->groupBy('T_WEB_Articoli.idArticolo', 'T_WEB_Articoli.Titolo')
->orderBy('T_WEB_Articoli.Titolo')
->take(10)
2024-11-27 14:41:18 +01:00
->get();
2024-11-27 14:27:16 +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-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]);
} 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 = ArticleController::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\n";
foreach ($articoliVenduti as $articoloVenduto) {
$csvContent .= "{$articoloVenduto->idArticolo},{$articoloVenduto->Titolo},{$articoloVenduto->totaleVenduto}\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);
}
}
}