Add custom labels printed before box and canister slips
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -344,6 +344,40 @@ form {
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.custom-lines {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.custom-line {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.custom-line input {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.custom-line .btn {
|
||||
flex: none;
|
||||
width: 2.5rem;
|
||||
padding-left: 0;
|
||||
padding-right: 0;
|
||||
}
|
||||
|
||||
#add-custom-line {
|
||||
align-self: flex-start;
|
||||
}
|
||||
|
||||
.custom-label-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.35rem;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
@@ -6,6 +6,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const totalWeightInput = document.getElementById('total-weight');
|
||||
const canisterWeightInput = document.getElementById('canister-weight');
|
||||
const canistersPerBoxInput = document.getElementById('canisters-per-box');
|
||||
const customLines = document.getElementById('custom-lines');
|
||||
const previewDisplay = document.getElementById('preview-display');
|
||||
const form = document.getElementById('order-form');
|
||||
|
||||
@@ -58,6 +59,45 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
dateInput.addEventListener('change', updateNextControl);
|
||||
dateInput.addEventListener('input', updateNextControl);
|
||||
|
||||
function collectCustomLabels() {
|
||||
const lines = [];
|
||||
customLines.querySelectorAll('.custom-line').forEach((row) => {
|
||||
const weight = parseFloat(row.querySelector('.custom-weight').value);
|
||||
const count = parseInt(row.querySelector('.custom-count').value, 10);
|
||||
if (weight > 0 && count >= 1) lines.push({ weight, count });
|
||||
});
|
||||
return lines;
|
||||
}
|
||||
|
||||
function addCustomLine() {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'custom-line';
|
||||
const weight = document.createElement('input');
|
||||
weight.type = 'number';
|
||||
weight.className = 'custom-weight';
|
||||
weight.required = true;
|
||||
weight.step = '0.1';
|
||||
weight.min = '0.1';
|
||||
weight.placeholder = 'kg';
|
||||
weight.setAttribute('aria-label', 'Weight (kg)');
|
||||
const count = document.createElement('input');
|
||||
count.type = 'number';
|
||||
count.className = 'custom-count';
|
||||
count.required = true;
|
||||
count.step = '1';
|
||||
count.min = '1';
|
||||
count.value = '1';
|
||||
count.setAttribute('aria-label', 'Count');
|
||||
const remove = document.createElement('button');
|
||||
remove.type = 'button';
|
||||
remove.className = 'btn btn-ghost btn-sm custom-remove';
|
||||
remove.textContent = '×';
|
||||
remove.setAttribute('aria-label', 'Remove');
|
||||
row.append(weight, count, remove);
|
||||
customLines.appendChild(row);
|
||||
weight.focus();
|
||||
}
|
||||
|
||||
// Auto calculate previews
|
||||
function updatePreview() {
|
||||
const tw = parseFloat(totalWeightInput.value) || 0;
|
||||
@@ -66,11 +106,15 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
const cc = Math.ceil(tw / cw);
|
||||
const bc = Math.ceil(cc / cpb);
|
||||
const pages = Math.ceil((cc + bc) / 4) || 0;
|
||||
let cu = 0;
|
||||
customLines.querySelectorAll('.custom-count').forEach((input) => {
|
||||
cu += parseInt(input.value, 10) || 0;
|
||||
});
|
||||
const pages = Math.ceil((cc + bc + cu) / 4) || 0;
|
||||
|
||||
const t = translations[currentLang] || translations.en;
|
||||
previewDisplay.textContent =
|
||||
`${t.canisters_unit}: ${cc} | ${t.boxes_unit}: ${bc} | ${pages} ${t.preview_pages}`;
|
||||
`${t.canisters_unit}: ${cc} | ${t.boxes_unit}: ${bc} | ${t.custom_unit}: ${cu} | ${pages} ${t.preview_pages}`;
|
||||
}
|
||||
window.updateLabelsPreview = updatePreview;
|
||||
|
||||
@@ -78,6 +122,17 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
totalWeightInput.addEventListener('input', updatePreview);
|
||||
canisterWeightInput.addEventListener('input', updatePreview);
|
||||
canistersPerBoxInput.addEventListener('input', updatePreview);
|
||||
customLines.addEventListener('input', updatePreview);
|
||||
customLines.addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('.custom-remove');
|
||||
if (!btn) return;
|
||||
btn.closest('.custom-line').remove();
|
||||
updatePreview();
|
||||
});
|
||||
document.getElementById('add-custom-line').addEventListener('click', () => {
|
||||
addCustomLine();
|
||||
updatePreview();
|
||||
});
|
||||
updatePreview();
|
||||
|
||||
// Auto weight + date visibility from product selection
|
||||
@@ -116,7 +171,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
next_control_date,
|
||||
total_weight: parseFloat(totalWeightInput.value),
|
||||
canister_weight: parseFloat(canisterWeightInput.value),
|
||||
canisters_per_box: parseInt(canistersPerBoxInput.value)
|
||||
canisters_per_box: parseInt(canistersPerBoxInput.value),
|
||||
custom_labels: collectCustomLabels()
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -129,6 +185,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
if (res.ok) {
|
||||
showToast(currentLang === 'fa' ? 'سفارش به صف چاپ اضافه شد' : 'Order added to print queue!');
|
||||
form.reset();
|
||||
customLines.replaceChildren();
|
||||
dateInput.value = localDmy(new Date());
|
||||
updateNextControl();
|
||||
syncDateFields();
|
||||
|
||||
@@ -32,6 +32,10 @@ const translations = {
|
||||
'boxes': 'Boxes',
|
||||
'canisters_unit': 'Canisters',
|
||||
'boxes_unit': 'Boxes',
|
||||
'custom': 'Custom',
|
||||
'custom_unit': 'Custom',
|
||||
'custom_labels': 'Custom Labels',
|
||||
'add_custom_label': '+ Add custom label',
|
||||
'pages_unit': 'Page(s)',
|
||||
'preview_pages': 'pages',
|
||||
'pages_needed': 'Pages Needed',
|
||||
@@ -83,6 +87,10 @@ const translations = {
|
||||
'boxes': 'تعداد کارتن',
|
||||
'canisters_unit': 'گالن',
|
||||
'boxes_unit': 'کارتن',
|
||||
'custom': 'سفارشی',
|
||||
'custom_unit': 'سفارشی',
|
||||
'custom_labels': 'برچسب سفارشی',
|
||||
'add_custom_label': '+ افزودن برچسب سفارشی',
|
||||
'pages_unit': 'صفحه',
|
||||
'preview_pages': 'صفحه',
|
||||
'pages_needed': 'تعداد صفحات',
|
||||
@@ -181,7 +189,10 @@ async function adjustCount(id, field, delta) {
|
||||
if (next < 0) return;
|
||||
const other = document.getElementById(otherId);
|
||||
const otherVal = other ? parseInt(other.textContent, 10) : 0;
|
||||
if (next === 0 && otherVal === 0) return;
|
||||
if (next === 0 && otherVal === 0) {
|
||||
const custom = document.getElementById(`custom-${id}`);
|
||||
if (!custom || !custom.querySelector('.cl-count')) return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/orders/${id}`, {
|
||||
@@ -207,6 +218,86 @@ async function adjustCount(id, field, delta) {
|
||||
}
|
||||
}
|
||||
|
||||
function renderCustomLabels(id, labels, pending) {
|
||||
const cell = document.getElementById(`custom-${id}`);
|
||||
if (!cell) return;
|
||||
const lines = (labels || []).filter((line) => line && Number(line.count) > 0);
|
||||
if (!lines.length) {
|
||||
cell.textContent = '—';
|
||||
return;
|
||||
}
|
||||
const list = document.createElement('div');
|
||||
list.className = 'custom-label-list';
|
||||
lines.forEach((line, i) => {
|
||||
const weight = Number(line.weight);
|
||||
const count = Number(line.count);
|
||||
const row = document.createElement('div');
|
||||
if (!pending) {
|
||||
row.className = 'mono';
|
||||
row.textContent = `${weight} kg × ${count}`;
|
||||
} else {
|
||||
row.className = 'count-stepper';
|
||||
row.dataset.weight = String(weight);
|
||||
const label = document.createElement('span');
|
||||
label.className = 'stepper-label';
|
||||
label.textContent = `${weight} kg`;
|
||||
const down = document.createElement('button');
|
||||
down.type = 'button';
|
||||
down.className = 'stepper-btn';
|
||||
down.textContent = '−';
|
||||
down.setAttribute('aria-label', '-');
|
||||
down.addEventListener('click', () => adjustCustomCount(id, i, -1));
|
||||
const countEl = document.createElement('span');
|
||||
countEl.className = 'cl-count';
|
||||
countEl.textContent = String(count);
|
||||
const up = document.createElement('button');
|
||||
up.type = 'button';
|
||||
up.className = 'stepper-btn';
|
||||
up.textContent = '+';
|
||||
up.setAttribute('aria-label', '+');
|
||||
up.addEventListener('click', () => adjustCustomCount(id, i, 1));
|
||||
row.append(label, down, countEl, up);
|
||||
}
|
||||
list.appendChild(row);
|
||||
});
|
||||
cell.replaceChildren(list);
|
||||
}
|
||||
|
||||
async function adjustCustomCount(id, index, delta) {
|
||||
const cell = document.getElementById(`custom-${id}`);
|
||||
if (!cell) return;
|
||||
const rows = [...cell.querySelectorAll('.count-stepper')];
|
||||
const row = rows[index];
|
||||
if (!row) return;
|
||||
const next = parseInt(row.querySelector('.cl-count').textContent, 10) + delta;
|
||||
if (next < 0) return;
|
||||
const labels = rows.map((r, i) => ({
|
||||
weight: parseFloat(r.dataset.weight),
|
||||
count: i === index ? next : parseInt(r.querySelector('.cl-count').textContent, 10),
|
||||
})).filter((line) => line.count > 0);
|
||||
const cc = parseInt(document.getElementById(`cc-${id}`)?.textContent, 10) || 0;
|
||||
const bc = parseInt(document.getElementById(`bc-${id}`)?.textContent, 10) || 0;
|
||||
if (cc === 0 && bc === 0 && labels.length === 0) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/orders/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ custom_labels: labels }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
showToast(currentLang === 'fa' ? 'خطا در ویرایش' : 'Error updating order', true);
|
||||
return;
|
||||
}
|
||||
const order = await res.json();
|
||||
renderCustomLabels(id, order.custom_labels, true);
|
||||
const pages = document.getElementById(`pages-${id}`);
|
||||
if (pages) pages.textContent = order.canister_pages + order.box_pages;
|
||||
} catch (err) {
|
||||
showToast('Network error', true);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteOrder(id) {
|
||||
const msg = currentLang === 'fa' ? 'این سفارش حذف شود؟' : 'Remove this order?';
|
||||
if (!confirm(msg)) return;
|
||||
|
||||
Reference in New Issue
Block a user