2025-05-13 15:03:09 +02:00

329 lines
14 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('../include/headscript.php');
include("../class/company.php");
require_once '../class/db-functions.php'; // Includo DBHandlerSelect
$page = 'reports';
// Inizializzo la connessione con DBHandlerSelect
$dbHandler = new DBHandlerSelect();
$conn = $dbHandler->getConnection();
// Validazione iduserlogin
if (!isset($iduserlogin) || !is_numeric($iduserlogin)) {
die("Errore: ID utente non valido.");
}
// Recupera le impostazioni delle colonne e dell'ordinamento dal database
try {
$stmt = $conn->prepare("SELECT column_visibility, column_order FROM user_table_settings WHERE iduser = :iduser AND page = :page");
$stmt->execute(['iduser' => $iduserlogin, 'page' => $page]);
$userSettings = $stmt->fetch(PDO::FETCH_ASSOC);
$column_visibility = $userSettings['column_visibility'] ?? null;
$column_order = $userSettings['column_order'] ?? null;
} catch (PDOException $e) {
error_log("Errore query user_table_settings: " . $e->getMessage());
die("Errore nel caricamento delle impostazioni.");
}
// Query per ottenere tutti i report e i prodotti associati
try {
$stmt = $conn->prepare("
SELECT r.idreports, r.reportsNumberLab, r.reportDateIn, r.reportsRating, p.products_refnumber, p.products_description
FROM reports r
LEFT JOIN products p ON r.idproducts = p.idproducts
");
$stmt->execute();
$reports = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Errore query reports: " . $e->getMessage());
die("Errore nel caricamento dei report: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
}
?>
<!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">
<title>Reports</title>
<link rel="shortcut icon" href="../assets/images/favicon.ico">
<!-- Includi prima jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- DataTables CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.min.css">
<!-- DataTables Buttons CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.3.1/css/buttons.dataTables.min.css">
<!-- ColReorder CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/colreorder/1.5.6/css/colReorder.dataTables.min.css">
<!-- DataTables JS -->
<script src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>
<!-- DataTables Buttons JS -->
<script src="https://cdn.datatables.net/buttons/2.3.1/js/dataTables.buttons.min.js"></script>
<!-- Buttons ColVis JS -->
<script src="https://cdn.datatables.net/buttons/2.3.1/js/buttons.colVis.min.js"></script>
<!-- ColReorder JS -->
<script src="https://cdn.datatables.net/colreorder/1.5.6/js/dataTables.colReorder.min.js"></script>
<!-- Altri riferimenti al CSS e JS -->
<?php include('../products/cssinclude.php'); ?>
</head>
<style>
.bg-danger {
background-color: #ff4d4d !important;
/* Rosso per fallimenti */
}
.bg-success {
background-color: #28a745 !important;
/* Verde per successi */
}
.bg-warning {
background-color: #ffc107 !important;
/* Giallo per N/A o // */
}
.text-white {
color: white !important;
}
.text-dark {
color: black !important;
}
</style>
<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">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<h4 class="page-title">Reports</h4>
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12">
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table id="reportsTable" class="table table-striped table-hover" data-user="<?php echo htmlspecialchars($iduserlogin, ENT_QUOTES, 'UTF-8'); ?>" data-table="reports">
<thead>
<tr>
<th>Report Number</th>
<th>Report Date</th>
<th>Product Ref</th>
<th>Product Description</th>
<th>Rating</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($reports as $row) { ?>
<tr data-reportid="<?php echo htmlspecialchars($row['idreports'], ENT_QUOTES, 'UTF-8'); ?>">
<td><?php echo htmlspecialchars($row['reportsNumberLab'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlspecialchars($row['reportDateIn'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlspecialchars($row['products_refnumber'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlspecialchars($row['products_description'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlspecialchars($row['reportsRating'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
<td>
<button class="btn btn-info btn-sm show-analysis">Analysis</button>
<a class="btn btn-primary btn-sm report-details" href="../products/reportdetails.php?idreports=<?php echo htmlspecialchars($row['idreports'], ENT_QUOTES, 'UTF-8'); ?>">Details</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('../include/footer.php'); ?>
</div>
</div>
<script>
$(document).ready(function() {
var hasUserInteracted = false;
var columnSettings = {};
var userId = $('#reportsTable').data('user');
var tableName = $('#reportsTable').data('table');
$.ajax({
url: 'get_user_table_settings_reports.php',
method: 'POST',
data: {
user: userId,
table: tableName
},
async: false,
success: function(response) {
columnSettings = JSON.parse(response);
console.log("Impostazioni recuperate:", columnSettings);
},
error: function(xhr, status, error) {
console.error('Errore nel caricamento delle impostazioni:', xhr.responseText);
}
});
var table = $('#reportsTable').DataTable({
responsive: true,
pageLength: 50,
order: [
[0, 'asc']
],
colReorder: {
order: columnSettings && columnSettings.ColReorder ? columnSettings.ColReorder : null
},
dom: 'Bfrtip',
buttons: [{
extend: 'colvis',
text: 'Select Columns',
collectionLayout: 'fixed four-column',
postfixButtons: ['colvisRestore'],
columns: ':not(:last-child)'
}],
initComplete: function() {
if (columnSettings && columnSettings.columns) {
console.log("Applico visibilità colonne:", columnSettings.columns);
this.api().columns().every(function(index) {
var column = this;
column.visible(columnSettings.columns[index].visible, false);
});
}
this.api().columns.adjust().draw(false);
this.api().columns().every(function() {
var column = this;
if (column.index() !== 5) {
$('<input class="form-control form-control-sm" type="text" placeholder="Search"/>')
.appendTo($(column.header()))
.on('keyup change clear', function() {
if (column.search() !== this.value) {
column.search(this.value).draw();
}
});
}
});
},
stateSave: true,
stateSaveCallback: function(settings, data) {
if (hasUserInteracted) {
$.ajax({
url: 'save_user_table_settings_reports.php',
method: 'POST',
data: {
user: userId,
table: tableName,
settings: JSON.stringify(data)
},
success: function(response) {
console.log('Impostazioni salvate:', response);
},
error: function(xhr, status, error) {
console.error('Errore nel salvataggio delle impostazioni:', xhr.responseText);
}
});
}
}
});
table.on('column-reorder', function() {
hasUserInteracted = true;
console.log("Colonne riordinate");
});
table.on('column-visibility', function() {
hasUserInteracted = true;
console.log("Visibilità colonna cambiata");
});
$('#reportsTable').on('click', '.show-analysis', function() {
var tr = $(this).closest('tr');
var reportId = tr.data('reportid');
var row = $('#reportsTable').DataTable().row(tr);
var button = $(this);
button.prop('disabled', true);
button.html('<i class="fa fa-spinner fa-spin"></i> Loading...');
if (row.child.isShown()) {
row.child.hide();
tr.removeClass('shown');
button.html('Analysis');
button.prop('disabled', false);
return;
}
$.ajax({
url: 'get_analysis_by_report.php',
type: 'POST',
data: {
reportId: reportId
},
success: function(data) {
row.child(formatAnalysis(data)).show();
tr.addClass('shown');
},
complete: function() {
button.prop('disabled', false);
button.html('Analysis');
},
error: function(xhr) {
Swal.fire('Errore!', xhr.status === 404 ? 'Risorsa non trovata.' : 'Errore nel caricamento delle analisi.', 'error');
button.prop('disabled', false);
button.html('Analysis');
}
});
});
function formatAnalysis(data) {
var parsedData = JSON.parse(data);
var html = '<table class="table table-bordered child-table">';
html += '<thead><tr><th>Analysis Name</th><th>Final Rating</th></tr></thead>';
html += '<tbody>';
$.each(parsedData, function(index, analysis) {
var ratingClass = '';
var ratingValue = (analysis.finalRating || '').toUpperCase();
if (ratingValue === 'FAIL') {
ratingClass = 'bg-danger text-white';
} else if (ratingValue === 'PASS') {
ratingClass = 'bg-success text-white';
} else if (ratingValue === '//' || ratingValue === 'N/A' || ratingValue === 'DATA') {
ratingClass = 'bg-warning text-dark';
}
html += '<tr>';
html += '<td>' + (analysis.name ? analysis.name : 'N/A') + '</td>';
html += '<td class="' + ratingClass + '">' + (ratingValue || 'N/A') + '</td>';
html += '</tr>';
});
html += '</tbody></table>';
return html;
}
});
</script>
</body>
</html>