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

fix(A02): return actionable blocked opening preflight for unbalanced or empty input

* fix(A02): return imbalance diagnostics from preview instead of throwing early * test(A02): ensure imbalanced inputs return actionable issues without posting * fix(A02): return missing-lines issue for empty opening file preflight * test(A02): empty opening input returns actionable blocked report

davoodmehraban89 committed Oct 16, 2026 at 17:08 UTC 4f0a334133a1b7aabc29d8db7f56efee51bfdc1c
2 files changed +36 -5
src/core/OpeningBalanceService.js
+13 -5
@@ -92,11 +92,19 @@ async function examine(tx, companyId, input) {
92 }
93 // All chart rules are enforced even for readonly preview. This rejects
94 // inactive accounts, group parents, out-of-period dates and overflow.
95 - const validated = validateJournal({
96 - companyId, date: input.openingDate, lines: (input.lines || []).map(row => ({
97 - accountId: row.accountId, debit: row.debit, credit: row.credit
98 - }))
99 - }, accounts, period);
95 + // A dry-run should explain an unbalanced opening journal rather than
96 + // throwing before the user can see the specific discrepancy. Keep all
97 + // account/date/amount validations; only defer the balance requirement.
98 + const validated = input.lines.length
99 + ? validateJournal({
100 + companyId, date: input.openingDate, lines: input.lines.map(row => ({
101 + accountId: row.accountId, debit: row.debit, credit: row.credit
102 + }))
103 + }, accounts, period, {
104 + requireBalance: !problems.includes('UNBALANCED_OPENING_JOURNAL')
105 + })
106 + // An empty data-file is a blocked preview, not a failed posting attempt.
107 + : { ...report.journal };
108 if (validated.debit !== report.journal.debit ||
109 validated.credit !== report.journal.credit) {
110 throw Error('گزارش افتتاحیه با سطرهای سند مطابقت ندارد');
tests/a02-opening-posting-integration.test.mjs
+23
@@ -167,3 +167,26 @@ test('A02: ordinary users cannot preview or post ledger opening', async () => {
167 confirmation: 'CONFIRM_OPENING_LEDGER'
168 }), /فقط برای مدیر/);
169 });
170 +
171 +test('A02: an unbalanced opening preview displays its discrepancy without ledger mutations', async () => {
172 + const { input } = await setup();
173 + input.lines[4].credit = 699;
174 + const report = await OpeningBalanceService.preview(input);
175 + assert.equal(report.readiness, 'blocked');
176 + assert.ok(report.issues.includes('UNBALANCED_OPENING_JOURNAL'));
177 + assert.deepEqual(report.journal, { debit: 900, credit: 899 });
178 + assert.equal((await StorageService.getAll('journal_entries')).length, 0);
179 + await assert.rejects(OpeningBalanceService.commit(input, {
180 + confirmation: 'CONFIRM_OPENING_LEDGER'
181 + }), /UNBALANCED_OPENING_JOURNAL/);
182 + assert.equal((await StorageService.getAll('journal_entries')).length, 0);
183 +});
184 +
185 +test('A02: empty opening file produces a blocked report instead of a validation exception', async () => {
186 + const { input } = await setup();
187 + input.lines = [];
188 + const review = await OpeningBalanceService.preview(input);
189 + assert.equal(review.readiness, 'blocked');
190 + assert.ok(review.issues.includes('MISSING_OPENING_JOURNAL_LINES'));
191 + assert.equal((await StorageService.getAll('journal_entries')).length, 0);
192 +});