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

fix(P0): validate product and legacy stock amounts at every write boundary

davoodmehraban89 committed Oct 31, 2026 at 20:36 UTC af8105e30cb81afa7395d4d3c2eb78adba51fcbb
5 files changed +73 -7
src/controllers/ProductController.js
+11 -1
@@ -210,12 +210,22 @@ class ProductControllerImpl {
210 if (await RetailPostingService.isEnabled())
211 throw new Error('گردش دستی موجودی در شرکت یکپارچه مجاز نیست؛ از خرید، فروش، انتقال یا انبارگردانی استفاده کنید');
212 const user = Auth.current();
213 + const company = await DataScope.companyId();
214 + const product = await StorageService.get('products', productId);
215 + if (!product || product.companyId !== company)
216 + throw Error('کالای گردش انبار متعلق به شرکت فعال نیست');
217 + if (!['in','out','adjust'].includes(type))
218 + throw Error('نوع گردش انبار نامعتبر است');
219 + const quantity = Number(qty);
220 + if (!Number.isFinite(quantity) || quantity < 0 || (type !== 'adjust' && quantity === 0))
221 + throw Error('مقدار گردش انبار نامعتبر است');
222 + if (quantity > 0) qtyUnits(quantity);
223 const movement = {
224 id: StorageService.uid('mv_'),
225 ownerUserId: user.id,
226 productId,
227 type,
218 - qty: Number(qty) || 0,
228 + qty: quantity,
229 refType,
230 refId,
231 note,
src/views/InvoiceFormView.js
+10 -2
@@ -649,6 +649,14 @@ class InvoiceFormViewImpl {
649 const name = document.getElementById('qpName').value.trim();
650 if (!name) { Toast.warning('نام کالا الزامی است'); return; }
651
652 + let buyPrice, sellPrice;
653 + try {
654 + buyPrice = NumberInput.parseStrict(document.getElementById('qpBuyPrice'), { optional: true });
655 + sellPrice = NumberInput.parseStrict(document.getElementById('qpSellPrice'), { optional: true });
656 + } catch (error) {
657 + Toast.warning('قیمت کالای جدید نامعتبر است: ' + error.message);
658 + return;
659 + }
660 const data = {
661 code: document.getElementById('qpCode').value,
662 barcode: '',
@@ -656,8 +664,8 @@ class InvoiceFormViewImpl {
664 unit: document.getElementById('qpUnit').value || 'عدد',
665 unitId: null,
666 categoryId: null,
659 - buyPrice: NumberInput.parse(document.getElementById('qpBuyPrice').value),
660 - sellPrice: NumberInput.parse(document.getElementById('qpSellPrice').value),
667 + buyPrice,
668 + sellPrice,
669 trackInventory: document.getElementById('qpTrackInventory').checked,
670 isActiveSell: true,
671 isActiveBuy: true,
src/views/ProductsView.js
+14 -3
@@ -444,16 +444,27 @@ class ProductsViewImpl {
444 }
445
446 async saveProduct(id) {
447 + let buyPrice, sellPrice, minStock;
448 + try {
449 + buyPrice = NumberInput.parseStrict(document.getElementById('pBuyPrice'), { optional: true });
450 + sellPrice = NumberInput.parseStrict(document.getElementById('pSellPrice'), { optional: true });
451 + minStock = NumberInput.parseStrict(document.getElementById('pMinStock'), {
452 + decimals: 3, optional: true
453 + });
454 + } catch (error) {
455 + Toast.warning('قیمت یا نقطه سفارش نامعتبر است: ' + error.message);
456 + return;
457 + }
458 const data = {
459 code: document.getElementById('pCode').value,
460 barcode: document.getElementById('pBarcode').value,
461 name: document.getElementById('pName').value,
462 unitId: document.getElementById('pUnit').value || null,
463 categoryId: document.getElementById('pCategory').value || null,
453 - buyPrice: NumberInput.parse(document.getElementById('pBuyPrice').value),
454 - sellPrice: NumberInput.parse(document.getElementById('pSellPrice').value),
464 + buyPrice,
465 + sellPrice,
466 trackInventory: document.getElementById('pTrackInventory').checked,
456 - minStock: NumberInput.parse(document.getElementById('pMinStock')?.value || '0'),
467 + minStock,
468 description: document.getElementById('pDescription').value
469 };
470
src/views/StockKardexModal.js
+7 -1
@@ -189,7 +189,13 @@ class StockKardexModalImpl {
189 }
190
191 async saveAdjust(productId) {
192 - const newQty = NumberInput.parse(document.getElementById('adjQty').value);
192 + let newQty;
193 + try {
194 + newQty = NumberInput.parseStrict(document.getElementById('adjQty'), { decimals: 3 });
195 + } catch (error) {
196 + Toast.warning('موجودی معتبر نیست: ' + error.message);
197 + return;
198 + }
199 const note = (document.getElementById('adjNote').value || '').trim();
200
201 if (isNaN(newQty) || newQty < 0) {
tests/product-monetary-validation.test.mjs
+31
@@ -77,3 +77,34 @@ test('missing optional numeric values retain the zero default', async () => {
77 assert.equal(row.sellPrice, 0);
78 assert.equal(row.minStock, 0);
79 });
80 +
81 +
82 +test('manual legacy stock movement rejects company/type/negative/overprecision',async()=>{
83 + await setup();
84 + const p=await ProductController.createProduct({name:'Synthetic item',buyPrice:100,minStock:0.5});
85 + for(const [type,qty,id] of [
86 + ['in',0,p.id],['out',-1,p.id],['adjust',-1,p.id],['in',1.2345,p.id],
87 + ['adjust','bad',p.id],['unknown',1,p.id],['in',1,'not-a-product']
88 + ]) {
89 + await assert.rejects(ProductController.addStockMovement(id,type,qty),/نامعتبر|شرکت فعال|مقدار کالا/);
90 + assert.deepEqual(await StorageService.getByCompany('stock_movements','product-validation-company'),[]);
91 + }
92 + await ProductController.addStockMovement(p.id,'in',1.25);
93 + await ProductController.addStockMovement(p.id,'adjust',0);
94 + const moves=await StorageService.getByCompany('stock_movements','product-validation-company');
95 + assert.equal(moves.length,2);
96 + assert.deepEqual(moves.map(m=>m.qty).sort((a,b)=>a-b),[0,1.25]);
97 +});
98 +
99 +test('product price and stock editors validate text strictly before writes',async()=>{
100 + const fs=await import('node:fs');
101 + const source=path=>fs.readFileSync(new URL('../'+path,import.meta.url),'utf8');
102 + for(const [path,fragment] of [
103 + ['src/views/ProductsView.js',"NumberInput.parseStrict(document.getElementById('pBuyPrice')"],
104 + ['src/views/ProductsView.js',"NumberInput.parseStrict(document.getElementById('pSellPrice')"],
105 + ['src/views/ProductsView.js',"NumberInput.parseStrict(document.getElementById('pMinStock')"],
106 + ['src/views/StockKardexModal.js',"NumberInput.parseStrict(document.getElementById('adjQty')"],
107 + ['src/views/InvoiceFormView.js',"NumberInput.parseStrict(document.getElementById('qpBuyPrice')"],
108 + ['src/views/InvoiceFormView.js',"NumberInput.parseStrict(document.getElementById('qpSellPrice')"]
109 + ]) assert.ok(source(path).includes(fragment),path);
110 +});