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

feat(A02): expose validated opening ledger review in administrator workbench

* feat(A02): add atomic validated opening ledger posting for preloaded masters * feat(A02): whitelist atomic opening-ledger audit event * fix(A02): reserve opening balance journal source for controlled service * test(A02): cover company-safe opening journal preview, posting, rollback and audit * docs(A02): document controlled opening ledger input and remaining migration boundaries * feat(A02): add admin opening-file preflight and explicit confirm UI without auto-import * test(A02): verify admin-only opening wizard and preview cannot commit data * fix(A02): invalidate approved opening preview when the selected file changes * test(A02): prevent stale approval after switching opening files

davoodmehraban89 committed Oct 16, 2026 at 16:59 UTC b01fd338b9b3736147996d37bc9fd877ffa3ffed
2 files changed +195
src/views/AccountingView.js
+115
@@ -1,6 +1,7 @@
1 // Factor-easyPro V2 / Phase 1: isolated accounting workbench.
2 // Existing invoice, inventory and treasury workflows are deliberately untouched.
3 import { AccountingService as Ledger } from '../core/AccountingService.js';
4 +import { OpeningBalanceService } from '../core/OpeningBalanceService.js';
5 import { RetailPostingService as Retail } from '../core/RetailPostingService.js';
6 import { DataScope } from '../core/DataScope.js';
7 import { Auth } from '../core/Auth.js';
@@ -45,6 +46,7 @@ class AccountingViewImpl {
46 ['trial', 'تراز آزمایشی'],
47 ['reports', 'گزارش‌های مالی'],
48 ...(Auth.isAdmin() ? [['reconciliation','کنترل مغایرت']] : []),
49 + ...(Auth.isAdmin() ? [['opening','افتتاحیه کنترل‌شده']] : []),
50 ['integration', 'عملیات یکپارچه و انبار']
51 ];
52 return `<div class="page-title"><h2>حسابداری دوبل</h2>
@@ -75,10 +77,123 @@ class AccountingViewImpl {
77 case 'trial': return this.renderTrial();
78 case 'reports': return this.renderReports();
79 case 'reconciliation': return this.renderReconciliation();
80 + case 'opening': return Auth.isAdmin() ? this.renderOpening() : '<p>دسترسی مجاز نیست.</p>';
81 case 'integration': return this.renderIntegration();
82 default: return this.renderJournals();
83 }
84 }
85 + renderOpening() {
86 + if (!Auth.isAdmin()) return '<p>دسترسی مجاز نیست.</p>';
87 + return `
88 + <section class="card" style="max-width:850px">
89 + <h3>ثبت دفتر افتتاحیه — فقط داده‌های پایه موجود</h3>
90 + <p style="line-height:1.9">
91 + پیش از هر ثبت، باید اشخاص، حساب‌ها، موجودی انبار و بانک/صندوق در شرکت فعال
92 + ایجاد و کنترل شده باشند. این صفحه <strong>هیچ داده قدیمی را وارد نمی‌کند</strong>
93 + و فقط یک سند دفتر کل افتتاحیه تراز و تأییدشده ثبت می‌کند.
94 + </p>
95 + <p style="color:var(--text-muted);font-size:12px">
96 + فایل JSON باید حاوی periodId، openingDate، lines، contacts، stock و cash
97 + مطابق قرارداد docs/A02_OPENING_LEDGER_SCOPE_FA.md باشد.
98 + فایل برای بررسی در حافظه خوانده می‌شود و بدون تأیید شما نوشته نخواهد شد.
99 + </p>
100 + <div class="form-group">
101 + <label class="form-label" for="openingFileInput">فایل افتتاحیه برای بررسی</label>
102 + <input id="openingFileInput" class="form-control" type="file"
103 + accept=".json,application/json"
104 + onchange="window.AccountingView.resetOpeningPreview()" />
105 + </div>
106 + <button class="btn btn-secondary" onclick="window.AccountingView.previewOpeningFile()">
107 + کنترل مانده‌ها و دوره (بدون ثبت)
108 + </button>
109 + <div id="openingPreviewReport" style="margin-top:16px" role="status">
110 + <p>هنوز فایلی بررسی نشده است.</p>
111 + </div>
112 + </section>
113 + `;
114 + }
115 +
116 + resetOpeningPreview() {
117 + this._openingCandidate = null;
118 + this._openingReview = null;
119 + const host = document.getElementById('openingPreviewReport');
120 + if (host) host.textContent = 'فایل تغییر کرد؛ لازم است دوباره بررسی شود.';
121 + }
122 +
123 + async previewOpeningFile() {
124 + if (!Auth.isAdmin()) { Toast.error('دسترسی فقط برای مدیر مجاز است'); return; }
125 + const file = document.getElementById('openingFileInput')?.files?.[0];
126 + if (!file || file.size > 1024 * 1024) {
127 + Toast.error('یک فایل JSON معتبر و حداکثر ۱ مگابایتی انتخاب کنید');
128 + return;
129 + }
130 + const host = document.getElementById('openingPreviewReport');
131 + this._openingCandidate = null;
132 + this._openingReview = null;
133 + try {
134 + const raw = await file.text();
135 + const candidate = JSON.parse(raw);
136 + if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) {
137 + throw Error('ساختار فایل ورودی نامعتبر است');
138 + }
139 + const review = await OpeningBalanceService.preview(candidate);
140 + this._openingCandidate = candidate;
141 + this._openingReview = review;
142 + if (host) {
143 + const issues = review.issues.map(code => `<li>${html(code)}</li>`).join('');
144 + host.innerHTML = `
145 + <p><strong>وضعیت:</strong>
146 + ${review.readiness === 'ready-for-review' ? 'آماده تأیید' : 'ثبت مسدود'}
147 + </p>
148 + <p>بدهکار: ${Formatters.number(review.journal.debit)} ریال
149 + | بستانکار: ${Formatters.number(review.journal.credit)} ریال</p>
150 + ${issues ? `<ul style="color:var(--danger)">${issues}</ul>` :
151 + '<p>در کنترل‌های فعلی مغایرتی یافت نشد.</p>'}
152 + <button class="btn btn-danger" ${review.readiness === 'ready-for-review' ? '' : 'disabled'}
153 + onclick="window.AccountingView.confirmOpeningPosting()">
154 + ثبت قطعی سند افتتاحیه
155 + </button>
156 + <p style="font-size:12px;color:var(--text-muted)">
157 + در زمان ثبت، تمام کنترل‌ها دوباره در یک تراکنش انجام می‌شوند.
158 + بدون تأیید صریح هیچ داده‌ای نوشته نخواهد شد.
159 + </p>
160 + `;
161 + }
162 + } catch (error) {
163 + if (host) host.textContent = 'بررسی افتتاحیه ناموفق بود: ' + error.message;
164 + Toast.error('بررسی افتتاحیه متوقف شد: ' + error.message);
165 + }
166 + }
167 +
168 + confirmOpeningPosting() {
169 + if (!Auth.isAdmin() || this._openingReview?.readiness !== 'ready-for-review' ||
170 + !this._openingCandidate) {
171 + Toast.error('ابتدا فایل افتتاحیه را بدون مغایرت بررسی کنید');
172 + return;
173 + }
174 + Modal.confirm({
175 + title: 'تأیید ثبت قطعی افتتاحیه',
176 + message: 'این عملیات سند دفتر کل افتتاحیه و رویداد حسابرسی ثبت می‌کند. داده‌های پایه باید قبلاً کنترل و پشتیبان‌گیری شده باشند. آیا ثبت قطعی را تأیید می‌کنید؟',
177 + confirmText: 'تأیید ثبت قطعی',
178 + danger: true,
179 + onConfirm: async () => {
180 + const candidate = this._openingCandidate;
181 + this._openingCandidate = null;
182 + this._openingReview = null;
183 + try {
184 + const result = await OpeningBalanceService.commit(candidate, {
185 + confirmation: 'CONFIRM_OPENING_LEDGER'
186 + });
187 + Toast.success('سند افتتاحیه ' + result.journalId + ' با موفقیت ثبت شد');
188 + this.tab = 'journals';
189 + await this.refresh();
190 + } catch (error) {
191 + Toast.error('ثبت افتتاحیه انجام نشد: ' + error.message);
192 + }
193 + }
194 + });
195 + }
196 +
197 openPeriods() {
198 this.tab = 'periods';
199 return this.refresh();
tests/a02-opening-ledger-view.test.mjs new
+80
@@ -0,0 +1,80 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { AccountingView } from '../src/views/AccountingView.js';
5 +import { Auth } from '../src/core/Auth.js';
6 +import { OpeningBalanceService } from '../src/core/OpeningBalanceService.js';
7 +
8 +test('A02: opening ledger controls are hidden from ordinary users', () => {
9 + const prev = Auth.currentUser;
10 + try {
11 + Auth.currentUser = { id: 'staff', role: 'user' };
12 + assert.doesNotMatch(AccountingView.renderOpening(), /type="file"/);
13 + Auth.currentUser = { id: 'admin', role: 'admin' };
14 + const html = AccountingView.renderOpening();
15 + assert.match(html, /type="file"/);
16 + assert.match(html, /کنترل مانده‌ها و دوره/);
17 + assert.match(html, /هیچ داده قدیمی را وارد نمی‌کند/);
18 + assert.doesNotMatch(html, /ثبت خودکار/);
19 + } finally {
20 + Auth.currentUser = prev;
21 + }
22 +});
23 +
24 +test('A02: JSON preview UI calls only readonly preflight, not commit', async () => {
25 + const originalUser = Auth.currentUser;
26 + const oldDocument = globalThis.document;
27 + const oldPreview = OpeningBalanceService.preview;
28 + const oldCommit = OpeningBalanceService.commit;
29 + const file = { size: 75, text: async () => '{"periodId":"period-example","lines":[]}' };
30 + const host = { innerHTML: '', textContent: '' };
31 + let preflightCount = 0, committedCount = 0;
32 + try {
33 + Auth.currentUser = { id: 'admin', role: 'admin' };
34 + globalThis.document = {
35 + getElementById(id) {
36 + if (id === 'openingFileInput') return { files: [file] };
37 + if (id === 'openingPreviewReport') return host;
38 + return null;
39 + }
40 + };
41 + OpeningBalanceService.preview = async draft => {
42 + preflightCount++;
43 + assert.equal(draft.periodId, 'period-example');
44 + return { readiness: 'blocked', journal: { debit: 100, credit: 99 },
45 + issues: ['UNBALANCED_OPENING_JOURNAL'] };
46 + };
47 + OpeningBalanceService.commit = async () => { committedCount++; };
48 + await AccountingView.previewOpeningFile();
49 + assert.equal(preflightCount, 1);
50 + assert.equal(committedCount, 0);
51 + assert.match(host.innerHTML, /UNBALANCED_OPENING_JOURNAL/);
52 + assert.match(host.innerHTML, /disabled/);
53 + assert.equal(AccountingView._openingReview.readiness, 'blocked');
54 + } finally {
55 + Auth.currentUser = originalUser;
56 + globalThis.document = oldDocument;
57 + OpeningBalanceService.preview = oldPreview;
58 + OpeningBalanceService.commit = oldCommit;
59 + AccountingView._openingCandidate = null;
60 + AccountingView._openingReview = null;
61 + }
62 +});
63 +
64 +test('A02: switching input files invalidates an earlier approval', () => {
65 + const originalDocument = globalThis.document;
66 + const reportNode = { textContent: '' };
67 + try {
68 + AccountingView._openingCandidate = { periodId: 'OLD_PERIOD' };
69 + AccountingView._openingReview = { readiness: 'ready-for-review' };
70 + globalThis.document = {
71 + getElementById(id) { return id === 'openingPreviewReport' ? reportNode : null; }
72 + };
73 + AccountingView.resetOpeningPreview();
74 + assert.equal(AccountingView._openingCandidate, null);
75 + assert.equal(AccountingView._openingReview, null);
76 + assert.match(reportNode.textContent, /دوباره بررسی/);
77 + } finally {
78 + globalThis.document = originalDocument;
79 + }
80 +});