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

feat(A02): cross-check mapped Holoo exports against explicit declared source totals

davoodmehraban89 committed Oct 17, 2026 at 07:26 UTC 8e9cbc7313417b2ca7de40fdb728b151bff9c62f
1 file changed +85 -3
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.