fixed nuovo
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class AddCategoryToPauseReasons extends AbstractMigration
|
||||
{
|
||||
public function change(): void
|
||||
{
|
||||
$this->table('pause_reasons')
|
||||
->addColumn('category', 'enum', [
|
||||
'values' => ['planned', 'unplanned'],
|
||||
'null' => false,
|
||||
'default' => 'unplanned',
|
||||
'after' => 'is_problem',
|
||||
'comment' => 'planned = fermo pianificato (cambio, pulizia, mensa); unplanned = guasto, mancanza materiale, qualità',
|
||||
])
|
||||
->update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class AddDailyCapacityToProductionLines extends AbstractMigration
|
||||
{
|
||||
public function change(): void
|
||||
{
|
||||
$this->table('production_lines')
|
||||
->addColumn('max_hours_per_day', 'decimal', [
|
||||
'precision' => 4,
|
||||
'scale' => 2,
|
||||
'null' => false,
|
||||
'default' => 24.00,
|
||||
'after' => 'color',
|
||||
'comment' => 'Ore massime di disponibilita giornaliera della linea (default 24)',
|
||||
])
|
||||
->update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class AddPreferredLineToAuthUsers extends AbstractMigration
|
||||
{
|
||||
public function change(): void
|
||||
{
|
||||
$this->table('auth_users')
|
||||
->addColumn('preferred_line_id', 'integer', [
|
||||
'signed' => true, // production_lines.id è int(11) SIGNED → deve combaciare
|
||||
'limit' => 11,
|
||||
'null' => true,
|
||||
'default' => null,
|
||||
'after' => 'notify_webapp',
|
||||
'comment' => 'Linea di produzione preferita per la Line View (NULL = Tutte)',
|
||||
])
|
||||
->addForeignKey('preferred_line_id', 'production_lines', 'id', [
|
||||
'delete' => 'SET_NULL',
|
||||
'update' => 'NO_ACTION',
|
||||
])
|
||||
->update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
final class PauseReasonsPlannedSeeder extends AbstractSeed
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$data = [
|
||||
['name' => 'Cambio turno', 'is_problem' => 0, 'category' => 'planned'],
|
||||
['name' => 'Aggiunta / cambio mescola', 'is_problem' => 0, 'category' => 'planned'],
|
||||
['name' => 'Cambio produzione', 'is_problem' => 0, 'category' => 'planned'],
|
||||
['name' => 'Attrezzaggio / cambio stampo', 'is_problem' => 0, 'category' => 'planned'],
|
||||
['name' => 'Pulizia impianto', 'is_problem' => 0, 'category' => 'planned'],
|
||||
['name' => 'Manutenzione programmata', 'is_problem' => 0, 'category' => 'planned'],
|
||||
];
|
||||
|
||||
$this->table('pause_reasons')->insert($data)->saveData();
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,13 @@
|
||||
value="<?= htmlspecialchars($line['color'] ?? '#dc2626') ?>">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="maxHours" class="form-label fw-semibold">Ore massime al giorno</label>
|
||||
<input type="number" step="0.5" min="0" max="24" id="maxHours" name="max_hours_per_day"
|
||||
class="form-control" value="<?= htmlspecialchars($line['max_hours_per_day'] ?? 24) ?>">
|
||||
<small class="text-muted">Capacità teorica giornaliera. Default 24 (h24).</small>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="status" class="form-label fw-semibold">Stato</label>
|
||||
<select id="status" name="status" class="form-select">
|
||||
|
||||
@@ -174,6 +174,14 @@
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (userCan('production.stats.view')) : ?>
|
||||
<li>
|
||||
<a href="pause_stats.php">
|
||||
<i class='bx bx-radio-circle'></i>Pause & Fermi
|
||||
</a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (userCan('production.manager.view')) : ?>
|
||||
<li>
|
||||
<a href="manager_produzione.php">
|
||||
|
||||
@@ -148,6 +148,7 @@
|
||||
<th>Modello</th>
|
||||
<th>Marca</th>
|
||||
<th>Colore</th>
|
||||
<th>Max h/giorno</th>
|
||||
<th>Stato</th>
|
||||
<th>Azioni</th>
|
||||
</tr>
|
||||
@@ -159,7 +160,7 @@
|
||||
$stmt = $pdo->query("SELECT * FROM production_lines ORDER BY line_number ASC");
|
||||
|
||||
if ($stmt->rowCount() === 0) {
|
||||
echo "<tr><td colspan='8' class='text-muted'>Nessuna linea di produzione presente</td></tr>";
|
||||
echo "<tr><td colspan='9' class='text-muted'>Nessuna linea di produzione presente</td></tr>";
|
||||
} else {
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$badge = $row['status'] === 'active'
|
||||
@@ -174,9 +175,10 @@
|
||||
<td>{$lineNameEsc}</td>
|
||||
<td>" . htmlspecialchars($row['model']) . "</td>
|
||||
<td>" . htmlspecialchars($row['brand']) . "</td>
|
||||
<td>
|
||||
<td>
|
||||
<div style='width:28px; height:28px; border-radius:6px; border:1px solid #999; background: {$row['color']}; margin:auto;'></div>
|
||||
</td>
|
||||
<td>" . number_format((float)($row['max_hours_per_day'] ?? 24), 2) . " h</td>
|
||||
<td>{$badge}</td>
|
||||
<td>
|
||||
<button class='btn-action params' title='Parametri linea'
|
||||
@@ -243,6 +245,14 @@
|
||||
id="color" name="color" value="#dc2626" title="Scegli colore">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="maxHours" class="form-label fw-semibold">Ore massime al giorno</label>
|
||||
<input type="number" step="0.5" min="0" max="24" class="form-control"
|
||||
id="maxHours" name="max_hours_per_day" value="24"
|
||||
title="Capacità giornaliera della linea (default 24)">
|
||||
<small class="text-muted">Capacità teorica giornaliera. Default 24 (h24).</small>
|
||||
</div>
|
||||
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-add">💾 Salva</button>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,831 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
include('include/headscript.php');
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
$linee = $pdo->query("SELECT id, name, color FROM production_lines ORDER BY line_number")->fetchAll();
|
||||
$matrici = $pdo->query("SELECT id, nome FROM matrice ORDER BY nome")->fetchAll();
|
||||
$mescole = $pdo->query("SELECT id, nome FROM mescole ORDER BY nome")->fetchAll();
|
||||
|
||||
if (!empty($_GET['ajax'])) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$dateFrom = $_GET['date_from'] ?? '';
|
||||
$dateTo = $_GET['date_to'] ?? '';
|
||||
$lineRaw = $_GET['lines'] ?? '';
|
||||
$lineArr = $lineRaw !== '' ? array_values(array_filter(array_map('intval', explode(',', $lineRaw)))) : [];
|
||||
$matId = isset($_GET['matrice_id']) && $_GET['matrice_id'] !== '' ? (int)$_GET['matrice_id'] : null;
|
||||
$mescId = isset($_GET['mescola_id']) && $_GET['mescola_id'] !== '' ? (int)$_GET['mescola_id'] : null;
|
||||
|
||||
$where = ["pp.end_pause IS NOT NULL"];
|
||||
$params = [];
|
||||
if ($dateFrom !== '') {
|
||||
$where[] = "p.Data >= :date_from";
|
||||
$params['date_from'] = $dateFrom;
|
||||
}
|
||||
if ($dateTo !== '') {
|
||||
$where[] = "p.Data <= :date_to";
|
||||
$params['date_to'] = $dateTo;
|
||||
}
|
||||
if (!empty($lineArr)) {
|
||||
$where[] = "p.id_linea IN (" . implode(',', $lineArr) . ")";
|
||||
}
|
||||
if ($matId !== null) {
|
||||
$where[] = "p.idmatrice = :mat_id";
|
||||
$params['mat_id'] = $matId;
|
||||
}
|
||||
if ($mescId !== null) {
|
||||
$where[] = "EXISTS (SELECT 1 FROM productiondata_mescole pm
|
||||
WHERE pm.id_productiondata = p.id AND pm.id_mescola = :mesc_id)";
|
||||
$params['mesc_id'] = $mescId;
|
||||
}
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
try {
|
||||
$sqlKpi = "SELECT
|
||||
COUNT(*) AS n_pause, COUNT(DISTINCT pp.id_production) AS n_prod_con_pause,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS tot_s,
|
||||
COALESCE(AVG(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS media_s,
|
||||
COALESCE(MAX(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS max_s,
|
||||
COALESCE(SUM(CASE WHEN pr.is_problem=1 THEN 1 ELSE 0 END),0) AS n_problemi,
|
||||
COALESCE(SUM(CASE WHEN pr.category='planned' THEN TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause) ELSE 0 END),0) AS planned_s,
|
||||
COALESCE(SUM(CASE WHEN pr.category='unplanned' THEN TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause) ELSE 0 END),0) AS unplanned_s
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE $whereSql";
|
||||
$stmt = $pdo->prepare($sqlKpi);
|
||||
$stmt->execute($params);
|
||||
$kpi = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$sqlMotivo = "SELECT pr.id, pr.name AS motivo, pr.is_problem, pr.category,
|
||||
COUNT(pp.id) AS n_pause,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS durata_s,
|
||||
COALESCE(AVG(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS media_s
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE $whereSql GROUP BY pr.id ORDER BY durata_s DESC";
|
||||
$stmt = $pdo->prepare($sqlMotivo);
|
||||
$stmt->execute($params);
|
||||
$perMotivo = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$totDurata = array_sum(array_map(fn($x) => (int)$x['durata_s'], $perMotivo));
|
||||
$cum = 0;
|
||||
foreach ($perMotivo as &$m) {
|
||||
$cum += (int)$m['durata_s'];
|
||||
$m['cum_perc'] = ($totDurata > 0) ? round($cum / $totDurata * 100, 1) : 0;
|
||||
$m['perc'] = ($totDurata > 0) ? round($m['durata_s'] / $totDurata * 100, 1) : 0;
|
||||
}
|
||||
unset($m);
|
||||
|
||||
$sqlLinea = "SELECT l.name AS linea, l.color,
|
||||
COUNT(pp.id) AS n_pause,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS durata_s,
|
||||
COALESCE(SUM(CASE WHEN pr.is_problem=1 THEN 1 ELSE 0 END),0) AS n_problemi
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN production_lines l ON l.id = p.id_linea
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE $whereSql GROUP BY l.id ORDER BY durata_s DESC";
|
||||
$stmt = $pdo->prepare($sqlLinea);
|
||||
$stmt->execute($params);
|
||||
$perLinea = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$sqlGiorno = "SELECT p.Data AS giorno,
|
||||
COUNT(pp.id) AS n_pause,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS durata_s
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE $whereSql GROUP BY p.Data ORDER BY p.Data";
|
||||
$stmt = $pdo->prepare($sqlGiorno);
|
||||
$stmt->execute($params);
|
||||
$perGiorno = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$sqlDett = "SELECT pp.id, pp.start_pause, pp.end_pause, pp.note,
|
||||
TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause) AS durata_s,
|
||||
pr.name AS motivo, pr.is_problem, pr.category,
|
||||
l.name AS linea, m.nome AS matrice, p.id AS prod_id
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
LEFT JOIN production_lines l ON l.id = p.id_linea
|
||||
LEFT JOIN matrice m ON m.id = p.idmatrice
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE $whereSql ORDER BY pp.start_pause DESC LIMIT 500";
|
||||
$stmt = $pdo->prepare($sqlDett);
|
||||
$stmt->execute($params);
|
||||
$dettaglio = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'kpi' => [
|
||||
'n_pause' => (int)$kpi['n_pause'],
|
||||
'n_prod_con_pause' => (int)$kpi['n_prod_con_pause'],
|
||||
'tot_s' => (int)$kpi['tot_s'],
|
||||
'media_s' => (int)$kpi['media_s'],
|
||||
'max_s' => (int)$kpi['max_s'],
|
||||
'n_problemi' => (int)$kpi['n_problemi'],
|
||||
'planned_s' => (int)$kpi['planned_s'],
|
||||
'unplanned_s' => (int)$kpi['unplanned_s'],
|
||||
],
|
||||
'per_motivo' => $perMotivo,
|
||||
'per_linea' => $perLinea,
|
||||
'per_giorno' => $perGiorno,
|
||||
'dettaglio' => $dettaglio,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="it">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Statistiche Pause & Fermi</title>
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
|
||||
<style>
|
||||
.stats-header {
|
||||
background: linear-gradient(135deg, #b91c1c, #ef4444);
|
||||
color: #fff;
|
||||
padding: 1.1rem 1.4rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
border-radius: 1rem;
|
||||
flex-wrap: wrap;
|
||||
gap: .8rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.stats-header h4 {
|
||||
margin: 0;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.hbtn {
|
||||
background: rgba(255, 255, 255, .22);
|
||||
border: 1px solid rgba(255, 255, 255, .4);
|
||||
color: #fff;
|
||||
padding: .5rem 1rem;
|
||||
border-radius: .7rem;
|
||||
text-decoration: none;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.hbtn:hover {
|
||||
background: rgba(255, 255, 255, .34);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.hbtn.active {
|
||||
background: #fff;
|
||||
color: #b91c1c;
|
||||
}
|
||||
|
||||
.filters-bar {
|
||||
background: #fff;
|
||||
padding: 1rem 1.2rem;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
|
||||
gap: .9rem;
|
||||
align-items: end;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.filters-bar label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
color: #b91c1c;
|
||||
font-size: .8rem;
|
||||
margin-bottom: .3rem;
|
||||
}
|
||||
|
||||
.filters-bar input,
|
||||
.filters-bar select {
|
||||
width: 100%;
|
||||
padding: .55rem .7rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: .6rem;
|
||||
font-size: .95rem;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.line-buttons {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.line-btn2 {
|
||||
flex: 1;
|
||||
min-width: 70px;
|
||||
padding: .55rem .3rem;
|
||||
border: 2px solid transparent;
|
||||
border-radius: .6rem;
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
font-size: .9rem;
|
||||
opacity: .35;
|
||||
transition: .15s;
|
||||
}
|
||||
|
||||
.line-btn2.active {
|
||||
opacity: 1;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
|
||||
}
|
||||
|
||||
.filters-actions {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
}
|
||||
|
||||
.btn-apply {
|
||||
background: #dc2626;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: .6rem 1rem;
|
||||
border-radius: .6rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.btn-reset {
|
||||
background: #e2e8f0;
|
||||
color: #475569;
|
||||
border: none;
|
||||
padding: .6rem 1rem;
|
||||
border-radius: .6rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.kpi-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.kpi-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
border-left: 5px solid #ef4444;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-label {
|
||||
font-size: .72rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: .5px;
|
||||
color: #64748b;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-value {
|
||||
font-size: 1.7rem;
|
||||
font-weight: 800;
|
||||
color: #0f172a;
|
||||
margin-top: .2rem;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-sub {
|
||||
font-size: .8rem;
|
||||
color: #94a3b8;
|
||||
margin-top: .2rem;
|
||||
}
|
||||
|
||||
.kpi-card.amber {
|
||||
border-left-color: #f59e0b;
|
||||
}
|
||||
|
||||
.kpi-card.slate {
|
||||
border-left-color: #64748b;
|
||||
}
|
||||
|
||||
.kpi-card.green {
|
||||
border-left-color: #10b981;
|
||||
}
|
||||
|
||||
.kpi-card.red {
|
||||
border-left-color: #ef4444;
|
||||
}
|
||||
|
||||
.charts-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.chart-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
}
|
||||
|
||||
.chart-card.wide {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
.chart-card h6 {
|
||||
font-weight: 700;
|
||||
color: #1e293b;
|
||||
margin: 0 0 .8rem;
|
||||
font-size: .95rem;
|
||||
}
|
||||
|
||||
.chart-wrap {
|
||||
position: relative;
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.table-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
overflow-x: auto;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.table-card h6 {
|
||||
font-weight: 700;
|
||||
color: #1e293b;
|
||||
margin: 0 0 .8rem;
|
||||
}
|
||||
|
||||
table.stats-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: .85rem;
|
||||
}
|
||||
|
||||
table.stats-table th {
|
||||
background: #fee2e2;
|
||||
color: #991b1b;
|
||||
text-align: left;
|
||||
padding: .55rem .6rem;
|
||||
}
|
||||
|
||||
table.stats-table td {
|
||||
padding: .5rem .6rem;
|
||||
border-bottom: 1px solid #eef2f7;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
table.stats-table tr:hover td {
|
||||
background: #f8fafc;
|
||||
}
|
||||
|
||||
.pill {
|
||||
display: inline-block;
|
||||
padding: .15rem .55rem;
|
||||
border-radius: 999px;
|
||||
font-size: .72rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.pill-prob {
|
||||
background: #fee2e2;
|
||||
color: #991b1b;
|
||||
}
|
||||
|
||||
.pill-norm {
|
||||
background: #e0e7ff;
|
||||
color: #3730a3;
|
||||
}
|
||||
|
||||
.pill-plan {
|
||||
background: #fef3c7;
|
||||
color: #92400e;
|
||||
}
|
||||
|
||||
.pill-unplan {
|
||||
background: #fee2e2;
|
||||
color: #991b1b;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #94a3b8;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 3rem;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.empty {
|
||||
text-align: center;
|
||||
padding: 3rem;
|
||||
color: #94a3b8;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="wrapper toggled">
|
||||
<?php include('include/navbar.php'); ?>
|
||||
<?php include('include/topbar.php'); ?>
|
||||
|
||||
<div class="page-wrapper">
|
||||
<div class="page-content">
|
||||
|
||||
<div class="stats-header">
|
||||
<h4><i class="bi bi-pause-circle"></i> Statistiche Pause & Fermi</h4>
|
||||
<div class="header-actions">
|
||||
<a class="hbtn" href="production_stats.php"><i class="bi bi-speedometer2"></i> Produzione</a>
|
||||
<a class="hbtn active" href="pause_stats.php"><i class="bi bi-pause-circle"></i> Pause & Fermi</a>
|
||||
<a class="hbtn" href="production_dashboard.php">Torna</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters-bar">
|
||||
<div><label>Da (data)</label><input type="date" id="fDateFrom"></div>
|
||||
<div><label>A (data)</label><input type="date" id="fDateTo"></div>
|
||||
<div style="grid-column:span 2;">
|
||||
<label>Linee</label>
|
||||
<div class="line-buttons" id="lineButtons">
|
||||
<?php foreach ($linee as $l): ?>
|
||||
<button type="button" class="line-btn2 active" data-line="<?= (int)$l['id'] ?>"
|
||||
style="background:<?= htmlspecialchars($l['color'] ?: '#ef4444') ?>;">
|
||||
<?= htmlspecialchars($l['name']) ?>
|
||||
</button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div><label>Matrice</label><select id="fMatrice">
|
||||
<option value="">Tutte</option>
|
||||
<?php foreach ($matrici as $m): ?><option value="<?= (int)$m['id'] ?>"><?= htmlspecialchars($m['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div><label>Mescola</label><select id="fMescola">
|
||||
<option value="">Tutte</option>
|
||||
<?php foreach ($mescole as $me): ?><option value="<?= (int)$me['id'] ?>"><?= htmlspecialchars($me['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div class="filters-actions">
|
||||
<button class="btn-apply" id="btnApply"><i class="bi bi-funnel"></i> Applica</button>
|
||||
<button class="btn-reset" id="btnReset">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="kpi-grid" id="kpiGrid">
|
||||
<div class="loading">Caricamento…</div>
|
||||
</div>
|
||||
|
||||
<div class="charts-grid" id="chartsGrid" style="display:none;">
|
||||
<div class="chart-card wide">
|
||||
<h6>Pareto causali — tempo perso e cumulata</h6>
|
||||
<div class="chart-wrap"><canvas id="chartPareto"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Ripartizione tempo: pianificato vs non pianificato</h6>
|
||||
<div class="chart-wrap"><canvas id="chartCat"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Tempo perso per linea (ore)</h6>
|
||||
<div class="chart-wrap"><canvas id="chartLinea"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Numero fermi per causale</h6>
|
||||
<div class="chart-wrap"><canvas id="chartCount"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Andamento fermi giornaliero (ore)</h6>
|
||||
<div class="chart-wrap"><canvas id="chartGiorno"></canvas></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-card" id="tableCard" style="display:none;">
|
||||
<h6>Dettaglio eventi di pausa</h6>
|
||||
<table class="stats-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Inizio</th>
|
||||
<th>Fine</th>
|
||||
<th>Durata</th>
|
||||
<th>Motivo</th>
|
||||
<th>Tipo</th>
|
||||
<th>Categoria</th>
|
||||
<th>Linea</th>
|
||||
<th>Matrice</th>
|
||||
<th>Prod. #</th>
|
||||
<th>Nota</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="tableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div><!-- /.page-content -->
|
||||
</div><!-- /.page-wrapper -->
|
||||
</div><!-- /.wrapper -->
|
||||
|
||||
<?php include('jsinclude.php'); ?>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
|
||||
<script>
|
||||
function secToHms(s) {
|
||||
s = Math.max(0, parseInt(s || 0, 10));
|
||||
const h = String(Math.floor(s / 3600)).padStart(2, '0'),
|
||||
m = String(Math.floor((s % 3600) / 60)).padStart(2, '0'),
|
||||
x = String(s % 60).padStart(2, '0');
|
||||
return `${h}:${m}:${x}`;
|
||||
}
|
||||
|
||||
function secToH(s) {
|
||||
return parseInt(s || 0, 10) / 3600;
|
||||
}
|
||||
|
||||
function fmtDT(iso) {
|
||||
if (!iso) return '-';
|
||||
const d = new Date(iso.replace(' ', 'T'));
|
||||
if (isNaN(d)) return iso;
|
||||
const p = n => String(n).padStart(2, '0');
|
||||
return `${p(d.getDate())}/${p(d.getMonth()+1)} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
||||
}
|
||||
|
||||
function fmtDateIt(iso) {
|
||||
if (!iso) return '-';
|
||||
const p = iso.split('-');
|
||||
return p.length === 3 ? `${p[2]}/${p[1]}/${p[0]}` : iso;
|
||||
}
|
||||
let charts = {};
|
||||
|
||||
function destroyChart(k) {
|
||||
if (charts[k]) {
|
||||
charts[k].destroy();
|
||||
charts[k] = null;
|
||||
}
|
||||
}
|
||||
|
||||
function renderKpi(k) {
|
||||
const pctProblemi = k.n_pause > 0 ? Math.round(k.n_problemi / k.n_pause * 100) : 0;
|
||||
$('#kpiGrid').html(`
|
||||
<div class="kpi-card"><div class="kpi-label">Fermi totali</div><div class="kpi-value">${k.n_pause}</div><div class="kpi-sub">${k.n_prod_con_pause} produzioni coinvolte</div></div>
|
||||
<div class="kpi-card"><div class="kpi-label">Tempo perso</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.tot_s)}</div><div class="kpi-sub">totale fermi</div></div>
|
||||
<div class="kpi-card slate"><div class="kpi-label">Durata media</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.media_s)}</div><div class="kpi-sub">max ${secToHms(k.max_s)}</div></div>
|
||||
<div class="kpi-card red"><div class="kpi-label">Problemi tecnici</div><div class="kpi-value">${k.n_problemi}</div><div class="kpi-sub">${pctProblemi}% dei fermi</div></div>
|
||||
<div class="kpi-card green"><div class="kpi-label">Pianificato</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.planned_s)}</div><div class="kpi-sub">fermi previsti</div></div>
|
||||
<div class="kpi-card amber"><div class="kpi-label">Non pianificato</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.unplanned_s)}</div><div class="kpi-sub">da ridurre</div></div>
|
||||
`);
|
||||
}
|
||||
|
||||
function renderCharts(d) {
|
||||
const mot = d.per_motivo || [],
|
||||
linee = d.per_linea || [],
|
||||
giorni = d.per_giorno || [],
|
||||
k = d.kpi;
|
||||
destroyChart('pareto');
|
||||
charts.pareto = new Chart($('#chartPareto'), {
|
||||
data: {
|
||||
labels: mot.map(x => x.motivo),
|
||||
datasets: [{
|
||||
type: 'bar',
|
||||
label: 'Ore perse',
|
||||
data: mot.map(x => +secToH(x.durata_s).toFixed(2)),
|
||||
backgroundColor: mot.map(x => x.category === 'planned' ? '#f59e0b' : '#ef4444'),
|
||||
yAxisID: 'y'
|
||||
},
|
||||
{
|
||||
type: 'line',
|
||||
label: 'Cumulata %',
|
||||
data: mot.map(x => x.cum_perc),
|
||||
borderColor: '#1e293b',
|
||||
backgroundColor: '#1e293b',
|
||||
yAxisID: 'y1',
|
||||
tension: .2,
|
||||
pointRadius: 3
|
||||
}
|
||||
]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
position: 'left',
|
||||
title: {
|
||||
display: true,
|
||||
text: 'Ore'
|
||||
}
|
||||
},
|
||||
y1: {
|
||||
beginAtZero: true,
|
||||
max: 100,
|
||||
position: 'right',
|
||||
grid: {
|
||||
drawOnChartArea: false
|
||||
},
|
||||
title: {
|
||||
display: true,
|
||||
text: '% cumulata'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
destroyChart('cat');
|
||||
charts.cat = new Chart($('#chartCat'), {
|
||||
type: 'doughnut',
|
||||
data: {
|
||||
labels: ['Pianificato', 'Non pianificato'],
|
||||
datasets: [{
|
||||
data: [+secToH(k.planned_s).toFixed(2), +secToH(k.unplanned_s).toFixed(2)],
|
||||
backgroundColor: ['#f59e0b', '#ef4444']
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
plugins: {
|
||||
legend: {
|
||||
position: 'bottom'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
destroyChart('linea');
|
||||
charts.linea = new Chart($('#chartLinea'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: linee.map(x => x.linea),
|
||||
datasets: [{
|
||||
label: 'Ore perse',
|
||||
data: linee.map(x => +secToH(x.durata_s).toFixed(2)),
|
||||
backgroundColor: linee.map(x => x.color || '#3b82f6')
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
destroyChart('count');
|
||||
charts.count = new Chart($('#chartCount'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: mot.map(x => x.motivo),
|
||||
datasets: [{
|
||||
label: 'N. fermi',
|
||||
data: mot.map(x => x.n_pause),
|
||||
backgroundColor: '#64748b'
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
indexAxis: 'y',
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
x: {
|
||||
beginAtZero: true
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
destroyChart('giorno');
|
||||
charts.giorno = new Chart($('#chartGiorno'), {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: giorni.map(x => fmtDateIt(x.giorno)),
|
||||
datasets: [{
|
||||
label: 'Ore perse',
|
||||
data: giorni.map(x => +secToH(x.durata_s).toFixed(2)),
|
||||
borderColor: '#ef4444',
|
||||
backgroundColor: 'rgba(239,68,68,.15)',
|
||||
fill: true,
|
||||
tension: .3
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderTable(rows) {
|
||||
if (!rows || !rows.length) {
|
||||
$('#tableBody').html('<tr><td colspan="10" class="empty">Nessun fermo per i filtri selezionati</td></tr>');
|
||||
return;
|
||||
}
|
||||
let html = '';
|
||||
rows.forEach(r => {
|
||||
const tipo = (String(r.is_problem) === '1') ? '<span class="pill pill-prob">Problema</span>' : '<span class="pill pill-norm">Normale</span>';
|
||||
const cat = (r.category === 'planned') ? '<span class="pill pill-plan">Pianificato</span>' : '<span class="pill pill-unplan">Non pian.</span>';
|
||||
html += `<tr><td>${fmtDT(r.start_pause)}</td><td>${fmtDT(r.end_pause)}</td><td>${secToHms(r.durata_s)}</td>
|
||||
<td>${r.motivo||'-'}</td><td>${tipo}</td><td>${cat}</td>
|
||||
<td>${r.linea||'-'}</td><td>${r.matrice||'-'}</td><td>#${r.prod_id}</td>
|
||||
<td>${r.note?('<span class="muted">'+r.note+'</span>'):'<span class="muted">-</span>'}</td></tr>`;
|
||||
});
|
||||
$('#tableBody').html(html);
|
||||
}
|
||||
|
||||
function selectedLines() {
|
||||
return $('#lineButtons .line-btn2.active').map(function() {
|
||||
return $(this).data('line');
|
||||
}).get().join(',');
|
||||
}
|
||||
|
||||
function loadStats() {
|
||||
$('#kpiGrid').html('<div class="loading">Caricamento…</div>');
|
||||
$('#chartsGrid, #tableCard').hide();
|
||||
$.getJSON('', {
|
||||
ajax: 1,
|
||||
date_from: $('#fDateFrom').val(),
|
||||
date_to: $('#fDateTo').val(),
|
||||
lines: selectedLines(),
|
||||
matrice_id: $('#fMatrice').val(),
|
||||
mescola_id: $('#fMescola').val()
|
||||
},
|
||||
function(r) {
|
||||
if (!r.success) {
|
||||
$('#kpiGrid').html('<div class="empty">Errore: ' + (r.msg || '') + '</div>');
|
||||
return;
|
||||
}
|
||||
renderKpi(r.kpi);
|
||||
if (r.kpi.n_pause === 0) {
|
||||
$('#chartsGrid, #tableCard').hide();
|
||||
$('#tableBody').html('');
|
||||
return;
|
||||
}
|
||||
$('#chartsGrid, #tableCard').show();
|
||||
renderCharts(r);
|
||||
renderTable(r.dettaglio);
|
||||
}).fail(function() {
|
||||
$('#kpiGrid').html('<div class="empty">Errore di comunicazione con il server.</div>');
|
||||
});
|
||||
}
|
||||
$(function() {
|
||||
const today = new Date(),
|
||||
past = new Date();
|
||||
past.setDate(today.getDate() - 30);
|
||||
$('#fDateTo').val(today.toISOString().slice(0, 10));
|
||||
$('#fDateFrom').val(past.toISOString().slice(0, 10));
|
||||
$('#lineButtons').on('click', '.line-btn2', function() {
|
||||
$(this).toggleClass('active');
|
||||
if ($('#lineButtons .line-btn2.active').length === 0) {
|
||||
$('#lineButtons .line-btn2').addClass('active');
|
||||
}
|
||||
loadStats();
|
||||
});
|
||||
$('#btnApply').on('click', loadStats);
|
||||
$('#btnReset').on('click', function() {
|
||||
$('#lineButtons .line-btn2').addClass('active');
|
||||
$('#fMatrice,#fMescola').val('');
|
||||
$('#fDateTo').val(today.toISOString().slice(0, 10));
|
||||
$('#fDateFrom').val(past.toISOString().slice(0, 10));
|
||||
loadStats();
|
||||
});
|
||||
loadStats();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
@@ -23,12 +23,42 @@ $dashboardSections = [
|
||||
'permission' => 'production.line_view.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Statistiche',
|
||||
'label' => 'Magazzino',
|
||||
'icon' => '📦',
|
||||
'class' => 'btn-magazzino',
|
||||
'url' => 'warehouse_dashboard.php',
|
||||
'permission' => 'warehouse.dashboard.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Smart-Alert',
|
||||
'icon' => '⏰',
|
||||
'class' => 'btn-scadenziario',
|
||||
'url' => 'scadenzario/index.php',
|
||||
'permission' => 'deadlines.view',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'id' => 'secDirezione',
|
||||
'title' => 'Direzione',
|
||||
'subtitle' => 'Statistiche, analisi fermi e strumenti di supervisione',
|
||||
'icon' => '📊',
|
||||
'open' => false,
|
||||
'buttons' => [
|
||||
[
|
||||
'label' => 'Statistiche Produzione',
|
||||
'icon' => '📈',
|
||||
'class' => 'btn-statistiche',
|
||||
'url' => 'production_stats.php',
|
||||
'permission' => 'production.stats.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Pause & Fermi',
|
||||
'icon' => '🛑',
|
||||
'class' => 'btn-pause-stats',
|
||||
'url' => 'pause_stats.php',
|
||||
'permission' => 'production.stats.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Manager',
|
||||
'icon' => '👔',
|
||||
@@ -43,20 +73,6 @@ $dashboardSections = [
|
||||
'url' => 'manager_stats.php',
|
||||
'permission' => 'production.manager_stats.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Magazzino',
|
||||
'icon' => '📦',
|
||||
'class' => 'btn-magazzino',
|
||||
'url' => 'warehouse_dashboard.php',
|
||||
'permission' => 'warehouse.dashboard.view',
|
||||
],
|
||||
[
|
||||
'label' => 'Smart-Alert',
|
||||
'icon' => '⏰',
|
||||
'class' => 'btn-scadenziario',
|
||||
'url' => 'scadenzario/index.php',
|
||||
'permission' => 'deadlines.view',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
@@ -421,6 +437,11 @@ $dashboardSections = [
|
||||
background: linear-gradient(135deg, #d2f7d9, #e7ffea);
|
||||
}
|
||||
|
||||
.btn-pause-stats {
|
||||
background: linear-gradient(135deg, #fca5a5, #fecaca);
|
||||
color: #7f1d1d;
|
||||
}
|
||||
|
||||
.btn-magazzino {
|
||||
background: linear-gradient(135deg, #b9e3ffff, #d7f1ffff);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ $statusPausaId = $statusPausa['id'];
|
||||
// --- PAUSE REASONS ---
|
||||
$pauseReasons = $pdo->query("SELECT id, name, is_problem FROM pause_reasons ORDER BY name")->fetchAll();
|
||||
|
||||
// --- DATI PER MODALE "NUOVO" (riga libera) ---
|
||||
$allMatrici = $pdo->query("SELECT id, nome FROM matrice ORDER BY nome")->fetchAll();
|
||||
$allMescole = $pdo->query("SELECT id, nome FROM mescole WHERE is_active = 1 ORDER BY nome")->fetchAll();
|
||||
|
||||
|
||||
// --- AVVIO / RIPRESA PRODUZIONE ---
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['start_production'])) {
|
||||
@@ -62,6 +66,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['start_production']))
|
||||
// 3) Ripresa: da PAUSA (7) → PRODUZIONE (2) **senza toccare start_time**
|
||||
} elseif ((int)$currentStatus === (int)$statusPausaId) {
|
||||
|
||||
// Chiudo l'ultima pausa aperta di questa produzione
|
||||
$sqlClose = "UPDATE production_pauses
|
||||
SET end_pause = UTC_TIMESTAMP()
|
||||
WHERE id_production = :id AND end_pause IS NULL
|
||||
ORDER BY start_pause DESC
|
||||
LIMIT 1";
|
||||
$stmtClose = $pdo->prepare($sqlClose);
|
||||
$stmtClose->execute(['id' => $id]);
|
||||
|
||||
$sqlUpdate = "UPDATE productiondata
|
||||
SET id_status = :status,
|
||||
end_time = NULL
|
||||
@@ -162,36 +175,61 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['stop_production']))
|
||||
$reason = $_POST['reason'] ?? 'fine';
|
||||
$note = trim($_POST['note'] ?? '');
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Se sto stoppando mentre è in pausa, chiudo la pausa aperta
|
||||
$sqlClose = "UPDATE production_pauses
|
||||
SET end_pause = UTC_TIMESTAMP()
|
||||
WHERE id_production = :id AND end_pause IS NULL";
|
||||
$pdo->prepare($sqlClose)->execute(['id' => $id]);
|
||||
|
||||
// Leggo dati produzione
|
||||
$sql = "SELECT start_time, id_status FROM productiondata WHERE id = :id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute(['id' => $id]);
|
||||
$row = $stmt->fetch();
|
||||
$seconds = 0;
|
||||
if ($row && $row['start_time'] && $row['id_status'] == $statusProduzioneId) {
|
||||
|
||||
// Tempo di occupazione linea (LORDO, pause incluse): start -> ora
|
||||
$occupatoSec = 0;
|
||||
if ($row && $row['start_time']) {
|
||||
$start = new DateTime($row['start_time'], new DateTimeZone('UTC'));
|
||||
$now = new DateTime('now', new DateTimeZone('UTC'));
|
||||
$seconds = $now->getTimestamp() - $start->getTimestamp();
|
||||
$occupatoSec = max(0, $now->getTimestamp() - $start->getTimestamp());
|
||||
}
|
||||
|
||||
// Somma totale delle pause di questa produzione (in secondi)
|
||||
$sqlPause = "SELECT COALESCE(SUM(TIMESTAMPDIFF(SECOND, start_pause, end_pause)), 0)
|
||||
FROM production_pauses
|
||||
WHERE id_production = :id AND end_pause IS NOT NULL";
|
||||
$stmtP = $pdo->prepare($sqlPause);
|
||||
$stmtP->execute(['id' => $id]);
|
||||
$pauseSec = (int)$stmtP->fetchColumn();
|
||||
|
||||
// Stato di destinazione
|
||||
$nextStatus = ($reason === 'problema') ? 8 : $statusQualita; // 8 = Problema in produzione
|
||||
|
||||
$sql = "UPDATE productiondata
|
||||
SET id_status = :status,
|
||||
end_time = UTC_TIMESTAMP(),
|
||||
note_operatore = :note
|
||||
WHERE id = :id AND id_status IN ($statusProduzioneId, $statusPausaId)";
|
||||
SET id_status = :status,
|
||||
end_time = UTC_TIMESTAMP(),
|
||||
note_operatore = :note,
|
||||
tempo_totale_produzione = :occupato,
|
||||
seconds_pause = :pause,
|
||||
seconds_produzione = :netto
|
||||
WHERE id = :id AND id_status IN ($statusProduzioneId, $statusPausaId)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
'status' => $nextStatus,
|
||||
'seconds' => $seconds,
|
||||
'note' => $note ?: null,
|
||||
'id' => $id,
|
||||
'produzione' => $statusProduzioneId,
|
||||
'pausa' => $statusPausaId
|
||||
'status' => $nextStatus,
|
||||
'note' => $note ?: null,
|
||||
'occupato' => $occupatoSec,
|
||||
'pause' => $pauseSec,
|
||||
'netto' => max(0, $occupatoSec - $pauseSec),
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) $pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
@@ -208,11 +246,34 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['save_final_data']))
|
||||
$reason = $_POST['reason'] ?? 'fine';
|
||||
|
||||
try {
|
||||
// Recupero tempo totale già calcolato
|
||||
$sql = "SELECT tempo_totale_produzione FROM productiondata WHERE id = :id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Chiudo eventuali pause ancora aperte
|
||||
$pdo->prepare("UPDATE production_pauses SET end_pause = UTC_TIMESTAMP()
|
||||
WHERE id_production = :id AND end_pause IS NULL")
|
||||
->execute(['id' => $id]);
|
||||
|
||||
// Leggo start_time per calcolare l'occupazione
|
||||
$stmt = $pdo->prepare("SELECT start_time FROM productiondata WHERE id = :id");
|
||||
$stmt->execute(['id' => $id]);
|
||||
$seconds = (int)$stmt->fetchColumn();
|
||||
$startTime = $stmt->fetchColumn();
|
||||
|
||||
// Occupazione LORDA (start -> ora), pause incluse
|
||||
$occupatoSec = 0;
|
||||
if ($startTime) {
|
||||
$start = new DateTime($startTime, new DateTimeZone('UTC'));
|
||||
$now = new DateTime('now', new DateTimeZone('UTC'));
|
||||
$occupatoSec = max(0, $now->getTimestamp() - $start->getTimestamp());
|
||||
}
|
||||
|
||||
// Somma pause chiuse
|
||||
$stmtP = $pdo->prepare("SELECT COALESCE(SUM(TIMESTAMPDIFF(SECOND, start_pause, end_pause)), 0)
|
||||
FROM production_pauses
|
||||
WHERE id_production = :id AND end_pause IS NOT NULL");
|
||||
$stmtP->execute(['id' => $id]);
|
||||
$pauseSec = (int)$stmtP->fetchColumn();
|
||||
|
||||
$seconds = $occupatoSec; // hourprod = tempo di occupazione (come da tua logica)
|
||||
|
||||
// Calcolo scarto %
|
||||
$totaleKg = $kgprod + $scarto;
|
||||
@@ -227,14 +288,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['save_final_data']))
|
||||
|
||||
// Update finale
|
||||
$sql = "UPDATE productiondata
|
||||
SET kgprod = :kgprod,
|
||||
mtprod = :mtprod,
|
||||
scarto = :scarto,
|
||||
scarto_perc = :scarto_perc,
|
||||
note_operatore = :note,
|
||||
hourprod = SEC_TO_TIME(:seconds),
|
||||
id_status = :status,
|
||||
end_time = COALESCE(end_time, UTC_TIMESTAMP())
|
||||
SET kgprod = :kgprod,
|
||||
mtprod = :mtprod,
|
||||
scarto = :scarto,
|
||||
scarto_perc = :scarto_perc,
|
||||
note_operatore = :note,
|
||||
hourprod = SEC_TO_TIME(:seconds),
|
||||
tempo_totale_produzione = :occupato,
|
||||
seconds_pause = :pause,
|
||||
seconds_produzione = :netto,
|
||||
id_status = :status,
|
||||
end_time = COALESCE(end_time, UTC_TIMESTAMP())
|
||||
WHERE id = :id";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
@@ -245,12 +309,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['save_final_data']))
|
||||
'scarto_perc' => $scartoPerc,
|
||||
'note' => $note,
|
||||
'seconds' => $seconds,
|
||||
'occupato' => $occupatoSec,
|
||||
'pause' => $pauseSec,
|
||||
'netto' => max(0, $occupatoSec - $pauseSec),
|
||||
'status' => $nextStatus,
|
||||
'id' => $id
|
||||
]);
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) $pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
@@ -258,12 +327,91 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['save_final_data']))
|
||||
|
||||
|
||||
|
||||
// --- CREAZIONE RIGA LIBERA (Nuovo) + avvio immediato ---
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['create_quick'])) {
|
||||
$idMatrice = (int)($_POST['id_matrice'] ?? 0);
|
||||
$idLinea = (int)($_POST['id_linea'] ?? 0);
|
||||
$mescRaw = $_POST['id_mescole'] ?? '';
|
||||
$mescIds = $mescRaw !== '' ? array_values(array_unique(array_filter(array_map('intval', explode(',', $mescRaw))))) : [];
|
||||
$descr = trim($_POST['descrizione'] ?? '');
|
||||
|
||||
try {
|
||||
if ($idMatrice <= 0) {
|
||||
echo json_encode(['success' => false, 'msg' => 'Seleziona una matrice.']);
|
||||
exit;
|
||||
}
|
||||
if ($idLinea <= 0) {
|
||||
echo json_encode(['success' => false, 'msg' => 'Seleziona una linea.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$avvia = !empty($_POST['avvia']);
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Priorità: in fondo alla coda della linea (max+1)
|
||||
$stmtPri = $pdo->prepare("SELECT COALESCE(MAX(priority),0)+1
|
||||
FROM productiondata
|
||||
WHERE id_linea = :linea AND id_status = :programmato");
|
||||
$stmtPri->execute(['linea' => $idLinea, 'programmato' => $statusProgrammatoId]);
|
||||
$nextPriority = (int)$stmtPri->fetchColumn();
|
||||
|
||||
// Se "avvia": nasce in PRODUZIONE (2) con start_time.
|
||||
// Altrimenti: PROGRAMMATO (6), pronto per essere avviato dopo.
|
||||
$statusIns = $avvia ? $statusProduzioneId : $statusProgrammatoId;
|
||||
$startExpr = $avvia ? 'UTC_TIMESTAMP()' : 'NULL';
|
||||
|
||||
$sqlIns = "INSERT INTO productiondata
|
||||
(Data, idmatrice, id_linea, note_extra, priority,
|
||||
id_status, start_time, end_time,
|
||||
fermomacchina, hour, hourprod, kgprod, mtprod, scarto)
|
||||
VALUES
|
||||
(CURDATE(), :idmatrice, :id_linea, :note_extra, :priority,
|
||||
:status, $startExpr, NULL,
|
||||
0, '00:00:00', '00:00:00', 0, 0, 0)";
|
||||
$stmtIns = $pdo->prepare($sqlIns);
|
||||
$stmtIns->execute([
|
||||
'idmatrice' => $idMatrice,
|
||||
'id_linea' => $idLinea,
|
||||
'note_extra' => $descr ?: null,
|
||||
'priority' => $nextPriority,
|
||||
'status' => $statusIns,
|
||||
]);
|
||||
|
||||
$newId = (int)$pdo->lastInsertId();
|
||||
|
||||
if (!empty($mescIds)) {
|
||||
$stmtM = $pdo->prepare("INSERT INTO productiondata_mescole (id_productiondata, id_mescola) VALUES (:pid, :mid)");
|
||||
foreach ($mescIds as $mid) {
|
||||
if ($mid > 0) {
|
||||
$stmtM->execute(['pid' => $newId, 'mid' => $mid]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(['success' => true, 'id' => $newId]);
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) $pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
// --- AJAX PRINCIPALE: carica i record della data selezionata ---
|
||||
if (!empty($_GET['ajax'])) {
|
||||
|
||||
$lineRaw = $_GET['line'] ?? '';
|
||||
$lineArray = $lineRaw !== '' ? explode(',', $lineRaw) : [];
|
||||
|
||||
// Linee già occupate da una produzione attiva (in produzione=2 o in pausa=7).
|
||||
// Usato per bloccare l'avvio di una seconda produzione sulla stessa linea.
|
||||
$lineeOccupate = [];
|
||||
$stmtOcc = $pdo->query("SELECT DISTINCT id_linea FROM productiondata WHERE id_status IN (2, 7)");
|
||||
foreach ($stmtOcc->fetchAll(PDO::FETCH_COLUMN) as $lid) {
|
||||
$lineeOccupate[(int)$lid] = true;
|
||||
}
|
||||
|
||||
// --- RECORD IN PRODUZIONE (2,7,8)
|
||||
// --- RECORD IN PRODUZIONE (2,7,8)
|
||||
$sql = "SELECT
|
||||
@@ -396,6 +544,53 @@ if (!empty($_GET['ajax'])) {
|
||||
<title>Linea Produzione - Tablet</title>
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet">
|
||||
<style>
|
||||
/* Select2 dentro il modale + z-index sopra l'overlay */
|
||||
.select2-container {
|
||||
width: 100% !important;
|
||||
}
|
||||
|
||||
.select2-container--default .select2-selection--single {
|
||||
height: auto;
|
||||
padding: 0.6rem 0.6rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.6rem;
|
||||
font-size: 1.15rem;
|
||||
}
|
||||
|
||||
.select2-container--default .select2-selection--single .select2-selection__arrow {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.select2-dropdown {
|
||||
z-index: 2000;
|
||||
}
|
||||
|
||||
.select2-container--open {
|
||||
z-index: 2000;
|
||||
}
|
||||
|
||||
/* Select2 MULTIPLO: chip grandi e comodi per tablet */
|
||||
.select2-container--default .select2-selection--multiple {
|
||||
min-height: 3.2rem;
|
||||
padding: 0.3rem 0.5rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.6rem;
|
||||
}
|
||||
|
||||
.select2-container--default .select2-selection--multiple .select2-selection__choice {
|
||||
font-size: 1.05rem;
|
||||
padding: 0.35rem 0.7rem;
|
||||
border-radius: 0.5rem;
|
||||
margin-top: 0.35rem;
|
||||
}
|
||||
|
||||
.select2-container--default .select2-results__option {
|
||||
font-size: 1.1rem;
|
||||
padding: 0.7rem 0.9rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<style>
|
||||
* {
|
||||
@@ -1124,7 +1319,13 @@ if (!empty($_GET['ajax'])) {
|
||||
<div class="card-header">
|
||||
<h5 style="color:#ffffff;">Linea di Produzione</h5>
|
||||
|
||||
<button class="back-btn" onclick="location.href='production_dashboard.php'">Torna</button>
|
||||
<div style="display:flex; gap:0.8rem; align-items:center;">
|
||||
<button class="back-btn" id="btnNuovoQuick"
|
||||
style="background:rgba(16,185,129,0.9); border-color:rgba(16,185,129,1);">
|
||||
+ Nuovo
|
||||
</button>
|
||||
<button class="back-btn" onclick="location.href='production_dashboard.php'">Torna</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters">
|
||||
@@ -1159,6 +1360,54 @@ if (!empty($_GET['ajax'])) {
|
||||
<?php include('include/footer.php'); ?>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<!-- MODALE NUOVO -->
|
||||
<div id="quickCreateModal" class="custom-modal">
|
||||
<div class="modal-content" style="max-width:900px; width:92%; text-align:left;">
|
||||
<h3 style="text-align:center;">Nuovo avvio rapido</h3>
|
||||
|
||||
<label style="font-weight:600; display:block; margin-top:1rem;">Descrizione</label>
|
||||
<input type="text" id="qcDescrizione" class="input-big" placeholder="Descrizione...">
|
||||
|
||||
<div style="display:flex; gap:1.2rem; flex-wrap:wrap;">
|
||||
<div style="flex:1; min-width:260px;">
|
||||
<label style="font-weight:600; display:block;">Matrice *</label>
|
||||
<select id="qcMatrice" class="input-big">
|
||||
<option value="">Seleziona matrice</option>
|
||||
<?php foreach ($allMatrici as $mx): ?>
|
||||
<option value="<?= (int)$mx['id'] ?>"><?= htmlspecialchars($mx['nome']) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div style="flex:1; min-width:260px;">
|
||||
<label style="font-weight:600; display:block;">Linea *</label>
|
||||
<select id="qcLinea" class="input-big">
|
||||
<option value="">Seleziona linea</option>
|
||||
<?php foreach ($linee as $l): ?>
|
||||
<option value="<?= (int)$l['id'] ?>"><?= htmlspecialchars($l['name']) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label style="font-weight:600; display:block;">Mescole (una o più, opzionale)</label>
|
||||
<select id="qcMescola" class="input-big" multiple>
|
||||
<?php foreach ($allMescole as $me): ?>
|
||||
<option value="<?= (int)$me['id'] ?>"><?= htmlspecialchars($me['nome']) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<div class="modal-buttons" style="flex-wrap:wrap;">
|
||||
<button class="modal-btn modal-cancel" id="qcCancel">Annulla</button>
|
||||
<button class="modal-btn modal-confirm" id="qcCreateQueue"
|
||||
style="background:#3b82f6;">Crea in coda</button>
|
||||
<button class="modal-btn modal-confirm" id="qcCreateStart">Crea e avvia</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Notifica sonora -->
|
||||
<audio id="beepSound" src="beep.wav" preload="auto"></audio>
|
||||
|
||||
@@ -1271,6 +1520,7 @@ if (!empty($_GET['ajax'])) {
|
||||
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||
<script>
|
||||
function showPhotoSuccess() {
|
||||
$("#photoMessageError").hide();
|
||||
@@ -1998,13 +2248,9 @@ if (!empty($_GET['ajax'])) {
|
||||
break;
|
||||
|
||||
case 'stop':
|
||||
if (reason === 'fine') {
|
||||
// Apertura modale dati finali
|
||||
openFinalDataModal(id, reason, note);
|
||||
} else {
|
||||
// Problema → stop diretto
|
||||
stopProduction(id, reason, note);
|
||||
}
|
||||
// Sia "fine" che "problema" aprono il modale dati finali.
|
||||
// Il reason viene passato e conservato per il salvataggio.
|
||||
openFinalDataModal(id, reason, note);
|
||||
break;
|
||||
}
|
||||
});
|
||||
@@ -2166,6 +2412,94 @@ if (!empty($_GET['ajax'])) {
|
||||
|
||||
|
||||
|
||||
// --- NUOVO AVVIO RAPIDO ---
|
||||
function initQuickSelects() {
|
||||
$('#qcMatrice').select2({
|
||||
dropdownParent: $('#quickCreateModal .modal-content'),
|
||||
placeholder: 'Cerca matrice...',
|
||||
allowClear: true,
|
||||
width: '100%'
|
||||
});
|
||||
$('#qcMescola').select2({
|
||||
dropdownParent: $('#quickCreateModal .modal-content'),
|
||||
placeholder: 'Cerca e aggiungi mescole...',
|
||||
allowClear: true,
|
||||
width: '100%',
|
||||
closeOnSelect: false
|
||||
});
|
||||
}
|
||||
|
||||
function destroyQuickSelects() {
|
||||
if ($('#qcMatrice').data('select2')) $('#qcMatrice').select2('destroy');
|
||||
if ($('#qcMescola').data('select2')) $('#qcMescola').select2('destroy');
|
||||
}
|
||||
|
||||
function closeQuickModal() {
|
||||
destroyQuickSelects();
|
||||
$('#quickCreateModal').removeClass('active');
|
||||
}
|
||||
|
||||
$('#btnNuovoQuick').on('click', function() {
|
||||
$('#qcDescrizione').val('');
|
||||
$('#qcMatrice').val('');
|
||||
$('#qcMescola').val(null);
|
||||
$('#qcLinea').val('');
|
||||
$('#quickCreateModal').addClass('active');
|
||||
initQuickSelects();
|
||||
});
|
||||
|
||||
$('#qcCancel').on('click', closeQuickModal);
|
||||
|
||||
$(document).on('click', '#quickCreateModal', function(e) {
|
||||
if ($(e.target).is('#quickCreateModal')) closeQuickModal();
|
||||
});
|
||||
|
||||
function submitQuickCreate(avvia) {
|
||||
const idMatrice = $('#qcMatrice').val();
|
||||
const idLinea = $('#qcLinea').val();
|
||||
const mescoleArr = $('#qcMescola').val() || []; // array di id
|
||||
const descr = $('#qcDescrizione').val().trim();
|
||||
|
||||
if (!idMatrice) {
|
||||
alert('Seleziona una matrice.');
|
||||
return;
|
||||
}
|
||||
if (!idLinea) {
|
||||
alert('Seleziona una linea.');
|
||||
return;
|
||||
}
|
||||
|
||||
$('#qcCreateQueue, #qcCreateStart').prop('disabled', true);
|
||||
|
||||
$.post('', {
|
||||
create_quick: 1,
|
||||
avvia: avvia ? 1 : 0,
|
||||
id_matrice: idMatrice,
|
||||
id_linea: idLinea,
|
||||
id_mescole: mescoleArr.join(','), // CSV di id
|
||||
descrizione: descr
|
||||
}, function(r) {
|
||||
$('#qcCreateQueue, #qcCreateStart').prop('disabled', false);
|
||||
if (r.success) {
|
||||
closeQuickModal();
|
||||
playBeep();
|
||||
loadRecords();
|
||||
} else {
|
||||
alert(r.msg || 'Errore durante la creazione.');
|
||||
}
|
||||
}, 'json').fail(function() {
|
||||
$('#qcCreateQueue, #qcCreateStart').prop('disabled', false);
|
||||
alert('Errore di comunicazione con il server.');
|
||||
});
|
||||
}
|
||||
|
||||
$('#qcCreateQueue').on('click', function() {
|
||||
submitQuickCreate(false);
|
||||
});
|
||||
$('#qcCreateStart').on('click', function() {
|
||||
submitQuickCreate(true);
|
||||
});
|
||||
|
||||
loadRecords();
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,929 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
include('include/headscript.php');
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
/* 2=produzione, 3=qualità(fine ok), 7=pausa, 8=problema. "Concluse"=3,8 */
|
||||
$STATI_CONCLUSI = [3, 8];
|
||||
|
||||
$linee = $pdo->query("SELECT id, name, color, max_hours_per_day FROM production_lines ORDER BY line_number")->fetchAll();
|
||||
$matrici = $pdo->query("SELECT id, nome FROM matrice ORDER BY nome")->fetchAll();
|
||||
$mescole = $pdo->query("SELECT id, nome FROM mescole ORDER BY nome")->fetchAll();
|
||||
|
||||
if (!empty($_GET['ajax'])) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$dateFrom = $_GET['date_from'] ?? '';
|
||||
$dateTo = $_GET['date_to'] ?? '';
|
||||
// linee multiple (bottoni): CSV di id. Vuoto = tutte.
|
||||
$lineRaw = $_GET['lines'] ?? '';
|
||||
$lineArr = $lineRaw !== '' ? array_values(array_filter(array_map('intval', explode(',', $lineRaw)))) : [];
|
||||
$matId = isset($_GET['matrice_id']) && $_GET['matrice_id'] !== '' ? (int)$_GET['matrice_id'] : null;
|
||||
$mescId = isset($_GET['mescola_id']) && $_GET['mescola_id'] !== '' ? (int)$_GET['mescola_id'] : null;
|
||||
$granul = ($_GET['granularity'] ?? 'day') === 'month' ? 'month' : 'day';
|
||||
|
||||
$where = ["p.id_status IN (" . implode(',', $STATI_CONCLUSI) . ")"];
|
||||
$params = [];
|
||||
if ($dateFrom !== '') {
|
||||
$where[] = "p.Data >= :date_from";
|
||||
$params['date_from'] = $dateFrom;
|
||||
}
|
||||
if ($dateTo !== '') {
|
||||
$where[] = "p.Data <= :date_to";
|
||||
$params['date_to'] = $dateTo;
|
||||
}
|
||||
if (!empty($lineArr)) {
|
||||
$where[] = "p.id_linea IN (" . implode(',', $lineArr) . ")";
|
||||
}
|
||||
if ($matId !== null) {
|
||||
$where[] = "p.idmatrice = :mat_id";
|
||||
$params['mat_id'] = $matId;
|
||||
}
|
||||
if ($mescId !== null) {
|
||||
$where[] = "EXISTS (SELECT 1 FROM productiondata_mescole pm
|
||||
WHERE pm.id_productiondata = p.id AND pm.id_mescola = :mesc_id)";
|
||||
$params['mesc_id'] = $mescId;
|
||||
}
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
try {
|
||||
/* ---------- KPI ---------- */
|
||||
$sqlKpi = "SELECT
|
||||
COUNT(*) AS n_produzioni,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS tot_occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS tot_pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS tot_netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS tot_kg,
|
||||
COALESCE(SUM(p.mtprod),0) AS tot_mt,
|
||||
COALESCE(SUM(p.scarto),0) AS tot_scarto
|
||||
FROM productiondata p WHERE $whereSql";
|
||||
$stmt = $pdo->prepare($sqlKpi);
|
||||
$stmt->execute($params);
|
||||
$kpi = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$effTempo = ($kpi['tot_occupato_s'] > 0) ? round($kpi['tot_netto_s'] / $kpi['tot_occupato_s'] * 100, 1) : 0;
|
||||
$denomScarto = $kpi['tot_kg'] + $kpi['tot_scarto'];
|
||||
$scartoPercTot = ($denomScarto > 0) ? round($kpi['tot_scarto'] / $denomScarto * 100, 2) : 0;
|
||||
|
||||
/* ---------- SATURAZIONE (uso di max_hours_per_day) ----------
|
||||
Capacità teorica = somma, per ogni linea nel perimetro,
|
||||
di max_hours_per_day * numero giorni di calendario del range.
|
||||
*/
|
||||
$giorniRange = 1;
|
||||
if ($dateFrom !== '' && $dateTo !== '') {
|
||||
$d1 = new DateTime($dateFrom);
|
||||
$d2 = new DateTime($dateTo);
|
||||
$giorniRange = max(1, $d1->diff($d2)->days + 1);
|
||||
}
|
||||
// Linee da considerare per la capacità: quelle filtrate, o tutte
|
||||
$capSql = "SELECT COALESCE(SUM(max_hours_per_day),0) AS somma_ore_giorno
|
||||
FROM production_lines"
|
||||
. (!empty($lineArr) ? " WHERE id IN (" . implode(',', $lineArr) . ")" : "");
|
||||
$capOreGiorno = (float)$pdo->query($capSql)->fetchColumn();
|
||||
$capacitaTeoricaS = $capOreGiorno * 3600 * $giorniRange;
|
||||
$saturazione = ($capacitaTeoricaS > 0)
|
||||
? round($kpi['tot_occupato_s'] / $capacitaTeoricaS * 100, 1) : 0;
|
||||
|
||||
/* ---------- PER LINEA ---------- */
|
||||
$sqlLinea = "SELECT l.id, l.name AS linea, l.color, l.max_hours_per_day, COUNT(*) AS n_prod,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS kg, COALESCE(SUM(p.scarto),0) AS scarto
|
||||
FROM productiondata p JOIN production_lines l ON l.id = p.id_linea
|
||||
WHERE $whereSql GROUP BY l.id ORDER BY l.line_number";
|
||||
$stmt = $pdo->prepare($sqlLinea);
|
||||
$stmt->execute($params);
|
||||
$perLinea = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($perLinea as &$row) {
|
||||
$row['eff_tempo'] = ($row['occupato_s'] > 0) ? round($row['netto_s'] / $row['occupato_s'] * 100, 1) : 0;
|
||||
$den = $row['kg'] + $row['scarto'];
|
||||
$row['scarto_perc'] = ($den > 0) ? round($row['scarto'] / $den * 100, 2) : 0;
|
||||
// saturazione della singola linea
|
||||
$capLineaS = (float)$row['max_hours_per_day'] * 3600 * $giorniRange;
|
||||
$row['saturazione'] = ($capLineaS > 0) ? round($row['occupato_s'] / $capLineaS * 100, 1) : 0;
|
||||
}
|
||||
unset($row);
|
||||
|
||||
/* ---------- SERIE TEMPORALE (granularità) ---------- */
|
||||
if ($granul === 'month') {
|
||||
$periodExpr = "DATE_FORMAT(p.Data, '%Y-%m')";
|
||||
} else {
|
||||
$periodExpr = "p.Data";
|
||||
}
|
||||
$sqlSerie = "SELECT $periodExpr AS periodo,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS kg, COALESCE(SUM(p.scarto),0) AS scarto
|
||||
FROM productiondata p WHERE $whereSql
|
||||
GROUP BY periodo ORDER BY periodo";
|
||||
$stmt = $pdo->prepare($sqlSerie);
|
||||
$stmt->execute($params);
|
||||
$serie = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($serie as &$s) {
|
||||
$s['eff_tempo'] = ($s['occupato_s'] > 0) ? round($s['netto_s'] / $s['occupato_s'] * 100, 1) : 0;
|
||||
$den = $s['kg'] + $s['scarto'];
|
||||
$s['scarto_perc'] = ($den > 0) ? round($s['scarto'] / $den * 100, 2) : 0;
|
||||
}
|
||||
unset($s);
|
||||
|
||||
/* ---------- PAUSE CATEGORIA ---------- */
|
||||
$sqlCat = "SELECT pr.category AS category,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS durata_s
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE pp.end_pause IS NOT NULL AND $whereSql GROUP BY pr.category";
|
||||
$stmt = $pdo->prepare($sqlCat);
|
||||
$stmt->execute($params);
|
||||
$plannedS = 0;
|
||||
$unplannedS = 0;
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $c) {
|
||||
if ($c['category'] === 'planned') $plannedS = (int)$c['durata_s'];
|
||||
else $unplannedS += (int)$c['durata_s'];
|
||||
}
|
||||
|
||||
/* ---------- DETTAGLIO ---------- */
|
||||
$sqlDett = "SELECT p.id, p.Data, p.conferma_ordine, p.note_extra,
|
||||
l.name AS linea, m.nome AS matrice, c.nome AS cliente,
|
||||
p.tempo_totale_produzione AS occupato_s, p.seconds_pause AS pause_s, p.seconds_produzione AS netto_s,
|
||||
p.kgprod, p.mtprod, p.scarto, p.scarto_perc, p.id_status,
|
||||
(SELECT GROUP_CONCAT(mes.nome ORDER BY mes.nome SEPARATOR ', ')
|
||||
FROM productiondata_mescole pm JOIN mescole mes ON mes.id = pm.id_mescola
|
||||
WHERE pm.id_productiondata = p.id) AS mescole
|
||||
FROM productiondata p
|
||||
LEFT JOIN production_lines l ON l.id = p.id_linea
|
||||
LEFT JOIN matrice m ON m.id = p.idmatrice
|
||||
LEFT JOIN clients c ON c.id = p.id_cliente
|
||||
WHERE $whereSql ORDER BY p.Data DESC, p.id DESC LIMIT 500";
|
||||
$stmt = $pdo->prepare($sqlDett);
|
||||
$stmt->execute($params);
|
||||
$dettaglio = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'granularity' => $granul,
|
||||
'kpi' => [
|
||||
'n_produzioni' => (int)$kpi['n_produzioni'],
|
||||
'tot_occupato_s' => (int)$kpi['tot_occupato_s'],
|
||||
'tot_pause_s' => (int)$kpi['tot_pause_s'],
|
||||
'tot_netto_s' => (int)$kpi['tot_netto_s'],
|
||||
'tot_kg' => (float)$kpi['tot_kg'],
|
||||
'tot_mt' => (float)$kpi['tot_mt'],
|
||||
'tot_scarto' => (float)$kpi['tot_scarto'],
|
||||
'eff_tempo' => $effTempo,
|
||||
'scarto_perc_tot' => $scartoPercTot,
|
||||
'planned_s' => $plannedS,
|
||||
'unplanned_s' => $unplannedS,
|
||||
'saturazione' => $saturazione,
|
||||
'giorni_range' => $giorniRange,
|
||||
],
|
||||
'per_linea' => $perLinea,
|
||||
'serie' => $serie,
|
||||
'dettaglio' => $dettaglio,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="it">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Statistiche Produzione</title>
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
|
||||
<style>
|
||||
.stats-header {
|
||||
background: linear-gradient(135deg, #1d4ed8, #3b82f6);
|
||||
color: #fff;
|
||||
padding: 1.1rem 1.4rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
border-radius: 1rem;
|
||||
flex-wrap: wrap;
|
||||
gap: .8rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.stats-header h4 {
|
||||
margin: 0;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.hbtn {
|
||||
background: rgba(255, 255, 255, .22);
|
||||
border: 1px solid rgba(255, 255, 255, .4);
|
||||
color: #fff;
|
||||
padding: .5rem 1rem;
|
||||
border-radius: .7rem;
|
||||
text-decoration: none;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.hbtn:hover {
|
||||
background: rgba(255, 255, 255, .34);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.hbtn.active {
|
||||
background: #fff;
|
||||
color: #1d4ed8;
|
||||
}
|
||||
|
||||
.filters-bar {
|
||||
background: #fff;
|
||||
padding: 1rem 1.2rem;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
|
||||
gap: .9rem;
|
||||
align-items: end;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.filters-bar label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
color: #1e40af;
|
||||
font-size: .8rem;
|
||||
margin-bottom: .3rem;
|
||||
}
|
||||
|
||||
.filters-bar input,
|
||||
.filters-bar select {
|
||||
width: 100%;
|
||||
padding: .55rem .7rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: .6rem;
|
||||
font-size: .95rem;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.line-buttons {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.line-btn2 {
|
||||
flex: 1;
|
||||
min-width: 70px;
|
||||
padding: .55rem .3rem;
|
||||
border: 2px solid transparent;
|
||||
border-radius: .6rem;
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
font-size: .9rem;
|
||||
opacity: .35;
|
||||
transition: .15s;
|
||||
}
|
||||
|
||||
.line-btn2.active {
|
||||
opacity: 1;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
|
||||
}
|
||||
|
||||
.gran-toggle {
|
||||
display: flex;
|
||||
gap: .3rem;
|
||||
}
|
||||
|
||||
.gran-btn {
|
||||
flex: 1;
|
||||
padding: .55rem .3rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
background: #fff;
|
||||
border-radius: .6rem;
|
||||
cursor: pointer;
|
||||
font-weight: 600;
|
||||
font-size: .85rem;
|
||||
color: #475569;
|
||||
}
|
||||
|
||||
.gran-btn.active {
|
||||
background: #2563eb;
|
||||
color: #fff;
|
||||
border-color: #2563eb;
|
||||
}
|
||||
|
||||
.filters-actions {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
}
|
||||
|
||||
.btn-apply {
|
||||
background: #2563eb;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: .6rem 1rem;
|
||||
border-radius: .6rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.btn-reset {
|
||||
background: #e2e8f0;
|
||||
color: #475569;
|
||||
border: none;
|
||||
padding: .6rem 1rem;
|
||||
border-radius: .6rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.kpi-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.kpi-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
border-left: 5px solid #3b82f6;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-label {
|
||||
font-size: .72rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: .5px;
|
||||
color: #64748b;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-value {
|
||||
font-size: 1.7rem;
|
||||
font-weight: 800;
|
||||
color: #0f172a;
|
||||
margin-top: .2rem;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
.kpi-card .kpi-sub {
|
||||
font-size: .8rem;
|
||||
color: #94a3b8;
|
||||
margin-top: .2rem;
|
||||
}
|
||||
|
||||
.kpi-card.green {
|
||||
border-left-color: #10b981;
|
||||
}
|
||||
|
||||
.kpi-card.amber {
|
||||
border-left-color: #f59e0b;
|
||||
}
|
||||
|
||||
.kpi-card.red {
|
||||
border-left-color: #ef4444;
|
||||
}
|
||||
|
||||
.kpi-card.violet {
|
||||
border-left-color: #8b5cf6;
|
||||
}
|
||||
|
||||
.kpi-card.teal {
|
||||
border-left-color: #14b8a6;
|
||||
}
|
||||
|
||||
.charts-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.chart-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
}
|
||||
|
||||
.chart-card.wide {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
.chart-card h6 {
|
||||
font-weight: 700;
|
||||
color: #1e293b;
|
||||
margin: 0 0 .8rem;
|
||||
font-size: .95rem;
|
||||
}
|
||||
|
||||
.chart-wrap {
|
||||
position: relative;
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.table-card {
|
||||
background: #fff;
|
||||
border-radius: 1rem;
|
||||
padding: 1.1rem 1.2rem;
|
||||
box-shadow: 0 4px 15px rgba(0, 0, 0, .05);
|
||||
overflow-x: auto;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.table-card h6 {
|
||||
font-weight: 700;
|
||||
color: #1e293b;
|
||||
margin: 0 0 .8rem;
|
||||
}
|
||||
|
||||
table.stats-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: .85rem;
|
||||
}
|
||||
|
||||
table.stats-table th {
|
||||
background: #e7f1ff;
|
||||
color: #0b3d91;
|
||||
text-align: left;
|
||||
padding: .55rem .6rem;
|
||||
}
|
||||
|
||||
table.stats-table td {
|
||||
padding: .5rem .6rem;
|
||||
border-bottom: 1px solid #eef2f7;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
table.stats-table tr:hover td {
|
||||
background: #f8fafc;
|
||||
}
|
||||
|
||||
.pill {
|
||||
display: inline-block;
|
||||
padding: .15rem .55rem;
|
||||
border-radius: 999px;
|
||||
font-size: .72rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.pill-ok {
|
||||
background: #d1fae5;
|
||||
color: #065f46;
|
||||
}
|
||||
|
||||
.pill-prob {
|
||||
background: #fee2e2;
|
||||
color: #991b1b;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #94a3b8;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
padding: 3rem;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.empty {
|
||||
text-align: center;
|
||||
padding: 3rem;
|
||||
color: #94a3b8;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="wrapper toggled">
|
||||
<?php include('include/navbar.php'); ?>
|
||||
<?php include('include/topbar.php'); ?>
|
||||
|
||||
<div class="page-wrapper">
|
||||
<div class="page-content">
|
||||
|
||||
<div class="stats-header">
|
||||
<h4><i class="bi bi-bar-chart-line"></i> Statistiche Produzione</h4>
|
||||
<div class="header-actions">
|
||||
<a class="hbtn active" href="production_stats.php"><i class="bi bi-speedometer2"></i> Produzione</a>
|
||||
<a class="hbtn" href="pause_stats.php"><i class="bi bi-pause-circle"></i> Pause & Fermi</a>
|
||||
<a class="hbtn" href="production_dashboard.php">Torna</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters-bar">
|
||||
<div><label>Da (data)</label><input type="date" id="fDateFrom"></div>
|
||||
<div><label>A (data)</label><input type="date" id="fDateTo"></div>
|
||||
|
||||
<div style="grid-column:span 2;">
|
||||
<label>Linee</label>
|
||||
<div class="line-buttons" id="lineButtons">
|
||||
<?php foreach ($linee as $l): ?>
|
||||
<button type="button" class="line-btn2 active"
|
||||
data-line="<?= (int)$l['id'] ?>"
|
||||
style="background:<?= htmlspecialchars($l['color'] ?: '#3b82f6') ?>;">
|
||||
<?= htmlspecialchars($l['name']) ?>
|
||||
</button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div><label>Matrice</label><select id="fMatrice">
|
||||
<option value="">Tutte</option>
|
||||
<?php foreach ($matrici as $m): ?><option value="<?= (int)$m['id'] ?>"><?= htmlspecialchars($m['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div><label>Mescola</label><select id="fMescola">
|
||||
<option value="">Tutte</option>
|
||||
<?php foreach ($mescole as $me): ?><option value="<?= (int)$me['id'] ?>"><?= htmlspecialchars($me['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
|
||||
<div>
|
||||
<label>Vista temporale</label>
|
||||
<div class="gran-toggle">
|
||||
<button type="button" class="gran-btn active" data-gran="day">Giorno</button>
|
||||
<button type="button" class="gran-btn" data-gran="month">Mese</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters-actions">
|
||||
<button class="btn-apply" id="btnApply"><i class="bi bi-funnel"></i> Applica</button>
|
||||
<button class="btn-reset" id="btnReset">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="kpi-grid" id="kpiGrid">
|
||||
<div class="loading">Caricamento…</div>
|
||||
</div>
|
||||
|
||||
<div class="charts-grid" id="chartsGrid" style="display:none;">
|
||||
<div class="chart-card wide">
|
||||
<h6 id="titSerie">Efficienza tempo nel periodo (%)</h6>
|
||||
<div class="chart-wrap"><canvas id="chartSerie"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Efficienza tempo per linea (%)</h6>
|
||||
<div class="chart-wrap"><canvas id="chartEffLinea"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Saturazione linea (% su capacità)</h6>
|
||||
<div class="chart-wrap"><canvas id="chartSat"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Kg prodotti vs scarto per linea</h6>
|
||||
<div class="chart-wrap"><canvas id="chartKgLinea"></canvas></div>
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<h6>Scarto % per linea</h6>
|
||||
<div class="chart-wrap"><canvas id="chartScartoLinea"></canvas></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-card" id="tableCard" style="display:none;">
|
||||
<h6>Dettaglio produzioni</h6>
|
||||
<table class="stats-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Data</th>
|
||||
<th>Linea</th>
|
||||
<th>Matrice</th>
|
||||
<th>Cliente</th>
|
||||
<th>Mescole</th>
|
||||
<th>Occupato</th>
|
||||
<th>Pause</th>
|
||||
<th>Netto</th>
|
||||
<th>Eff.%</th>
|
||||
<th>Kg</th>
|
||||
<th>Mt</th>
|
||||
<th>Scarto</th>
|
||||
<th>Scarto%</th>
|
||||
<th>Esito</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="tableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div><!-- /.page-content -->
|
||||
</div><!-- /.page-wrapper -->
|
||||
</div><!-- /.wrapper -->
|
||||
|
||||
<?php include('jsinclude.php'); ?>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
|
||||
<script>
|
||||
// il codice sotto usa jQuery (già caricato nel <head>) e Chart.js
|
||||
function secToHms(s) {
|
||||
s = Math.max(0, parseInt(s || 0, 10));
|
||||
const h = String(Math.floor(s / 3600)).padStart(2, '0'),
|
||||
m = String(Math.floor((s % 3600) / 60)).padStart(2, '0'),
|
||||
x = String(s % 60).padStart(2, '0');
|
||||
return `${h}:${m}:${x}`;
|
||||
}
|
||||
|
||||
function fmtDateIt(iso) {
|
||||
if (!iso) return '-';
|
||||
const p = iso.split('-');
|
||||
return p.length === 3 ? `${p[2]}/${p[1]}/${p[0]}` : iso;
|
||||
}
|
||||
|
||||
function fmtMonth(ym) {
|
||||
if (!ym) return '-';
|
||||
const p = ym.split('-');
|
||||
if (p.length < 2) return ym;
|
||||
const mesi = ['gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic'];
|
||||
return `${mesi[parseInt(p[1],10)-1]} ${p[0]}`;
|
||||
}
|
||||
|
||||
function num(v, d = 2) {
|
||||
return Number(v || 0).toLocaleString('it-IT', {
|
||||
minimumFractionDigits: d,
|
||||
maximumFractionDigits: d
|
||||
});
|
||||
}
|
||||
let charts = {};
|
||||
|
||||
function destroyChart(k) {
|
||||
if (charts[k]) {
|
||||
charts[k].destroy();
|
||||
charts[k] = null;
|
||||
}
|
||||
}
|
||||
|
||||
let currentGran = 'day';
|
||||
|
||||
function renderKpi(k) {
|
||||
$('#kpiGrid').html(`
|
||||
<div class="kpi-card"><div class="kpi-label">Produzioni</div><div class="kpi-value">${k.n_produzioni}</div><div class="kpi-sub">record conclusi</div></div>
|
||||
<div class="kpi-card green"><div class="kpi-label">Efficienza tempo</div><div class="kpi-value">${k.eff_tempo}%</div><div class="kpi-sub">netto / occupato</div></div>
|
||||
<div class="kpi-card teal"><div class="kpi-label">Saturazione</div><div class="kpi-value">${k.saturazione}%</div><div class="kpi-sub">su ${k.giorni_range} gg di capacità</div></div>
|
||||
<div class="kpi-card amber"><div class="kpi-label">Tempo in pausa</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.tot_pause_s)}</div><div class="kpi-sub">pian. ${secToHms(k.planned_s)} · non ${secToHms(k.unplanned_s)}</div></div>
|
||||
<div class="kpi-card violet"><div class="kpi-label">Kg prodotti</div><div class="kpi-value" style="font-size:1.4rem;">${num(k.tot_kg)}</div><div class="kpi-sub">${num(k.tot_mt)} metri</div></div>
|
||||
<div class="kpi-card red"><div class="kpi-label">Scarto</div><div class="kpi-value" style="font-size:1.4rem;">${k.scarto_perc_tot}%</div><div class="kpi-sub">${num(k.tot_scarto)} kg scartati</div></div>
|
||||
`);
|
||||
}
|
||||
|
||||
function renderCharts(d) {
|
||||
const linee = d.per_linea || [],
|
||||
serie = d.serie || [],
|
||||
gran = d.granularity;
|
||||
const labels = serie.map(x => gran === 'month' ? fmtMonth(x.periodo) : fmtDateIt(x.periodo));
|
||||
|
||||
$('#titSerie').text(gran === 'month' ? 'Efficienza tempo per mese (%)' : 'Efficienza tempo per giorno (%)');
|
||||
|
||||
destroyChart('serie');
|
||||
charts.serie = new Chart($('#chartSerie'), {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels,
|
||||
datasets: [{
|
||||
label: 'Efficienza %',
|
||||
data: serie.map(x => x.eff_tempo),
|
||||
borderColor: '#10b981',
|
||||
backgroundColor: 'rgba(16,185,129,.15)',
|
||||
fill: true,
|
||||
tension: .3,
|
||||
pointRadius: 3
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
max: 100
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
destroyChart('effLinea');
|
||||
charts.effLinea = new Chart($('#chartEffLinea'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: linee.map(x => x.linea),
|
||||
datasets: [{
|
||||
label: 'Efficienza %',
|
||||
data: linee.map(x => x.eff_tempo),
|
||||
backgroundColor: linee.map(x => x.color || '#3b82f6')
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
max: 100
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
destroyChart('sat');
|
||||
charts.sat = new Chart($('#chartSat'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: linee.map(x => x.linea),
|
||||
datasets: [{
|
||||
label: 'Saturazione %',
|
||||
data: linee.map(x => x.saturazione),
|
||||
backgroundColor: linee.map(x => x.color || '#14b8a6')
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
max: 100
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
destroyChart('kgLinea');
|
||||
charts.kgLinea = new Chart($('#chartKgLinea'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: linee.map(x => x.linea),
|
||||
datasets: [{
|
||||
label: 'Kg prodotti',
|
||||
data: linee.map(x => x.kg),
|
||||
backgroundColor: '#8b5cf6'
|
||||
}, {
|
||||
label: 'Kg scarto',
|
||||
data: linee.map(x => x.scarto),
|
||||
backgroundColor: '#ef4444'
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
destroyChart('scartoLinea');
|
||||
charts.scartoLinea = new Chart($('#chartScartoLinea'), {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: linee.map(x => x.linea),
|
||||
datasets: [{
|
||||
label: 'Scarto %',
|
||||
data: linee.map(x => x.scarto_perc),
|
||||
backgroundColor: '#f59e0b'
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderTable(rows) {
|
||||
if (!rows || !rows.length) {
|
||||
$('#tableBody').html('<tr><td colspan="14" class="empty">Nessun dato per i filtri selezionati</td></tr>');
|
||||
return;
|
||||
}
|
||||
let html = '';
|
||||
rows.forEach(r => {
|
||||
const esito = (String(r.id_status) === '8') ? '<span class="pill pill-prob">Problema</span>' : '<span class="pill pill-ok">Completata</span>';
|
||||
const eff = (r.occupato_s > 0) ? Math.round(r.netto_s / r.occupato_s * 100) : 0;
|
||||
html += `<tr><td>${fmtDateIt(r.Data)}</td><td>${r.linea||'-'}</td>
|
||||
<td>${r.matrice||'-'}${r.note_extra?'<div class="muted" style="font-size:.72rem;">'+r.note_extra+'</div>':''}</td>
|
||||
<td>${r.cliente||'-'}</td><td>${r.mescole||'<span class="muted">-</span>'}</td>
|
||||
<td>${secToHms(r.occupato_s)}</td><td>${secToHms(r.pause_s)}</td><td>${secToHms(r.netto_s)}</td>
|
||||
<td>${eff}%</td><td>${num(r.kgprod)}</td><td>${num(r.mtprod)}</td><td>${num(r.scarto)}</td>
|
||||
<td>${r.scarto_perc!==null?num(r.scarto_perc)+'%':'-'}</td><td>${esito}</td></tr>`;
|
||||
});
|
||||
$('#tableBody').html(html);
|
||||
}
|
||||
|
||||
function selectedLines() {
|
||||
return $('#lineButtons .line-btn2.active').map(function() {
|
||||
return $(this).data('line');
|
||||
}).get().join(',');
|
||||
}
|
||||
|
||||
function loadStats() {
|
||||
$('#kpiGrid').html('<div class="loading">Caricamento…</div>');
|
||||
$('#chartsGrid, #tableCard').hide();
|
||||
$.getJSON('', {
|
||||
ajax: 1,
|
||||
date_from: $('#fDateFrom').val(),
|
||||
date_to: $('#fDateTo').val(),
|
||||
lines: selectedLines(),
|
||||
matrice_id: $('#fMatrice').val(),
|
||||
mescola_id: $('#fMescola').val(),
|
||||
granularity: currentGran
|
||||
},
|
||||
function(r) {
|
||||
if (!r.success) {
|
||||
$('#kpiGrid').html('<div class="empty">Errore: ' + (r.msg || '') + '</div>');
|
||||
return;
|
||||
}
|
||||
renderKpi(r.kpi);
|
||||
if (r.kpi.n_produzioni === 0) {
|
||||
$('#chartsGrid, #tableCard').hide();
|
||||
$('#tableBody').html('');
|
||||
return;
|
||||
}
|
||||
$('#chartsGrid, #tableCard').show();
|
||||
renderCharts(r);
|
||||
renderTable(r.dettaglio);
|
||||
}).fail(function() {
|
||||
$('#kpiGrid').html('<div class="empty">Errore di comunicazione con il server.</div>');
|
||||
});
|
||||
}
|
||||
|
||||
$(function() {
|
||||
const today = new Date(),
|
||||
past = new Date();
|
||||
past.setDate(today.getDate() - 30);
|
||||
$('#fDateTo').val(today.toISOString().slice(0, 10));
|
||||
$('#fDateFrom').val(past.toISOString().slice(0, 10));
|
||||
|
||||
// Bottoni linea: toggle, mai zero (se azzeri torni a tutte)
|
||||
$('#lineButtons').on('click', '.line-btn2', function() {
|
||||
$(this).toggleClass('active');
|
||||
if ($('#lineButtons .line-btn2.active').length === 0) {
|
||||
$('#lineButtons .line-btn2').addClass('active');
|
||||
}
|
||||
loadStats();
|
||||
});
|
||||
|
||||
// Granularità
|
||||
$('.gran-btn').on('click', function() {
|
||||
$('.gran-btn').removeClass('active');
|
||||
$(this).addClass('active');
|
||||
currentGran = $(this).data('gran');
|
||||
loadStats();
|
||||
});
|
||||
|
||||
$('#btnApply').on('click', loadStats);
|
||||
$('#btnReset').on('click', function() {
|
||||
$('#lineButtons .line-btn2').addClass('active');
|
||||
$('#fMatrice,#fMescola').val('');
|
||||
$('.gran-btn').removeClass('active');
|
||||
$('.gran-btn[data-gran="day"]').addClass('active');
|
||||
currentGran = 'day';
|
||||
$('#fDateTo').val(today.toISOString().slice(0, 10));
|
||||
$('#fDateFrom').val(past.toISOString().slice(0, 10));
|
||||
loadStats();
|
||||
});
|
||||
|
||||
loadStats();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -81,6 +81,13 @@ if ($matricePhoto) {
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<?php if (!empty($r['note_extra'])): ?>
|
||||
<div>
|
||||
<strong>Descrizione</strong>
|
||||
<span data-field="note_extra"><?= htmlspecialchars($r['note_extra']) ?></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div><strong>Linea</strong> <span><?= htmlspecialchars($r['linea']) ?></span></div>
|
||||
<div><strong>Matrice</strong> <span><?= htmlspecialchars($r['matrice']) ?></span></div>
|
||||
|
||||
@@ -258,9 +265,22 @@ if ($matricePhoto) {
|
||||
<div class="photo-tools-row"
|
||||
style="margin-top:0.75rem; display:flex; flex-wrap:wrap; gap:0.6rem; align-items:center;">
|
||||
|
||||
<button class="action-btn start-btn" data-id="<?= $r['id'] ?>" style="flex:0 0 auto; min-width:240px;">
|
||||
AVVIA PRODUZIONE
|
||||
</button>
|
||||
<?php
|
||||
// La linea è occupata se ha già una produzione attiva (2 o 7),
|
||||
// esclusa (ovviamente) questa stessa card.
|
||||
$lineaOccupata = !empty($lineeOccupate[(int)$r['id_linea']]);
|
||||
?>
|
||||
<?php if ($lineaOccupata): ?>
|
||||
<button class="action-btn start-btn" data-id="<?= $r['id'] ?>"
|
||||
disabled
|
||||
style="flex:0 0 auto; min-width:240px; background:#cbd5e1; color:#64748b; cursor:not-allowed; opacity:0.85;">
|
||||
LINEA OCCUPATA
|
||||
</button>
|
||||
<?php else: ?>
|
||||
<button class="action-btn start-btn" data-id="<?= $r['id'] ?>" style="flex:0 0 auto; min-width:240px;">
|
||||
AVVIA PRODUZIONE
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php foreach ($toolsArray as $toolName): ?>
|
||||
<span class="badge tools-badge"
|
||||
|
||||
@@ -12,6 +12,9 @@ try {
|
||||
$brand = $_POST['brand'] ?? '';
|
||||
$status = $_POST['status'] ?? 'active';
|
||||
$color = $_POST['color'] ?? '#dc2626';
|
||||
$maxHours = isset($_POST['max_hours_per_day']) ? (float)$_POST['max_hours_per_day'] : 24;
|
||||
if ($maxHours < 0) $maxHours = 0;
|
||||
if ($maxHours > 24) $maxHours = 24;
|
||||
|
||||
if (!$lineNumber || !$name) {
|
||||
echo json_encode(['success' => false, 'message' => 'Numero linea e nome sono obbligatori.']);
|
||||
@@ -19,10 +22,10 @@ try {
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO production_lines (line_number, name, model, brand, status, color)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO production_lines (line_number, name, model, brand, status, color, max_hours_per_day)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
$stmt->execute([$lineNumber, $name, $model, $brand, $status, $color]);
|
||||
$stmt->execute([$lineNumber, $name, $model, $brand, $status, $color, $maxHours]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
include('include/headscript.php');
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
/* 2=produzione, 3=qualità(fine ok), 7=pausa, 8=problema. "Concluse"=3,8 */
|
||||
$STATI_CONCLUSI = [3, 8];
|
||||
|
||||
$linee = $pdo->query("SELECT id, name FROM production_lines ORDER BY line_number")->fetchAll();
|
||||
$matrici = $pdo->query("SELECT id, nome FROM matrice ORDER BY nome")->fetchAll();
|
||||
$mescole = $pdo->query("SELECT id, nome FROM mescole ORDER BY nome")->fetchAll();
|
||||
|
||||
if (!empty($_GET['ajax'])) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$dateFrom = $_GET['date_from'] ?? '';
|
||||
$dateTo = $_GET['date_to'] ?? '';
|
||||
$lineId = isset($_GET['line_id']) && $_GET['line_id'] !== '' ? (int)$_GET['line_id'] : null;
|
||||
$matId = isset($_GET['matrice_id']) && $_GET['matrice_id'] !== '' ? (int)$_GET['matrice_id'] : null;
|
||||
$mescId = isset($_GET['mescola_id']) && $_GET['mescola_id'] !== '' ? (int)$_GET['mescola_id'] : null;
|
||||
|
||||
$where = ["p.id_status IN (" . implode(',', $STATI_CONCLUSI) . ")"];
|
||||
$params = [];
|
||||
if ($dateFrom !== '') { $where[] = "p.Data >= :date_from"; $params['date_from'] = $dateFrom; }
|
||||
if ($dateTo !== '') { $where[] = "p.Data <= :date_to"; $params['date_to'] = $dateTo; }
|
||||
if ($lineId !== null) { $where[] = "p.id_linea = :line_id"; $params['line_id'] = $lineId; }
|
||||
if ($matId !== null) { $where[] = "p.idmatrice = :mat_id"; $params['mat_id'] = $matId; }
|
||||
if ($mescId !== null) {
|
||||
$where[] = "EXISTS (SELECT 1 FROM productiondata_mescole pm
|
||||
WHERE pm.id_productiondata = p.id AND pm.id_mescola = :mesc_id)";
|
||||
$params['mesc_id'] = $mescId;
|
||||
}
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
try {
|
||||
$sqlKpi = "SELECT
|
||||
COUNT(*) AS n_produzioni,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS tot_occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS tot_pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS tot_netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS tot_kg,
|
||||
COALESCE(SUM(p.mtprod),0) AS tot_mt,
|
||||
COALESCE(SUM(p.scarto),0) AS tot_scarto
|
||||
FROM productiondata p WHERE $whereSql";
|
||||
$stmt = $pdo->prepare($sqlKpi); $stmt->execute($params);
|
||||
$kpi = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$effTempo = ($kpi['tot_occupato_s'] > 0) ? round($kpi['tot_netto_s'] / $kpi['tot_occupato_s'] * 100, 1) : 0;
|
||||
$denomScarto = $kpi['tot_kg'] + $kpi['tot_scarto'];
|
||||
$scartoPercTot = ($denomScarto > 0) ? round($kpi['tot_scarto'] / $denomScarto * 100, 2) : 0;
|
||||
|
||||
$sqlLinea = "SELECT l.id, l.name AS linea, l.max_hours_per_day, COUNT(*) AS n_prod,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS kg, COALESCE(SUM(p.scarto),0) AS scarto
|
||||
FROM productiondata p JOIN production_lines l ON l.id = p.id_linea
|
||||
WHERE $whereSql GROUP BY l.id ORDER BY l.line_number";
|
||||
$stmt = $pdo->prepare($sqlLinea); $stmt->execute($params);
|
||||
$perLinea = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($perLinea as &$row) {
|
||||
$row['eff_tempo'] = ($row['occupato_s'] > 0) ? round($row['netto_s'] / $row['occupato_s'] * 100, 1) : 0;
|
||||
$den = $row['kg'] + $row['scarto'];
|
||||
$row['scarto_perc'] = ($den > 0) ? round($row['scarto'] / $den * 100, 2) : 0;
|
||||
} unset($row);
|
||||
|
||||
$sqlGiorno = "SELECT p.Data AS giorno,
|
||||
COALESCE(SUM(p.tempo_totale_produzione),0) AS occupato_s,
|
||||
COALESCE(SUM(p.seconds_pause),0) AS pause_s,
|
||||
COALESCE(SUM(p.seconds_produzione),0) AS netto_s,
|
||||
COALESCE(SUM(p.kgprod),0) AS kg, COALESCE(SUM(p.scarto),0) AS scarto
|
||||
FROM productiondata p WHERE $whereSql GROUP BY p.Data ORDER BY p.Data";
|
||||
$stmt = $pdo->prepare($sqlGiorno); $stmt->execute($params);
|
||||
$perGiorno = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($perGiorno as &$g) {
|
||||
$g['eff_tempo'] = ($g['occupato_s'] > 0) ? round($g['netto_s'] / $g['occupato_s'] * 100, 1) : 0;
|
||||
$den = $g['kg'] + $g['scarto'];
|
||||
$g['scarto_perc'] = ($den > 0) ? round($g['scarto'] / $den * 100, 2) : 0;
|
||||
} unset($g);
|
||||
|
||||
$sqlCat = "SELECT pr.category AS category,
|
||||
COALESCE(SUM(TIMESTAMPDIFF(SECOND, pp.start_pause, pp.end_pause)),0) AS durata_s
|
||||
FROM production_pauses pp
|
||||
JOIN productiondata p ON p.id = pp.id_production
|
||||
JOIN pause_reasons pr ON pr.id = pp.reason_id
|
||||
WHERE pp.end_pause IS NOT NULL AND $whereSql GROUP BY pr.category";
|
||||
$stmt = $pdo->prepare($sqlCat); $stmt->execute($params);
|
||||
$plannedS = 0; $unplannedS = 0;
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $c) {
|
||||
if ($c['category'] === 'planned') $plannedS = (int)$c['durata_s']; else $unplannedS += (int)$c['durata_s'];
|
||||
}
|
||||
|
||||
$sqlDett = "SELECT p.id, p.Data, p.conferma_ordine, p.note_extra,
|
||||
l.name AS linea, m.nome AS matrice, c.nome AS cliente,
|
||||
p.tempo_totale_produzione AS occupato_s, p.seconds_pause AS pause_s, p.seconds_produzione AS netto_s,
|
||||
p.kgprod, p.mtprod, p.scarto, p.scarto_perc, p.id_status,
|
||||
(SELECT GROUP_CONCAT(mes.nome ORDER BY mes.nome SEPARATOR ', ')
|
||||
FROM productiondata_mescole pm JOIN mescole mes ON mes.id = pm.id_mescola
|
||||
WHERE pm.id_productiondata = p.id) AS mescole
|
||||
FROM productiondata p
|
||||
LEFT JOIN production_lines l ON l.id = p.id_linea
|
||||
LEFT JOIN matrice m ON m.id = p.idmatrice
|
||||
LEFT JOIN clients c ON c.id = p.id_cliente
|
||||
WHERE $whereSql ORDER BY p.Data DESC, p.id DESC LIMIT 500";
|
||||
$stmt = $pdo->prepare($sqlDett); $stmt->execute($params);
|
||||
$dettaglio = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'kpi' => [
|
||||
'n_produzioni'=>(int)$kpi['n_produzioni'],'tot_occupato_s'=>(int)$kpi['tot_occupato_s'],
|
||||
'tot_pause_s'=>(int)$kpi['tot_pause_s'],'tot_netto_s'=>(int)$kpi['tot_netto_s'],
|
||||
'tot_kg'=>(float)$kpi['tot_kg'],'tot_mt'=>(float)$kpi['tot_mt'],'tot_scarto'=>(float)$kpi['tot_scarto'],
|
||||
'eff_tempo'=>$effTempo,'scarto_perc_tot'=>$scartoPercTot,'planned_s'=>$plannedS,'unplanned_s'=>$unplannedS,
|
||||
],
|
||||
'per_linea'=>$perLinea,'per_giorno'=>$perGiorno,'dettaglio'=>$dettaglio,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Statistiche Produzione</title>
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
|
||||
<style>
|
||||
* { box-sizing: border-box; }
|
||||
body { background:#f1f5f9; font-family:'Segoe UI',sans-serif; }
|
||||
.stats-header { background:linear-gradient(135deg,#1d4ed8,#3b82f6); color:#fff; padding:1.2rem 1.5rem;
|
||||
display:flex; justify-content:space-between; align-items:center; border-radius:0 0 1rem 1rem; flex-wrap:wrap; gap:.8rem; }
|
||||
.stats-header h4 { margin:0; font-weight:600; }
|
||||
.header-actions { display:flex; gap:.5rem; flex-wrap:wrap; }
|
||||
.hbtn { background:rgba(255,255,255,.22); border:1px solid rgba(255,255,255,.4); color:#fff; padding:.5rem 1rem; border-radius:.7rem; text-decoration:none; font-weight:600; }
|
||||
.hbtn:hover { background:rgba(255,255,255,.34); color:#fff; }
|
||||
.hbtn.active { background:#fff; color:#1d4ed8; }
|
||||
.filters-bar { background:#fff; margin:1rem; padding:1rem 1.2rem; border-radius:1rem; box-shadow:0 4px 15px rgba(0,0,0,.05);
|
||||
display:grid; grid-template-columns:repeat(auto-fit,minmax(160px,1fr)); gap:.9rem; align-items:end; }
|
||||
.filters-bar label { display:block; font-weight:600; color:#1e40af; font-size:.8rem; margin-bottom:.3rem; }
|
||||
.filters-bar input, .filters-bar select { width:100%; padding:.55rem .7rem; border:1px solid #cbd5e1; border-radius:.6rem; font-size:.95rem; background:#fff; }
|
||||
.filters-actions { display:flex; gap:.5rem; }
|
||||
.btn-apply { background:#2563eb; color:#fff; border:none; padding:.6rem 1rem; border-radius:.6rem; font-weight:600; cursor:pointer; flex:1; }
|
||||
.btn-reset { background:#e2e8f0; color:#475569; border:none; padding:.6rem 1rem; border-radius:.6rem; font-weight:600; cursor:pointer; }
|
||||
.kpi-grid { margin:0 1rem 1rem; display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:1rem; }
|
||||
.kpi-card { background:#fff; border-radius:1rem; padding:1.1rem 1.2rem; box-shadow:0 4px 15px rgba(0,0,0,.05); border-left:5px solid #3b82f6; }
|
||||
.kpi-card .kpi-label { font-size:.72rem; text-transform:uppercase; letter-spacing:.5px; color:#64748b; font-weight:700; }
|
||||
.kpi-card .kpi-value { font-size:1.7rem; font-weight:800; color:#0f172a; margin-top:.2rem; line-height:1.1; }
|
||||
.kpi-card .kpi-sub { font-size:.8rem; color:#94a3b8; margin-top:.2rem; }
|
||||
.kpi-card.green { border-left-color:#10b981; } .kpi-card.amber { border-left-color:#f59e0b; }
|
||||
.kpi-card.red { border-left-color:#ef4444; } .kpi-card.violet { border-left-color:#8b5cf6; }
|
||||
.charts-grid { margin:0 1rem 1rem; display:grid; grid-template-columns:repeat(auto-fit,minmax(360px,1fr)); gap:1rem; }
|
||||
.chart-card { background:#fff; border-radius:1rem; padding:1.1rem 1.2rem; box-shadow:0 4px 15px rgba(0,0,0,.05); }
|
||||
.chart-card h6 { font-weight:700; color:#1e293b; margin:0 0 .8rem; font-size:.95rem; }
|
||||
.chart-wrap { position:relative; height:280px; }
|
||||
.table-card { background:#fff; border-radius:1rem; margin:0 1rem 2rem; padding:1.1rem 1.2rem; box-shadow:0 4px 15px rgba(0,0,0,.05); overflow-x:auto; }
|
||||
.table-card h6 { font-weight:700; color:#1e293b; margin:0 0 .8rem; }
|
||||
table.stats-table { width:100%; border-collapse:collapse; font-size:.85rem; }
|
||||
table.stats-table th { background:#e7f1ff; color:#0b3d91; text-align:left; padding:.55rem .6rem; }
|
||||
table.stats-table td { padding:.5rem .6rem; border-bottom:1px solid #eef2f7; color:#334155; }
|
||||
table.stats-table tr:hover td { background:#f8fafc; }
|
||||
.pill { display:inline-block; padding:.15rem .55rem; border-radius:999px; font-size:.72rem; font-weight:700; }
|
||||
.pill-ok { background:#d1fae5; color:#065f46; } .pill-prob { background:#fee2e2; color:#991b1b; }
|
||||
.muted { color:#94a3b8; } .loading { text-align:center; padding:3rem; color:#64748b; } .empty { text-align:center; padding:3rem; color:#94a3b8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<?php include('include/navbar.php'); ?>
|
||||
<?php include('include/topbar.php'); ?>
|
||||
|
||||
<div class="stats-header">
|
||||
<h4><i class="bi bi-bar-chart-line"></i> Statistiche Produzione</h4>
|
||||
<div class="header-actions">
|
||||
<a class="hbtn active" href="production_stats.php"><i class="bi bi-speedometer2"></i> Produzione</a>
|
||||
<a class="hbtn" href="pause_stats.php"><i class="bi bi-pause-circle"></i> Pause & Fermi</a>
|
||||
<a class="hbtn" href="production_dashboard.php">Torna</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="filters-bar">
|
||||
<div><label>Da (data)</label><input type="date" id="fDateFrom"></div>
|
||||
<div><label>A (data)</label><input type="date" id="fDateTo"></div>
|
||||
<div><label>Linea</label><select id="fLine"><option value="">Tutte</option>
|
||||
<?php foreach ($linee as $l): ?><option value="<?= (int)$l['id'] ?>"><?= htmlspecialchars($l['name']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div><label>Matrice</label><select id="fMatrice"><option value="">Tutte</option>
|
||||
<?php foreach ($matrici as $m): ?><option value="<?= (int)$m['id'] ?>"><?= htmlspecialchars($m['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div><label>Mescola</label><select id="fMescola"><option value="">Tutte</option>
|
||||
<?php foreach ($mescole as $me): ?><option value="<?= (int)$me['id'] ?>"><?= htmlspecialchars($me['nome']) ?></option><?php endforeach; ?>
|
||||
</select></div>
|
||||
<div class="filters-actions">
|
||||
<button class="btn-apply" id="btnApply"><i class="bi bi-funnel"></i> Applica</button>
|
||||
<button class="btn-reset" id="btnReset">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="kpi-grid" id="kpiGrid"><div class="loading">Caricamento…</div></div>
|
||||
|
||||
<div class="charts-grid" id="chartsGrid" style="display:none;">
|
||||
<div class="chart-card"><h6>Efficienza tempo per linea (%)</h6><div class="chart-wrap"><canvas id="chartEffLinea"></canvas></div></div>
|
||||
<div class="chart-card"><h6>Efficienza tempo giornaliera (%)</h6><div class="chart-wrap"><canvas id="chartEffGiorno"></canvas></div></div>
|
||||
<div class="chart-card"><h6>Kg prodotti vs scarto per linea</h6><div class="chart-wrap"><canvas id="chartKgLinea"></canvas></div></div>
|
||||
<div class="chart-card"><h6>Scarto % per linea</h6><div class="chart-wrap"><canvas id="chartScartoLinea"></canvas></div></div>
|
||||
</div>
|
||||
|
||||
<div class="table-card" id="tableCard" style="display:none;">
|
||||
<h6>Dettaglio produzioni</h6>
|
||||
<table class="stats-table">
|
||||
<thead><tr>
|
||||
<th>Data</th><th>Linea</th><th>Matrice</th><th>Cliente</th><th>Mescole</th>
|
||||
<th>Occupato</th><th>Pause</th><th>Netto</th><th>Eff.%</th>
|
||||
<th>Kg</th><th>Mt</th><th>Scarto</th><th>Scarto%</th><th>Esito</th>
|
||||
</tr></thead>
|
||||
<tbody id="tableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
|
||||
<script>
|
||||
(function forceSidebarCollapsed(){
|
||||
try{
|
||||
const b=document.body,h=document.documentElement;
|
||||
b.setAttribute('data-sidebar-size','condensed');
|
||||
b.setAttribute('data-sidenav-size','condensed');
|
||||
b.classList.add('sidebar-enable','enlarge-menu','sidebar-collapse','vertical-collapsed');
|
||||
h.setAttribute('data-sidebar-size','sm');
|
||||
b.classList.remove('sidebar-open','sidebar-enable-mobile');
|
||||
}catch(e){}
|
||||
})();
|
||||
|
||||
function secToHms(s){ s=Math.max(0,parseInt(s||0,10)); const h=String(Math.floor(s/3600)).padStart(2,'0'),m=String(Math.floor((s%3600)/60)).padStart(2,'0'),x=String(s%60).padStart(2,'0'); return `${h}:${m}:${x}`; }
|
||||
function fmtDateIt(iso){ if(!iso) return '-'; const p=iso.split('-'); return p.length===3?`${p[2]}/${p[1]}/${p[0]}`:iso; }
|
||||
function num(v,d=2){ return Number(v||0).toLocaleString('it-IT',{minimumFractionDigits:d,maximumFractionDigits:d}); }
|
||||
let charts={}; function destroyChart(k){ if(charts[k]){ charts[k].destroy(); charts[k]=null; } }
|
||||
|
||||
function renderKpi(k){
|
||||
$('#kpiGrid').html(`
|
||||
<div class="kpi-card"><div class="kpi-label">Produzioni</div><div class="kpi-value">${k.n_produzioni}</div><div class="kpi-sub">record conclusi</div></div>
|
||||
<div class="kpi-card green"><div class="kpi-label">Efficienza tempo</div><div class="kpi-value">${k.eff_tempo}%</div><div class="kpi-sub">netto / occupato</div></div>
|
||||
<div class="kpi-card"><div class="kpi-label">Tempo occupato</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.tot_occupato_s)}</div><div class="kpi-sub">linea impegnata</div></div>
|
||||
<div class="kpi-card amber"><div class="kpi-label">Tempo in pausa</div><div class="kpi-value" style="font-size:1.25rem;">${secToHms(k.tot_pause_s)}</div><div class="kpi-sub">pian. ${secToHms(k.planned_s)} · non ${secToHms(k.unplanned_s)}</div></div>
|
||||
<div class="kpi-card violet"><div class="kpi-label">Kg prodotti</div><div class="kpi-value" style="font-size:1.4rem;">${num(k.tot_kg)}</div><div class="kpi-sub">${num(k.tot_mt)} metri</div></div>
|
||||
<div class="kpi-card red"><div class="kpi-label">Scarto</div><div class="kpi-value" style="font-size:1.4rem;">${k.scarto_perc_tot}%</div><div class="kpi-sub">${num(k.tot_scarto)} kg scartati</div></div>
|
||||
`);
|
||||
}
|
||||
function renderCharts(d){
|
||||
const linee=d.per_linea||[], giorni=d.per_giorno||[];
|
||||
destroyChart('effLinea');
|
||||
charts.effLinea=new Chart($('#chartEffLinea'),{type:'bar',data:{labels:linee.map(x=>x.linea),datasets:[{label:'Efficienza %',data:linee.map(x=>x.eff_tempo),backgroundColor:'#3b82f6'}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{beginAtZero:true,max:100}},plugins:{legend:{display:false}}}});
|
||||
destroyChart('effGiorno');
|
||||
charts.effGiorno=new Chart($('#chartEffGiorno'),{type:'line',data:{labels:giorni.map(x=>fmtDateIt(x.giorno)),datasets:[{label:'Efficienza %',data:giorni.map(x=>x.eff_tempo),borderColor:'#10b981',backgroundColor:'rgba(16,185,129,.15)',fill:true,tension:.3}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{beginAtZero:true,max:100}},plugins:{legend:{display:false}}}});
|
||||
destroyChart('kgLinea');
|
||||
charts.kgLinea=new Chart($('#chartKgLinea'),{type:'bar',data:{labels:linee.map(x=>x.linea),datasets:[{label:'Kg prodotti',data:linee.map(x=>x.kg),backgroundColor:'#8b5cf6'},{label:'Kg scarto',data:linee.map(x=>x.scarto),backgroundColor:'#ef4444'}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{beginAtZero:true}}}});
|
||||
destroyChart('scartoLinea');
|
||||
charts.scartoLinea=new Chart($('#chartScartoLinea'),{type:'bar',data:{labels:linee.map(x=>x.linea),datasets:[{label:'Scarto %',data:linee.map(x=>x.scarto_perc),backgroundColor:'#f59e0b'}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{beginAtZero:true}},plugins:{legend:{display:false}}}});
|
||||
}
|
||||
function renderTable(rows){
|
||||
if(!rows||!rows.length){ $('#tableBody').html('<tr><td colspan="14" class="empty">Nessun dato per i filtri selezionati</td></tr>'); return; }
|
||||
let html='';
|
||||
rows.forEach(r=>{
|
||||
const esito=(String(r.id_status)==='8')?'<span class="pill pill-prob">Problema</span>':'<span class="pill pill-ok">Completata</span>';
|
||||
const eff=(r.occupato_s>0)?Math.round(r.netto_s/r.occupato_s*100):0;
|
||||
html+=`<tr><td>${fmtDateIt(r.Data)}</td><td>${r.linea||'-'}</td>
|
||||
<td>${r.matrice||'-'}${r.note_extra?'<div class="muted" style="font-size:.72rem;">'+r.note_extra+'</div>':''}</td>
|
||||
<td>${r.cliente||'-'}</td><td>${r.mescole||'<span class="muted">-</span>'}</td>
|
||||
<td>${secToHms(r.occupato_s)}</td><td>${secToHms(r.pause_s)}</td><td>${secToHms(r.netto_s)}</td>
|
||||
<td>${eff}%</td><td>${num(r.kgprod)}</td><td>${num(r.mtprod)}</td><td>${num(r.scarto)}</td>
|
||||
<td>${r.scarto_perc!==null?num(r.scarto_perc)+'%':'-'}</td><td>${esito}</td></tr>`;
|
||||
});
|
||||
$('#tableBody').html(html);
|
||||
}
|
||||
function loadStats(){
|
||||
$('#kpiGrid').html('<div class="loading">Caricamento…</div>'); $('#chartsGrid, #tableCard').hide();
|
||||
$.getJSON('',{ajax:1,date_from:$('#fDateFrom').val(),date_to:$('#fDateTo').val(),line_id:$('#fLine').val(),matrice_id:$('#fMatrice').val(),mescola_id:$('#fMescola').val()},
|
||||
function(r){
|
||||
if(!r.success){ $('#kpiGrid').html('<div class="empty">Errore: '+(r.msg||'')+'</div>'); return; }
|
||||
renderKpi(r.kpi);
|
||||
if(r.kpi.n_produzioni===0){ $('#chartsGrid, #tableCard').hide(); $('#tableBody').html(''); return; }
|
||||
$('#chartsGrid, #tableCard').show(); renderCharts(r); renderTable(r.dettaglio);
|
||||
}).fail(function(){ $('#kpiGrid').html('<div class="empty">Errore di comunicazione con il server.</div>'); });
|
||||
}
|
||||
$(function(){
|
||||
const today=new Date(), past=new Date(); past.setDate(today.getDate()-30);
|
||||
$('#fDateTo').val(today.toISOString().slice(0,10)); $('#fDateFrom').val(past.toISOString().slice(0,10));
|
||||
$('#btnApply').on('click',loadStats);
|
||||
$('#btnReset').on('click',function(){ $('#fLine,#fMatrice,#fMescola').val(''); $('#fDateTo').val(today.toISOString().slice(0,10)); $('#fDateFrom').val(past.toISOString().slice(0,10)); loadStats(); });
|
||||
loadStats();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -18,6 +18,9 @@ try {
|
||||
$brand = trim($_POST['brand'] ?? '');
|
||||
$status = trim($_POST['status'] ?? '');
|
||||
$color = trim($_POST['color'] ?? '#dc2626');
|
||||
$maxHours = isset($_POST['max_hours_per_day']) ? (float)$_POST['max_hours_per_day'] : 24;
|
||||
if ($maxHours < 0) $maxHours = 0;
|
||||
if ($maxHours > 24) $maxHours = 24;
|
||||
|
||||
if ($lineNumber === '' || $lineName === '') {
|
||||
echo json_encode(['success' => false, 'message' => 'Numero linea e nome sono obbligatori.']);
|
||||
@@ -26,24 +29,26 @@ try {
|
||||
|
||||
$sql = "
|
||||
UPDATE production_lines
|
||||
SET line_number = :line_number,
|
||||
name = :name,
|
||||
model = :model,
|
||||
brand = :brand,
|
||||
status = :status,
|
||||
color = :color
|
||||
SET line_number = :line_number,
|
||||
name = :name,
|
||||
model = :model,
|
||||
brand = :brand,
|
||||
status = :status,
|
||||
color = :color,
|
||||
max_hours_per_day = :max_hours_per_day
|
||||
WHERE id = :id
|
||||
";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
':line_number' => $lineNumber,
|
||||
':name' => $lineName,
|
||||
':model' => $model,
|
||||
':brand' => $brand,
|
||||
':status' => $status,
|
||||
':color' => $color,
|
||||
':id' => $id
|
||||
':line_number' => $lineNumber,
|
||||
':name' => $lineName,
|
||||
':model' => $model,
|
||||
':brand' => $brand,
|
||||
':status' => $status,
|
||||
':color' => $color,
|
||||
':max_hours_per_day' => $maxHours,
|
||||
':id' => $id
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
|
||||
Reference in New Issue
Block a user