فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(A02): add admin opening-file preflight and explicit confirm UI without auto-import
davoodmehraban89 committed
Oct 16, 2026 at 16:49 UTC
64f0e3e11bd99d9bb20a2cfe097a07a7d44c7cf9
1 file changed
+107
src/views/AccountingView.js
+107
@@ -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,115 @@ 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
+ </div>
105
+ <button class="btn btn-secondary" onclick="window.AccountingView.previewOpeningFile()">
106
+ کنترل ماندهها و دوره (بدون ثبت)
107
+ </button>
108
+ <div id="openingPreviewReport" style="margin-top:16px" role="status">
109
+ <p>هنوز فایلی بررسی نشده است.</p>
110
+ </div>
111
+ </section>
112
+ `;
113
+ }
114
+
115
+ async previewOpeningFile() {
116
+ if (!Auth.isAdmin()) { Toast.error('دسترسی فقط برای مدیر مجاز است'); return; }
117
+ const file = document.getElementById('openingFileInput')?.files?.[0];
118
+ if (!file || file.size > 1024 * 1024) {
119
+ Toast.error('یک فایل JSON معتبر و حداکثر ۱ مگابایتی انتخاب کنید');
120
+ return;
121
+ }
122
+ const host = document.getElementById('openingPreviewReport');
123
+ this._openingCandidate = null;
124
+ this._openingReview = null;
125
+ try {
126
+ const raw = await file.text();
127
+ const candidate = JSON.parse(raw);
128
+ if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) {
129
+ throw Error('ساختار فایل ورودی نامعتبر است');
130
+ }
131
+ const review = await OpeningBalanceService.preview(candidate);
132
+ this._openingCandidate = candidate;
133
+ this._openingReview = review;
134
+ if (host) {
135
+ const issues = review.issues.map(code => `<li>${html(code)}</li>`).join('');
136
+ host.innerHTML = `
137
+ <p><strong>وضعیت:</strong>
138
+ ${review.readiness === 'ready-for-review' ? 'آماده تأیید' : 'ثبت مسدود'}
139
+ </p>
140
+ <p>بدهکار: ${Formatters.number(review.journal.debit)} ریال
141
+ | بستانکار: ${Formatters.number(review.journal.credit)} ریال</p>
142
+ ${issues ? `<ul style="color:var(--danger)">${issues}</ul>` :
143
+ '<p>در کنترلهای فعلی مغایرتی یافت نشد.</p>'}
144
+ <button class="btn btn-danger" ${review.readiness === 'ready-for-review' ? '' : 'disabled'}
145
+ onclick="window.AccountingView.confirmOpeningPosting()">
146
+ ثبت قطعی سند افتتاحیه
147
+ </button>
148
+ <p style="font-size:12px;color:var(--text-muted)">
149
+ در زمان ثبت، تمام کنترلها دوباره در یک تراکنش انجام میشوند.
150
+ بدون تأیید صریح هیچ دادهای نوشته نخواهد شد.
151
+ </p>
152
+ `;
153
+ }
154
+ } catch (error) {
155
+ if (host) host.textContent = 'بررسی افتتاحیه ناموفق بود: ' + error.message;
156
+ Toast.error('بررسی افتتاحیه متوقف شد: ' + error.message);
157
+ }
158
+ }
159
+
160
+ confirmOpeningPosting() {
161
+ if (!Auth.isAdmin() || this._openingReview?.readiness !== 'ready-for-review' ||
162
+ !this._openingCandidate) {
163
+ Toast.error('ابتدا فایل افتتاحیه را بدون مغایرت بررسی کنید');
164
+ return;
165
+ }
166
+ Modal.confirm({
167
+ title: 'تأیید ثبت قطعی افتتاحیه',
168
+ message: 'این عملیات سند دفتر کل افتتاحیه و رویداد حسابرسی ثبت میکند. دادههای پایه باید قبلاً کنترل و پشتیبانگیری شده باشند. آیا ثبت قطعی را تأیید میکنید؟',
169
+ confirmText: 'تأیید ثبت قطعی',
170
+ danger: true,
171
+ onConfirm: async () => {
172
+ const candidate = this._openingCandidate;
173
+ this._openingCandidate = null;
174
+ this._openingReview = null;
175
+ try {
176
+ const result = await OpeningBalanceService.commit(candidate, {
177
+ confirmation: 'CONFIRM_OPENING_LEDGER'
178
+ });
179
+ Toast.success('سند افتتاحیه ' + result.journalId + ' با موفقیت ثبت شد');
180
+ this.tab = 'journals';
181
+ await this.refresh();
182
+ } catch (error) {
183
+ Toast.error('ثبت افتتاحیه انجام نشد: ' + error.message);
184
+ }
185
+ }
186
+ });
187
+ }
188
+
189
openPeriods() {
190
this.tab = 'periods';
191
return this.refresh();