barcode enhancing
This commit is contained in:
parent
07acd2fa84
commit
8fccefd923
11
index.php
11
index.php
@ -12194,25 +12194,26 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<div class="modal-body text-center">
|
||||
<div id="barcodeContainer" class="p-4 bg-white border mb-3 mx-auto" style="width: fit-content;">
|
||||
<div id="barcodeContainer" class="p-4 bg-white border mb-3 mx-auto" style="width: fit-content; max-width: 100%;">
|
||||
<div id="barcodeLabelName" class="fw-bold small mb-1"></div>
|
||||
<svg id="barcodeSvg"></svg>
|
||||
<svg id="barcodeSvg" style="max-width: 100%; height: auto;"></svg>
|
||||
<div id="barcodeLabelPrice" class="fw-bold mt-1"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label small">Number of Labels</label>
|
||||
<input type="number" id="barcodeQty" class="form-control form-control-sm mx-auto" value="1" min="1" style="width: 80px;">
|
||||
</div>
|
||||
<div class="row mb-3 mx-auto" style="max-width: 200px;">
|
||||
<div class="row mb-2 mx-auto" style="max-width: 200px;">
|
||||
<div class="col-6">
|
||||
<label class="form-label small">Width (mm)</label>
|
||||
<input type="number" id="barcodeWidth" class="form-control form-control-sm" value="40" min="10">
|
||||
<input type="number" id="barcodeWidth" class="form-control form-control-sm" value="50" min="10">
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<label class="form-label small">Height (mm)</label>
|
||||
<input type="number" id="barcodeHeight" class="form-control form-control-sm" value="25" min="10">
|
||||
<input type="number" id="barcodeHeight" class="form-control form-control-sm" value="30" min="10">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-text text-center mb-1">For best scanner reliability, start at 50 × 30 mm or larger.</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal" data-en="Close" data-ar="إغلاق">Close</button>
|
||||
|
||||
@ -1,54 +1,190 @@
|
||||
function escapeBarcodeLabelHtml(value) {
|
||||
return String(value || '')
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function getSingleBarcodeLabelData() {
|
||||
return window.currentSingleBarcodeLabel || null;
|
||||
}
|
||||
|
||||
function getSingleBarcodePrintDimensions() {
|
||||
return {
|
||||
width: Math.max(parseInt(document.getElementById('barcodeWidth').value, 10) || 50, 10),
|
||||
height: Math.max(parseInt(document.getElementById('barcodeHeight').value, 10) || 30, 10)
|
||||
};
|
||||
}
|
||||
|
||||
function getSingleBarcodeOptions(sku, labelWidthMm, labelHeightMm) {
|
||||
const skuText = String(sku || '');
|
||||
const skuLength = skuText.length;
|
||||
const compact = labelWidthMm <= 40 || labelHeightMm <= 25;
|
||||
const extraCompact = labelWidthMm <= 32 || labelHeightMm <= 20;
|
||||
const showValue = !extraCompact && labelHeightMm >= 24 && skuLength <= 18;
|
||||
|
||||
let moduleWidth = 1.8;
|
||||
if (skuLength >= 18 || labelWidthMm <= 35) {
|
||||
moduleWidth = 1.1;
|
||||
} else if (skuLength >= 14 || labelWidthMm <= 40) {
|
||||
moduleWidth = 1.3;
|
||||
} else if (labelWidthMm <= 50) {
|
||||
moduleWidth = 1.6;
|
||||
}
|
||||
|
||||
let barcodeHeight = 56;
|
||||
if (labelHeightMm <= 20) {
|
||||
barcodeHeight = 30;
|
||||
} else if (labelHeightMm <= 25) {
|
||||
barcodeHeight = 38;
|
||||
} else if (labelHeightMm <= 30) {
|
||||
barcodeHeight = 46;
|
||||
} else if (labelHeightMm <= 35) {
|
||||
barcodeHeight = 56;
|
||||
} else {
|
||||
barcodeHeight = 64;
|
||||
}
|
||||
|
||||
const quietZone = extraCompact ? 4 : compact ? 6 : 10;
|
||||
|
||||
return {
|
||||
format: "CODE128",
|
||||
lineColor: "#000",
|
||||
width: moduleWidth,
|
||||
height: barcodeHeight,
|
||||
displayValue: showValue,
|
||||
fontSize: compact ? 10 : 12,
|
||||
textMargin: compact ? 2 : 4,
|
||||
margin: quietZone,
|
||||
marginTop: 0,
|
||||
marginBottom: showValue ? 2 : 0,
|
||||
fontOptions: "bold"
|
||||
};
|
||||
}
|
||||
|
||||
function buildSingleBarcodeNameHtml(nameAr, nameEn) {
|
||||
const lines = [];
|
||||
if (nameAr) {
|
||||
lines.push('<div class="label-name-ar">' + escapeBarcodeLabelHtml(nameAr) + '</div>');
|
||||
}
|
||||
if (nameEn) {
|
||||
lines.push('<div class="label-name-en">' + escapeBarcodeLabelHtml(nameEn) + '</div>');
|
||||
}
|
||||
return lines.join('');
|
||||
}
|
||||
|
||||
function buildSingleBarcodeSvgMarkup(sku, labelWidthMm, labelHeightMm) {
|
||||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
JsBarcode(svg, sku, getSingleBarcodeOptions(sku, labelWidthMm, labelHeightMm));
|
||||
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
|
||||
svg.style.width = '100%';
|
||||
svg.style.height = 'auto';
|
||||
return svg.outerHTML;
|
||||
}
|
||||
|
||||
function renderSingleBarcodePreview() {
|
||||
const label = getSingleBarcodeLabelData();
|
||||
if (!label) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dimensions = getSingleBarcodePrintDimensions();
|
||||
const width = dimensions.width;
|
||||
const height = dimensions.height;
|
||||
const nameContainer = document.getElementById('barcodeLabelName');
|
||||
const priceContainer = document.getElementById('barcodeLabelPrice');
|
||||
const previewContainer = document.getElementById('barcodeContainer');
|
||||
const svg = document.getElementById('barcodeSvg');
|
||||
|
||||
if (!nameContainer || !priceContainer || !previewContainer || !svg) {
|
||||
return;
|
||||
}
|
||||
|
||||
nameContainer.innerHTML = buildSingleBarcodeNameHtml(label.nameAr, label.nameEn);
|
||||
priceContainer.textContent = label.price ? 'OMR ' + label.price : '';
|
||||
previewContainer.style.width = Math.min(Math.max(width * 4, 180), 320) + 'px';
|
||||
previewContainer.style.maxWidth = '100%';
|
||||
previewContainer.style.padding = height <= 25 ? '12px' : '16px';
|
||||
|
||||
svg.innerHTML = '';
|
||||
JsBarcode(svg, label.sku, getSingleBarcodeOptions(label.sku, width, height));
|
||||
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
|
||||
svg.style.width = '100%';
|
||||
svg.style.height = 'auto';
|
||||
}
|
||||
|
||||
function initSingleBarcodePreviewControls() {
|
||||
['barcodeWidth', 'barcodeHeight'].forEach((id) => {
|
||||
const input = document.getElementById(id);
|
||||
if (!input || input.dataset.barcodePreviewBound === '1') {
|
||||
return;
|
||||
}
|
||||
|
||||
input.dataset.barcodePreviewBound = '1';
|
||||
input.addEventListener('input', renderSingleBarcodePreview);
|
||||
input.addEventListener('change', renderSingleBarcodePreview);
|
||||
});
|
||||
}
|
||||
|
||||
initSingleBarcodePreviewControls();
|
||||
|
||||
window.printItemBarcode = function(sku, nameAr, nameEn, price) {
|
||||
if (!sku) {
|
||||
Swal.fire('Error', 'This item has no SKU/Barcode assigned.', 'error');
|
||||
return;
|
||||
}
|
||||
document.getElementById('barcodeLabelName').innerHTML = '<div style="font-weight:bold; font-size:12px; direction:rtl; margin-bottom:2px;">' + nameAr + '</div><div style="font-size:10px;">' + nameEn + '</div>';
|
||||
document.getElementById('barcodeLabelPrice').textContent = 'OMR ' + price;
|
||||
|
||||
JsBarcode("#barcodeSvg", sku, {
|
||||
format: "CODE128",
|
||||
lineColor: "#000",
|
||||
width: 2,
|
||||
height: 50,
|
||||
displayValue: true
|
||||
});
|
||||
|
||||
|
||||
window.currentSingleBarcodeLabel = {
|
||||
sku: String(sku),
|
||||
nameAr: nameAr || '',
|
||||
nameEn: nameEn || '',
|
||||
price: price || ''
|
||||
};
|
||||
|
||||
renderSingleBarcodePreview();
|
||||
|
||||
const modal = new bootstrap.Modal(document.getElementById('barcodePrintModal'));
|
||||
modal.show();
|
||||
};
|
||||
|
||||
window.executeBarcodePrint = function() {
|
||||
const qty = parseInt(document.getElementById('barcodeQty').value) || 1;
|
||||
const width = parseInt(document.getElementById('barcodeWidth').value) || 40;
|
||||
const height = parseInt(document.getElementById('barcodeHeight').value) || 25;
|
||||
|
||||
// Get content
|
||||
const nameHtml = document.getElementById('barcodeLabelName').innerHTML;
|
||||
const price = document.getElementById('barcodeLabelPrice').innerText;
|
||||
const svg = document.getElementById('barcodeSvg').outerHTML;
|
||||
|
||||
// Create a hidden iframe
|
||||
const qty = Math.max(parseInt(document.getElementById('barcodeQty').value, 10) || 1, 1);
|
||||
const dimensions = getSingleBarcodePrintDimensions();
|
||||
const width = dimensions.width;
|
||||
const height = dimensions.height;
|
||||
const label = getSingleBarcodeLabelData();
|
||||
|
||||
if (!label) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nameHtml = buildSingleBarcodeNameHtml(label.nameAr, label.nameEn);
|
||||
const price = label.price ? 'OMR ' + escapeBarcodeLabelHtml(label.price) : '';
|
||||
const svg = buildSingleBarcodeSvgMarkup(label.sku, width, height);
|
||||
const compactClass = width <= 40 || height <= 25 ? 'label-compact' : '';
|
||||
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.style.position = 'absolute';
|
||||
iframe.style.width = '0px';
|
||||
iframe.style.height = '0px';
|
||||
iframe.style.border = 'none';
|
||||
document.body.appendChild(iframe);
|
||||
|
||||
|
||||
const doc = iframe.contentWindow.document;
|
||||
|
||||
|
||||
let labelsHtml = '';
|
||||
for (let i = 0; i < qty; i++) {
|
||||
labelsHtml += `
|
||||
<div class="label-container">
|
||||
<div class="label-name" style="height: auto; overflow: visible;">${nameHtml}</div>
|
||||
${svg}
|
||||
<div class="label-price">${price}</div>
|
||||
<div class="label-container ${compactClass}">
|
||||
${nameHtml ? `<div class="label-name">${nameHtml}</div>` : ''}
|
||||
<div class="barcode-wrap">${svg}</div>
|
||||
${price ? `<div class="label-price">${price}</div>` : ''}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
doc.open();
|
||||
doc.write(`
|
||||
<html>
|
||||
@ -62,17 +198,62 @@
|
||||
page-break-after: always;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
box-sizing: border-box;
|
||||
padding: 1mm;
|
||||
padding: 1.5mm 2mm;
|
||||
gap: 0.75mm;
|
||||
}
|
||||
.label-container:last-child { page-break-after: avoid; }
|
||||
.label-name { font-weight: bold; margin-bottom: 2px; line-height: 1.1; max-width: 100%; }
|
||||
.label-price { font-size: 12px; font-weight: bold; margin-top: 2px; }
|
||||
svg { max-width: 100%; height: auto; max-height: 70%; display: block; }
|
||||
.label-name {
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
line-height: 1.05;
|
||||
}
|
||||
.label-name-ar,
|
||||
.label-name-en {
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.label-name-ar {
|
||||
font-weight: 700;
|
||||
font-size: 11px;
|
||||
direction: rtl;
|
||||
}
|
||||
.label-name-en { font-size: 9px; }
|
||||
.label-price {
|
||||
font-size: 11px;
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.label-compact {
|
||||
padding: 1mm 1.5mm;
|
||||
gap: 0.5mm;
|
||||
}
|
||||
.label-compact .label-name-ar { font-size: 10px; }
|
||||
.label-compact .label-name-en { font-size: 8px; }
|
||||
.label-compact .label-price { font-size: 10px; }
|
||||
.barcode-wrap {
|
||||
width: 100%;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0 1.5mm;
|
||||
overflow: hidden;
|
||||
}
|
||||
.barcode-wrap svg {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
overflow: visible;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@ -81,7 +262,7 @@
|
||||
</html>
|
||||
`);
|
||||
doc.close();
|
||||
|
||||
|
||||
iframe.contentWindow.focus();
|
||||
setTimeout(() => {
|
||||
iframe.contentWindow.print();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user