فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A05): guard fiscal close against corrupt ledger or open stocktakes
* fix(A05): block fiscal close with unbalanced journals or open stocktakes * test(A05): enforce balanced posted journal and closed stocktake before period close
davoodmehraban89 committed
Oct 14, 2026 at 23:52 UTC
720c5cb7ceb87cc96de41ba25c183204345f527d
2 files changed
+90
-3
src/core/AccountingService.js
+27
-3
@@ -147,13 +147,37 @@ class AccountingServiceImpl {
147
async closePeriod(periodId) {
148
if (!Auth.isAdmin()) throw new Error('بستن دوره مالی فقط برای مدیر مجاز است');
149
const companyId = await DataScope.companyId();
150
- return StorageService.transaction(['fiscal_periods', 'journal_entries', 'audit_events'], async tx => {
150
+ return StorageService.transaction([
151
+ 'fiscal_periods', 'journal_entries', 'journal_lines',
152
+ 'account_chart', 'stocktakes', 'audit_events'
153
+ ], async tx => {
154
const period = await periodFor(tx, companyId, periodId);
155
ensurePeriodOpen(period);
153
- const journals = await entriesFor(tx, companyId);
154
- if (journals.some(j => j.periodId === periodId && j.status !== 'posted')) {
156
+ const journals = (await entriesFor(tx, companyId)).filter(j => j.periodId === periodId);
157
+ if (journals.some(j => j.status !== 'posted')) {
158
throw new Error('دوره دارای اسناد پیشنویس است؛ ابتدا تعیین تکلیف شوند');
159
}
160
+ const stocktakes = (await tx.getAllByIndex('stocktakes', 'companyId', companyId))
161
+ .filter(row => row.status === 'draft');
162
+ if (stocktakes.length) {
163
+ throw new Error('انبارگردانی باز وجود دارد؛ ابتدا شمارش را نهایی کنید');
164
+ }
165
+ const accounts = await tx.getAllByIndex('account_chart', 'companyId', companyId);
166
+ const lines = await tx.getAllByIndex('journal_lines', 'companyId', companyId);
167
+ const byJournal = new Map();
168
+ for (const line of lines) {
169
+ if (!byJournal.has(line.journalId)) byJournal.set(line.journalId, []);
170
+ byJournal.get(line.journalId).push(line);
171
+ }
172
+ for (const journal of journals) {
173
+ if (journal.date < period.startDate || journal.date > period.endDate) {
174
+ throw new Error('دوره شامل سندی با تاریخ خارج از محدوده است');
175
+ }
176
+ const rowLines = byJournal.get(journal.id) || [];
177
+ if (rowLines.length < 2) throw new Error('دوره شامل سند قطعی بدون سطرهای کامل است');
178
+ validateJournalLines({ companyId, lines: rowLines }, accounts,
179
+ { requireBalance: true, allowInactive: true, allowParent: true });
180
+ }
181
const closed = { ...period, status: 'closed', closed: true, closedBy: actor(), closedAt: new Date().toISOString() };
182
await tx.put('fiscal_periods', closed);
183
await AuditService.append(tx, { companyId, eventType: 'period_closed', sourceType: 'fiscal_period',
tests/a05-fiscal-close-gates.test.mjs
new
+63
@@ -0,0 +1,63 @@
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 { AccountingService } from '../src/core/AccountingService.js';
6
+import { AuditService } from '../src/core/AuditService.js';
7
+import { CompanyService } from '../src/core/CompanyService.js';
8
+import { Auth } from '../src/core/Auth.js';
9
+
10
+async function setup() {
11
+ await StorageService.clearAll();
12
+ Auth.currentUser = { id: 'manager-a05', role: 'admin', username: 'manager' };
13
+ await StorageService.put('companies', { id: 'a05', name: 'Test A05' });
14
+ CompanyService.activeCompanyId = 'a05';
15
+ const period = await AccountingService.createPeriod({
16
+ name: '1405', startDate: '1405/01/01', endDate: '1405/12/29'
17
+ });
18
+ await AccountingService.seedDefaultChart();
19
+ const accounts = await AccountingService.listAccounts();
20
+ return { period, debit: accounts.find(x => x.code === '102'),
21
+ credit: accounts.find(x => x.code === '401') };
22
+}
23
+
24
+test('A05: a historically unbalanced posted journal must block fiscal close', async () => {
25
+ const { period, debit, credit } = await setup();
26
+ await StorageService.put('journal_entries', {
27
+ id: 'corrupt-posted', companyId: 'a05', periodId: period.id,
28
+ date: '1405/08/20', status: 'posted'
29
+ });
30
+ await StorageService.put('journal_lines', {
31
+ id: 'line-1', companyId: 'a05', journalId: 'corrupt-posted',
32
+ accountId: debit.id, debit: 100, credit: 0
33
+ });
34
+ await StorageService.put('journal_lines', {
35
+ id: 'line-2', companyId: 'a05', journalId: 'corrupt-posted',
36
+ accountId: credit.id, debit: 0, credit: 90
37
+ });
38
+ await assert.rejects(AccountingService.closePeriod(period.id), /تراز|سطرها/);
39
+ assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, false);
40
+ assert.ok(!(await AuditService.list()).some(e => e.eventType === 'period_closed'));
41
+
42
+ await StorageService.put('journal_lines', {
43
+ id: 'line-2', companyId: 'a05', journalId: 'corrupt-posted',
44
+ accountId: credit.id, debit: 0, credit: 100
45
+ });
46
+ await AccountingService.closePeriod(period.id);
47
+ assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, true);
48
+ assert.equal((await AuditService.list()).filter(e => e.eventType === 'period_closed').length, 1);
49
+});
50
+
51
+test('A05: an open stocktake blocks period close without changing the period', async () => {
52
+ const { period } = await setup();
53
+ await StorageService.put('stocktakes', {
54
+ id: 'stocktake-draft', companyId: 'a05', warehouseId: 'wh',
55
+ status: 'draft'
56
+ });
57
+ await assert.rejects(AccountingService.closePeriod(period.id), /انبارگردانی باز/);
58
+ assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, false);
59
+ assert.ok(!(await AuditService.list()).some(e => e.eventType === 'period_closed'));
60
+ await StorageService.delete('stocktakes', 'stocktake-draft');
61
+ await AccountingService.closePeriod(period.id);
62
+ await assert.rejects(AccountingService.closePeriod(period.id), /بسته/);
63
+});