فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A04): protect finalized ledger, cash and stock during complete backup restore
davoodmehraban89 committed
Oct 16, 2026 at 17:27 UTC
69dd13ba4c8366fb1a97326122eb66d6dbfb2609
1 file changed
+40
-1
src/core/StorageService.js
+40
-1
@@ -45,6 +45,32 @@ const INDEXES = {
45
const CREDENTIAL_FIELDS = ['pass', 'pwHash', 'pwSalt', 'pwIter', 'pwAlgo'];
46
47
const APPEND_ONLY_STORES = new Set(['audit_events', 'document_corrections']);
48
+const FINALIZED_COMMERCE_STORES = new Set([
49
+ 'stock_movements', 'settlements', 'transactions'
50
+]);
51
+
52
+async function existingFinalizedRecord(tx, store, incoming, existing) {
53
+ if (!existing) return false;
54
+ if (FINALIZED_COMMERCE_STORES.has(store)) return true;
55
+ if (store === 'journal_entries') return existing.status === 'posted' || incoming.status === 'posted';
56
+ if (store === 'invoices' || store === 'cheques' || store === 'expenses') {
57
+ return Boolean(existing.financePosted || incoming.financePosted);
58
+ }
59
+ if (store === 'treasury') {
60
+ return existing.recordType === 'transaction' || incoming.recordType === 'transaction';
61
+ }
62
+ if (store === 'journal_lines' || store === 'invoice_items') {
63
+ const parentStore = store === 'journal_lines' ? 'journal_entries' : 'invoices';
64
+ const relation = store === 'journal_lines' ? 'journalId' : 'invoiceId';
65
+ const a = await tx.get(parentStore, existing[relation]);
66
+ const b = existing[relation] === incoming[relation] ? a :
67
+ await tx.get(parentStore, incoming[relation]);
68
+ return parentStore === 'journal_entries'
69
+ ? a?.status === 'posted' || b?.status === 'posted'
70
+ : Boolean(a?.financePosted || b?.financePosted);
71
+ }
72
+ return false;
73
+}
74
75
// Compare snapshot metadata without depending on JSON property insertion order.
76
function snapshotComparable(value) {
@@ -383,7 +409,20 @@ class StorageServiceImpl {
409
}
410
}
411
} else {
386
- await Promise.all(items.map(item => tx.putRaw(store, item)));
412
+ for (const item of items) {
413
+ // A full backup is not an authorization to rewrite posted invoices,
414
+ // journals, settlements, cheques, money or inventory movements.
415
+ // Older snapshots may add missing rows but conflicting historical
416
+ // financial facts fail the complete restore transaction.
417
+ const previous = item.id === undefined ? null : await tx.get(store, item.id);
418
+ if (previous && await existingFinalizedRecord(tx, store, item, previous)) {
419
+ if (!sameSnapshotRecord(previous, item)) {
420
+ throw new Error('بازنویسی سند قطعی مالی در بازیابی کامل مجاز نیست');
421
+ }
422
+ continue;
423
+ }
424
+ await tx.putRaw(store, item);
425
+ }
426
}
427
}
428
});