zibo-dashboard/public/userarea/manager_stats.php
2025-11-26 11:24:29 +01:00

281 lines
9.1 KiB
PHP

<?php
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// --- Filtro periodo ---
$from = $_GET['from'] ?? date('Y-m-01');
$to = $_GET['to'] ?? date('Y-m-t');
// --- Query base ---
$baseWhere = "WHERE p.data_produzione BETWEEN :from AND :to";
// --- KPI ---
$kpi = $pdo->prepare("
SELECT
COUNT(*) AS totale,
SUM(p.ore_previste) AS orepreviste,
SUM(p.metri) AS metri,
SUM(p.kg_sp) AS kgsp
FROM productiondata p
$baseWhere
");
$kpi->execute(['from' => $from, 'to' => $to]);
$kpi = $kpi->fetch(PDO::FETCH_ASSOC);
// --- Grafico stati ---
$stati = $pdo->prepare("
SELECT s.nome, COUNT(*) AS totale
FROM productiondata p
LEFT JOIN production_status s ON p.id_status = s.id
$baseWhere
GROUP BY p.id_status
");
$stati->execute(['from' => $from, 'to' => $to]);
$data_stati = $stati->fetchAll(PDO::FETCH_ASSOC);
// --- Grafico per linea ---
$linee = $pdo->prepare("
SELECT l.name, COUNT(*) AS totale
FROM productiondata p
LEFT JOIN production_lines l ON p.id_linea = l.id
$baseWhere
GROUP BY p.id_linea
");
$linee->execute(['from' => $from, 'to' => $to]);
$data_linee = $linee->fetchAll(PDO::FETCH_ASSOC);
// --- Metri per mescola ---
$mescole = $pdo->prepare("
SELECT ms.nome, SUM(p.metri) AS metri
FROM productiondata p
LEFT JOIN mescole ms ON p.idmescola = ms.id
$baseWhere
GROUP BY p.idmescola
");
$mescole->execute(['from' => $from, 'to' => $to]);
$data_mescole = $mescole->fetchAll(PDO::FETCH_ASSOC);
// --- Ore previste per cliente ---
$clienti = $pdo->prepare("
SELECT c.nome, SUM(p.ore_previste) AS ore
FROM productiondata p
LEFT JOIN clients c ON p.id_cliente = c.id
$baseWhere
GROUP BY p.id_cliente
");
$clienti->execute(['from' => $from, 'to' => $to]);
$data_clienti = $clienti->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="it">
<head>
<?php include('cssinclude.php'); ?>
<title>Statistiche Produzione | <?= $titlewebsite ?></title>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<style>
.kpi-box {
background: #f8f9fa;
border-radius: 10px;
padding: 18px;
text-align: center;
border: 1px solid #dee2e6;
transition: 0.2s;
}
.kpi-box:hover {
background: #eef2ff;
}
.kpi-value {
font-size: 1.8rem;
font-weight: bold;
}
.kpi-label {
font-size: .95rem;
color: #555;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<!-- ====================== -->
<!-- FILTRO PERIODO -->
<!-- ====================== -->
<div class="card mb-4">
<div class="card-header">Filtra periodo</div>
<div class="card-body">
<form method="GET" class="row g-3">
<div class="col-md-4">
<label class="form-label">Dal</label>
<input type="date" name="from" value="<?= $from ?>" class="form-control">
</div>
<div class="col-md-4">
<label class="form-label">Al</label>
<input type="date" name="to" value="<?= $to ?>" class="form-control">
</div>
<div class="col-md-4 d-flex align-items-end">
<button class="btn btn-primary w-100">Applica filtro</button>
</div>
</form>
</div>
</div>
<!-- ====================== -->
<!-- KPI BOX -->
<!-- ====================== -->
<div class="row g-3 mb-4">
<div class="col-md-3">
<div class="kpi-box">
<div class="kpi-value"><?= $kpi['totale'] ?></div>
<div class="kpi-label">Produzioni</div>
</div>
</div>
<div class="col-md-3">
<div class="kpi-box">
<div class="kpi-value"><?= number_format($kpi['orepreviste'], 2, ',', '.') ?></div>
<div class="kpi-label">Ore previste</div>
</div>
</div>
<div class="col-md-3">
<div class="kpi-box">
<div class="kpi-value"><?= number_format($kpi['metri'], 2, ',', '.') ?></div>
<div class="kpi-label">Metri totali</div>
</div>
</div>
<div class="col-md-3">
<div class="kpi-box">
<div class="kpi-value"><?= number_format($kpi['kgsp'], 2, ',', '.') ?></div>
<div class="kpi-label">Kg SP totali</div>
</div>
</div>
</div>
<!-- ====================== -->
<!-- GRAFICO STATI -->
<!-- ====================== -->
<div class="card mb-4">
<div class="card-header">Distribuzione per stato</div>
<div class="card-body">
<div id="chartStati"></div>
</div>
</div>
<!-- ====================== -->
<!-- GRAFICO PER LINEA -->
<!-- ====================== -->
<div class="card mb-4">
<div class="card-header">Produzioni per linea</div>
<div class="card-body">
<div id="chartLinee"></div>
</div>
</div>
<!-- ====================== -->
<!-- METRI PER MESCOLA -->
<!-- ====================== -->
<div class="card mb-4">
<div class="card-header">Metri programmati per mescola</div>
<div class="card-body">
<div id="chartMescole"></div>
</div>
</div>
<!-- ====================== -->
<!-- ORE PER CLIENTE -->
<!-- ====================== -->
<div class="card mb-4">
<div class="card-header">Ore previste per cliente</div>
<div class="card-body">
<div id="chartClienti"></div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<script>
// ======== GRAFICO STATI ========
new ApexCharts(document.querySelector("#chartStati"), {
chart: {
type: 'donut',
height: 350
},
labels: <?= json_encode(array_column($data_stati, 'nome')) ?>,
series: <?= json_encode(array_map('intval', array_column($data_stati, 'totale'))) ?>,
}).render();
// ======== GRAFICO PER LINEA ========
new ApexCharts(document.querySelector("#chartLinee"), {
chart: {
type: 'bar',
height: 350
},
series: [{
name: 'Produzioni',
data: <?= json_encode(array_map('intval', array_column($data_linee, 'totale'))) ?>
}],
xaxis: {
categories: <?= json_encode(array_column($data_linee, 'name')) ?>
}
}).render();
// ======== METRI PER MESCOLA ========
new ApexCharts(document.querySelector("#chartMescole"), {
chart: {
type: 'bar',
height: 350
},
series: [{
name: 'Metri',
data: <?= json_encode(array_map('floatval', array_column($data_mescole, 'metri'))) ?>
}],
xaxis: {
categories: <?= json_encode(array_column($data_mescole, 'nome')) ?>
}
}).render();
// ======== ORE PER CLIENTE ========
new ApexCharts(document.querySelector("#chartClienti"), {
chart: {
type: 'bar',
height: 350
},
plotOptions: {
bar: {
horizontal: true
}
},
series: [{
name: 'Ore previste',
data: <?= json_encode(array_map('floatval', array_column($data_clienti, 'ore'))) ?>
}],
xaxis: {
categories: <?= json_encode(array_column($data_clienti, 'nome')) ?>
}
}).render();
</script>
</body>
</html>