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

fix(A04): validate company-restored posted invoices, cheques and treasury journal links

davoodmehraban89 committed Oct 16, 2026 at 17:30 UTC 66c10395c8adbfa170fdbf9a14dd29af4a8bf3bc
1 file changed +46
src/core/CompanyBackupService.js
+46
@@ -84,6 +84,52 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
84 }
85 }
86
87 + // A restored row labelled as finance-posted must point to the real
88 + // posted journal produced by its source command. Otherwise a partial
89 + // backup could fabricate a "paid/posted" invoice without any ledger.
90 + for (const [store, kind] of [
91 + ['invoices', 'invoice'], ['expenses', 'expense'], ['cheques', 'cheque']
92 + ]) {
93 + for (const row of data[store] || []) {
94 + if (!row.financePosted) continue;
95 + if (!isId(row.journalId)) {
96 + throw Error('سند مالی قطعی بازیابی‌شده فاقد شناسه دفتر کل است');
97 + }
98 + const journal = await tx.get('journal_entries', row.journalId);
99 + if (!journal || journal.companyId !== companyId ||
100 + journal.status !== 'posted' ||
101 + journal.sourceType !== kind || journal.sourceId !== row.id) {
102 + throw Error('ارجاع دفتر کل سند مالی بازیابی‌شده نامعتبر است');
103 + }
104 + if (store === 'cheques' && row.statusVersion === 2) {
105 + const statusJournal = await tx.get('journal_entries', row.statusJournalId);
106 + if (!statusJournal || statusJournal.companyId !== companyId ||
107 + statusJournal.status !== 'posted' ||
108 + statusJournal.sourceType !== 'cheque_status' ||
109 + statusJournal.sourceId !== row.id) {
110 + throw Error('ارجاع سند تغییر وضعیت چک بازیابی‌شده نامعتبر است');
111 + }
112 + }
113 + }
114 + }
115 + for (const row of data.treasury || []) {
116 + if (row.recordType !== 'transaction' || !row.financePosted) continue;
117 + if (!isId(row.journalId)) {
118 + throw Error('تراکنش مالی بازیابی‌شده فاقد سند دفتر کل است');
119 + }
120 + const journal = await tx.get('journal_entries', row.journalId);
121 + const journalTypes = new Set(['treasury', 'expense', 'cheque_status']);
122 + if (!journal || journal.companyId !== companyId ||
123 + journal.status !== 'posted' || !journalTypes.has(journal.sourceType)) {
124 + throw Error('تراکنش خزانه به سند دفتر کل معتبر اشاره ندارد');
125 + }
126 + if (journal.sourceType === 'treasury' && journal.sourceId !== row.id ||
127 + journal.sourceType === 'expense' && journal.sourceId !== row.refExpenseId ||
128 + journal.sourceType === 'cheque_status' && journal.sourceId !== row.refChequeId) {
129 + throw Error('سند دفتر کل تراکنش خزانه به عملیات دیگری تعلق دارد');
130 + }
131 + }
132 +
133 if (COMPANY_BACKUP_STORES.filter(s => V2_STORES.has(s)).some(s => (data[s] || []).length)) {
134 const accounts = await tx.getAllByIndex('account_chart', 'companyId', companyId);
135 const periods = await tx.getAllByIndex('fiscal_periods', 'companyId', companyId);