فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(finance): deterministically round fractional invoice lines to whole rial
davoodmehraban89 committed
Oct 31, 2026 at 18:29 UTC
4a2fbb5d73c7a4107b9f6b39613342c8d04c06a6
4 files changed
+63
-4
src/controllers/InvoiceController.js
+6
-3
@@ -9,6 +9,7 @@ import { SettingsController } from './SettingsController.js';
9
import { LicenseService } from '../core/LicenseService.js';
10
import { DataScope } from '../core/DataScope.js';
11
import { RetailPostingService } from '../core/RetailPostingService.js';
12
+import { amount, lineGrossRials } from '../core/RetailFinanceCore.mjs';
13
14
class InvoiceControllerImpl {
15
// شمارهی بعدی سریال (بر اساس نوع سند)
@@ -38,9 +39,11 @@ class InvoiceControllerImpl {
39
}
40
const user = Auth.current();
41
const items = (data.items || []).map(it => {
41
- const qty = Number(it.qty) || 0;
42
- const price = Number(it.price) || 0;
43
- const lineSubtotal = qty * price;
42
+ const qty = Number(it.qty);
43
+ const price = amount(it.price ?? 0, 'قیمت');
44
+ // Both UI and persisted invoice use the exact same whole-rial rule.
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;
49
if (it.discountType === 'percent') {
src/core/RetailFinanceCore.mjs
+12
@@ -25,6 +25,18 @@ export function qtyUnits(q) {
25
Math.abs(scaled-Math.round(scaled))>1e-7) throw Error('مقدار کالا نامعتبر است');
26
return Math.round(scaled);
27
}
28
+// Round a quantity multiplied by a whole-rial unit price ONCE per line.
29
+// Integer thousandths and bigint avoid binary-float centering errors and
30
+// enforce the application's whole-rial accounting policy at the source.
31
+export function lineGrossRials(quantity, unitPrice) {
32
+ const units = qtyUnits(quantity);
33
+ const price = amount(unitPrice, 'قیمت');
34
+ const gross = (BigInt(units) * BigInt(price) + BigInt(QTY_SCALE) / 2n) /
35
+ BigInt(QTY_SCALE);
36
+ if (gross > BigInt(Number.MAX_SAFE_INTEGER))
37
+ throw Error('جمع ردیف فاکتور از محدوده مجاز ریالی بیشتر است');
38
+ return Number(gross);
39
+}
40
export const fromQtyUnits=q=>q/QTY_SCALE;
41
const plus=(a,b)=>{const n=a+b;if(!Number.isSafeInteger(n))throw Error('سرریز موجودی یا مبلغ');return n};
42
export function receiveStock(current,quantity,totalCost) {
src/views/InvoiceFormView.js
+8
-1
@@ -12,6 +12,7 @@ import { numberToWords } from '../utils/NumberToWords.js';
12
import { NumberInput } from '../utils/NumberInput.js';
13
import { SettingsController } from '../controllers/SettingsController.js';
14
import { RetailPostingService } from '../core/RetailPostingService.js';
15
+import { lineGrossRials } from '../core/RetailFinanceCore.mjs';
16
17
let ctx = {
18
editId: null,
@@ -493,7 +494,13 @@ class InvoiceFormViewImpl {
494
ctx.data.items.forEach((item, idx) => {
495
const qty = Number(item.qty) || 0;
496
const price = Number(item.price) || 0;
496
- const lineSubtotal = qty * price;
497
+ // While an input is incomplete/invalid, show a provisional zero; the
498
+ // persistence boundary rejects it rather than silently changing it.
499
+ let lineSubtotal = 0;
500
+ if (qty > 0 && Number.isSafeInteger(price) && price >= 0) {
501
+ try { lineSubtotal = lineGrossRials(qty, price); }
502
+ catch (_) { lineSubtotal = 0; }
503
+ }
504
let lineDiscount = 0;
505
const discInput = Number(item.discountInput !== undefined ? item.discountInput : item.discount) || 0;
506
if (item.discountType === 'percent') {
tests/fractional-invoice-rial-rounding.test.mjs
new
+37
@@ -0,0 +1,37 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { readFileSync } from 'node:fs';
4
+import { lineGrossRials, qtyUnits } from '../src/core/RetailFinanceCore.mjs';
5
+
6
+test('fractional quantities round exactly to whole rial, with deterministic half-up', () => {
7
+ assert.equal(lineGrossRials(2.5, 1201), 3003);
8
+ assert.equal(lineGrossRials(0.001, 500), 1);
9
+ assert.equal(lineGrossRials(0.001, 499), 0);
10
+ assert.equal(lineGrossRials(0.125, 1000), 125);
11
+ assert.equal(lineGrossRials(1234.567, 1000), 1234567);
12
+ assert.equal(lineGrossRials(1.999, 1501), 3000);
13
+});
14
+
15
+test('invalid monetary unit price and overprecise quantities cannot create invoice totals', () => {
16
+ for (const p of [-1, 1.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1, '2.5']) {
17
+ assert.throws(() => lineGrossRials(2.5, p), /نامعتبر|صحیح/);
18
+ }
19
+ for (const q of [-1, 0, 1.2345, Infinity, NaN]) {
20
+ assert.throws(() => lineGrossRials(q, 1000), /نامعتبر/);
21
+ }
22
+ assert.equal(qtyUnits(1.234), 1234);
23
+});
24
+
25
+test('fractional amount multiplication enforces the safe rial range without overflow', () => {
26
+ assert.equal(lineGrossRials(1, Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER);
27
+ assert.throws(() => lineGrossRials(2, Number.MAX_SAFE_INTEGER), /محدوده/);
28
+});
29
+
30
+test('invoice UI preview and persistence use identical gross line function', () => {
31
+ const controller = readFileSync(new URL('../src/controllers/InvoiceController.js', import.meta.url), 'utf8');
32
+ const view = readFileSync(new URL('../src/views/InvoiceFormView.js', import.meta.url), 'utf8');
33
+ assert.match(controller, /lineSubtotal = lineGrossRials\(qty, price\)/);
34
+ assert.match(view, /lineSubtotal = lineGrossRials\(qty, price\)/);
35
+ assert.ok(!controller.includes('const lineSubtotal = qty * price'));
36
+ assert.ok(!view.includes('const lineSubtotal = qty * price'));
37
+});