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

fix(finance): reject invoice overpayment and row over-discount

davoodmehraban89 committed Nov 1, 2026 at 17:37 UTC 2d09a1587c67ae8e156af3e2190637c2ba376cad
3 files changed +32 -4
src/controllers/InvoiceController.js
+5 -2
@@ -57,7 +57,8 @@ class InvoiceControllerImpl {
57 } else {
58 lineDiscount = amount(it.discountInput ?? it.discount ?? 0,'تخفیف ثابت ردیف');
59 }
60 - lineDiscount = Math.max(0, Math.min(lineDiscount, lineSubtotal));
60 + if (lineDiscount > lineSubtotal)
61 + throw Error('تخفیف ردیف بیش از مبلغ ناخالص است');
62 const lineTotal = lineSubtotal - lineDiscount;
63 return {
64 productId: it.productId || null,
@@ -101,7 +102,9 @@ class InvoiceControllerImpl {
102 const grandTotal = amount(afterDiscount + vatAmount, 'جمع کل فاکتور');
103
104 // پرداخت
104 - const paidAmount = Math.min(amount(data.paidAmount ?? 0,'مبلغ پرداخت'), grandTotal);
105 + const paidAmount = amount(data.paidAmount ?? 0, 'مبلغ پرداخت');
106 + if (paidAmount > grandTotal)
107 + throw Error('مبلغ پرداخت بیش از مبلغ فاکتور است');
108 const remaining = Math.max(0, grandTotal - paidAmount);
109
110 const baseData = {
src/views/InvoiceFormView.js
+6 -2
@@ -763,7 +763,7 @@ class InvoiceFormViewImpl {
763 return;
764 }
765
766 - let discountInput, vatRate;
766 + let discountInput, vatRate, partialPaidAmount;
767 try {
768 // Read raw user-visible values once, before any financial write. A
769 // loose parser must never silently convert "2x5" to 25 or bad VAT to 0.
@@ -791,7 +791,7 @@ class InvoiceFormViewImpl {
791 decimals: 2, optional: true
792 });
793 if (document.getElementById('invPayment').value === 'partial')
794 - NumberInput.parseStrict(document.getElementById('invPaidAmount'), { optional: true });
794 + partialPaidAmount = NumberInput.parseStrict(document.getElementById('invPaidAmount'), { optional: true });
795 } catch (error) {
796 Toast.warning('ورودی عددی قابل ذخیره نیست: ' + error.message);
797 return;
@@ -804,6 +804,10 @@ class InvoiceFormViewImpl {
804 }
805
806 this._recalc(); // read payment state from the same calculation shown in the summary
807 + if (partialPaidAmount !== undefined && partialPaidAmount > ctx.totals.grandTotal) {
808 + Toast.warning('مبلغ پرداخت‌شده نمی‌تواند از مبلغ کل فاکتور بیشتر باشد');
809 + return;
810 + }
811 const data = {
812 kind: document.getElementById('invKind').value,
813 number: document.getElementById('invNumber')?.value,
tests/invoice-discount-input-integrity.test.mjs
+21
@@ -57,6 +57,27 @@ test('invalid row discount must fail before creating a financial invoice or prei
57 }
58 });
59
60 +test('preinvoice rejects fixed row discount above gross before writing', async () => {
61 + await setup();
62 + const payload = base(0);
63 + payload.items[0].discountType = 'fixed';
64 + payload.items[0].discountInput = 3001;
65 + payload.items[0].discount = 3001;
66 + await assert.rejects(InvoiceController.save(payload), /تخفیف ردیف بیش از مبلغ ناخالص است/);
67 + assert.deepEqual(await StorageService.getByCompany('invoices', 'discount-test-company'), []);
68 +});
69 +
70 +test('overpayment above invoice total is rejected before writing a preinvoice', async () => {
71 + await setup();
72 + await assert.rejects(InvoiceController.save({ ...base(10), paidAmount: 2701 }), /مبلغ پرداخت بیش از مبلغ فاکتور است/);
73 + assert.deepEqual(await StorageService.getByCompany('invoices', 'discount-test-company'), []);
74 +});
75 +
76 +test('partial payment form rejects amounts above the invoice total before save', () => {
77 + const view = readFileSync(new URL('../src/views/InvoiceFormView.js', import.meta.url), 'utf8');
78 + assert.match(view, /partialPaidAmount !== undefined && partialPaidAmount > ctx\.totals\.grandTotal/);
79 +});
80 +
81 test('UI updates discountInput, not already-calculated rial discount, when typing', () => {
82 const view = readFileSync(new URL('../src/views/InvoiceFormView.js', import.meta.url), 'utf8');
83 assert.match(view, /if \(field === 'discount'\) \{[\s\S]*?ctx\.data\.items\[idx\]\.discountInput = NumberInput\.parseStrict\(value/);