فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(A05): add read-only company-scoped settlement consistency checker
davoodmehraban89 committed
Oct 14, 2026 at 22:56 UTC
1503fd4b5d93b3bbebe42035e19058f72b1f4c19
1 file changed
+87
src/core/FinancialDiagnosticsService.js
new
+87
@@ -0,0 +1,87 @@
1
+import { StorageService } from './StorageService.js';
2
+import { DataScope } from './DataScope.js';
3
+
4
+/**
5
+ * Read-only, company-scoped financial consistency checks.
6
+ * This service never repairs numbers, posts journals, updates invoice status,
7
+ * or reads credentials. Investigate every anomaly before any corrective posting.
8
+ */
9
+export async function inspectSettlementConsistency({ maxIssues = 100 } = {}) {
10
+ if (!Number.isSafeInteger(maxIssues) || maxIssues < 1 || maxIssues > 1000) {
11
+ throw new RangeError('حداکثر تعداد مغایرتها باید بین ۱ تا ۱۰۰۰ باشد');
12
+ }
13
+ const companyId = await DataScope.companyId();
14
+ return StorageService.transaction(['invoices', 'settlements', 'treasury'], async tx => {
15
+ const invoices = (await tx.getAll('invoices'))
16
+ .filter(row => row.companyId === companyId && row.financePosted && !row.isPreInvoice);
17
+ const settlements = (await tx.getAll('settlements'))
18
+ .filter(row => row.companyId === companyId);
19
+ const transactions = new Set((await tx.getAll('treasury'))
20
+ .filter(row => row.companyId === companyId && row.recordType === 'transaction')
21
+ .map(row => row.id));
22
+
23
+ const invoiceById = new Map(invoices.map(row => [row.id, row]));
24
+ const allocated = new Map();
25
+ const issues = [];
26
+ let issueCount = 0;
27
+ const add = (code, documentId, details = {}) => {
28
+ issueCount++;
29
+ if (issues.length < maxIssues) issues.push({ code, documentId, ...details });
30
+ };
31
+ const validAmount = n => Number.isSafeInteger(n) && n >= 0;
32
+
33
+ for (const s of settlements) {
34
+ if (!invoiceById.has(s.invoiceId)) {
35
+ add('ORPHAN_SETTLEMENT', s.id, { invoiceId: s.invoiceId });
36
+ continue;
37
+ }
38
+ if (!Number.isSafeInteger(s.amount) || s.amount <= 0) {
39
+ add('INVALID_SETTLEMENT_AMOUNT', s.id, { invoiceId: s.invoiceId });
40
+ continue;
41
+ }
42
+ if (!transactions.has(s.transactionId)) {
43
+ add('MISSING_TREASURY_TRANSACTION', s.id, { invoiceId: s.invoiceId });
44
+ }
45
+ const cumulative = (allocated.get(s.invoiceId) || 0) + s.amount;
46
+ if (!Number.isSafeInteger(cumulative)) {
47
+ add('SETTLEMENT_AMOUNT_OVERFLOW', s.id, { invoiceId: s.invoiceId });
48
+ continue;
49
+ }
50
+ allocated.set(s.invoiceId, cumulative);
51
+ }
52
+
53
+ for (const invoice of invoices) {
54
+ const gross = invoice.grandTotal;
55
+ const paid = invoice.paidAmount;
56
+ if (!validAmount(gross) || !validAmount(paid)) {
57
+ add('INVALID_INVOICE_AMOUNT', invoice.id);
58
+ continue;
59
+ }
60
+ const settled = allocated.get(invoice.id) || 0;
61
+ if (settled !== paid) {
62
+ add('SETTLEMENT_TOTAL_MISMATCH', invoice.id, { expected: settled, actual: paid });
63
+ }
64
+ if (paid > gross) add('INVOICE_OVERPAID', invoice.id, { grandTotal: gross, paid });
65
+ const remaining = gross - paid;
66
+ if (invoice.remaining !== remaining) {
67
+ add('INVOICE_REMAINING_MISMATCH', invoice.id, { expected: remaining, actual: invoice.remaining });
68
+ }
69
+ if (['paid', 'partial', 'unpaid'].includes(invoice.status)) {
70
+ const expected = paid === 0 ? 'unpaid' : paid === gross ? 'paid' : 'partial';
71
+ if (invoice.status !== expected) {
72
+ add('INVOICE_STATUS_MISMATCH', invoice.id, { expected, actual: invoice.status });
73
+ }
74
+ }
75
+ }
76
+
77
+ return Object.freeze({
78
+ companyId,
79
+ checkedInvoices: invoices.length,
80
+ checkedSettlements: settlements.length,
81
+ issueCount,
82
+ healthy: issueCount === 0,
83
+ truncated: issueCount > issues.length,
84
+ issues
85
+ });
86
+ }, 'readonly');
87
+}