فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A05): block fiscal close with unbalanced journals or open stocktakes
davoodmehraban89 committed
Oct 14, 2026 at 23:44 UTC
0830fabde0bac5fdc9a724b829a0993c9c300789
1 file changed
+27
-3
src/core/AccountingService.js
+27
-3
@@ -144,13 +144,37 @@ class AccountingServiceImpl {
144
async closePeriod(periodId) {
145
if (!Auth.isAdmin()) throw new Error('بستن دوره مالی فقط برای مدیر مجاز است');
146
const companyId = await DataScope.companyId();
147
- return StorageService.transaction(['fiscal_periods', 'journal_entries', 'audit_events'], async tx => {
147
+ return StorageService.transaction([
148
+ 'fiscal_periods', 'journal_entries', 'journal_lines',
149
+ 'account_chart', 'stocktakes', 'audit_events'
150
+ ], async tx => {
151
const period = await periodFor(tx, companyId, periodId);
152
ensurePeriodOpen(period);
150
- const journals = await entriesFor(tx, companyId);
151
- if (journals.some(j => j.periodId === periodId && j.status !== 'posted')) {
153
+ const journals = (await entriesFor(tx, companyId)).filter(j => j.periodId === periodId);
154
+ if (journals.some(j => j.status !== 'posted')) {
155
throw new Error('دوره دارای اسناد پیشنویس است؛ ابتدا تعیین تکلیف شوند');
156
}
157
+ const stocktakes = (await tx.getAllByIndex('stocktakes', 'companyId', companyId))
158
+ .filter(row => row.status === 'draft');
159
+ if (stocktakes.length) {
160
+ throw new Error('انبارگردانی باز وجود دارد؛ ابتدا شمارش را نهایی کنید');
161
+ }
162
+ const accounts = await tx.getAllByIndex('account_chart', 'companyId', companyId);
163
+ const lines = await tx.getAllByIndex('journal_lines', 'companyId', companyId);
164
+ const byJournal = new Map();
165
+ for (const line of lines) {
166
+ if (!byJournal.has(line.journalId)) byJournal.set(line.journalId, []);
167
+ byJournal.get(line.journalId).push(line);
168
+ }
169
+ for (const journal of journals) {
170
+ if (journal.date < period.startDate || journal.date > period.endDate) {
171
+ throw new Error('دوره شامل سندی با تاریخ خارج از محدوده است');
172
+ }
173
+ const rowLines = byJournal.get(journal.id) || [];
174
+ if (rowLines.length < 2) throw new Error('دوره شامل سند قطعی بدون سطرهای کامل است');
175
+ validateJournalLines({ companyId, lines: rowLines }, accounts,
176
+ { requireBalance: true, allowInactive: true, allowParent: true });
177
+ }
178
const closed = { ...period, status: 'closed', closed: true, closedBy: actor(), closedAt: new Date().toISOString() };
179
await tx.put('fiscal_periods', closed);
180
await AuditService.append(tx, { companyId, eventType: 'period_closed', sourceType: 'fiscal_period',