Add orders tab for recent SystemGroup delivery notes

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-11 13:47:25 +03:30
parent 570b084cc1
commit c1c870e911
18 changed files with 684 additions and 4 deletions

View File

@@ -133,6 +133,21 @@ html[lang="fa"] body {
align-items: center;
}
@media (max-width: 800px) {
.navbar {
overflow: visible;
}
.nav-container {
flex-wrap: wrap;
padding: 0.75rem 1rem;
gap: 0.5rem 1rem;
}
.nav-links,
.nav-setup {
gap: 1rem;
}
}
.nav-logo {
display: flex;
align-items: center;
@@ -327,6 +342,25 @@ html[lang="fa"] body {
font-weight: 600;
}
.card-header-wrap {
flex-wrap: wrap;
gap: 0.75rem;
}
.card-header-wrap > div:first-child {
flex: 1 1 16rem;
}
.card-subtitle {
margin-top: 0.25rem;
color: var(--text-secondary);
font-size: 0.875rem;
}
.card-subtitle .mono {
margin-inline-start: 0.35rem;
}
/* Forms */
form {
padding: 1.5rem;
@@ -465,6 +499,11 @@ input:focus, select:focus {
font-size: 0.875rem;
}
.btn:disabled {
opacity: 0.6;
cursor: wait;
}
.btn-lg {
padding: 1rem 2rem;
font-size: 1.125rem;
@@ -631,6 +670,26 @@ button.badge:hover {
background: var(--bg-hover);
}
.data-table tbody tr.row-new {
background: color-mix(in srgb, var(--success) 16%, transparent);
}
.data-table tr.group-row td {
background: var(--bg-muted);
font-weight: 600;
color: var(--text-primary);
}
a.code-link {
color: var(--accent);
font-weight: 600;
text-decoration: none;
}
a.code-link:hover {
text-decoration: underline;
}
.empty-state {
text-align: center;
color: var(--text-secondary);

View File

@@ -0,0 +1,180 @@
const seenOrderKeys = new Set();
let ordersLoaded = false;
let lastSince = '';
let lastRows = null;
let productCodes = new Set();
function t(key) {
const pack = translations[currentLang] || translations.en;
return pack[key] || translations.en[key] || key;
}
function orderKey(row) {
return `${row.delivery_note_number}|${row.product_code}`;
}
function dateKey(raw) {
const match = /^(\d{4})\/(\d{1,2})\/(\d{1,2})$/.exec((raw || '').trim());
if (!match) return 0;
return (+match[1]) * 10000 + (+match[2]) * 100 + (+match[3]);
}
function groupedRows(rows) {
const groups = new Map();
rows.forEach((row) => {
const name = (row.customer_name || '').trim() || '—';
if (!groups.has(name)) groups.set(name, []);
groups.get(name).push(row);
});
const list = [...groups.entries()];
list.forEach(([, items]) => {
items.sort((a, b) => dateKey(b.delivery_note_date) - dateKey(a.delivery_note_date));
});
list.sort((a, b) => dateKey(b[1][0].delivery_note_date) - dateKey(a[1][0].delivery_note_date));
return list;
}
function cell(text, className, ltr) {
const td = document.createElement('td');
if (className) td.className = className;
if (ltr) td.dir = 'ltr';
td.textContent = text || '';
return td;
}
function codeCell(row) {
const td = document.createElement('td');
td.className = 'mono';
const code = (row.product_code || '').trim();
if (!productCodes.has(code)) {
td.textContent = code;
return td;
}
const link = document.createElement('a');
const weight = (row.remaining_quantity || '').trim();
link.className = 'code-link';
link.href = `/entry/?code=${encodeURIComponent(code)}&weight=${encodeURIComponent(weight)}`;
link.textContent = code;
td.appendChild(link);
return td;
}
function renderSince() {
const wrap = document.getElementById('crm-since');
const el = document.getElementById('crm-since-date');
if (!wrap || !el) return;
if (!lastSince) {
wrap.hidden = true;
el.textContent = '';
return;
}
wrap.hidden = false;
el.textContent = lastSince;
}
function renderChrome() {
renderSince();
if (lastRows) renderOrders(lastRows, false);
}
function renderOrders(rows, markNew) {
const body = document.getElementById('crm-orders-body');
const count = document.getElementById('crm-count');
const status = document.getElementById('crm-orders-status');
body.replaceChildren();
count.textContent = String(rows.length);
if (!rows.length) {
status.hidden = false;
status.textContent = t('no_crm_orders');
document.getElementById('crm-orders-table').hidden = true;
return 0;
}
status.hidden = true;
status.textContent = '';
document.getElementById('crm-orders-table').hidden = false;
let added = 0;
groupedRows(rows).forEach(([company, items]) => {
const head = document.createElement('tr');
head.className = 'group-row';
const label = document.createElement('td');
label.colSpan = 6;
label.textContent = company;
head.appendChild(label);
body.appendChild(head);
items.forEach((row) => {
const key = orderKey(row);
const isNew = markNew && !seenOrderKeys.has(key);
if (isNew) added += 1;
const tr = document.createElement('tr');
if (isNew) tr.className = 'row-new';
if (row.notes) tr.title = row.notes;
tr.append(
cell(row.delivery_note_date, 'mono', true),
codeCell(row),
cell(row.product_title),
cell(row.note_quantity, 'mono'),
cell(row.delivered_quantity, 'mono'),
cell(row.remaining_quantity, 'mono'),
);
body.appendChild(tr);
seenOrderKeys.add(key);
});
});
return added;
}
function setStatus(message) {
const status = document.getElementById('crm-orders-status');
const body = document.getElementById('crm-orders-body');
body.replaceChildren();
document.getElementById('crm-count').textContent = '0';
document.getElementById('crm-orders-table').hidden = true;
if (!message) {
status.hidden = true;
status.textContent = '';
return;
}
status.hidden = false;
status.textContent = message;
}
async function loadCrmOrders() {
const button = document.getElementById('crm-refresh');
button.disabled = true;
if (!ordersLoaded) setStatus(t('crm_loading'));
try {
const [ordersRes, productsRes] = await Promise.all([
fetch('/api/crm-orders', { cache: 'no-store' }),
fetch('/api/products'),
]);
if (productsRes.ok) {
const products = await productsRes.json().catch(() => []);
productCodes = new Set((products || []).map((p) => (p.code || '').trim()));
}
const data = await ordersRes.json().catch(() => ({}));
if (!ordersRes.ok) {
const message = data.detail || t('no_crm_orders');
if (!ordersLoaded) setStatus(message);
showToast(message, true);
return;
}
lastSince = data.since || '';
lastRows = data.orders || [];
renderSince();
const added = renderOrders(lastRows, ordersLoaded);
ordersLoaded = true;
if (added > 0) showToast(t('crm_new'));
} catch (err) {
setStatus(t('no_crm_orders'));
showToast('Network error', true);
} finally {
button.disabled = false;
}
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('crm-refresh').addEventListener('click', loadCrmOrders);
loadCrmOrders();
});
document.addEventListener('slip-language', renderChrome);

View File

@@ -146,6 +146,24 @@ document.addEventListener('DOMContentLoaded', () => {
});
syncDateFields();
const params = new URLSearchParams(location.search);
const presetCode = params.get('code');
if (presetCode) {
const opt = [...productSelect.options].find((o) => o.dataset.code === presetCode);
if (opt) {
productSelect.value = opt.value;
productSelect.dispatchEvent(new Event('change'));
}
}
const presetWeight = params.get('weight');
if (presetWeight) {
const n = Number(String(presetWeight).replace(/[,،]/g, ''));
if (Number.isFinite(n) && n > 0) {
totalWeightInput.value = String(n);
updatePreview();
}
}
// Submit new order
form.addEventListener('submit', async (e) => {
e.preventDefault();

View File

@@ -2,6 +2,7 @@ const translations = {
'en': {
'app_title': 'Label Printer',
'data_entry': 'Data Entry',
'crm_orders': 'Orders',
'products': 'Products',
'print_queue': 'Print Queue',
'calibrate': 'Calibrate',
@@ -47,6 +48,18 @@ const translations = {
'status_cancelled': 'Cancelled',
'add_to_queue': 'Add to Print Queue',
'recent_orders': 'Recent Orders',
'crm_orders_title': 'Orders',
'refresh': 'Refresh',
'customer': 'Customer',
'delivery_note': 'Note #',
'note_qty': 'Ordered',
'delivered_qty': 'Delivered',
'remaining_qty': 'Remaining',
'product_title': 'Title',
'crm_loading': 'Loading orders…',
'no_crm_orders': 'No orders after this date',
'orders_after': 'Orders after',
'crm_new': 'New orders added',
'status': 'Status',
'no_orders': 'No orders yet',
'no_pending_orders': 'No pending orders to print.',
@@ -61,6 +74,7 @@ const translations = {
'fa': {
'app_title': 'پرینتر لیبل',
'data_entry': 'ثبت اطلاعات',
'crm_orders': 'حواله‌ها',
'products': 'محصولات',
'print_queue': 'صف چاپ',
'calibrate': 'کالیبراسیون',
@@ -106,6 +120,18 @@ const translations = {
'status_cancelled': 'لغو شده',
'add_to_queue': 'افزودن به صف چاپ',
'recent_orders': 'سفارشات اخیر',
'crm_orders_title': 'حواله‌ها',
'refresh': 'به‌روزرسانی',
'customer': 'مشتری',
'delivery_note': 'شماره حواله',
'note_qty': 'مقدار حواله',
'delivered_qty': 'تحویل‌شده',
'remaining_qty': 'مانده',
'product_title': 'عنوان',
'crm_loading': 'در حال دریافت سفارش‌ها…',
'no_crm_orders': 'سفارشی بعد از این تاریخ نیست',
'orders_after': 'حواله‌های بعد از',
'crm_new': 'سفارش جدید اضافه شد',
'status': 'وضعیت',
'no_orders': 'سفارشی ثبت نشده',
'no_pending_orders': 'سفارشی در انتظار چاپ نیست.',
@@ -173,6 +199,7 @@ function applyLanguage(lang) {
if (typeof window.updateLabelsPreview === 'function') {
window.updateLabelsPreview();
}
document.dispatchEvent(new Event('slip-language'));
}
function toggleLanguage() {