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

feat(A02): compare explicit Holoo source control totals without import or data disclosure

Add source-operator-declared record-count and exact integer-rial sum reconciliation for already-mapped CSV/TSV exports, after explicit rial/toman unit selection. Fail closed for source totals mismatch, missing data/mappings, malformed controls and aggregate overflow. Seven synthetic regression tests, bounded diagnostics with no raw financial/customer values and source table whitelist hardening. Explicitly keep canCommit=false, independentlyVerified=false: no real Holoo backup or independent accountant validation received. Exact SHA PR Quality #39000719017 (220 passing Node tests, Vite/audit, Windows preview installer) + installed Windows Native #39000718964 (14 financial scenarios, upgrade/reopen, recovery, RTL PDF, offline and 60k workload) passed. No public release or acceptance flips.

davoodmehraban89 committed Oct 17, 2026 at 07:33 UTC 4d62f0385e3347b88a25623c917e739936754a37
3 files changed +183 -3
docs/adr/ADR-002-holoo-migration-compatibility-fa.md
+6
@@ -49,6 +49,12 @@ previewHolooDelimitedExport({
49
50 تمام نتایج فقط شمارش رکورد، کد ایراد و شماره ردیف‌اند؛ **متن خام فایل، نام شخص، شماره حساب و شماره چک در خروجی قرار نمی‌گیرد**. هنوز تشخیص اصالت فایل هلو، خواندن `.bak`، تبدیل کامل فاکتورها و ثبت داده در دیتابیس مقصد پشتیبانی نمی‌شود. برای مانده افتتاحیه واقعی باید همه جدول‌های مربوط و تأیید حسابدار فراهم باشد.
51
52 +## کنترل تطبیق جمع‌ها با گزارش منبع — مرحله بدون نوشتن
53 +
54 +پیش از مهاجرت، اپراتور می‌تواند جمع‌های اعلامی در گزارش حسابداری **همان نسخه هلو** را در `declaredControlTotals` وارد کند؛ از جمله تعداد رکوردها، جمع مبالغ ریالی فاکتورها، وصول‌شده‌ها، مطالبات/بدهی اشخاص و چک‌ها. ماژول فقط با ارقام صحیح و محدودشده، تعداد ردیف‌های استخراج‌شده و جمع همان ستون نگاشت‌شده را مقایسه می‌کند؛ مبلغ‌ها با `moneyUnit` صریح به **ریال** تبدیل شده‌اند. اختلاف تعداد، مبلغ، ستون غایب، مجموعه داده غایب، مقدار نامعتبر و سرریز جمع، `blocked` می‌شوند. کد اختلاف‌ها **هیچ مبلغ یا نام شخصی نشان نمی‌دهد**.
55 +
56 +این ارقام `declared` ادعای منبع هستند و **به‌خودی‌خود قابل اعتماد نیستند**؛ `independentlyVerified:false` و `canCommit:false` حتی در حالت تمام تطبیق‌ها باقی می‌مانند. این ابزار فقط مرحله اولیه گزارش مالی با داده مصنوعی/جدولی است؛ نه ابزار تبدیل بکاپ اختصاصی و نه پذیرش حسابدار. برای داده واقعی هنوز نیاز به فایل مجاز و کنترل مستقل داریم.
57 +
58 ## مانع مرحله بعد
59 **نیاز به یک فایل واقعی متعلق به مالک** است (فایل اصلی بدون تغییر می‌ماند). ابتدا پسوند، امضای فایل، نسخه هلو و امکان خواندن امن/مجاز در محیط ایزوله بررسی می‌شود. سپس آداپتور همان فرمت بر اساس شواهد واقعی توسعه داده می‌شود. پیش از اتصال به CI عمومی، اطلاعات اشخاص، مالی، بانکی و رمزها حذف/ناشناس می‌شوند.
60
src/core/HolooDelimitedExportPreflight.mjs
+85 -3
@@ -95,14 +95,80 @@ export function parseStrictDelimitedRows(text, { delimiter = ',' } = {}) {
95 return { headers, rows: data };
96 }
97
98 +/**
99 + * Source-report control totals are *claims provided by the operator*,
100 + * NEVER external accountant signoff. This function cannot read Holoo data
101 + * directly and must never leak individual rows or financial amounts.
102 + *
103 + * All claims are denominated in RIAL after explicit source-unit conversion.
104 + * Supported claims: { [table]: { count, ...sumFields } }.
105 + */
106 +const SUM_FIELDS = Object.freeze({
107 + contacts: ['receivable', 'payable'],
108 + products: [],
109 + invoices: ['grandTotal', 'paidAmount'],
110 + invoiceLines: [],
111 + cheques: ['amount']
112 +});
113 +function compareDeclaredControlTotals(datasets, mappings, declared, maxIssues) {
114 + if (declared === null || declared === undefined) {
115 + return { checks: 0, issueCount: 0, issues: [] };
116 + }
117 + if (!validMap(declared)) reject('CONTROL_TOTALS_INVALID');
118 + let checks = 0, issueCount = 0;
119 + const issues = [];
120 + const add = (code, table) => {
121 + issueCount++;
122 + if (issues.length < maxIssues) issues.push(Object.freeze({ code, table, rowIndex: null }));
123 + };
124 + for (const [table, values] of Object.entries(declared)) {
125 + if (!Object.hasOwn(SUM_FIELDS, table) || !validMap(values) ||
126 + !Object.keys(values).length ||
127 + Object.keys(values).some(key => key !== 'count' && !SUM_FIELDS[table].includes(key))) {
128 + reject('CONTROL_TOTALS_SCHEMA_INVALID');
129 + }
130 + const rows = datasets[table];
131 + if (!rows) {
132 + add('CONTROL_SOURCE_DATASET_MISSING', table);
133 + continue;
134 + }
135 + for (const [key, expected] of Object.entries(values)) {
136 + if (!Number.isSafeInteger(expected) || expected < 0) reject('CONTROL_TOTAL_NOT_SAFE_RIAL');
137 + checks++;
138 + if (key === 'count') {
139 + if (rows.length !== expected) add('CONTROL_COUNT_MISMATCH', table);
140 + continue;
141 + }
142 + const sourceColumn = mappings[table]?.[key];
143 + if (typeof sourceColumn !== 'string' || !sourceColumn) {
144 + add('CONTROL_SOURCE_MAPPING_MISSING', table);
145 + continue;
146 + }
147 + let sum = 0n, invalid = false;
148 + for (const row of rows) {
149 + const value = row[sourceColumn];
150 + if (!Number.isSafeInteger(value) || value < 0) {
151 + invalid = true;
152 + break;
153 + }
154 + sum += BigInt(value);
155 + }
156 + if (invalid) add('CONTROL_SOURCE_AMOUNT_INVALID', table);
157 + else if (sum > BigInt(Number.MAX_SAFE_INTEGER)) add('CONTROL_SOURCE_SUM_OVERFLOW', table);
158 + else if (sum !== BigInt(expected)) add('CONTROL_AMOUNT_MISMATCH', table);
159 + }
160 + }
161 + return { checks, issueCount, issues };
162 +}
163 +
164 export function previewHolooDelimitedExport({
99 - sourceSystem, moneyUnit, tables, mappings
165 + sourceSystem, moneyUnit, tables, mappings, declaredControlTotals = null
166 } = {}, { maxIssues = 100 } = {}) {
167 if (sourceSystem !== 'holoo') reject('SOURCE_SYSTEM_NOT_HOLOO');
168 if (!['rial', 'toman'].includes(moneyUnit)) reject('MONEY_UNIT_REQUIRED');
169 if (!validMap(tables) || !validMap(mappings)) reject('SOURCE_MAPPING_REQUIRED');
104 - if (Object.keys(tables).some(name => !(name in COLUMNS)) ||
105 - Object.keys(mappings).some(name => !(name in COLUMNS))) {
170 + if (Object.keys(tables).some(name => !Object.hasOwn(COLUMNS, name)) ||
171 + Object.keys(mappings).some(name => !Object.hasOwn(COLUMNS, name))) {
172 reject('SOURCE_UNSUPPORTED_TABLE');
173 }
174 const datasets = {};
@@ -130,8 +196,24 @@ export function previewHolooDelimitedExport({
196 const report = previewHolooExtract({
197 sourceSystem: 'holoo', datasets, columns: mappings
198 }, { maxIssues });
199 + const controls = compareDeclaredControlTotals(
200 + datasets, mappings, declaredControlTotals, maxIssues
201 + );
202 + const totalIssues = report.issueCount + controls.issueCount;
203 + const mergedIssues = Object.freeze([
204 + ...report.issues,
205 + ...controls.issues.slice(0, Math.max(0, maxIssues - report.issues.length))
206 + ]);
207 return Object.freeze({
208 ...report,
209 + status: totalIssues ? 'blocked' : 'requires-source-and-accountant-review',
210 + issueCount: totalIssues,
211 + truncated: totalIssues > mergedIssues.length,
212 + issues: mergedIssues,
213 + declaredControlChecks: controls.checks,
214 + sourceControlClaimsPresent: declaredControlTotals !== null &&
215 + declaredControlTotals !== undefined,
216 + independentlyVerified: false,
217 inputType: 'explicit-mapped-delimited-export',
218 sourceCurrencyUnit: moneyUnit,
219 // This parser is not an authenticity check for Holoo CSV exports.
tests/a02-holoo-delimited-export.test.mjs
+92
@@ -108,3 +108,95 @@ test('A02: reject large cells and unsupported separator without emitting source
108 assert.throws(()=>parseStrictDelimitedRows('id,name\n1,'+'z'.repeat(270000)),/CSV_CELL_TOO_LARGE/);
109 assert.throws(()=>parseStrictDelimitedRows('id|name\n1|x',{delimiter:'|'}),/CSV_UNSUPPORTED_DELIMITER/);
110 });
111 +
112 +
113 +test('A02: declared Holoo report control counts and rial totals match explicit mapped data', () => {
114 + const sample = fixture();
115 + sample.declaredControlTotals = {
116 + contacts: { count: 1, receivable: 0, payable: 0 },
117 + products: { count: 1 },
118 + invoices: { count: 1, grandTotal: 300, paidAmount: 100 },
119 + invoiceLines: { count: 1 }, cheques: { count: 1, amount: 50 }
120 + };
121 + const before = structuredClone(sample);
122 + const result = previewHolooDelimitedExport(sample);
123 + assert.equal(result.issueCount, 0, JSON.stringify(result.issues));
124 + assert.equal(result.declaredControlChecks, 10);
125 + assert.equal(result.sourceControlClaimsPresent, true);
126 + assert.equal(result.independentlyVerified, false);
127 + assert.equal(result.canCommit, false);
128 + assert.deepEqual(sample, before);
129 +});
130 +
131 +test('A02: source-report amount and row-count differences fail without leaking actual financial numbers', () => {
132 + const sample = fixture();
133 + sample.declaredControlTotals = { invoices: { count: 2, grandTotal: 97531, paidAmount: 99999 } };
134 + const result = previewHolooDelimitedExport(sample);
135 + const codes = result.issues.map(x => x.code);
136 + for (const code of ['CONTROL_COUNT_MISMATCH','CONTROL_AMOUNT_MISMATCH'])
137 + assert.ok(codes.includes(code));
138 + for (const secret of ['97531','99999','مشتری'])
139 + assert.ok(!JSON.stringify(result).includes(secret));
140 + assert.equal(result.canCommit, false);
141 + assert.equal(result.status, 'blocked');
142 +});
143 +
144 +test('A02: explicit toman source amounts are reconciled only against correctly converted rial controls', () => {
145 + const sample = fixture();
146 + sample.moneyUnit = 'toman';
147 + sample.declaredControlTotals = { invoices: { grandTotal: 3000, paidAmount: 1000 } };
148 + assert.equal(previewHolooDelimitedExport(sample).issueCount, 0);
149 + sample.declaredControlTotals.invoices.grandTotal = 300;
150 + const report = previewHolooDelimitedExport(sample);
151 + assert.ok(report.issues.some(x => x.code === 'CONTROL_AMOUNT_MISMATCH'));
152 +});
153 +
154 +test('A02: controls never claim success when their table is missing or the source amount is invalid', () => {
155 + const sample = fixture();
156 + sample.declaredControlTotals = { cheques: { count: 1 } };
157 + delete sample.tables.cheques;
158 + const missing = previewHolooDelimitedExport(sample);
159 + assert.ok(missing.issues.some(x => x.code === 'CONTROL_SOURCE_DATASET_MISSING'));
160 + sample.tables.cheques = {text:'id,amount\nQ1,N/A\n'};
161 + sample.declaredControlTotals = { cheques: { amount: 50 } };
162 + const invalid = previewHolooDelimitedExport(sample);
163 + assert.ok(invalid.issues.some(x => x.code === 'CONTROL_SOURCE_AMOUNT_INVALID'));
164 +});
165 +
166 +test('A02: large per-row safe amounts detect aggregate overflow and never silently round', () => {
167 + const sample = fixture();
168 + sample.tables.invoices.text =
169 + 'id,contact,gross,paid\nI1,C1,9007199254740990,0\nI2,C1,9007199254740990,0\n';
170 + sample.declaredControlTotals = { invoices: { grandTotal: 9007199254740990 } };
171 + const result = previewHolooDelimitedExport(sample);
172 + assert.ok(result.issues.some(x => x.code === 'CONTROL_SOURCE_SUM_OVERFLOW'));
173 +});
174 +
175 +test('A02: reject unauthorized control schema, unsafe numbers and prototype-name tables', () => {
176 + const sample = fixture();
177 + sample.declaredControlTotals = { invoices: { amount: 10 } };
178 + assert.throws(() => previewHolooDelimitedExport(sample), /CONTROL_TOTALS_SCHEMA_INVALID/);
179 + sample.declaredControlTotals = { invoices: { grandTotal: Number.MAX_SAFE_INTEGER + 1 } };
180 + assert.throws(() => previewHolooDelimitedExport(sample), /CONTROL_TOTAL_NOT_SAFE_RIAL/);
181 + sample.declaredControlTotals = { constructor: { count: 1 } };
182 + assert.throws(() => previewHolooDelimitedExport(sample), /CONTROL_TOTALS_SCHEMA_INVALID/);
183 + delete sample.declaredControlTotals;
184 + sample.tables.constructor = { text: 'id\nx\n' };
185 + assert.throws(() => previewHolooDelimitedExport(sample), /SOURCE_UNSUPPORTED_TABLE/);
186 +});
187 +
188 +test('A02: bounded combined diagnostics preserve total count without leaking raw data', () => {
189 + const sample = fixture();
190 + sample.tables.cheques.text = 'id,amount\nX,-45\n';
191 + sample.declaredControlTotals = {
192 + cheques: { count: 2, amount: 123456 },
193 + invoices: { count: 0, grandTotal: 999999 }
194 + };
195 + const result = previewHolooDelimitedExport(sample, { maxIssues: 2 });
196 + assert.equal(result.issues.length, 2);
197 + assert.equal(result.truncated, true);
198 + assert.ok(result.issueCount > 2);
199 + assert.equal(result.independentlyVerified, false);
200 + assert.ok(!JSON.stringify(result).includes('123456'));
201 + assert.ok(!JSON.stringify(result).includes('999999'));
202 +});