فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(P0): preserve invoice row discount input rather than reapplying computed rial discount
davoodmehraban89 committed
Oct 31, 2026 at 18:32 UTC
32e11a1b7b7e93d4c6ea7e514ee3034713bacd44
3 files changed
+76
-2
src/controllers/InvoiceController.js
+8
-1
@@ -45,7 +45,14 @@ class InvoiceControllerImpl {
45
// Reject quantities with >3 decimals and monetary overflow before writes.
46
const lineSubtotal = lineGrossRials(qty, price);
47
let lineDiscount = 0;
48
- const discInput = Number(it.discount) || 0;
48
+ // The form stores the USER'S percentage in discountInput and the
49
+ // resulting rial discount in discount. Reusing discount as a percent
50
+ // would apply the reduction twice when saving a line.
51
+ const discInput = Number(it.discountInput ?? it.discount ?? 0);
52
+ if (!Number.isFinite(discInput) || discInput < 0 ||
53
+ (it.discountType === 'percent' ? discInput > 100 : !Number.isSafeInteger(discInput))) {
54
+ throw Error('تخفیف ردیف فاکتور نامعتبر است');
55
+ }
56
if (it.discountType === 'percent') {
57
lineDiscount = Math.round(lineSubtotal * discInput / 100);
58
} else {
src/views/InvoiceFormView.js
+5
-1
@@ -413,7 +413,11 @@ class InvoiceFormViewImpl {
413
}
414
415
_onFieldChange(idx, field, value) {
416
- if (field === 'qty' || field === 'price' || field === 'discount') {
416
+ if (field === 'discount') {
417
+ // The textbox contains raw percent/rial input, not the computed rial
418
+ // discount. _recalc() sets item.discount only after using this value.
419
+ ctx.data.items[idx].discountInput = NumberInput.parse(value);
420
+ } else if (field === 'qty' || field === 'price') {
421
ctx.data.items[idx][field] = NumberInput.parse(value);
422
} else {
423
ctx.data.items[idx][field] = value;
tests/invoice-discount-input-integrity.test.mjs
new
+63
@@ -0,0 +1,63 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { readFileSync } from 'node:fs';
4
+import 'fake-indexeddb/auto';
5
+import { StorageService } from '../src/core/StorageService.js';
6
+import { Auth } from '../src/core/Auth.js';
7
+import { CompanyService } from '../src/core/CompanyService.js';
8
+
9
+globalThis.window ??= globalThis;
10
+const { InvoiceController } = await import('../src/controllers/InvoiceController.js');
11
+
12
+async function setup() {
13
+ await StorageService.clearAll();
14
+ Auth.currentUser = { id: 'fractional-discount-admin', username: 'admin', role: 'admin' };
15
+ await StorageService.put('companies', { id: 'discount-test-company', name: 'Synthetic only' });
16
+ CompanyService.activeCompanyId = 'discount-test-company';
17
+}
18
+
19
+const base = discountInput => ({
20
+ kind: 'sale', isPreInvoice: true, date: '1405/07/18',
21
+ contactId: 'synthetic-contact', contactName: 'Synthetic',
22
+ items: [{productId: 'synthetic-product', productName: 'Synthetic',
23
+ qty: 2.5, price: 1200, discountType: 'percent',
24
+ discountInput, discount: 300}],
25
+ discountType: 'fixed', discountInput: 0,
26
+ vatEnabled: false, vatRate: 0, paidAmount: 0,
27
+ paymentMethod: 'credit'
28
+});
29
+
30
+test('fractional invoice with 10% row discount saves 10%, not computed 300% or 100%', async () => {
31
+ await setup();
32
+ const invoice = await InvoiceController.save(base(10));
33
+ assert.equal(invoice.items[0].lineSubtotal, 3000);
34
+ assert.equal(invoice.items[0].discountInput, 10);
35
+ assert.equal(invoice.items[0].discount, 300);
36
+ assert.equal(invoice.items[0].lineTotal, 2700);
37
+ assert.equal(invoice.subtotal, 2700);
38
+ assert.equal(invoice.grandTotal, 2700);
39
+ assert.equal((await StorageService.get('invoices', invoice.id)).grandTotal, 2700);
40
+});
41
+
42
+test('legacy line percent discount uses raw discount when discountInput is absent', async () => {
43
+ await setup();
44
+ const payload = base(10);
45
+ delete payload.items[0].discountInput;
46
+ payload.items[0].discount = 10;
47
+ const invoice = await InvoiceController.save(payload);
48
+ assert.equal(invoice.items[0].discount, 300);
49
+ assert.equal(invoice.items[0].lineTotal, 2700);
50
+});
51
+
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)), /تخفیف ردیف فاکتور نامعتبر/);
56
+ assert.deepEqual(await StorageService.getByCompany('invoices', 'discount-test-company'), []);
57
+ }
58
+});
59
+
60
+test('UI updates discountInput, not already-calculated rial discount, when typing', () => {
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
+});