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

fix/phase-a-backup-integrity-checksum
a02/strict-export-preflight-20261018 audit/a01-readonly-invoice-metadata-20261101 ci/a06-require-trusted-issuer-for-release ci/main-postmerge-phase-a-validation-20261020 ci/phase-a-controlled-public-release design/issue-5-commercial-corrections-adr docs/a01-a02-a05-oct23-verified-evidence docs/a01-cheque-endorsement-evidence-20261022 docs/a01-cheque-event-date-evidence-20261021 docs/a01-explicit-refund-reason-evidence-20261021 docs/a01-refund-auth-status-20261020 docs/a01-refund-trace-evidence-20261020 docs/a02-102-evidence-20261018 docs/a02-pr104-evidence-20261018 docs/a02-pr106-evidence-20261019 docs/a02-pr108-ten-tests-20261019 docs/a02-private-sql-backup-evidence-20261111 docs/a03-merge-status-sync-20261020 docs/a04-software-gate-acceptance-20261020 docs/feature-matrix-20261014 docs/finora-master-execution-playbook docs/four-phase-execution-20261014 docs/four-phase-roadmap-20261014 docs/holoo-lab-stabilization-phase1-20261111 docs/holoo-runtime-schema-audit-20261111 docs/ops-142-governance-5-1-3-20261030 docs/phase-a-a01-a02-a06-a08-verified-evidence-20261023 docs/phase-a-a01-a05-a06-evidence-20261023 docs/phase-a-independent-acceptance-evidence docs/phase-a-pr110-evidence-20261019 docs/project-status-phase-a-20261014 feat/a01-referenced-return-corrections feat/a01-referenced-returns-on-main feat/a02-atomic-opening-ledger-preflight feat/a02-holoo-compatibility-preflight feat/a02-holoo-declared-control-totals feat/a02-holoo-explicit-csv-gate feat/a02-holoo-safe-zip-intake-offline-webview2-20261030 feat/a02-offline-sql-server-provisioning-20261030 feat/a02-opening-balance-dry-run feat/a02-opening-ledger-admin-ui feat/a02-opt-in-isolated-sql-restore-executor-20261030 feat/a02-private-holoo-zip-staging-20261030 feat/a02-safe-isolated-restore-plan-20261030 feat/a02-sql-backup-header-verifyonly-20261030 feat/a03-audit-schema-atomic-events feat/a03-complete-manual-journal-audit feat/a05-cheque-treasury-reconciliation feat/a06-signed-offline-license-activation feat/a08-synthetic-performance-baseline feat/phase-a-accounting-benchmark-return-preview feat/phase-a-ledger-settlement-diagnostics feature/v2-reconciliation-20261013 fix/a01-atomic-confirm-ui-one-shot-20261031 fix/a01-cheque-clearance-explicit-account-20261023 fix/a01-cheque-contractual-due-date-validity-20261023 fix/a01-cheque-endorsement-counterparty-20261022 fix/a01-cheque-event-date-open-period-20261021 fix/a01-cumulative-return-amount-cap fix/a01-explicit-refund-reason-approval-20261021 fix/a01-manager-authorized-customer-refunds-20261020 fix/a01-no-implicit-refund fix/a01-one-shot-confirm-on-atomic-import-20261031 fix/a01-refund-and-overcollection-net-cash fix/a01-refund-correction-trace-20261020 fix/a02-atomic-excel-master-merge-20261031 fix/a02-canonical-mapping-contract-20261018 fix/a02-disable-destructive-excel-replace-20261031 fix/a02-holoo-invoice-line-coverage-20261023 fix/a02-holoo-preflight-required-invoice-lines-20261023 fix/a02-mapping-contract-20261018 fix/a02-opening-preflight-unbalanced-report fix/a02-orphan-mapping-20261018 fix/a02-private-excel-import-session-20261031 fix/a02-strict-export-input-20261019 fix/a03-append-only-full-backup-import fix/a03-append-only-write-guard fix/a03-block-transaction-clear-append-only-20261016 fix/a03-chain-validation-20261020 fix/a03-company-scoped-audit-data-access fix/a03-deny-direct-audit-write-20261019 fix/a03-escaped-expense-list-20261031 fix/a03-escaped-financial-record-ids-20261031 fix/a03-financial-ledger-actions-and-print-20261031 fix/a04-backup-checksum-preflight fix/a04-company-audit-replay-stable-compare fix/a04-company-backup-source-isolation-20261020 fix/a04-full-restore-posted-financial-integrity fix/a04-reject-mixed-company-backup-20261019 fix/a04-replace-immutable-posted-ledger fix/a04-restore-financial-journal-references fix/a04-service-source-company-boundary-20261020 fix/a05-block-inactive-treasury-settlements fix/a05-cheque-cash-evidence-reconciliation-20261023 fix/a05-cheque-ledger-and-bank-reconciliation-20261023 fix/a05-fiscal-close-invariant-gates fix/a06-backup-restore-user-privileges fix/a06-license-user-company-quota-20261023 fix/a06-malformed-trial-state-fail-closed fix/a06-offline-clock-fail-closed-20261019 fix/a07-indexeddb-blocked-startup fix/a07-offline-bundled-assets fix/a07-version-and-startup-banner fix/issue-7-company-backup-isolation fix/p0-deterministic-fractional-invoice-money-20261031 fix/p0-fractional-quantity-input-20261031 fix/p0-fractional-quantity-integrated-20261031 fix/p0-invoice-posting-invariants-20261031 fix/p0-legacy-stock-and-product-inputs-20261031 fix/p0-reports-immutable-cogs-20261101 fix/p0-verified-financial-percent-20261031 fix/p0-zero-discount-report-revenue-20261101 fix/phase-a-backup-app-version fix/phase-a-backup-integrity-checksum fix/phase-a-backup-record-id-validation-20261014 fix/phase-a-backup-version-validation-20261014 fix/phase-a-closure-pack3-20261101 fix/phase-a-entitlement-payload-validation fix/phase-a-gate-exact-evidence-20261019 fix/phase-a-pack4-stock-scope-20261101 fix/phase-a-pack5-integrated-evidence-20261101 fix/phase-a-pack6-atomic-legacy-20261101 fix/phase-a-restore-envelope-and-rollback fix/phase-a-settings-admin-gate fix/print-browser-header-footer-20261014 fix/q01-expense-security-probe-exact-option-20261031 fix/q01-financial-grid-labels-20261031 fix/q01-form-label-association-20261031 fix/q01-native-evidence-integrity-20261030 fix/q01-number-input-persian-decimal-20261031 fix/q01-responsive-dashboard-and-cheque-table-20261031 fix/q01-safe-ui-action-audit-coverage-20261031 fix/q01-visible-desktop-sidebar-and-safe-confirm-20261030 fix/roadmap-metadata-sync-20261014 gate/a03-audit-metadata-validation-20261019 integration/a02-windows-holoo-setup-candidate-20261030 license-server main perf/a07-lazy-load-excel perf/a08-cheque-reconciliation-account-index-20261023 perf/a08-paginated-reports-20261101 phase1-restore-hardening planning/holoo-execution-20261013 planning/roadmap-project-management-20261014 qa/native-click-coverage-audit-20261030 release/v2.0.0 test/a01-native-endorsement-mandatory-20261023 test/a01-native-purchase-return-guardrails test/a01-native-sale-return-financial-guardrails test/a01-safe-integer-boundary-20261019 test/a02-native-opening-dry-run-rejections test/a02-native-positive-opening-ledger test/a04-native-corrupt-fbackup-20261016 test/a04-native-encrypted-company-backup test/a04-native-encrypted-company-backup-main test/a04-native-late-transaction-rollback test/a04-native-windows-backup-recovery test/a04-real-edge-v6-v7-migration test/a04-real-native-interrupted-restore test/a05-native-draft-ledger-and-repeat-close-20261016 test/a05-native-fiscal-close-guardrails test/a07-native-multipage-print-engine test/a07-native-origin-provenance test/a07-native-webview2-offline-routes test/a07-windows-offline-startup-smoke test/a08-installed-webview2-60k-baseline test/a08-native-webview2-heap-metrics test/a08-windows-synthetic-performance-baseline test/github-write-access-20261009 test/native-win-ui-routes-after-finance-backup test/phase-a-combined-a01-a05-a08-final-evidence test/phase-a-combined-native-acceptance-matrix test/phase-a-ledger-invariants-20261014 test/phase-a-native-all-routes-on-merged-lab test/phase-a-native-encrypted-backup test/phase-a-native-finance-backup-matrix test/phase-a-native-ui-and-backup-smoke test/phase-a-windows-native-installer-lab work/a08-dispatch-activation-20261123 work/a08-memory-stability-20261123 work/finance-curated-20261123
js 286 lines 13.8 KB
Raw
1 // ============================================================
2 // ContactLedgerModal — کارت حساب اشخاص (دفتر معین)
3 // ============================================================
4
5 import { ContactController } from '../controllers/ContactController.js';
6 import { Modal } from '../core/Modal.js';
7 import { Toast } from '../core/Toast.js';
8 import { Formatters } from '../utils/Formatters.js';
9 import { Jalali } from '../utils/Jalali.js';
10
11 class ContactLedgerModalImpl {
12 async open(contactId) {
13 const contact = await ContactController.get(contactId);
14 if (!contact) { Toast.error('شخص یافت نشد'); return; }
15
16 const ledger = await ContactController.getLedger(contactId);
17
18 const body = this._buildBody(contact, ledger);
19 const footer = `
20 <button class="btn btn-secondary" onclick="FINORA.Modal.close()">بستن</button>
21 <button class="btn" onclick="window.ContactLedgerModal._printLedger('${contactId}')">🖨️ چاپ صورت‌حساب</button>
22 `;
23
24 Modal.open({
25 title: `📒 کارت حساب: ${contact.name}`,
26 body,
27 footer,
28 size: 'lg'
29 });
30
31 window.__ledgerContactId = contactId;
32 }
33
34 _buildBody(contact, ledger) {
35 const balance = ledger.finalBalance;
36 const isDebtor = balance > 0;
37 const isCreditor = balance < 0;
38 const isSettled = balance === 0;
39
40 const balanceColor = isDebtor ? 'var(--danger)' : isCreditor ? 'var(--success)' : 'var(--text-muted)';
41 const balanceText = isSettled ? 'تسویه' :
42 isDebtor ? Formatters.money(balance) + ' بدهکار' :
43 Formatters.money(Math.abs(balance)) + ' بستانکار';
44
45 const entityLabel = contact.entityType === 'legal' ? 'حقوقی' : 'حقیقی';
46 const roleLabel = contact.role === 'customer' ? 'مشتری' :
47 contact.role === 'supplier' ? 'تامین‌کننده' : 'هر دو';
48
49 // ریز تراکنش‌ها
50 const rowsHtml = ledger.rows.length === 0
51 ? `<tr><td colspan="7" style="text-align:center;padding:30px;color:var(--text-muted)">
52 <div style="font-size:40px;margin-bottom:8px;opacity:.4">📭</div>
53 هیچ تراکنشی برای این شخص ثبت نشده
54 </td></tr>`
55 : ledger.rows.map(r => {
56 const typeColors = {
57 invoice_sale: 'badge-success',
58 invoice_purchase: 'badge-warning',
59 return_sale: 'badge-danger',
60 return_purchase: 'badge-danger',
61 receipt: 'badge-info',
62 payment: 'badge-danger',
63 cheque_in: 'badge-muted',
64 cheque_out: 'badge-muted'
65 };
66 const typeCls = typeColors[r.type] || 'badge-muted';
67
68 let balanceHtml;
69 if (r.isCheque) {
70 balanceHtml = `<span style="color:var(--text-muted);font-size:11px">— (در جریان: ${Formatters.money(r.chequeAmount || 0)})</span>`;
71 } else {
72 const bal = r.balance;
73 balanceHtml = `<strong style="color:${bal > 0 ? 'var(--danger)' : bal < 0 ? 'var(--success)' : 'var(--text-muted)'}">${Formatters.money(Math.abs(bal))}</strong> <small style="color:var(--text-muted);font-size:9.5px">${bal > 0 ? 'بد' : bal < 0 ? 'بس' : ''}</small>`;
74 }
75
76 return `
77 <tr>
78 <td style="font-size:11.5px;width:110px">${Formatters.toPersianDigits(r.date || '—')}</td>
79 <td><span class="badge ${typeCls}" style="font-size:10px">${this._esc(r.typeLabel)}</span></td>
80 <td style="font-size:11.5px;color:var(--text-muted)">${r.refNumber ? '#' + Formatters.toPersianDigits(r.refNumber) : '—'}</td>
81 <td style="text-align:left;font-weight:600;color:var(--danger)">
82 ${r.debit > 0 ? Formatters.money(r.debit) : '—'}
83 </td>
84 <td style="text-align:left;font-weight:600;color:var(--success)">
85 ${r.credit > 0 ? Formatters.money(r.credit) : '—'}
86 </td>
87 <td style="text-align:left">${balanceHtml}</td>
88 <td style="font-size:11px;color:var(--text-muted)">${this._esc(r.note || '—')}</td>
89 </tr>
90 `;
91 }).join('');
92
93 const tags = (contact.tags || []).map(t => `<span class="badge badge-muted" style="font-size:10px;margin-left:4px">${this._esc(t)}</span>`).join('');
94
95 return `
96 <div style="background:linear-gradient(135deg,#f0fdfa,#eff6ff);border-radius:12px;padding:16px;margin-bottom:16px;border:1px solid #bfdbfe">
97 <div style="display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:12px">
98 <div style="flex:1;min-width:220px">
99 <div style="font-size:16px;font-weight:800;margin-bottom:4px">${this._esc(contact.name)}</div>
100 <div style="font-size:12px;color:var(--text-muted);margin-bottom:4px">
101 <span class="badge ${contact.entityType === 'legal' ? 'badge-warning' : 'badge-success'}">${entityLabel}</span>
102 <span class="badge badge-info" style="margin-right:4px">${roleLabel}</span>
103 ${tags}
104 </div>
105 ${contact.mobile ? `<div style="font-size:11.5px;color:var(--text-muted)">📱 ${Formatters.phone(contact.mobile)}</div>` : ''}
106 ${contact.nationalId ? `<div style="font-size:11.5px;color:var(--text-muted)">🆔 ${Formatters.toPersianDigits(contact.nationalId)}</div>` : ''}
107 </div>
108 <div style="text-align:left;min-width:200px">
109 <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">مانده حساب</div>
110 <div style="font-size:20px;font-weight:800;color:${balanceColor}">${balanceText}</div>
111 </div>
112 </div>
113 </div>
114
115 <div class="grid-kpi" style="margin-bottom:14px">
116 <div class="card" style="margin:0;padding:12px 14px;border-top:3px solid var(--danger)">
117 <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">جمع بدهی (بدهکار)</div>
118 <div style="font-size:15px;font-weight:800;color:var(--danger)">${Formatters.money(ledger.totalDebit)}</div>
119 </div>
120 <div class="card" style="margin:0;padding:12px 14px;border-top:3px solid var(--success)">
121 <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">جمع پرداختی (بستانکار)</div>
122 <div style="font-size:15px;font-weight:800;color:var(--success)">${Formatters.money(ledger.totalCredit)}</div>
123 </div>
124 <div class="card" style="margin:0;padding:12px 14px;border-top:3px solid var(--primary)">
125 <div style="font-size:11px;color:var(--text-muted);margin-bottom:4px">تعداد تراکنش</div>
126 <div style="font-size:15px;font-weight:800;color:var(--primary)">${Formatters.number(ledger.rows.length)}</div>
127 </div>
128 </div>
129
130 <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px">
131 <div style="font-size:12.5px;color:var(--text-muted)">ریز تراکنش‌ها</div>
132 </div>
133
134 <div class="table-wrap" style="max-height:400px;overflow-y:auto">
135 <table style="min-width:750px">
136 <thead style="position:sticky;top:0;z-index:2">
137 <tr>
138 <th style="width:110px">تاریخ</th>
139 <th style="width:130px">نوع</th>
140 <th style="width:90px">مرجع</th>
141 <th style="width:130px;text-align:left">بدهکار</th>
142 <th style="width:130px;text-align:left">بستانکار</th>
143 <th style="width:160px;text-align:left">مانده</th>
144 <th>شرح</th>
145 </tr>
146 </thead>
147 <tbody>${rowsHtml}</tbody>
148 </table>
149 </div>
150 `;
151 }
152
153 // ============================================================
154 // چاپ صورت‌حساب
155 // ============================================================
156 async _printLedger(contactId) {
157 const contact = await ContactController.get(contactId);
158 if (!contact) return;
159 const ledger = await ContactController.getLedger(contactId);
160
161 const balance = ledger.finalBalance;
162 const isDebtor = balance > 0;
163 const isCreditor = balance < 0;
164 const balanceText = balance === 0 ? 'تسویه' :
165 isDebtor ? Formatters.money(balance) + ' بدهکار' :
166 Formatters.money(Math.abs(balance)) + ' بستانکار';
167
168 const rowsHtml = ledger.rows.map(r => {
169 let balanceCell;
170 if (r.isCheque) {
171 balanceCell = `در جریان: ${Formatters.money(r.chequeAmount || 0)}`;
172 } else {
173 const bal = r.balance;
174 balanceCell = `${Formatters.money(Math.abs(bal))} ${bal > 0 ? 'بد' : bal < 0 ? 'بس' : ''}`;
175 }
176
177 return `
178 <tr>
179 <td>${Formatters.toPersianDigits(r.date || '—')}</td>
180 <td>${this._esc(r.typeLabel)}</td>
181 <td>${r.refNumber ? '#' + Formatters.toPersianDigits(r.refNumber) : '—'}</td>
182 <td>${r.debit > 0 ? Formatters.money(r.debit) : '—'}</td>
183 <td>${r.credit > 0 ? Formatters.money(r.credit) : '—'}</td>
184 <td>${balanceCell}</td>
185 </tr>
186 `;
187 }).join('');
188
189 const printWin = window.open('', '_blank', 'width=900,height=700');
190 if (!printWin) { Toast.error('پاپ‌آپ را فعال کنید'); return; }
191
192 printWin.document.write(`
193 <!DOCTYPE html>
194 <html lang="fa" dir="rtl">
195 <head>
196 <meta charset="UTF-8" />
197 <title>صورت‌حساب ${contact.name}</title>
198 <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/misc/Farsi-Digits/Vazirmatn-FD-font-face.css" />
199 <style>
200 @page { size: A4 portrait; margin: 10mm; }
201 * { box-sizing: border-box; margin: 0; padding: 0; }
202 html, body { direction: rtl; text-align: right; font-family: 'Vazirmatn FD', Tahoma, sans-serif; background: #fff; color: #000; }
203 body { padding: 10px; }
204 h1 { font-size: 18px; color: #0d9488; text-align: center; margin-bottom: 6px; }
205 .info { text-align: center; font-size: 12px; color: #64748b; margin-bottom: 16px; }
206 .contact-card { border: 1.5px solid #0d9488; border-radius: 10px; padding: 12px 16px; margin-bottom: 16px; display: flex; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
207 .contact-card .name { font-size: 14px; font-weight: 800; }
208 .contact-card .details { font-size: 11.5px; color: #64748b; line-height: 1.8; }
209 .contact-card .balance-box { text-align: left; }
210 .contact-card .balance-label { font-size: 11px; color: #64748b; }
211 .contact-card .balance-val { font-size: 16px; font-weight: 800; margin-top: 4px; color: ${isDebtor ? '#dc2626' : isCreditor ? '#059669' : '#64748b'}; }
212 .summary { display: flex; gap: 10px; margin-bottom: 16px; }
213 .summary > div { flex: 1; border: 1px solid #cbd5e1; border-radius: 8px; padding: 10px; text-align: center; }
214 .summary .val { font-size: 16px; font-weight: 800; margin-top: 4px; }
215 .summary .lbl { font-size: 11px; color: #64748b; }
216 table { width: 100%; border-collapse: collapse; }
217 th, td { padding: 7px 9px; border: 1px solid #cbd5e1; text-align: right; font-size: 11px; }
218 th { background: #f1f5f9; font-weight: 700; }
219 .footer { margin-top: 20px; text-align: center; font-size: 10px; color: #94a3b8; padding-top: 10px; border-top: 1px dashed #cbd5e1; }
220 </style>
221 </head>
222 <body>
223 <h1>صورت‌حساب شخص</h1>
224 <div class="info">تاریخ چاپ: ${Jalali.today()}</div>
225
226 <div class="contact-card">
227 <div>
228 <div class="name">${this._esc(contact.name)}</div>
229 <div class="details">
230 ${contact.entityType === 'legal' ? 'شخص حقوقی' : 'شخص حقیقی'}
231 ${contact.nationalId ? ' | کد ملی/شناسه: ' + Formatters.toPersianDigits(contact.nationalId) : ''}
232 ${contact.mobile ? '<br>همراه: ' + Formatters.phone(contact.mobile) : ''}
233 ${contact.address ? '<br>نشانی: ' + this._esc(contact.address) : ''}
234 </div>
235 </div>
236 <div class="balance-box">
237 <div class="balance-label">مانده حساب نهایی</div>
238 <div class="balance-val">${balanceText}</div>
239 </div>
240 </div>
241
242 <div class="summary">
243 <div>
244 <div class="lbl">جمع بدهی</div>
245 <div class="val" style="color:#dc2626">${Formatters.money(ledger.totalDebit)}</div>
246 </div>
247 <div>
248 <div class="lbl">جمع پرداختی</div>
249 <div class="val" style="color:#059669">${Formatters.money(ledger.totalCredit)}</div>
250 </div>
251 <div>
252 <div class="lbl">تعداد تراکنش</div>
253 <div class="val" style="color:#0d9488">${Formatters.number(ledger.rows.length)}</div>
254 </div>
255 </div>
256
257 <table>
258 <thead>
259 <tr>
260 <th style="width:90px">تاریخ</th>
261 <th style="width:130px">نوع</th>
262 <th style="width:70px">مرجع</th>
263 <th style="text-align:left">بدهکار (ریال)</th>
264 <th style="text-align:left">بستانکار (ریال)</th>
265 <th style="text-align:left">مانده</th>
266 </tr>
267 </thead>
268 <tbody>${rowsHtml}</tbody>
269 </table>
270
271 <div class="footer">Factor-easyPro - سامانه مدیریت کسب‌وکار</div>
272 </body>
273 </html>
274 `);
275 printWin.document.close();
276 setTimeout(() => { printWin.focus(); printWin.print(); }, 500);
277 }
278
279 _esc(s) {
280 if (!s) return '';
281 return String(s).replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
282 }
283 }
284
285 export const ContactLedgerModal = new ContactLedgerModalImpl();
286 window.ContactLedgerModal = ContactLedgerModal;