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

fix(finance): serialize legacy payments and scope stock movement cleanup

Factor EasyPro QA committed Nov 1, 2026 at 21:46 UTC 3d2016749994f8df61d5df9937260522b2a98cd5
2 files changed +93 -13
src/controllers/InvoiceController.js
+23 -13
@@ -198,7 +198,7 @@ class InvoiceControllerImpl {
198 const all = await DataScope.list('stock_movements');
199
200 // حذف حرکات قبلی این فاکتور
201 - for (const m of all.filter(x => x.refId === invoice.id)) {
201 + for (const m of all.filter(x => x.refType === 'invoice' && x.refId === invoice.id)) {
202 await StorageService.delete('stock_movements', m.id);
203 }
204
@@ -267,7 +267,7 @@ class InvoiceControllerImpl {
267 if (record.financePosted) throw new Error('فاکتور قطعی دارای سند مالی است و قابل حذف نیست؛ از سند مرجوعی استفاده کنید');
268 const user = Auth.current();
269 const movements = await DataScope.list('stock_movements');
270 - for (const m of movements.filter(x => x.refId === id)) {
270 + for (const m of movements.filter(x => x.refType === 'invoice' && x.refId === id)) {
271 await StorageService.delete('stock_movements', m.id);
272 }
273 return await StorageService.delete('invoices', id);
@@ -297,18 +297,28 @@ class InvoiceControllerImpl {
297 }
298
299 // علامت‌گذاری به‌عنوان پرداخت‌شده
300 - async markPaid(id, amount = null) {
301 - const inv = await StorageService.get('invoices', id);
302 - if (!inv || inv.companyId !== await DataScope.companyId()) throw new Error('فاکتور در این شرکت یافت نشد');
303 - if (inv.financePosted) throw new Error('برای تسویه فاکتور دارای سند مالی، از بخش دریافت و پرداخت استفاده کنید');
304 - const pay = amount === null ? inv.grandTotal : Number(amount);
305 - inv.paidAmount = (Number(inv.paidAmount) || 0) + pay;
306 - inv.paidAmount = Math.min(inv.paidAmount, inv.grandTotal);
307 - inv.remaining = Math.max(0, inv.grandTotal - inv.paidAmount);
308 - inv.status = inv.remaining === 0 ? 'paid' : (inv.paidAmount > 0 ? 'partial' : 'unpaid');
309 - return await StorageService.put('invoices', await DataScope.stamp('invoices', inv));
300 + async markPaid(id, payment = null) {
301 + // The invoice read, validation and write must share one IndexedDB transaction.
302 + // Concurrent payment attempts serialize on the invoices object store.
303 + const companyId = await DataScope.companyId();
304 + return StorageService.transaction(['invoices'], async tx => {
305 + const inv = await tx.get('invoices', id);
306 + if (!inv || inv.companyId !== companyId) throw new Error('فاکتور در شرکت جاری یافت نشد');
307 + if (inv.financePosted) throw new Error('پرداخت فاکتور ثبت مالی‌شده از این مسیر مجاز نیست');
308 + if (inv.isPreInvoice) throw new Error('ثبت پرداخت برای پیش‌فاکتور مجاز نیست');
309 + const total = amount(inv.grandTotal, 'مبلغ فاکتور');
310 + const alreadyPaid = amount(inv.paidAmount ?? 0, 'مبلغ پرداخت‌شده');
311 + if (alreadyPaid > total) throw new Error('مبلغ پرداخت‌شده بیشتر از مبلغ فاکتور است');
312 + const remaining = total - alreadyPaid;
313 + const pay = payment === null ? remaining : amount(payment, 'مبلغ پرداخت');
314 + if (pay <= 0 || pay > remaining) throw new Error('مبلغ پرداخت باید مثبت و حداکثر برابر مانده فاکتور باشد');
315 + inv.paidAmount = alreadyPaid + pay;
316 + inv.remaining = total - inv.paidAmount;
317 + inv.status = inv.remaining === 0 ? 'paid' : 'partial';
318 + inv.companyId = companyId;
319 + return tx.put('invoices', inv);
320 + });
321 }
311 -
322 // آمار برای داشبورد
323 async getStats() {
324 const all = await DataScope.list('invoices');
tests/invoice-markpaid-concurrency.test.mjs new
+70
@@ -0,0 +1,70 @@
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 { InvoiceController } = await import('../src/controllers/InvoiceController.js');
10 +
11 +async function seed(patch = {}) {
12 + await StorageService.clearAll();
13 + Auth.currentUser = { id: 'markpaid-admin', username: 'admin', role: 'admin' };
14 + await StorageService.put('companies', { id: 'markpaid-company', name: 'Synthetic' });
15 + CompanyService.activeCompanyId = 'markpaid-company';
16 + await StorageService.put('invoices', {
17 + id: 'markpaid-invoice', companyId: 'markpaid-company', kind: 'sale',
18 + isPreInvoice: false, grandTotal: 1000, paidAmount: 0, remaining: 1000,
19 + status: 'unpaid', ...patch
20 + });
21 +}
22 +
23 +test('concurrent payment attempts cannot overpay or lose updates', async () => {
24 + await seed();
25 + const results = await Promise.allSettled([
26 + InvoiceController.markPaid('markpaid-invoice', 600),
27 + InvoiceController.markPaid('markpaid-invoice', 600)
28 + ]);
29 + assert.equal(results.filter(r => r.status === 'fulfilled').length, 1);
30 + assert.equal(results.filter(r => r.status === 'rejected').length, 1);
31 + const invoice = await StorageService.get('invoices', 'markpaid-invoice');
32 + assert.equal(invoice.paidAmount, 600);
33 + assert.equal(invoice.remaining, 400);
34 + assert.equal(invoice.status, 'partial');
35 +});
36 +
37 +test('markPaid without amount pays only outstanding balance', async () => {
38 + await seed({ paidAmount: 300, remaining: 700, status: 'partial' });
39 + await InvoiceController.markPaid('markpaid-invoice');
40 + const invoice = await StorageService.get('invoices', 'markpaid-invoice');
41 + assert.equal(invoice.paidAmount, 1000);
42 + assert.equal(invoice.remaining, 0);
43 + assert.equal(invoice.status, 'paid');
44 +});
45 +
46 +test('invalid payments and preinvoices fail closed without writes', async () => {
47 + for (const payment of [0, -1, 1001, NaN, Infinity, '12x']) {
48 + await seed();
49 + await assert.rejects(InvoiceController.markPaid('markpaid-invoice', payment));
50 + assert.equal((await StorageService.get('invoices', 'markpaid-invoice')).paidAmount, 0);
51 + }
52 + await seed({ isPreInvoice: true });
53 + await assert.rejects(InvoiceController.markPaid('markpaid-invoice', 10), /پیش‌فاکتور/);
54 + await seed({ financePosted: true });
55 + await assert.rejects(InvoiceController.markPaid('markpaid-invoice', 10));
56 +});
57 +
58 +test('payment cannot modify another company invoice', async () => {
59 + await seed({ companyId: 'other-company' });
60 + await assert.rejects(InvoiceController.markPaid('markpaid-invoice', 10));
61 + assert.equal((await StorageService.get('invoices', 'markpaid-invoice')).paidAmount, 0);
62 +});
63 +test('invoice deletion preserves unrelated stock movements with matching refId', async () => {
64 + await seed();
65 + await StorageService.put('stock_movements', { id: 'invoice-movement', companyId: 'markpaid-company', refId: 'markpaid-invoice', refType: 'invoice', qty: 1 });
66 + await StorageService.put('stock_movements', { id: 'other-movement', companyId: 'markpaid-company', refId: 'markpaid-invoice', refType: 'stocktake', qty: 2 });
67 + await InvoiceController.delete('markpaid-invoice');
68 + assert.equal(await StorageService.get('stock_movements', 'invoice-movement'), undefined);
69 + assert.equal((await StorageService.get('stock_movements', 'other-movement')).qty, 2);
70 +});
\ No newline at end of file