reportify_mncl/public/userarea/statkpi/substance-detail.php

301 lines
13 KiB
PHP

<?php include('../include/headscript.php'); ?>
<?php include("../class/company.php");
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);
// Recupera l'ID della sostanza dalla query string
$substance_id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;
// Query per ottenere i dettagli della sostanza
$query = "
SELECT cv.namecompoundsvocabulary AS substance_name
FROM compundsvocabulary cv
WHERE cv.idcompoundsvocabulary = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $substance_id);
$stmt->execute();
$result = $stmt->get_result();
$substance = $result->fetch_assoc();
// Query per ottenere i valori della sostanza
$queryValues = "
SELECT
rp.result_Value,
rp.result_UnitofMeasure,
r.reportDateIn,
r.reportsNumberLab,
p.products_refnumber,
p.products_description,
r.idreports
FROM result_project rp
LEFT JOIN compundsvocabulary cv ON rp.result_AnalytsName = cv.idcompoundsvocabulary
LEFT JOIN reports r ON rp.idreports = r.idreports
LEFT JOIN products p ON r.idproducts = p.idproducts
WHERE cv.idcompoundsvocabulary = ?
AND (rp.result_Value REGEXP '^[0-9]+([,][0-9]+)?$' OR rp.result_Value LIKE '<%')
ORDER BY r.reportDateIn ASC;
";
$stmtValues = $conn->prepare($queryValues);
$stmtValues->bind_param("i", $substance_id);
$stmtValues->execute();
$resultValues = $stmtValues->get_result();
$values = [];
while ($row = $resultValues->fetch_assoc()) {
$values[] = $row;
}
// Query per calcolare la percentuale di valori detected
$queryDetectablePercentage = "
SELECT
ROUND(
(COUNT(CASE WHEN rp.result_Value NOT LIKE '<%' THEN 1 END) / COUNT(*)) * 100, 2
) AS detectable_percentage
FROM result_project rp
LEFT JOIN compundsvocabulary cv ON rp.result_AnalytsName = cv.idcompoundsvocabulary
WHERE cv.idcompoundsvocabulary = ? AND cv.component_type = 'CH';
";
$stmtDetectablePercentage = $conn->prepare($queryDetectablePercentage);
$stmtDetectablePercentage->bind_param("i", $substance_id);
$stmtDetectablePercentage->execute();
$resultDetectablePercentage = $stmtDetectablePercentage->get_result();
$detectableData = $resultDetectablePercentage->fetch_assoc();
$detectablePercentage = $detectableData['detectable_percentage'] ?? 0;
// Determina la classe del badge in base alla percentuale
$badgeClass = '';
if ($detectablePercentage < 3) {
$badgeClass = 'bg-success'; // Verde
} elseif ($detectablePercentage >= 3 && $detectablePercentage < 10) {
$badgeClass = 'bg-info'; // Azzurro
} elseif ($detectablePercentage >= 10 && $detectablePercentage < 30) {
$badgeClass = 'bg-warning'; // Arancio
} else {
$badgeClass = 'bg-danger'; // Rosso
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<?php include('../include/seo.php'); ?>
<link rel="shortcut icon" href="../assets/images/favicon.ico">
<link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="../assets/css/icons.css" rel="stylesheet" type="text/css">
<link href="../assets/css/style.css" rel="stylesheet" type="text/css">
<link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.min.css" rel="stylesheet">
<script src="../assets/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.print.min.js"></script>
</head>
<body class="fixed-left">
<div id="wrapper">
<?php include('../include/navigationbar.php'); ?>
<div class="content-page">
<div class="content">
<?php include('../include/topbar.php'); ?>
<div class="page-content-wrapper">
<div class="container-fluid">
<br>
<div class="row">
<div class="col-md-12">
<div class="card shadow">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<h1 class="mb-1 text-primary">
<i class="fas fa-flask"></i>
<?php echo htmlspecialchars($substance['substance_name']); ?>
<span class="badge <?php echo $badgeClass; ?> px-3 py-2">
<?php echo $detectablePercentage; ?>% Detected
</span>
</h1>
<p class="text-muted mb-0">Comprehensive analysis and details</p>
</div>
<div class="text-end">
<span class="badge bg-success px-3 py-2">
ID: <?php echo $substance_id; ?>
</span>
<span class="badge bg-info px-3 py-2">
Last Updated: <?php echo date('d M Y'); ?>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Grafico -->
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">Distribution of Values</h5>
<div id="value-chart"></div>
</div>
</div>
</div>
</div>
<!-- Tabella -->
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">Values Table</h5>
<table id="values-table" class="display nowrap" style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Report Number</th>
<th>Product Ref</th>
<th>Value</th>
<th>Unit</th>
</tr>
</thead>
<tbody>
<?php foreach ($values as $value): ?>
<tr>
<td><?php echo htmlspecialchars($value['reportDateIn']); ?></td>
<td>
<a href="../products/reportdetails.php?idreports=<?php echo $value['idreports']; ?>" style="text-decoration:none;color:#007bff;">
<?php echo htmlspecialchars($value['reportsNumberLab']); ?>
</a>
</td>
<td><?php echo htmlspecialchars($value['products_refnumber']); ?></td>
<td>
<?php
$rawValue = str_replace(',', '.', $value['result_Value']);
if (str_starts_with($rawValue, '<')) {
echo '<span style="color: gray;">' . htmlspecialchars($rawValue) . '</span>';
} else {
echo '<span style="color: green;">' . htmlspecialchars($rawValue) . '</span>';
}
?>
</td>
<td><?php echo htmlspecialchars($value['result_UnitofMeasure']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('../include/footer.php'); ?>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const values = <?php echo json_encode(array_values(array_filter($values, function ($row) {
$rawValue = str_replace(',', '.', $row['result_Value']);
return is_numeric($rawValue) || str_starts_with($rawValue, '<');
}))); ?>;
const dataPoints = values.map((val) => ({
x: val.reportDateIn,
y: parseFloat(val.result_Value.replace(',', '.')) || null,
}));
// Configura il grafico scatter
const options = {
series: [{
name: 'Value',
data: dataPoints,
}, ],
chart: {
type: 'scatter',
height: 500,
},
xaxis: {
type: 'datetime',
title: {
text: 'Date',
},
labels: {
datetimeFormatter: {
year: 'yyyy',
month: 'MMM yyyy',
day: 'dd MMM',
},
},
},
yaxis: {
title: {
text: 'Value',
},
},
markers: {
size: 5,
},
};
const chart = new ApexCharts(document.querySelector("#value-chart"), options);
chart.render();
// Configurazione DataTables
$('#values-table').DataTable({
pageLength: 100,
dom: 'Bfrtip',
buttons: ['copy', 'csv', 'excel', 'pdf', 'print'],
order: [
[0, 'asc']
], // Ordina per data in modo crescente
columnDefs: [{
targets: 0, // Colonna delle date
render: function(data, type, row) {
if (type === 'display' || type === 'filter') {
return new Date(data).toLocaleDateString();
}
return data; // Mantiene il formato originale per l'ordinamento
},
}, ],
});
});
</script>
</body>
</html>