Add Jalali date range filters on CRM orders

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-21 13:44:08 +03:30
parent ee218bccae
commit 8280eeed7c
8 changed files with 84 additions and 33 deletions

View File

@@ -412,7 +412,7 @@ form {
#crm-filters {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1rem;
padding: 1rem 1.5rem;
border-bottom: 1px solid var(--border);
@@ -422,12 +422,23 @@ form {
display: none !important;
}
@media (max-width: 1100px) {
#crm-filters {
grid-template-columns: 1fr 1fr;
}
}
@media (max-width: 700px) {
#crm-filters {
grid-template-columns: 1fr;
}
}
jdp-container {
--jdp-primary: var(--accent);
font-family: inherit;
}
.suggest {
position: relative;
z-index: 2;

View File

@@ -24,18 +24,29 @@ function companyName(row) {
return (row.customer_name || '').trim() || '—';
}
function rowMatches(row, companyQuery, codeQuery) {
function rowMatches(row, companyQuery, codeQuery, start, end) {
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));
if (company && !name.includes(company)) return false;
if (code && !product.includes(code)) return false;
const startKey = dateKey(start);
const endKey = dateKey(end);
if (!startKey && !endKey) return true;
const rowDate = dateKey(row.delivery_note_date);
if (!rowDate) return false;
if (startKey && rowDate < startKey) return false;
if (endKey && rowDate > endKey) return false;
return true;
}
function filteredRows() {
const company = document.getElementById('crm-company').value;
const code = document.getElementById('crm-code').value;
return (lastRows || []).filter((row) => rowMatches(row, company, code));
const start = document.getElementById('crm-start').value;
const end = document.getElementById('crm-end').value;
return (lastRows || []).filter((row) => rowMatches(row, company, code, start, end));
}
function suggestionValues(values, query) {
@@ -48,13 +59,17 @@ function suggestionValues(values, query) {
}
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');
const row = { customer_name: 'شرکت آلفا', product_code: 'PR-10', delivery_note_date: '1403/08/15' };
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', delivery_note_date: '1403/01/01' }, '—', '', '', ''), 'blank company');
console.assert(suggestionValues(['شرکت بتا', 'شرکت آلفا', 'شرکت آلفا'], 'آلفا').join() === 'شرکت آلفا', 'suggest');
console.assert(rowMatches(row, '', '', '1403/08/15', ''), 'on start');
console.assert(rowMatches(row, '', '', '1403/08/01', '1403/08/20'), 'in range');
console.assert(!rowMatches(row, '', '', '1403/08/16', ''), 'before start');
console.assert(!rowMatches(row, '', '', '', '1403/08/14'), 'after end');
const totals = qtyTotals([
{ note_quantity: '10', delivered_quantity: '4', remaining_quantity: '6' },
{ note_quantity: '2.5', delivered_quantity: '0', remaining_quantity: '2.5' },
@@ -124,17 +139,10 @@ function codeCell(row) {
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 syncDateDefaults() {
const start = document.getElementById('crm-start');
if (!start) return;
if (!start.value.trim() && lastSince) start.value = lastSince;
}
function loginForm() {
@@ -154,8 +162,12 @@ function showLogin() {
if (filters) filters.hidden = true;
const company = document.getElementById('crm-company');
const code = document.getElementById('crm-code');
const start = document.getElementById('crm-start');
const end = document.getElementById('crm-end');
if (company) company.value = '';
if (code) code.value = '';
if (start) start.value = '';
if (end) end.value = '';
}
function showSession() {
@@ -172,7 +184,7 @@ function showSession() {
function renderChrome() {
if (!loginForm().hidden) setStatus(t('sg_need_login'));
if (lastUsername) showSession();
renderSince();
syncDateDefaults();
if (lastRows) renderOrders(filteredRows(), false);
}
@@ -264,7 +276,6 @@ async function applyOrdersResponse(ordersRes) {
const rejected = data.code === 'login_rejected';
seenOrderKeys.clear();
lastSince = '';
renderSince();
showLogin();
setStatus(t('sg_need_login'));
if (rejected) showToast(t('sg_rejected'), true);
@@ -280,7 +291,7 @@ async function applyOrdersResponse(ordersRes) {
lastUsername = data.username || '';
lastRows = data.orders || [];
showSession();
renderSince();
syncDateDefaults();
const added = renderOrders(filteredRows(), ordersLoaded);
ordersLoaded = true;
if (added > 0) showToast(t('crm_new'));
@@ -342,7 +353,6 @@ async function signOut() {
}
seenOrderKeys.clear();
lastSince = '';
renderSince();
showLogin();
setStatus(t('sg_need_login'));
} catch (err) {
@@ -425,6 +435,20 @@ function bindSuggest(inputId, listId, valuesFn) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('crm-refresh').addEventListener('click', loadCrmOrders);
document.getElementById('crm-forget').addEventListener('click', signOut);
['crm-start', 'crm-end'].forEach((id) => {
const input = document.getElementById(id);
input.addEventListener('input', applyFilter);
input.addEventListener('jdp:change', applyFilter);
});
if (window.jalaliDatepicker) {
jalaliDatepicker.startWatch({
selector: '#crm-filters input[data-jdp]',
autoReadOnlyInput: false,
showCloseBtn: true,
time: false,
zIndex: 200,
});
}
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);

View File

@@ -69,6 +69,8 @@ const translations = {
'no_crm_orders': 'No orders after this date',
'no_crm_matches': 'No orders match this search',
'orders_after': 'Orders after',
'start_date': 'From date',
'end_date': 'To date',
'crm_new': 'New orders added',
'status': 'Status',
'no_orders': 'No orders yet',
@@ -151,6 +153,8 @@ const translations = {
'no_crm_orders': 'سفارشی بعد از این تاریخ نیست',
'no_crm_matches': 'حواله‌ای با این جستجو پیدا نشد',
'orders_after': 'حواله‌های بعد از',
'start_date': 'از تاریخ',
'end_date': 'تا تاریخ',
'crm_new': 'سفارش جدید اضافه شد',
'status': 'وضعیت',
'no_orders': 'سفارشی ثبت نشده',

View File

@@ -0,0 +1 @@
/* vendored @majidh1/jalalidatepicker@1.0.0 — https://github.com/majidh1/JalaliDatePicker */

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long