Add per-customer quantity subtotals on CRM orders

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-18 09:59:17 +03:30
parent d7a68c2e51
commit f5632ed464
5 changed files with 52 additions and 3 deletions

View File

@@ -749,6 +749,13 @@ button.badge:hover {
color: var(--text-primary);
}
.data-table tr.subtotal-row td {
background: color-mix(in srgb, var(--bg-muted) 55%, transparent);
font-weight: 600;
color: var(--text-secondary);
border-top: 1px solid var(--border);
}
a.code-link {
color: var(--accent);
font-weight: 600;

View File

@@ -55,6 +55,12 @@ function checkRowMatches() {
console.assert(!rowMatches(row, 'آلفا', 'zz'), 'both');
console.assert(rowMatches({ customer_name: ' ', product_code: 'A' }, '—', ''), 'blank company');
console.assert(suggestionValues(['شرکت بتا', 'شرکت آلفا', 'شرکت آلفا'], 'آلفا').join() === 'شرکت آلفا', 'suggest');
const totals = qtyTotals([
{ note_quantity: '10', delivered_quantity: '4', remaining_quantity: '6' },
{ note_quantity: '2.5', delivered_quantity: '0', remaining_quantity: '2.5' },
]);
console.assert(totals.note === 12.5 && totals.delivered === 4 && totals.remaining === 8.5, 'qty totals');
console.assert(formatQty(12.5) === '12.5' && formatQty(4) === '4', 'format qty');
}
function groupedRows(rows) {
@@ -72,6 +78,27 @@ function groupedRows(rows) {
return list;
}
function parseQty(raw) {
const n = Number(String(raw || '').trim().replace(/,/g, ''));
return Number.isFinite(n) ? n : 0;
}
function formatQty(n) {
if (!Number.isFinite(n)) return '';
return Number.isInteger(n) ? String(n) : String(parseFloat(n.toFixed(6)));
}
function qtyTotals(items) {
return items.reduce(
(sum, row) => ({
note: sum.note + parseQty(row.note_quantity),
delivered: sum.delivered + parseQty(row.delivered_quantity),
remaining: sum.remaining + parseQty(row.remaining_quantity),
}),
{ note: 0, delivered: 0, remaining: 0 },
);
}
function cell(text, className, ltr) {
const td = document.createElement('td');
if (className) td.className = className;
@@ -192,6 +219,19 @@ function renderOrders(rows, markNew) {
body.appendChild(tr);
seenOrderKeys.add(key);
});
const totals = qtyTotals(items);
const foot = document.createElement('tr');
foot.className = 'subtotal-row';
const totalLabel = document.createElement('td');
totalLabel.colSpan = 3;
totalLabel.textContent = t('subtotal');
foot.append(
totalLabel,
cell(formatQty(totals.note), 'mono'),
cell(formatQty(totals.delivered), 'mono'),
cell(formatQty(totals.remaining), 'mono'),
);
body.appendChild(foot);
});
return added;
}

View File

@@ -57,6 +57,7 @@ const translations = {
'delivered_qty': 'Delivered',
'remaining_qty': 'Remaining',
'product_title': 'Title',
'subtotal': 'Subtotal',
'crm_loading': 'Loading orders…',
'sg_username': 'Username',
'sg_password': 'Password',
@@ -138,6 +139,7 @@ const translations = {
'delivered_qty': 'تحویل‌شده',
'remaining_qty': 'مانده',
'product_title': 'عنوان',
'subtotal': 'جمع',
'crm_loading': 'در حال دریافت سفارش‌ها…',
'sg_username': 'نام کاربری',
'sg_password': 'رمز عبور',