Files
itflow/functions/print_sheet.php

334 lines
21 KiB
PHP

<?php
/*
* Printed sheets - a ticket's work order (agent/ticket.php) and an asset's spec sheet
* (agent/asset.php). Each page draws its sheet hidden on screen; printing the page - its Print, or
* the browser's own - prints the sheet and nothing else (css .itflow-print-sheet), so what comes
* out is the same either way. Black on white in either theme, with the company's letterhead.
*
* The work order is safe to hand to the client: its public replies only, and the fill-ins the
* client can see - never internal notes. The spec sheet never shows credentials or license keys,
* as it gets left in racks and on desks; its QR code opens the asset in ITFlow, signed in.
*/
/** The letterhead: the company on the left, what this is and its reference on the right - then its title. */
function printSheetHeadHtml($kind, $reference, $title, array $subtitle = [], array $meta = [], $aside_html = '') {
$company = billingCompany();
$company_lines = array_filter([
formatAddress($company['company_address'] ?? '', $company['company_city'] ?? '', $company['company_state'] ?? '', $company['company_zip'] ?? '', '', ', '),
implode(' · ', array_filter([$company['phone_display'] ?? '', $company['company_email'] ?? '', $company['company_website'] ?? ''])),
]);
$html = '<div class="itflow-print-head">'
. '<div class="itflow-print-company">'
. (!empty($company['logo_url']) ? '<img class="itflow-print-logo" src="' . escapeHtml($company['logo_url']) . '" alt="">' : '')
. '<div><div class="itflow-print-company-name">' . escapeHtml($company['company_name'] ?? '') . '</div>'
. implode('', array_map(function ($line) {
return '<div>' . escapeHtml($line) . '</div>';
}, $company_lines))
. '</div></div>'
. '<div class="itflow-print-doc"><div class="itflow-print-kind">' . escapeHtml($kind) . '</div>'
. ($reference !== '' ? '<div class="itflow-print-ref">' . escapeHtml($reference) . '</div>' : '')
. implode('', array_map(function ($line) {
return '<div>' . escapeHtml($line) . '</div>';
}, array_filter($meta)))
. '</div>'
. ($aside_html !== '' ? '<div class="itflow-print-qr">' . $aside_html . '</div>' : '')
. '</div>';
$html .= '<h1 class="itflow-print-title">' . escapeHtml($title) . '</h1>';
$subtitle = array_filter(array_map('trim', $subtitle), 'strlen');
if ($subtitle) {
$html .= '<div class="itflow-print-subtitle">' . escapeHtml(implode(' · ', $subtitle)) . '</div>';
}
return $html;
}
/** Label and value pairs, leaving out the empty ones - [label => value HTML]. '' when none are left. */
function printSheetFieldsHtml(array $fields) {
$html = '';
foreach ($fields as $label => $value) {
if (trim(strip_tags((string) $value)) === '') {
continue;
}
$html .= '<dt>' . escapeHtml($label) . '</dt><dd>' . $value . '</dd>';
}
return $html === '' ? '' : '<dl class="itflow-print-fields">' . $html . '</dl>';
}
/** A boxed block with a small heading - '' when it has nothing in it. */
function printSheetBoxHtml($heading, $body_html) {
if (trim(strip_tags((string) $body_html, '<img>')) === '') {
return '';
}
return '<div class="itflow-print-box"><div class="itflow-print-heading">' . escapeHtml($heading) . '</div>' . $body_html . '</div>';
}
/** A section of the sheet - a heading over its body; '' when it has nothing in it. */
function printSheetSectionHtml($heading, $body_html, $attributes = '') {
if (trim(strip_tags((string) $body_html, '<img><input>')) === '') {
return '';
}
return '<section class="itflow-print-section" ' . $attributes . '><h2 class="itflow-print-heading">' . escapeHtml($heading) . '</h2>' . $body_html . '</section>';
}
/** Ruled lines to write on. */
function printSheetLinesHtml($count) {
return '<div class="itflow-print-lines">' . str_repeat('<div></div>', max(1, intval($count))) . '</div>';
}
/** Who printed it and when, for the foot of a sheet. */
function printSheetPrintedText() {
global $session_name;
return 'Printed ' . formatDateTime(date('Y-m-d H:i:s')) . (!empty($session_name) ? ' by ' . $session_name : '');
}
/** Lines of text with line breaks kept, escaped. */
function printSheetText($text) {
return nl2br(escapeHtml(trim((string) $text)));
}
/*
* A ticket's work order: who and where, the job's details, its assets, the description, the
* fill-ins the client can see, the tasks to tick, its public replies oldest first, and lines to
* sign off on-site work. $ticket is agent/ticket.php's row (the ticket with its client, contact,
* location, asset, assignee, status and category); $replies its replies, any order.
*/
function ticketWorkOrderHtml(array $ticket, $purifier, array $replies) {
global $mysqli;
$ticket_id = intval($ticket['ticket_id']);
$client_id = intval($ticket['ticket_client_id']);
// Who it's for
$contact_lines = [];
if (!empty($ticket['contact_name'])) {
$contact_lines[] = '<div class="fw-semibold">' . escapeHtml($ticket['contact_name']) . '</div>'
. (!empty($ticket['contact_title']) ? '<div>' . escapeHtml($ticket['contact_title']) . '</div>' : '');
foreach ([['contact_phone', 'contact_phone_country_code', 'Phone'], ['contact_mobile', 'contact_mobile_country_code', 'Mobile']] as [$phone, $code, $label]) {
if (!empty($ticket[$phone])) {
$contact_lines[] = '<div>' . $label . ' ' . escapeHtml(formatPhoneNumber($ticket[$phone], $ticket[$code] ?? ''))
. ($phone === 'contact_phone' && !empty($ticket['contact_extension']) ? ' x' . escapeHtml($ticket['contact_extension']) : '') . '</div>';
}
}
if (!empty($ticket['contact_email'])) {
$contact_lines[] = '<div>' . escapeHtml($ticket['contact_email']) . '</div>';
}
}
$for_html = ($client_id ? '<div class="itflow-print-strong">' . escapeHtml($ticket['client_name']) . '</div>' : '') . implode('', $contact_lines);
// Where - the site, how to get in
$site_html = '';
if (intval($ticket['location_id'] ?? 0)) {
$address = locationAddressText($ticket, true);
$site_html = '<div class="itflow-print-strong">' . escapeHtml($ticket['location_name']) . '</div>'
. ($address !== '' ? '<div>' . printSheetText($address) . '</div>' : '')
. (!empty($ticket['location_phone']) ? '<div>Phone ' . escapeHtml(formatPhoneNumber($ticket['location_phone'], $ticket['location_phone_country_code'] ?? '')) . (!empty($ticket['location_phone_extension']) ? ' x' . escapeHtml($ticket['location_phone_extension']) : '') . '</div>' : '')
. (!empty($ticket['location_hours']) ? '<div>Hours ' . escapeHtml($ticket['location_hours']) . '</div>' : '')
. (trim((string) ($ticket['location_notes'] ?? '')) !== '' ? '<div class="itflow-print-note">' . printSheetText($ticket['location_notes']) . '</div>' : '');
}
// The job
$schedule = !empty($ticket['ticket_schedule']) ? ticketScheduleShortText($ticket['ticket_schedule'], $ticket['ticket_schedule_end'] ?? null) : '';
$job_html = printSheetFieldsHtml([
'Status' => escapeHtml($ticket['ticket_status_name'] ?? ''),
'Priority' => escapeHtml($ticket['ticket_priority'] ?? ''),
'Category' => escapeHtml($ticket['category_name'] ?? ''),
'Assigned' => escapeHtml(intval($ticket['ticket_assigned_to'] ?? 0) ? ($ticket['user_name'] ?? '') : 'Not assigned'),
'Scheduled' => escapeHtml($schedule . ($schedule !== '' && intval($ticket['ticket_onsite'] ?? 0) ? ', on site' : '')),
'Opened' => escapeHtml(formatDate($ticket['ticket_created_at'])),
'Their ref' => escapeHtml($ticket['ticket_vendor_ticket_number'] ?? ''),
]);
// Its assets - the ticket's own first
$assets = [];
$asset_sql = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_serial, interface_ip FROM assets
LEFT JOIN asset_interfaces ON interface_asset_id = asset_id AND interface_primary = 1
WHERE asset_id = " . intval($ticket['ticket_asset_id']) . " OR asset_id IN (SELECT asset_id FROM ticket_assets WHERE ticket_id = $ticket_id)
ORDER BY asset_id = " . intval($ticket['ticket_asset_id']) . " DESC, asset_name ASC");
while ($asset = mysqli_fetch_assoc($asset_sql)) {
$assets[] = '<tr><td class="fw-semibold">' . escapeHtml($asset['asset_name']) . '</td><td>' . escapeHtml($asset['asset_type']) . '</td><td>' . escapeHtml(trim($asset['asset_make'] . ' ' . $asset['asset_model'])) . '</td>'
. '<td class="font-monospace">' . escapeHtml($asset['asset_serial']) . '</td><td class="font-monospace">' . escapeHtml($asset['interface_ip']) . '</td></tr>';
}
$assets_html = $assets ? '<table class="itflow-print-table"><thead><tr><th>Asset</th><th>Type</th><th>Model</th><th>Serial</th><th>IP</th></tr></thead><tbody>' . implode('', $assets) . '</tbody></table>' : '';
// The fill-ins the client can see
$fill_ins = [];
foreach (ticketFillInsStored('ticket', $ticket_id) as $answer) {
if ($answer['client']) {
$fill_ins[$answer['name']] = printSheetText(ticketFillInText($answer)) ?: '<span class="itflow-print-muted">Not answered</span>';
}
}
// The tasks, to tick
$tasks = [];
$task_sql = mysqli_query($mysqli, "SELECT task_name, task_completed_at FROM tasks WHERE task_ticket_id = $ticket_id ORDER BY task_order ASC, task_id ASC");
while ($task = mysqli_fetch_assoc($task_sql)) {
$done = !empty($task['task_completed_at']);
$tasks[] = '<li class="' . ($done ? 'is-done' : '') . '"><span class="itflow-print-check" aria-hidden="true">' . ($done ? '&#10003;' : '') . '</span>' . escapeHtml($task['task_name']) . '</li>';
}
$tasks_html = $tasks ? '<ul class="itflow-print-tasks">' . implode('', $tasks) . '</ul>' : '';
// The replies the client saw - oldest first, as it happened
$public = array_values(array_filter($replies, function ($reply) {
return ($reply['ticket_reply_type'] ?? '') !== 'Internal';
}));
usort($public, function ($a, $b) {
return intval($a['ticket_reply_id']) <=> intval($b['ticket_reply_id']);
});
$replies_html = '';
foreach ($public as $reply) {
$from_client = $reply['ticket_reply_type'] === 'Client';
$who = $from_client ? ($reply['contact_name'] ?: 'The client') : ($reply['user_name'] ?: 'Your team');
$replies_html .= '<div class="itflow-print-reply' . ($from_client ? ' is-client' : '') . '">'
. '<div class="itflow-print-reply-head"><span class="fw-semibold">' . escapeHtml($who) . '</span>'
. '<span>' . ($from_client ? 'From the client' : 'Reply') . ' &middot; ' . escapeHtml(formatDateTime($reply['ticket_reply_created_at'])) . '</span></div>'
. '<div class="itflow-print-prose">' . ticketHtmlForAgents($purifier, $reply['ticket_reply']) . '</div></div>';
}
// Signing off the work, on site
$assignee = intval($ticket['ticket_assigned_to'] ?? 0) ? (string) ($ticket['user_name'] ?? '') : '';
$sign_html = '<div class="itflow-print-times">'
. implode('', array_map(function ($label) {
return '<div><div class="itflow-print-sign-line"></div><span>' . $label . '</span></div>';
}, ['Date', 'Arrived', 'Left', 'Time on site'])) . '</div>'
. '<div class="itflow-print-label">Work done</div>' . printSheetLinesHtml(5)
. '<div class="itflow-print-sign">'
. '<div><div class="itflow-print-sign-line">' . escapeHtml($assignee) . '</div><span>Technician</span></div>'
. '<div><div class="itflow-print-sign-line"></div><span>Signature</span></div>'
. '<div><div class="itflow-print-sign-line">' . (!empty($ticket['contact_name']) ? escapeHtml($ticket['contact_name']) : '') . '</div><span>Client name</span></div>'
. '<div><div class="itflow-print-sign-line"></div><span>Client signature</span></div>'
. '</div>';
$reference = ticketReference($ticket);
$html = '<div class="itflow-print-sheet" data-print-sheet="work-order">'
. printSheetHeadHtml('Work order', $reference, (string) $ticket['ticket_subject'], [], ['Opened ' . formatDate($ticket['ticket_created_at'])])
. '<div class="itflow-print-grid">'
. printSheetBoxHtml('For', $for_html)
. printSheetBoxHtml('Site', $site_html)
. printSheetBoxHtml('Job', $job_html)
. '</div>'
. printSheetSectionHtml('Assets', $assets_html)
. printSheetSectionHtml('Description', '<div class="itflow-print-prose">' . ticketHtmlForAgents($purifier, $ticket['ticket_details']) . '</div>')
. printSheetSectionHtml('Details', printSheetFieldsHtml($fill_ins))
. printSheetSectionHtml('Tasks', $tasks_html, 'data-print-tasks')
. printSheetSectionHtml('Conversation', $replies_html, 'data-print-replies')
. '<section class="itflow-print-section itflow-print-signoff" data-print-signoff><h2 class="itflow-print-heading">Sign-off</h2>' . $sign_html . '</section>'
. '<div class="itflow-print-foot">' . escapeHtml($reference . ' · ' . printSheetPrintedText()) . '</div>'
. '</div>';
return $html;
}
/*
* An asset's spec sheet: what it is, its hardware and network, where it is and who has it, its
* interfaces, what is in it, the licenses and services it is part of, and its quick notes - with
* a QR code that opens it in ITFlow. $row is agent/asset.php's row; $more what the page worked
* out for its tabs (interfaces and their links, licenses, services, rack, guests, holds, specs).
*/
function assetSpecSheetHtml(array $row, array $more) {
global $config_base_url;
$asset_id = intval($row['asset_id']);
$client_id = intval($row['client_id']);
// Hardware
$hardware = [
'Type' => escapeHtml($row['asset_type']),
'Model' => escapeHtml(trim($row['asset_make'] . ' ' . $row['asset_model'])),
'Serial' => '<span class="font-monospace">' . escapeHtml($row['asset_serial']) . '</span>',
(assetTypeOsLabel($row['asset_type']) === 'Firmware' ? 'Firmware' : 'OS') => escapeHtml($row['asset_os']),
];
// Its type's details - dates as dates, as "in 23 days" goes stale on paper
foreach ($more['spec_rows'] ?? [] as $spec_row) {
$hardware[$spec_row['label']] = escapeHtml($spec_row['flag']
? formatDate($spec_row['flag']['date']) . ($spec_row['flag']['tone'] === 'danger' ? ' - overdue' : '')
: $spec_row['text']);
}
if (!empty($more['guests'])) {
$hardware['Runs'] = escapeHtml(implode(', ', $more['guests']));
}
$warranty = assetWarranty($row['asset_warranty_expire'] ?? '');
$hardware['Warranty'] = $warranty ? escapeHtml(($warranty['tone'] === 'danger' ? 'Ended ' : 'Until ') . formatDate($warranty['date'])) : '';
$hardware['Purchased'] = escapeHtml(formatDate($row['asset_purchase_date']));
$hardware['Installed'] = escapeHtml(formatDate($row['asset_install_date']));
$hardware['Vendor'] = escapeHtml($row['vendor_name']);
$hardware['Order ref'] = escapeHtml($row['asset_purchase_reference']);
// Network - its primary address, and its links
$network = [
'IP' => '<span class="font-monospace">' . escapeHtml($row['interface_ip']) . '</span>',
'NAT' => '<span class="font-monospace">' . escapeHtml($row['interface_nat_ip']) . '</span>',
'IPv6' => '<span class="font-monospace">' . escapeHtml($row['interface_ipv6']) . '</span>',
'MAC' => '<span class="font-monospace">' . escapeHtml($row['interface_mac']) . '</span>',
'Network' => escapeHtml($row['network_name']),
];
foreach (assetLinks($row['asset_uri'], $row['asset_uri_2'], $row['asset_uri_client']) as [$label, $url]) {
$network[$label === 'Client link' ? 'Client link' : 'Link'] = ($network[$label === 'Client link' ? 'Client link' : 'Link'] ?? '') . '<div class="text-break">' . escapeHtml($url) . '</div>';
}
// Where it is, and who has it
$rack = $more['rack'] ?? null;
$where = [
'Client' => escapeHtml($row['client_name']),
'Location' => escapeHtml($row['location_name']) . (trim((string) $row['asset_physical_location']) !== '' ? '<div>' . escapeHtml($row['asset_physical_location']) . '</div>' : ''),
'Rack' => $rack ? escapeHtml($rack['rack_name']) . '<div>' . rackPositionLabel($rack['unit_start_number'], $rack['unit_end_number']) . ', ' . ($rack['unit_face'] === 'rear' ? 'rear' : 'front')
. (trim((string) ($rack['holder_name'] ?? '')) !== '' ? escapeHtml(' - slot ' . intval($rack['unit_position']) . ' in ' . $rack['holder_name']) : '') . '</div>' : '',
'Assigned' => !empty($row['contact_name']) ? escapeHtml($row['contact_name'])
. (!empty($row['contact_phone']) ? '<div>' . escapeHtml(formatPhoneNumber($row['contact_phone'], $row['contact_phone_country_code']) . (!empty($row['contact_extension']) ? ' x' . $row['contact_extension'] : '')) . '</div>' : '')
. (!empty($row['contact_email']) ? '<div>' . escapeHtml($row['contact_email']) . '</div>' : '') : '',
];
// Interfaces
$interface_rows = '';
foreach ($more['interfaces'] ?? [] as $interface_id => $interface) {
$link = $more['interface_links'][$interface_id] ?? null;
$details = implode(' · ', array_filter([$interface['interface_type'], $interface['interface_description']]));
$interface_rows .= '<tr><td><span class="fw-semibold">' . escapeHtml($interface['interface_name']) . '</span>' . (intval($interface['interface_primary']) ? ' (primary)' : '')
. ($details !== '' ? '<div class="itflow-print-muted">' . escapeHtml($details) . '</div>' : '') . '</td>'
. '<td>' . escapeHtml($interface['network_name']) . ($interface['interface_vlans'] ? '<div class="itflow-print-muted">VLAN ' . escapeHtml($interface['interface_vlans']) . '</div>' : '') . '</td>'
. '<td class="font-monospace">' . escapeHtml($interface['interface_ip']) . ($interface['interface_ipv6'] ? '<div>' . escapeHtml($interface['interface_ipv6']) . '</div>' : '') . '</td>'
. '<td class="font-monospace">' . escapeHtml($interface['interface_mac']) . '</td>'
. '<td>' . ($link ? escapeHtml($link['asset_name'] . ' ' . $link['interface_name']) : '') . '</td></tr>';
}
$interfaces_html = $interface_rows !== '' ? '<table class="itflow-print-table"><thead><tr><th>Interface</th><th>Network</th><th>Address</th><th>MAC</th><th>Connected to</th></tr></thead><tbody>' . $interface_rows . '</tbody></table>' : '';
// What is in it - a chassis and its nodes
$holds_rows = '';
foreach ($more['holds'] ?? [] as $held) {
// rackUnitView() items come escaped
$holds_rows .= '<tr><td>Slot ' . intval($held['slot']) . '</td><td class="fw-semibold">' . $held['name'] . '</td><td>' . ($held['make_model'] ?: $held['type_label']) . '</td></tr>';
}
$holds_html = $holds_rows !== '' ? '<table class="itflow-print-table"><tbody>' . $holds_rows . '</tbody></table>' : '';
// Licenses - names, never their keys
$license_rows = '';
foreach ($more['software'] ?? [] as $software) {
$license_rows .= '<tr><td class="fw-semibold">' . escapeHtml(trim($software['software_name'] . ' ' . $software['software_version'])) . '</td><td>' . escapeHtml($software['software_type']) . '</td>'
. '<td>' . escapeHtml($software['software_license_type']) . '</td><td>' . (!empty($software['software_expire']) ? 'Expires ' . escapeHtml(formatDate($software['software_expire'])) : '') . '</td></tr>';
}
$licenses_html = $license_rows !== '' ? '<table class="itflow-print-table"><tbody>' . $license_rows . '</tbody></table>' : '';
// Services it is part of
$service_rows = '';
foreach ($more['services'] ?? [] as $service) {
$service_rows .= '<tr><td class="fw-semibold">' . escapeHtml($service['service_name']) . '</td><td>' . escapeHtml($service['service_importance']) . ' importance</td><td>' . escapeHtml($service['service_description']) . '</td></tr>';
}
$services_html = $service_rows !== '' ? '<table class="itflow-print-table"><tbody>' . $service_rows . '</tbody></table>' : '';
$url = "https://$config_base_url/agent/asset.php?client_id=$client_id&asset_id=$asset_id";
$qr = qrCodeSvg($url, 'role="img" aria-label="QR code that opens this asset in ITFlow"');
return '<div class="itflow-print-sheet" data-print-sheet="spec-sheet">'
. printSheetHeadHtml('Spec sheet', '', (string) $row['asset_name'],
[(string) $row['asset_type'], trim($row['asset_make'] . ' ' . $row['asset_model']), (string) $row['asset_status'], (string) $row['asset_description']],
[(string) $row['client_name']], $qr)
. '<div class="itflow-print-grid">'
. printSheetBoxHtml('Hardware', printSheetFieldsHtml($hardware))
. printSheetBoxHtml('Network', printSheetFieldsHtml($network))
. printSheetBoxHtml('Where and who', printSheetFieldsHtml($where))
. '</div>'
. printSheetSectionHtml('Interfaces', $interfaces_html)
. printSheetSectionHtml('Devices in it', $holds_html)
. printSheetSectionHtml('Licenses', $licenses_html)
. printSheetSectionHtml('Services', $services_html)
. printSheetSectionHtml('Notes', trim((string) $row['asset_notes']) !== '' ? '<div>' . printSheetText($row['asset_notes']) . '</div>' : '')
. '<div class="itflow-print-foot">' . escapeHtml(printSheetPrintedText() . ' · Scan the code to open it in ITFlow') . '</div>'
. '</div>';
}