فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(A02): add non-mutating opening balance preflight for historical companies
davoodmehraban89 committed
Oct 14, 2026 at 23:30 UTC
f2700f7f7b134cab23f8917bd86f79c88c47e358
1 file changed
+102
src/core/OpeningBalanceDryRun.mjs
new
+102
@@ -0,0 +1,102 @@
1
+/**
2
+ * Deterministic preflight for a historical-company opening balance import.
3
+ * IMPORTANT: No database access or writes, journal posting or activation here.
4
+ * The application must never infer opening balances from incomplete legacy
5
+ * invoices or silently change cash and inventory during import.
6
+ */
7
+const id = value => typeof value === 'string' && value.trim() !== '';
8
+const rial = (value, label, { signed = false } = {}) => {
9
+ if (!Number.isSafeInteger(value) || (!signed && value < 0)) {
10
+ throw new RangeError(label + ' باید ریال صحیح و معتبر باشد');
11
+ }
12
+ return value;
13
+};
14
+const plus = (a, b, label) => {
15
+ const result = a + b;
16
+ if (!Number.isSafeInteger(result)) throw new RangeError('سرریز ' + label);
17
+ return result;
18
+};
19
+function unique(rows, key, label) {
20
+ const seen = new Set();
21
+ for (const row of rows) {
22
+ const value = key(row);
23
+ if (!id(value) || seen.has(value)) throw Error('شناسه ناقص یا تکراری ' + label);
24
+ seen.add(value);
25
+ }
26
+}
27
+function rows(input, name) {
28
+ if (!Array.isArray(input)) throw TypeError('فهرست ' + name + ' نامعتبر است');
29
+ return input;
30
+}
31
+
32
+export function previewOpeningBalances({
33
+ companyId, openingDate, period,
34
+ lines = [], contacts = [], stock = [], cash = [],
35
+ priorPostedJournals = 0
36
+} = {}) {
37
+ if (!id(companyId) || !period || period.companyId !== companyId ||
38
+ period.closed || period.status === 'closed' ||
39
+ typeof openingDate !== 'string' ||
40
+ !/^1[34-5]\d{2}\/\d{2}\/\d{2}$/.test(openingDate) ||
41
+ openingDate < period.startDate || openingDate > period.endDate) {
42
+ throw Error('شرکت، دوره یا تاریخ افتتاحیه نامعتبر است');
43
+ }
44
+ for (const group of [lines, contacts, stock, cash]) rows(group, 'افتتاحیه');
45
+ unique(lines, r => r.accountId, 'حساب افتتاحیه');
46
+ unique(contacts, r => r.contactId, 'شخص');
47
+ unique(stock, r => JSON.stringify([r.warehouseId, r.productId]), 'موجودی انبار');
48
+ unique(cash, r => r.accountId, 'بانک یا صندوق');
49
+
50
+ const allowedRoles = new Set(['receivable', 'payable', 'inventory', 'cash', 'equity', 'other']);
51
+ const financial = { debit: 0, credit: 0 };
52
+ const roleNet = { receivable: 0, payable: 0, inventory: 0, cash: 0 };
53
+ for (const row of lines) {
54
+ if (!allowedRoles.has(row.role)) throw Error('نقش حساب افتتاحیه نامعتبر است');
55
+ const debit = rial(row.debit, 'بدهکار');
56
+ const credit = rial(row.credit, 'بستانکار');
57
+ if (debit && credit) throw Error('حساب همزمان بدهکار و بستانکار نمیشود');
58
+ financial.debit = plus(financial.debit, debit, 'جمع بدهکار');
59
+ financial.credit = plus(financial.credit, credit, 'جمع بستانکار');
60
+ if (row.role in roleNet) {
61
+ const normal = row.role === 'payable' ? credit - debit : debit - credit;
62
+ roleNet[row.role] = plus(roleNet[row.role], normal, 'مانده نقش');
63
+ }
64
+ }
65
+
66
+ const sources = { receivable: 0, payable: 0, inventory: 0, cash: 0 };
67
+ for (const row of contacts) {
68
+ sources.receivable = plus(sources.receivable, rial(row.receivable, 'طلب اشخاص'), 'طلب اشخاص');
69
+ sources.payable = plus(sources.payable, rial(row.payable, 'بدهی اشخاص'), 'بدهی اشخاص');
70
+ }
71
+ for (const row of stock) {
72
+ if (!id(row.warehouseId) || !id(row.productId) ||
73
+ !Number.isSafeInteger(row.quantityUnits) || row.quantityUnits < 0) {
74
+ throw Error('موجودی افتتاحیه کالا نامعتبر است');
75
+ }
76
+ sources.inventory = plus(sources.inventory, rial(row.value, 'ارزش موجودی'), 'ارزش موجودی');
77
+ }
78
+ for (const row of cash) {
79
+ sources.cash = plus(sources.cash, rial(row.balance, 'مانده صندوق', { signed: true }), 'مانده نقد');
80
+ }
81
+ if (!Number.isSafeInteger(priorPostedJournals) || priorPostedJournals < 0) {
82
+ throw Error('تعداد اسناد پیشین نامعتبر است');
83
+ }
84
+ const issues = [];
85
+ if (priorPostedJournals > 0) issues.push('EXISTING_POSTED_JOURNALS');
86
+ if (!lines.length) issues.push('MISSING_OPENING_JOURNAL_LINES');
87
+ if (financial.debit !== financial.credit) issues.push('UNBALANCED_OPENING_JOURNAL');
88
+ const differences = {};
89
+ for (const role of Object.keys(sources)) {
90
+ differences[role] = sources[role] - roleNet[role];
91
+ if (!Number.isSafeInteger(differences[role])) throw new RangeError('سرریز اختلاف افتتاحیه');
92
+ if (differences[role] !== 0) issues.push('OPENING_' + role.toUpperCase() + '_MISMATCH');
93
+ }
94
+ return Object.freeze({
95
+ companyId, openingDate, readiness: issues.length === 0 ? 'ready-for-review' : 'blocked',
96
+ issues: Object.freeze(issues), journal: Object.freeze(financial),
97
+ sources: Object.freeze(sources), differences: Object.freeze(differences),
98
+ sourceCounts: Object.freeze({ contacts: contacts.length, stock: stock.length,
99
+ cash: cash.length, lines: lines.length }),
100
+ writePerformed: false
101
+ });
102
+}