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

feat(A03): atomically audit manual journal posting, reversal and fiscal closing

davoodmehraban89 committed Oct 14, 2026 at 23:22 UTC 3b757c59c7b5d78fbfbd824c232aa4c28f57e7b7
1 file changed +12 -3
src/core/AccountingService.js
+12 -3
@@ -3,6 +3,7 @@
3 import { StorageService } from './StorageService.js';
4 import { DataScope } from './DataScope.js';
5 import { Auth } from './Auth.js';
6 +import { AuditService } from './AuditService.js';
7 import { buildLedgerReports } from './LedgerReports.mjs';
8 import { reconcileFinancialData } from './ReconciliationCore.mjs';
9 import {
@@ -143,7 +144,7 @@ class AccountingServiceImpl {
144 async closePeriod(periodId) {
145 if (!Auth.isAdmin()) throw new Error('بستن دوره مالی فقط برای مدیر مجاز است');
146 const companyId = await DataScope.companyId();
146 - return StorageService.transaction(['fiscal_periods', 'journal_entries'], async tx => {
147 + return StorageService.transaction(['fiscal_periods', 'journal_entries', 'audit_events'], async tx => {
148 const period = await periodFor(tx, companyId, periodId);
149 ensurePeriodOpen(period);
150 const journals = await entriesFor(tx, companyId);
@@ -152,6 +153,8 @@ class AccountingServiceImpl {
153 }
154 const closed = { ...period, status: 'closed', closed: true, closedBy: actor(), closedAt: new Date().toISOString() };
155 await tx.put('fiscal_periods', closed);
156 + await AuditService.append(tx, { companyId, eventType: 'period_closed', sourceType: 'fiscal_period',
157 + sourceId: periodId, commandKey: 'close-period:' + periodId, businessDate: period.endDate });
158 return closed;
159 });
160 }
@@ -247,7 +250,7 @@ class AccountingServiceImpl {
250 const lines = cleanLines(data.lines);
251 const sourceKey = data.sourceType && data.sourceId
252 ? journalSourceKey(companyId, data.sourceType, data.sourceId, data.revision || 1) : undefined;
250 - return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines'], async tx => {
253 + return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines', 'audit_events'], async tx => {
254 const period = await periodFor(tx, companyId, data.periodId);
255 const accounts = await tx.getAllByIndex('account_chart', 'companyId', companyId);
256 const totals = validateJournal({ companyId, date: data.date, lines }, accounts, period);
@@ -264,6 +267,9 @@ class AccountingServiceImpl {
267 for (const [position, line] of lines.entries()) {
268 await tx.put('journal_lines', { ...line, id: uid('jl_'), companyId, journalId: id, position });
269 }
270 + await AuditService.append(tx, { companyId, eventType: 'journal_posted', sourceType: 'journal',
271 + sourceId: journal.id, commandKey: 'manual-journal:' + (sourceKey || journal.id),
272 + businessDate: journal.date, lineCount: lines.length });
273 return journal;
274 });
275 }
@@ -300,7 +306,7 @@ class AccountingServiceImpl {
306 async reverseJournal(originalId, { periodId, date, description = '' }) {
307 requireAccountingAdmin();
308 const companyId = await DataScope.companyId();
303 - return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines'], async tx => {
309 + return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines', 'audit_events'], async tx => {
310 const original = await tx.get('journal_entries', originalId);
311 if (!original || original.companyId !== companyId || original.status !== 'posted' ||
312 original.reversedById || original.reversalOfId) throw new Error('این سند قابل برگشت نیست');
@@ -327,6 +333,9 @@ class AccountingServiceImpl {
333 await tx.put('journal_lines', { ...line, id: uid('jl_'), companyId, journalId: id, position });
334 }
335 await tx.put('journal_entries', { ...original, reversedById: id, reversedAt: journal.postedAt });
336 + await AuditService.append(tx, { companyId, eventType: 'journal_reversed', sourceType: 'journal',
337 + sourceId: id, commandKey: 'manual-reversal:' + originalId, businessDate: date,
338 + lineCount: lines.length });
339 return journal;
340 });
341 }