فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(finance): validate product rial prices and reorder precision before writes
davoodmehraban89 committed
Oct 31, 2026 at 18:26 UTC
717cc6e688a3acb80b6fca32785372d08eadd155
2 files changed
+101
-6
src/controllers/ProductController.js
+22
-6
@@ -6,6 +6,22 @@ import { StorageService } from '../core/StorageService.js';
6
import { Auth } from '../core/Auth.js';
7
import { DataScope } from '../core/DataScope.js';
8
import { RetailPostingService } from '../core/RetailPostingService.js';
9
+import { amount, qtyUnits } from '../core/RetailFinanceCore.mjs';
10
+
11
+// Validate at the persistence boundary: UI formatting is not a financial
12
+// validator and must never coerce invalid product prices to zero.
13
+function productRial(value, label) {
14
+ return amount(value === null || value === undefined || value === '' ? 0 : value, label);
15
+}
16
+
17
+function reorderPoint(value) {
18
+ if (value === null || value === undefined || value === '') return 0;
19
+ const quantity = Number(value);
20
+ if (quantity === 0) return 0;
21
+ qtyUnits(quantity); // enforce positive, finite and at most three decimals
22
+ return quantity;
23
+}
24
+
25
26
class ProductControllerImpl {
27
// ----- واحدها -----
@@ -114,12 +130,12 @@ class ProductControllerImpl {
130
categoryId: data.categoryId || null,
131
unitId: data.unitId || null,
132
unit: data.unit || 'عدد',
117
- buyPrice: Number(data.buyPrice) || 0,
118
- sellPrice: Number(data.sellPrice) || 0,
133
+ buyPrice: productRial(data.buyPrice, 'قیمت خرید'),
134
+ sellPrice: productRial(data.sellPrice, 'قیمت فروش'),
135
trackInventory: data.trackInventory !== false,
136
isActiveSell: data.isActiveSell !== false,
137
isActiveBuy: data.isActiveBuy !== false,
122
- minStock: Number(data.minStock) || 0,
138
+ minStock: reorderPoint(data.minStock),
139
description: (data.description || '').trim()
140
};
141
return await StorageService.put('products', await DataScope.stamp('products', product));
@@ -142,12 +158,12 @@ class ProductControllerImpl {
158
categoryId: data.categoryId || null,
159
unitId: data.unitId || null,
160
unit: data.unit || existing.unit || 'عدد',
145
- buyPrice: Number(data.buyPrice) || 0,
146
- sellPrice: Number(data.sellPrice) || 0,
161
+ buyPrice: productRial(data.buyPrice, 'قیمت خرید'),
162
+ sellPrice: productRial(data.sellPrice, 'قیمت فروش'),
163
trackInventory: data.trackInventory !== false,
164
isActiveSell: data.isActiveSell !== false,
165
isActiveBuy: data.isActiveBuy !== false,
150
- minStock: Number(data.minStock) || 0,
166
+ minStock: reorderPoint(data.minStock),
167
description: (data.description || '').trim()
168
};
169
return await StorageService.put('products', await DataScope.stamp('products', updated));
tests/product-monetary-validation.test.mjs
new
+79
@@ -0,0 +1,79 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import 'fake-indexeddb/auto';
4
+import { StorageService } from '../src/core/StorageService.js';
5
+import { Auth } from '../src/core/Auth.js';
6
+import { CompanyService } from '../src/core/CompanyService.js';
7
+
8
+globalThis.window ??= globalThis;
9
+const { ProductController } = await import('../src/controllers/ProductController.js');
10
+
11
+async function setup() {
12
+ await StorageService.clearAll();
13
+ Auth.currentUser = { id: 'product-validation-admin', username: 'admin', role: 'admin' };
14
+ await StorageService.put('companies', { id: 'product-validation-company', name: 'Synthetic' });
15
+ CompanyService.activeCompanyId = 'product-validation-company';
16
+}
17
+const list = () => StorageService.getByCompany('products', 'product-validation-company');
18
+
19
+test('product create enforces safe nonnegative integral rial prices', async () => {
20
+ await setup();
21
+ for (const invalid of [-1, 1.5, Infinity, Number.MAX_SAFE_INTEGER + 1, 'oops']) {
22
+ await assert.rejects(ProductController.createProduct({
23
+ name: 'Rejected', buyPrice: invalid, sellPrice: 10
24
+ }), /نامعتبر|صحیح/);
25
+ assert.deepEqual(await list(), [], 'invalid product price must never write');
26
+ await assert.rejects(ProductController.createProduct({
27
+ name: 'Rejected', buyPrice: 10, sellPrice: invalid
28
+ }), /نامعتبر|صحیح/);
29
+ assert.deepEqual(await list(), []);
30
+ }
31
+});
32
+
33
+test('stock reorder point allows three decimals but rejects over-precision and negatives', async () => {
34
+ await setup();
35
+ for (const value of [-1, 1.2345, Infinity, Number.MAX_SAFE_INTEGER + 1]) {
36
+ await assert.rejects(ProductController.createProduct({
37
+ name: 'Rejected', minStock: value
38
+ }), /نامعتبر/);
39
+ assert.deepEqual(await list(), []);
40
+ }
41
+ await ProductController.createProduct({
42
+ name: 'Valid', code: 'FRACTIONAL', buyPrice: 1500, sellPrice: 2000, minStock: 2.125
43
+ });
44
+ const rows = await list();
45
+ assert.equal(rows.length, 1);
46
+ assert.equal(rows[0].minStock, 2.125);
47
+ assert.equal(rows[0].buyPrice, 1500);
48
+ assert.equal(rows[0].sellPrice, 2000);
49
+});
50
+
51
+test('invalid product updates retain original persisted prices and reorder point', async () => {
52
+ await setup();
53
+ await ProductController.createProduct({
54
+ name: 'Original', code: 'UNCHANGED', buyPrice: 1200, sellPrice: 1400, minStock: 0.25
55
+ });
56
+ const original = (await list())[0];
57
+ for (const patch of [
58
+ { buyPrice: 1.25 }, { sellPrice: -2 }, { minStock: 1.2345 }
59
+ ]) {
60
+ await assert.rejects(ProductController.updateProduct(original.id, {
61
+ name: original.name, code: original.code,
62
+ buyPrice: original.buyPrice, sellPrice: original.sellPrice,
63
+ minStock: original.minStock, ...patch
64
+ }), /نامعتبر|صحیح/);
65
+ const unchanged = await StorageService.get('products', original.id);
66
+ assert.equal(unchanged.buyPrice, 1200);
67
+ assert.equal(unchanged.sellPrice, 1400);
68
+ assert.equal(unchanged.minStock, 0.25);
69
+ }
70
+});
71
+
72
+test('missing optional numeric values retain the zero default', async () => {
73
+ await setup();
74
+ await ProductController.createProduct({ name: 'No prices' });
75
+ const row = (await list())[0];
76
+ assert.equal(row.buyPrice, 0);
77
+ assert.equal(row.sellPrice, 0);
78
+ assert.equal(row.minStock, 0);
79
+});