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

feat(A03): atomically audit period creation and all manual journal draft transitions

davoodmehraban89 committed Oct 14, 2026 at 23:40 UTC 1481dcc5971fad6643ccb7b0b8bb668febf97ebe
1 file changed +21 -5
src/core/AccountingService.js
+21 -5
@@ -134,9 +134,12 @@ class AccountingServiceImpl {
134 status: 'open', closed: false, createdBy: actor()
135 };
136 if (!period.name) throw new Error('نام دوره مالی الزامی است');
137 - return StorageService.transaction('fiscal_periods', async tx => {
137 + return StorageService.transaction(['fiscal_periods', 'audit_events'], async tx => {
138 validateFiscalPeriod(period, await tx.getAllByIndex('fiscal_periods', 'companyId', companyId));
139 await tx.put('fiscal_periods', period);
140 + await AuditService.append(tx, { companyId, eventType: 'period_opened',
141 + sourceType: 'fiscal_period', sourceId: period.id,
142 + commandKey: 'open-period:' + period.id, businessDate: period.startDate });
143 return period;
144 });
145 }
@@ -187,7 +190,7 @@ class AccountingServiceImpl {
190 const lines = cleanLines(data.lines);
191 const sourceKey = data.sourceType && data.sourceId
192 ? journalSourceKey(companyId, data.sourceType, data.sourceId, data.revision || 1) : undefined;
190 - return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines'], async tx => {
193 + return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines', 'audit_events'], async tx => {
194 const period = await periodFor(tx, companyId, data.periodId);
195 ensurePeriodOpen(period);
196 assertJalaliDate(data.date);
@@ -206,6 +209,9 @@ class AccountingServiceImpl {
209 for (const [position, line] of lines.entries()) {
210 await tx.put('journal_lines', { ...line, id: uid('jl_'), companyId, journalId: id, position });
211 }
212 + await AuditService.append(tx, { companyId, eventType: 'draft_created', sourceType: 'journal',
213 + sourceId: journal.id, commandKey: 'draft-create:' + journal.id,
214 + businessDate: journal.date, lineCount: lines.length });
215 return journal;
216 });
217 }
@@ -214,7 +220,7 @@ class AccountingServiceImpl {
220 requireAccountingAdmin();
221 const companyId = await DataScope.companyId();
222 const lines = cleanLines(data.lines);
217 - return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines'], async tx => {
223 + return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines', 'audit_events'], async tx => {
224 const old = await tx.get('journal_entries', id);
225 if (!old || old.companyId !== companyId || old.status !== 'draft') throw new Error('فقط پیش‌نویس این شرکت قابل ویرایش است');
226 const period = await periodFor(tx, companyId, data.periodId);
@@ -238,6 +244,10 @@ class AccountingServiceImpl {
244 for (const [position, line] of lines.entries()) {
245 await tx.put('journal_lines', { ...line, id: uid('jl_'), companyId, journalId: id, position });
246 }
247 + await AuditService.append(tx, { companyId, eventType: post ? 'draft_posted' : 'draft_updated',
248 + sourceType: 'journal', sourceId: journal.id,
249 + commandKey: 'draft-change:' + uid('edit_'),
250 + businessDate: journal.date, lineCount: lines.length });
251 return journal;
252 });
253 }
@@ -277,12 +287,15 @@ class AccountingServiceImpl {
287 async discardDraftJournal(id) {
288 requireAccountingAdmin();
289 const companyId = await DataScope.companyId();
280 - return StorageService.transaction(['journal_entries', 'journal_lines'], async tx => {
290 + return StorageService.transaction(['journal_entries', 'journal_lines', 'audit_events'], async tx => {
291 const old = await tx.get('journal_entries', id);
292 if (!old || old.companyId !== companyId || old.status !== 'draft') throw new Error('فقط پیش‌نویس این شرکت قابل حذف است');
293 const lines = await tx.getAllByIndex('journal_lines', 'journalId', id);
294 for (const line of lines) await tx.delete('journal_lines', line.id);
295 await tx.delete('journal_entries', id);
296 + await AuditService.append(tx, { companyId, eventType: 'draft_discarded',
297 + sourceType: 'journal', sourceId: id, commandKey: 'draft-discard:' + id,
298 + businessDate: old.date, lineCount: lines.length });
299 return true;
300 });
301 }
@@ -290,7 +303,7 @@ class AccountingServiceImpl {
303 async postDraftJournal(id) {
304 requireAccountingAdmin();
305 const companyId = await DataScope.companyId();
293 - return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines'], async tx => {
306 + return StorageService.transaction(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines', 'audit_events'], async tx => {
307 const old = await tx.get('journal_entries', id);
308 if (!old || old.companyId !== companyId || old.status !== 'draft') throw new Error('پیش‌نویس قابل ثبت پیدا نشد');
309 const period = await periodFor(tx, companyId, old.periodId);
@@ -299,6 +312,9 @@ class AccountingServiceImpl {
312 const totals = validateJournal({ companyId, date: old.date, lines }, accounts, period);
313 const journal = { ...old, ...totals, status: 'posted', postedBy: actor(), postedAt: new Date().toISOString() };
314 await tx.put('journal_entries', journal);
315 + await AuditService.append(tx, { companyId, eventType: 'draft_posted',
316 + sourceType: 'journal', sourceId: id, commandKey: 'draft-finalize:' + id,
317 + businessDate: journal.date, lineCount: lines.length });
318 return journal;
319 });
320 }