Files
product-slip/slips/templates/slips/print_page.html

303 lines
9.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Print Order #{{ order.id }}</title>
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
<style id="calibration-vars">
{{ calibration_css|safe }}
</style>
<style>
/* Base reset and print-specific settings */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #000;
}
/* Screen-only styling for the preview container */
.preview-controls {
background: #1e293b;
color: #fff;
padding: 1rem;
text-align: center;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.preview-controls button {
background: #3b82f6;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
}
.preview-controls button:hover:not(:disabled) {
background: #2563eb;
}
.preview-controls button:disabled {
opacity: 0.55;
cursor: wait;
}
.preview-controls .page-range {
display: inline-flex;
align-items: center;
gap: 8px;
margin-right: 12px;
font-family: sans-serif;
font-size: 14px;
vertical-align: middle;
}
.preview-controls .page-range input {
width: 4.5em;
padding: 8px 6px;
font-size: 16px;
border: 1px solid #475569;
border-radius: 6px;
background: #0f172a;
color: #fff;
}
.page-wrapper {
margin-top: 110px; /* Space for controls */
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
padding-bottom: 40px;
}
/* The physical A4 page representation */
.a4-page {
width: 210mm;
height: 297mm;
background: white;
position: relative;
box-shadow: 0 10px 25px rgba(0,0,0,0.15);
/* Important: Keep overflow visible for printing */
overflow: hidden;
page-break-after: always;
}
.a4-page.skip-print {
opacity: 0.4;
}
.page-badge {
position: absolute;
top: 8px;
right: 8px;
z-index: 2;
background: #1e293b;
color: #fff;
font-family: sans-serif;
font-size: 12px;
font-weight: bold;
padding: 4px 8px;
border-radius: 4px;
pointer-events: none;
}
/* CSS Grid for the 4 slips */
.slip-grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
width: 100%;
height: 100%;
}
.slip-quadrant {
position: relative;
width: 105mm; /* Half width */
height: 148.5mm; /* Half height */
}
/* Print Media Query */
@media print {
@page {
size: A4 portrait;
margin: 0; /* Zero margin to allow absolute positioning from edges */
}
body {
background: none;
margin: 0;
}
.preview-controls {
display: none;
}
.page-wrapper {
margin: 0;
padding: 0;
gap: 0;
display: block;
}
.a4-page {
box-shadow: none;
width: 100%;
height: 100vh; /* Fill printed page */
opacity: 1;
}
.a4-page.skip-print {
display: none !important;
}
.page-badge {
display: none;
}
}
</style>
</head>
<body>
<div class="preview-controls">
<p style="margin-bottom: 10px; font-family: sans-serif;">
Order #{{ order.id }} — {{ order.product.code }} <br>
{% if order.custom_count %}{{ order.custom_count }} Custom |{% endif %}
{{ order.canister_count }} Canisters |
{{ order.box_count }} Boxes |
{{ order.canister_pages|add:order.box_pages }} Pages
</p>
<label class="page-range">
Pages
<input id="start-from" type="number" min="1" max="{{ pages|length }}" value="1" {% if not pages %}disabled{% endif %}>
–
<input id="end-at" type="number" min="1" max="{{ pages|length }}" value="{{ pages|length }}" {% if not pages %}disabled{% endif %}>
/ {{ pages|length }}
</label>
<button id="btn-print" type="button" disabled>🖨️ Print Now</button>
<button type="button" onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
</div>
<div class="page-wrapper">
{% for page in pages %}
<div class="a4-page" data-page="{{ forloop.counter }}">
<div class="page-badge">Page {{ forloop.counter }} / {{ pages|length }}</div>
<div class="slip-grid">
{% for weight in page.slips %}
<div class="slip-quadrant">
<div class="var-field val-batch">{{ order.batch_number }}</div>
{% if show_dates %}
<div class="var-field val-date">{{ date_formatted }}</div>
<div class="var-field val-next">{{ next_control_formatted }}</div>
{% endif %}
<div class="var-field val-weight">{{ weight }}</div>
</div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
<script>
(function () {
const FIELDS = ['batch', 'date', 'next', 'weight'];
const varsEl = document.getElementById('calibration-vars');
const btn = document.getElementById('btn-print');
const startInput = document.getElementById('start-from');
const endInput = document.getElementById('end-at');
const pages = document.querySelectorAll('.a4-page');
const total = pages.length;
function cssFromCal(cal) {
const lines = [':root {'];
for (const field of FIELDS) {
const v = cal[field];
lines.push(' --' + field + '-top: ' + v.top + 'mm;');
lines.push(' --' + field + '-left: ' + v.left + 'mm;');
lines.push(' --' + field + '-font: ' + v.font + 'pt;');
lines.push(' --' + field + '-bottom-top: ' + v.bottom_top + 'mm;');
if (v.bottom_left != null) {
lines.push(' --' + field + '-bottom-left: ' + v.bottom_left + 'mm;');
}
if (v.right_left != null) {
lines.push(' --' + field + '-right-left: ' + v.right_left + 'mm;');
}
}
lines.push('}');
return lines.join('\n');
}
function clampPage(n) {
if (!total) return 1;
n = parseInt(n, 10);
if (isNaN(n) || n < 1) return 1;
if (n > total) return total;
return n;
}
function applySkip() {
let start = clampPage(startInput.value);
let end = clampPage(endInput.value);
if (start > end) end = start;
startInput.value = start;
endInput.value = end;
pages.forEach(function (el) {
const pageNum = parseInt(el.getAttribute('data-page'), 10);
el.classList.toggle('skip-print', pageNum < start || pageNum > end);
});
}
function seedFromQuery() {
const params = new URLSearchParams(window.location.search);
const from = params.get('from');
const to = params.get('to');
if (from != null) startInput.value = clampPage(from);
if (to != null) endInput.value = clampPage(to);
applySkip();
}
function enablePrint() {
btn.disabled = false;
btn.onclick = function () {
applySkip();
window.print();
};
}
startInput.addEventListener('change', applySkip);
startInput.addEventListener('input', applySkip);
endInput.addEventListener('change', applySkip);
endInput.addEventListener('input', applySkip);
seedFromQuery();
fetch('/api/calibration', { cache: 'no-store' })
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
})
.then(function (cal) {
varsEl.textContent = cssFromCal(cal);
})
.catch(function () {
// Keep server-rendered vars; still allow print
})
.finally(enablePrint);
})();
</script>
</body>
</html>