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';
46
['trial', 'تراز آزمایشی'],
47
['reports', 'گزارشهای مالی'],
48
...(Auth.isAdmin() ? [['reconciliation','کنترل مغایرت']] : []),
49
+ ...(Auth.isAdmin() ? [['opening','افتتاحیه کنترلشده']] : []),
50
['integration', 'عملیات یکپارچه و انبار']
51
];
52
return `<div class="page-title"><h2>حسابداری دوبل</h2>
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();