From c883063c08406b014ec96b75c0eaa9a47634bf64 Mon Sep 17 00:00:00 2001 From: diaco Date: Sat, 14 Nov 2026 08:44:34 +0330 Subject: [PATCH] Add company and product code search on orders Co-authored-by: Cursor --- slips/static/slips/css/style.css | 60 ++++++++++++ slips/static/slips/js/crm_orders.js | 126 +++++++++++++++++++++++++- slips/static/slips/js/i18n.js | 4 + slips/templates/slips/base.html | 2 +- slips/templates/slips/crm_orders.html | 18 +++- 5 files changed, 204 insertions(+), 6 deletions(-) diff --git a/slips/static/slips/css/style.css b/slips/static/slips/css/style.css index 8701ec5..2711c6e 100644 --- a/slips/static/slips/css/style.css +++ b/slips/static/slips/css/style.css @@ -410,6 +410,66 @@ form { display: none !important; } +#crm-filters { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 1rem; + padding: 1rem 1.5rem; + border-bottom: 1px solid var(--border); +} + +#crm-filters[hidden] { + display: none !important; +} + +@media (max-width: 700px) { + #crm-filters { + grid-template-columns: 1fr; + } +} + +.suggest { + position: relative; + z-index: 2; +} + +.suggest-list { + position: absolute; + z-index: 3; + inset-inline: 0; + top: calc(100% + 0.25rem); + margin: 0; + padding: 0.25rem 0; + list-style: none; + background: var(--bg-secondary); + border: 1px solid var(--border); + border-radius: 6px; + max-height: 12rem; + overflow: auto; + box-shadow: 0 8px 24px var(--shadow); +} + +.suggest-list[hidden] { + display: none !important; +} + +.suggest-list button { + display: block; + width: 100%; + text-align: start; + background: none; + border: 0; + color: inherit; + font: inherit; + padding: 0.5rem 0.75rem; + cursor: pointer; +} + +.suggest-list button:hover, +.suggest-list button[aria-selected="true"] { + background: var(--bg-hover); +} + #add-custom-line { align-self: flex-start; } diff --git a/slips/static/slips/js/crm_orders.js b/slips/static/slips/js/crm_orders.js index 8e68231..96a2903 100644 --- a/slips/static/slips/js/crm_orders.js +++ b/slips/static/slips/js/crm_orders.js @@ -20,10 +20,47 @@ function dateKey(raw) { return (+match[1]) * 10000 + (+match[2]) * 100 + (+match[3]); } +function companyName(row) { + return (row.customer_name || '').trim() || '—'; +} + +function rowMatches(row, companyQuery, codeQuery) { + const company = companyQuery.trim().toLocaleLowerCase(); + const code = codeQuery.trim().toLocaleLowerCase(); + const name = companyName(row).toLocaleLowerCase(); + const product = (row.product_code || '').trim().toLocaleLowerCase(); + return (!company || name.includes(company)) && (!code || product.includes(code)); +} + +function filteredRows() { + const company = document.getElementById('crm-company').value; + const code = document.getElementById('crm-code').value; + return (lastRows || []).filter((row) => rowMatches(row, company, code)); +} + +function suggestionValues(values, query) { + const q = query.trim().toLocaleLowerCase(); + const unique = [...new Set(values.map((value) => (value || '').trim()).filter(Boolean))]; + unique.sort((a, b) => a.localeCompare(b, 'fa')); + const matched = q ? unique.filter((value) => value.toLocaleLowerCase().includes(q)) : unique; + // ponytail: first 8 matches; raise the cap if a list is routinely longer + return matched.slice(0, 8); +} + +function checkRowMatches() { + const row = { customer_name: 'شرکت آلفا', product_code: 'PR-10' }; + console.assert(rowMatches(row, 'آلفا', ''), 'company'); + console.assert(rowMatches(row, '', 'pr-10'), 'code'); + console.assert(!rowMatches(row, 'بتا', ''), 'other company'); + console.assert(!rowMatches(row, 'آلفا', 'zz'), 'both'); + console.assert(rowMatches({ customer_name: ' ', product_code: 'A' }, '—', ''), 'blank company'); + console.assert(suggestionValues(['شرکت بتا', 'شرکت آلفا', 'شرکت آلفا'], 'آلفا').join() === 'شرکت آلفا', 'suggest'); +} + function groupedRows(rows) { const groups = new Map(); rows.forEach((row) => { - const name = (row.customer_name || '').trim() || '—'; + const name = companyName(row); if (!groups.has(name)) groups.set(name, []); groups.get(name).push(row); }); @@ -86,6 +123,12 @@ function showLogin() { lastRows = null; lastUsername = ''; ordersLoaded = false; + const filters = document.getElementById('crm-filters'); + if (filters) filters.hidden = true; + const company = document.getElementById('crm-company'); + const code = document.getElementById('crm-code'); + if (company) company.value = ''; + if (code) code.value = ''; } function showSession() { @@ -103,7 +146,7 @@ function renderChrome() { if (!loginForm().hidden) setStatus(t('sg_need_login')); if (lastUsername) showSession(); renderSince(); - if (lastRows) renderOrders(lastRows, false); + if (lastRows) renderOrders(filteredRows(), false); } function renderOrders(rows, markNew) { @@ -112,9 +155,10 @@ function renderOrders(rows, markNew) { const status = document.getElementById('crm-orders-status'); body.replaceChildren(); count.textContent = String(rows.length); + document.getElementById('crm-filters').hidden = !(lastRows && lastRows.length); if (!rows.length) { status.hidden = false; - status.textContent = t('no_crm_orders'); + status.textContent = lastRows && lastRows.length ? t('no_crm_matches') : t('no_crm_orders'); document.getElementById('crm-orders-table').hidden = true; return 0; } @@ -197,7 +241,7 @@ async function applyOrdersResponse(ordersRes) { lastRows = data.orders || []; showSession(); renderSince(); - const added = renderOrders(lastRows, ordersLoaded); + const added = renderOrders(filteredRows(), ordersLoaded); ordersLoaded = true; if (added > 0) showToast(t('crm_new')); } @@ -268,11 +312,85 @@ async function signOut() { } } +function applyFilter() { + if (lastRows) renderOrders(filteredRows(), false); +} + +function bindSuggest(inputId, listId, valuesFn) { + const input = document.getElementById(inputId); + const list = document.getElementById(listId); + + function close() { + list.hidden = true; + list.replaceChildren(); + input.setAttribute('aria-expanded', 'false'); + } + + function choose(value) { + input.value = value; + close(); + applyFilter(); + } + + function open() { + const items = suggestionValues(valuesFn(), input.value); + list.replaceChildren(...items.map((value) => { + const li = document.createElement('li'); + const button = document.createElement('button'); + button.type = 'button'; + button.textContent = value; + button.setAttribute('role', 'option'); + button.addEventListener('mousedown', (event) => { + event.preventDefault(); + choose(value); + }); + li.appendChild(button); + return li; + })); + const show = items.length > 0 && document.activeElement === input; + list.hidden = !show; + input.setAttribute('aria-expanded', show ? 'true' : 'false'); + } + + input.addEventListener('input', () => { + applyFilter(); + open(); + }); + input.addEventListener('focus', open); + input.addEventListener('blur', close); + input.addEventListener('keydown', (event) => { + if (event.key === 'Escape') { + close(); + return; + } + if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp' && event.key !== 'Enter') return; + if (list.hidden) open(); + const buttons = [...list.querySelectorAll('button')]; + if (!buttons.length) return; + event.preventDefault(); + const current = buttons.findIndex((button) => button.getAttribute('aria-selected') === 'true'); + if (event.key === 'Enter') { + if (current >= 0) choose(buttons[current].textContent); + return; + } + const step = event.key === 'ArrowDown' ? 1 : -1; + const nextIndex = current < 0 ? (step > 0 ? 0 : buttons.length - 1) : (current + step + buttons.length) % buttons.length; + const next = buttons[nextIndex]; + buttons.forEach((button) => button.removeAttribute('aria-selected')); + next.setAttribute('aria-selected', 'true'); + next.scrollIntoView({ block: 'nearest' }); + }); +} + document.addEventListener('DOMContentLoaded', () => { document.getElementById('crm-refresh').addEventListener('click', loadCrmOrders); document.getElementById('crm-forget').addEventListener('click', signOut); + bindSuggest('crm-company', 'crm-company-list', () => (lastRows || []).map(companyName)); + bindSuggest('crm-code', 'crm-code-list', () => (lastRows || []).map((row) => row.product_code)); loginForm().addEventListener('submit', signIn); loadCrmOrders(); }); +checkRowMatches(); + document.addEventListener('slip-language', renderChrome); diff --git a/slips/static/slips/js/i18n.js b/slips/static/slips/js/i18n.js index ae4b397..4095f8b 100644 --- a/slips/static/slips/js/i18n.js +++ b/slips/static/slips/js/i18n.js @@ -51,6 +51,7 @@ const translations = { 'crm_orders_title': 'Orders', 'refresh': 'Refresh', 'customer': 'Customer', + 'company_name': 'Company name', 'delivery_note': 'Note #', 'note_qty': 'Ordered', 'delivered_qty': 'Delivered', @@ -65,6 +66,7 @@ const translations = { 'sg_need_login': 'Sign in to load orders.', 'sg_rejected': 'SystemGroup rejected this username or password.', 'no_crm_orders': 'No orders after this date', + 'no_crm_matches': 'No orders match this search', 'orders_after': 'Orders after', 'crm_new': 'New orders added', 'status': 'Status', @@ -130,6 +132,7 @@ const translations = { 'crm_orders_title': 'حواله‌ها', 'refresh': 'به‌روزرسانی', 'customer': 'مشتری', + 'company_name': 'نام شرکت', 'delivery_note': 'شماره حواله', 'note_qty': 'مقدار حواله', 'delivered_qty': 'تحویل‌شده', @@ -144,6 +147,7 @@ const translations = { 'sg_need_login': 'برای دیدن حواله‌ها وارد شوید.', 'sg_rejected': 'نام کاربری یا رمز سامانه گروه نادرست است.', 'no_crm_orders': 'سفارشی بعد از این تاریخ نیست', + 'no_crm_matches': 'حواله‌ای با این جستجو پیدا نشد', 'orders_after': 'حواله‌های بعد از', 'crm_new': 'سفارش جدید اضافه شد', 'status': 'وضعیت', diff --git a/slips/templates/slips/base.html b/slips/templates/slips/base.html index 3180779..ab43387 100644 --- a/slips/templates/slips/base.html +++ b/slips/templates/slips/base.html @@ -10,7 +10,7 @@ - + + {% endblock %} -- 2.49.1