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

fix(finance): reject multirow safe-integer overflow and add malformed input regressions

davoodmehraban89 committed Oct 31, 2026 at 20:24 UTC e3b323e0fc522b2781e132e664d3fd803d2566c1
3 files changed +31 -3
src/controllers/InvoiceController.js
+4 -2
@@ -73,7 +73,9 @@ class InvoiceControllerImpl {
73 };
74 });
75
76 - const subtotal = items.reduce((s, it) => s + it.lineTotal, 0);
76 + // Every intermediate must fit a safe integer, including pre-invoices
77 + // that bypass the finalized double-entry posting boundary.
78 + const subtotal = items.reduce((sum, it) => amount(sum + it.lineTotal, 'جمع فاکتور'), 0);
79
80 // تخفیف کلی
81 let totalDiscount = 0;
@@ -96,7 +98,7 @@ class InvoiceControllerImpl {
98 let vatAmount = 0;
99 if (data.vatEnabled && data.kind !== 'non_formal')
100 vatAmount = percentOfRials(afterDiscount,vatRate,'درصد مالیات');
99 - const grandTotal = afterDiscount + vatAmount;
101 + const grandTotal = amount(afterDiscount + vatAmount, 'جمع کل فاکتور');
102
103 // پرداخت
104 const paidAmount = Math.min(amount(data.paidAmount ?? 0,'مبلغ پرداخت'), grandTotal);
src/core/RetailFinanceCore.mjs
+1
@@ -127,6 +127,7 @@ export function assertInvoiceTotals(inv) {
127 if(disc!==expected)throw Error('تخفیف کل با ورودی ثبت‌شده ناسازگار است');
128 }
129 if(inv.vatEnabled!==undefined) {
130 + if(typeof inv.vatEnabled!=='boolean')throw Error('وضعیت مالیات باید صریحاً صحیح یا غلط باشد');
131 const expected=inv.vatEnabled && inv.kind!=='non_formal'
132 ? percentOfRials(total-disc,inv.vatRate??0,'درصد مالیات') : 0;
133 if(vat!==expected)throw Error('مالیات فاکتور با نرخ و وضعیت مالیات ناسازگار است');
tests/invoice-discount-input-integrity.test.mjs
+26 -1
@@ -52,7 +52,7 @@ test('legacy line percent discount uses raw discount when discountInput is absen
52 test('invalid row discount must fail before creating a financial invoice or preinvoice', async () => {
53 await setup();
54 for (const invalid of [101, -1, Infinity, 'invalid']) {
55 - await assert.rejects(InvoiceController.save(base(invalid)), /تخفیف ردیف فاکتور نامعتبر/);
55 + await assert.rejects(InvoiceController.save(base(invalid)), /تخفیف ردیف فاکتور نامعتبر|درصد تخفیف ردیف/);
56 assert.deepEqual(await StorageService.getByCompany('invoices', 'discount-test-company'), []);
57 }
58 });
@@ -61,3 +61,28 @@ test('UI updates discountInput, not already-calculated rial discount, when typin
61 const view = readFileSync(new URL('../src/views/InvoiceFormView.js', import.meta.url), 'utf8');
62 assert.match(view, /if \(field === 'discount'\) \{[\s\S]*?ctx\.data\.items\[idx\]\.discountInput = NumberInput\.parse\(value\)/);
63 });
64 +
65 +
66 +test('preinvoice rejects safe-integer overflow across individually valid items before writing', async () => {
67 + await setup();
68 + const payload = base(10);
69 + payload.items = [
70 + { productId: 'synthetic-product-1', qty: 1, price: Number.MAX_SAFE_INTEGER, discountType: 'fixed', discountInput: 0 },
71 + { productId: 'synthetic-product-2', qty: 1, price: 2, discountType: 'fixed', discountInput: 0 }
72 + ];
73 + await assert.rejects(InvoiceController.save(payload), /جمع فاکتور نامعتبر/);
74 + assert.deepEqual(await StorageService.getByCompany('invoices', 'discount-test-company'), []);
75 +});
76 +
77 +test('malformed VAT, global discount or payment inputs are rejected before invoice save', async () => {
78 + await setup();
79 + for(const patch of [
80 + { vatEnabled: true, vatRate: -0.01 }, { vatEnabled: true, vatRate: 'bad' },
81 + { discountType: 'percent', discountInput: 100.001 },
82 + { discountType: 'fixed', discountInput: -1 },
83 + { paidAmount: Infinity }, { paidAmount: -1 }
84 + ]) {
85 + await assert.rejects(InvoiceController.save({ ...base(10), ...patch }), /درصد|نامعتبر|صحیح/);
86 + assert.deepEqual(await StorageService.getByCompany('invoices','discount-test-company'),[]);
87 + }
88 +});