Files
2026-08-12 21:29:55 +03:00

264 lines
12 KiB
PHP

<?php include(__DIR__ . '/../include/headscript.php'); ?>
<?php
require_once __DIR__ . '/include/functions.php';
/**
* Printable maintenance sheet, laid out like the client's paper form
* "mod. 6.3-1": machine header, list of interventions with letter codes and
* frequency, then the log of performed interventions with operator + signature.
* Print to PDF from the browser.
*/
$pdo = mnt_pdo();
if (!userCan('maintenance.equipment.view')) {
http_response_code(403);
exit('Permesso negato.');
}
$equipmentId = isset($_GET['id']) && is_numeric($_GET['id']) ? (int)$_GET['id'] : 0;
$stmt = $pdo->prepare("
SELECT e.*, c.name AS category_name, pl.name AS line_name, pl.line_number, d.name AS department_name
FROM inv_equipment e
LEFT JOIN inv_categories c ON c.id = e.category_id
LEFT JOIN production_lines pl ON pl.id = e.line_id
LEFT JOIN departments d ON d.id = e.department_id
WHERE e.id = ?
");
$stmt->execute([$equipmentId]);
$equipment = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$equipment) {
http_response_code(404);
exit('Attrezzatura non trovata.');
}
$stmt = $pdo->prepare("
SELECT m.*, sup.supplier_name,
CONCAT(a.first_name, ' ', a.last_name) AS assignee_name
FROM maint_maintenances m
LEFT JOIN suppliers sup ON sup.idsupplier = m.supplier_id
LEFT JOIN employees a ON a.id = m.assignee_employee_id
WHERE m.equipment_id = ? AND m.is_active = 1
ORDER BY m.code ASC, m.title ASC
");
$stmt->execute([$equipmentId]);
$maintenances = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $pdo->prepare("
SELECT i.*, m.code AS maintenance_code, m.title AS maintenance_title,
CONCAT(e.first_name, ' ', e.last_name) AS operator_full_name,
sup.supplier_name
FROM maint_interventions i
INNER JOIN maint_maintenances m ON m.id = i.maintenance_id
LEFT JOIN employees e ON e.id = i.operator_employee_id
LEFT JOIN suppliers sup ON sup.idsupplier = i.supplier_id
WHERE i.equipment_id = ?
ORDER BY i.performed_at DESC, i.id DESC
");
$stmt->execute([$equipmentId]);
$interventions = $stmt->fetchAll(PDO::FETCH_ASSOC);
$statusLabels = mnt_lookup($pdo, 'maint_status', ['planned' => 'Pianificato', 'in_progress' => 'In corso', 'completed' => 'Completato']);
$resultLabels = mnt_lookup($pdo, 'maint_result', []);
$executionLabels = mnt_execution_types();
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php
// Must sit in <head>, before any relative URL on the page
$mntScriptName = $_SERVER['SCRIPT_NAME'] ?? '';
$mntPos = strpos($mntScriptName, '/userarea/');
$printBase = $mntPos !== false ? substr($mntScriptName, 0, $mntPos + strlen('/userarea/')) : '/userarea/';
?>
<base href="<?= mnt_h($printBase) ?>">
<title>Scheda manutenzione — <?= mnt_h($equipment['name']) ?></title>
<style>
* { box-sizing: border-box; }
body { font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #000; margin: 0; padding: 18px; background: #fff; }
.sheet { max-width: 1000px; margin: 0 auto; }
.sheet-header { display: flex; justify-content: space-between; align-items: flex-start; border: 1px solid #000; padding: 8px 10px; }
.sheet-header h1 { font-size: 15px; margin: 0; text-transform: uppercase; letter-spacing: .02em; }
.sheet-header .form-ref { text-align: right; font-size: 10px; line-height: 1.5; }
.machine-row { border: 1px solid #000; border-top: none; padding: 8px 10px; }
.machine-row .label { font-weight: bold; text-transform: uppercase; }
.machine-meta { display: flex; flex-wrap: wrap; gap: 4px 22px; margin-top: 6px; font-size: 10.5px; }
table { width: 100%; border-collapse: collapse; margin-top: 14px; }
caption { text-align: left; font-weight: bold; text-transform: uppercase; font-size: 11.5px; padding: 6px 0; }
th, td { border: 1px solid #000; padding: 5px 6px; vertical-align: top; }
th { background: #ececec; text-transform: uppercase; font-size: 10px; text-align: left; }
.col-code { width: 60px; text-align: center; }
.col-freq { width: 190px; }
.col-date { width: 90px; }
.col-operator { width: 150px; }
.col-sign { width: 130px; }
.signature-img { max-height: 42px; max-width: 120px; display: block; }
.empty-row td { text-align: center; color: #666; font-style: italic; }
.toolbar { max-width: 1000px; margin: 0 auto 14px; display: flex; gap: 8px; justify-content: flex-end; }
.toolbar button, .toolbar a { font-family: inherit; font-size: 12px; padding: 7px 14px; border-radius: 5px; border: 1px solid #2f7d8f; background: #2f7d8f; color: #fff; cursor: pointer; text-decoration: none; }
.toolbar a { background: #fff; color: #2f7d8f; }
.footnote { margin-top: 14px; font-size: 9.5px; color: #444; }
@media print {
body { padding: 0; }
.toolbar { display: none; }
table { page-break-inside: auto; }
tr { page-break-inside: avoid; page-break-after: auto; }
thead { display: table-header-group; }
}
</style>
</head>
<body>
<div class="toolbar">
<a href="manutenzioni/equipment.php?id=<?= (int)$equipment['id'] ?>">← Torna alla scheda</a>
<button type="button" onclick="window.print()">Stampa / Salva PDF</button>
</div>
<div class="sheet">
<div class="sheet-header">
<h1>Scheda manutenzione</h1>
<div class="form-ref">
ZIBOGOMMA<br>
mod. 6.3-1<br>
Stampato il <?= date('d/m/Y') ?>
</div>
</div>
<div class="machine-row">
<span class="label">Macchina:</span>
<strong><?= mnt_h($equipment['name']) ?></strong>
<?php if ($equipment['manufacturer']): ?> — <?= mnt_h($equipment['manufacturer']) ?><?php endif; ?>
<?php if ($equipment['registration_number']): ?> — matricola <?= mnt_h($equipment['registration_number']) ?><?php endif; ?>
<div class="machine-meta">
<span><strong>Categoria:</strong> <?= mnt_h($equipment['category_name'] ?: '—') ?></span>
<?php if ($equipment['line_name']): ?>
<span><strong>Linea:</strong> <?= (int)$equipment['line_number'] ?> — <?= mnt_h($equipment['line_name']) ?></span>
<?php endif; ?>
<span><strong>Numero di serie:</strong> <?= mnt_h($equipment['serial_number'] ?: '—') ?></span>
<span><strong>Lotto:</strong> <?= mnt_h($equipment['batch_lot'] ?: '—') ?></span>
<span><strong>Messa in servizio:</strong> <?= mnt_format_date($equipment['commissioning_date']) ?></span>
<span><strong>Reparto:</strong> <?= mnt_h($equipment['department_name'] ?: '—') ?></span>
<span><strong>Ubicazione:</strong> <?= mnt_h($equipment['location'] ?: '—') ?></span>
</div>
</div>
<table>
<caption>Interventi previsti</caption>
<thead>
<tr>
<th class="col-code">Tipo</th>
<th>Intervento</th>
<th class="col-freq">Frequenza</th>
<th class="col-operator">Esecuzione</th>
</tr>
</thead>
<tbody>
<?php if (!$maintenances): ?>
<tr class="empty-row">
<td colspan="4">Nessuna manutenzione attiva definita.</td>
</tr>
<?php else: ?>
<?php foreach ($maintenances as $maintenance): ?>
<tr>
<td class="col-code"><strong><?= mnt_h($maintenance['code'] ?: '—') ?></strong></td>
<td>
<strong><?= mnt_h($maintenance['title']) ?></strong>
<?php if ($maintenance['is_critical']): ?> (critica)<?php endif; ?>
<?php if ($maintenance['description']): ?>
<div><?= nl2br(mnt_h($maintenance['description'])) ?></div>
<?php endif; ?>
</td>
<td><?= mnt_h(mnt_format_frequency(
$maintenance['frequency_value'] !== null ? (int)$maintenance['frequency_value'] : null,
$maintenance['frequency_unit'],
$maintenance['frequency_note']
)) ?></td>
<td>
<?= mnt_h($executionLabels[$maintenance['execution_type']] ?? '') ?>
<?php if ($maintenance['supplier_name']): ?>
<div><?= mnt_h($maintenance['supplier_name']) ?></div>
<?php elseif ($maintenance['assignee_name']): ?>
<div><?= mnt_h($maintenance['assignee_name']) ?></div>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<table>
<caption>Registro interventi eseguiti</caption>
<thead>
<tr>
<th class="col-date">Data</th>
<th class="col-code">Tipo</th>
<th>Intervento / note</th>
<th class="col-operator">Operatore</th>
<th class="col-sign">Firma</th>
</tr>
</thead>
<tbody>
<?php if (!$interventions): ?>
<tr class="empty-row">
<td colspan="5">Nessun intervento registrato.</td>
</tr>
<?php else: ?>
<?php foreach ($interventions as $intervention): ?>
<tr>
<td><?= mnt_format_date($intervention['performed_at']) ?></td>
<td class="col-code"><?= mnt_h($intervention['maintenance_code'] ?: '—') ?></td>
<td>
<strong><?= mnt_h($intervention['maintenance_title']) ?></strong>
<div>
<?= mnt_h($statusLabels[$intervention['status']] ?? $intervention['status']) ?>
<?php if ($intervention['result']): ?>
— <?= mnt_h($resultLabels[$intervention['result']] ?? $intervention['result']) ?>
<?php endif; ?>
</div>
<?php if ($intervention['notes']): ?>
<div><?= nl2br(mnt_h($intervention['notes'])) ?></div>
<?php endif; ?>
<?php if ($intervention['materials']): ?>
<div>Materiali: <?= mnt_h($intervention['materials']) ?></div>
<?php endif; ?>
</td>
<td>
<?= mnt_h($intervention['operator_full_name'] ?: ($intervention['operator_name'] ?: '—')) ?>
<?php if ($intervention['supplier_name']): ?>
<div><?= mnt_h($intervention['supplier_name']) ?></div>
<?php endif; ?>
</td>
<td>
<?php if ($intervention['signature_path']): ?>
<img class="signature-img" alt="Firma"
src="manutenzioni/ajax/download_file.php?scope=signature&id=<?= (int)$intervention['id'] ?>">
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<div class="footnote">
Documento generato automaticamente dal portale ZIBO — registro attrezzature e manutenzioni.
</div>
</div>
</body>
</html>