فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

fix(UI): reject malformed displayed finance inputs before writing invoice

davoodmehraban89 committed Oct 31, 2026 at 20:27 UTC b857894c75ec54563b4a2c714adc51c9f86e29ae
3 files changed +86 -7
src/utils/NumberInput.js
+20
@@ -30,6 +30,26 @@ export const NumberInput = {
30 return Number(cleaned) || 0;
31 },
32
33 + // Strictly validate user-visible fields before writing. Unlike parse(),
34 + // malformed strings are never silently transformed into a different amount.
35 + parseStrict(input, { decimals = 0, optional = false } = {}) {
36 + const value = typeof input === 'string' || typeof input === 'number'
37 + ? input : (input?.value ?? '');
38 + const normalized = latinDigits(value).replace(/[٬،]/g, ',').trim();
39 + if (optional && normalized === '') return 0;
40 + if (!Number.isInteger(decimals) || decimals < 0 || decimals > 3 ||
41 + !/^(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(normalized))
42 + throw Error('ورودی عددی نامعتبر است');
43 + const point = normalized.indexOf('.');
44 + if (point >= 0 && normalized.length - point - 1 > decimals)
45 + throw Error('تعداد رقم اعشار بیش از حد مجاز است');
46 + const num = Number(normalized.replace(/,/g, ''));
47 + if (!Number.isFinite(num) || num < 0 ||
48 + (decimals === 0 && !Number.isSafeInteger(num)))
49 + throw Error('مقدار عددی خارج از محدوده مجاز است');
50 + return num;
51 + },
52 +
53 value(el) {
54 return this.parse(el);
55 },
src/views/InvoiceFormView.js
+44 -7
@@ -204,7 +204,7 @@ class InvoiceFormViewImpl {
204 <option value="fixed" ${d.discountType === 'fixed' ? 'selected' : ''}>مبلغ</option>
205 <option value="percent" ${d.discountType === 'percent' ? 'selected' : ''}>درصد</option>
206 </select>
207 - <input type="text" inputmode="numeric" class="form-control" id="invDiscInput" aria-label="مقدار تخفیف کل فاکتور" style="width:110px;min-height:34px" value="${NumberInput.format(d.discountInput || 0)}" oninput="window.InvoiceFormView._recalc()" />
207 + <input type="text" inputmode="numeric" class="form-control" id="invDiscInput" aria-label="مقدار تخفیف کل فاکتور" style="width:110px;min-height:34px" data-decimals="${d.discountType === 'percent' ? 2 : 0}" value="${NumberInput.format(d.discountInput ?? 0, d.discountType === 'percent' ? 2 : 0)}" oninput="window.InvoiceFormView._recalc()" />
208 </div>
209
210 <div id="vatRow" style="display:${d.kind === 'non_formal' ? 'none' : 'flex'};gap:8px;align-items:center;margin-bottom:10px">
@@ -308,7 +308,7 @@ class InvoiceFormViewImpl {
308 <td><input type="text" inputmode="numeric" class="form-control row-price" aria-label="قیمت واحد ردیف فاکتور" style="min-height:36px;text-align:left" value="${NumberInput.format(item.price)}" oninput="window.InvoiceFormView._onFieldChange(${idx}, 'price', this.value)" /></td>
309 <td>
310 <div style="display:flex;gap:4px">
311 - <input type="text" inputmode="numeric" class="form-control row-disc" aria-label="تخفیف ردیف فاکتور" style="min-height:36px;text-align:left;flex:1" value="${NumberInput.format(item.discountType === 'percent' ? (item.discountInput || 0) : (item.discount || 0))}" oninput="window.InvoiceFormView._onFieldChange(${idx}, 'discount', this.value)" />
311 + <input type="text" inputmode="numeric" class="form-control row-disc" aria-label="تخفیف ردیف فاکتور" style="min-height:36px;text-align:left;flex:1" data-decimals="${item.discountType === 'percent' ? 2 : 0}" value="${NumberInput.format(item.discountInput ?? item.discount ?? 0, item.discountType === 'percent' ? 2 : 0)}" oninput="window.InvoiceFormView._onFieldChange(${idx}, 'discount', this.value)" />
312 <select class="form-control" style="min-height:36px;width:60px;padding:0 6px;text-align:center;font-size:12px" aria-label="نوع تخفیف ردیف" onchange="window.InvoiceFormView._onDiscTypeRowChange(${idx}, this.value)">
313 <option value="fixed" ${item.discountType === 'fixed' ? 'selected' : ''}>مبلغ</option>
314 <option value="percent" ${item.discountType === 'percent' ? 'selected' : ''}>٪</option>
@@ -430,7 +430,12 @@ class InvoiceFormViewImpl {
430 ctx.data.items[idx].discount = 0;
431 ctx.data.items[idx].discountInput = 0;
432 const row = document.querySelector(`#itemsBody tr[data-idx="${idx}"]`);
433 - if (row) row.querySelector('.row-disc').value = 0;
433 + if (row) {
434 + const input = row.querySelector('.row-disc');
435 + input.value = '0';
436 + input.dataset.decimals = type === 'percent' ? '2' : '0';
437 + input.setAttribute('inputmode', type === 'percent' ? 'decimal' : 'numeric');
438 + }
439 this._recalc();
440 }
441
@@ -469,7 +474,10 @@ class InvoiceFormViewImpl {
474
475 _onDiscTypeChange() {
476 ctx.data.discountType = document.getElementById('invDiscType').value;
472 - document.getElementById('invDiscInput').value = 0;
477 + const input = document.getElementById('invDiscInput');
478 + input.value = '0';
479 + input.dataset.decimals = ctx.data.discountType === 'percent' ? '2' : '0';
480 + input.setAttribute('inputmode', ctx.data.discountType === 'percent' ? 'decimal' : 'numeric');
481 ctx.data.discountInput = 0;
482 this._recalc();
483 }
@@ -739,7 +747,36 @@ class InvoiceFormViewImpl {
747 return;
748 }
749
742 - const validItems = ctx.data.items.filter(it => it.productId && (Number(it.qty) || 0) > 0);
750 + let discountInput, vatRate;
751 + try {
752 + // Read raw user-visible values once, before any financial write. A
753 + // loose parser must never silently convert "2x5" to 25 or bad VAT to 0.
754 + ctx.data.items.forEach((item, idx) => {
755 + if (!item.productId) return; // intentionally empty draft row
756 + const row = document.querySelector(`#itemsBody tr[data-idx="${idx}"]`);
757 + if (!row) throw Error('ردیف کالای انتخاب‌شده در فرم یافت نشد');
758 + item.qty = NumberInput.parseStrict(row.querySelector('.row-qty'), { decimals: 3 });
759 + if (item.qty <= 0) throw Error('مقدار کالا باید مثبت باشد');
760 + item.price = NumberInput.parseStrict(row.querySelector('.row-price'), { optional: true });
761 + item.discountInput = NumberInput.parseStrict(row.querySelector('.row-disc'), {
762 + decimals: item.discountType === 'percent' ? 2 : 0, optional: true
763 + });
764 + });
765 + discountInput = NumberInput.parseStrict(document.getElementById('invDiscInput'), {
766 + decimals: document.getElementById('invDiscType').value === 'percent' ? 2 : 0,
767 + optional: true
768 + });
769 + vatRate = NumberInput.parseStrict(document.getElementById('invVatRate'), {
770 + decimals: 2, optional: true
771 + });
772 + if (document.getElementById('invPayment').value === 'partial')
773 + NumberInput.parseStrict(document.getElementById('invPaidAmount'), { optional: true });
774 + } catch (error) {
775 + Toast.warning('ورودی عددی قابل ذخیره نیست: ' + error.message);
776 + return;
777 + }
778 +
779 + const validItems = ctx.data.items.filter(it => it.productId && it.qty > 0);
780 if (validItems.length === 0) {
781 Toast.warning('حداقل یک ردیف معتبر (کالا با مقدار) لازم است');
782 return;
@@ -755,9 +792,9 @@ class InvoiceFormViewImpl {
792 contactName: contact.name,
793 items: validItems,
794 discountType: document.getElementById('invDiscType').value,
758 - discountInput: NumberInput.parse(document.getElementById('invDiscInput').value),
795 + discountInput,
796 vatEnabled: document.getElementById('invVatEnabled')?.checked || false,
760 - vatRate: NumberInput.parse(document.getElementById('invVatRate')?.value),
797 + vatRate,
798 paymentMethod: document.getElementById('invPayment').value,
799 paidAmount: ctx.totals?.paidAmount ?? 0,
800 originalInvoiceId: document.getElementById('invOriginalInvoice')?.value || null,
tests/number-input-fractions.test.mjs
+22
@@ -110,3 +110,25 @@ test('over-precision is never silently truncated before the ledger validation',
110 assert.throws(() => qtyUnits(NumberInput.parse(input)), /مقدار کالا نامعتبر/);
111 assert.equal(qtyUnits(1.234), 1234);
112 });
113 +
114 +
115 +test('strict numeric parsing rejects malformed grouping, letters and silent data changes',()=>{
116 + for(const invalid of ['2x5','1,23','-1','12..5','1.2.3','1e2','10 ','1 000','', '10٫20a'])
117 + assert.throws(()=>NumberInput.parseStrict(invalid,{decimals:3}),/نامعتبر|مجاز/);
118 + assert.equal(NumberInput.parseStrict('',{optional:true}),0);
119 + assert.equal(NumberInput.parseStrict('۱٬۲۳۴٫۵',{decimals:3}),1234.5);
120 + assert.equal(NumberInput.parseStrict('1,234.125',{decimals:3}),1234.125);
121 + assert.equal(NumberInput.parseStrict('۱٬۰۰۰'),1000);
122 + assert.throws(()=>NumberInput.parseStrict('1.2345',{decimals:3}),/اعشار/);
123 + assert.throws(()=>NumberInput.parseStrict('10.5'),/اعشار/);
124 + assert.equal(NumberInput.parseStrict('10.50',{decimals:2}),10.5);
125 +});
126 +
127 +test('invoice write path validates displayed row and VAT inputs strictly',()=>{
128 + const src=readFileSync(new URL('../src/views/InvoiceFormView.js',import.meta.url),'utf8');
129 + assert.match(src,/item\.qty = NumberInput\.parseStrict/);
130 + assert.match(src,/item\.price = NumberInput\.parseStrict/);
131 + assert.match(src,/item\.discountInput = NumberInput\.parseStrict/);
132 + assert.match(src,/vatRate = NumberInput\.parseStrict/);
133 + assert.match(src,/data-decimals="\\?\$\{item\.discountType === 'percent' \? 2 : 0\}"/);
134 +});