@diy-accounting-uk/diya-gl 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/bin/diya-gl-mcp.js +10 -0
- package/bin/diya-gl-read-workbook.js +11 -0
- package/bin/diya-gl-recalc.js +10 -0
- package/bin/diya-gl-write-workbook.js +12 -0
- package/bin/diya-gl.js +24 -0
- package/bin/run-dist-bin.js +23 -0
- package/dist/app/bin/build-diya-gl-spec.js +680 -0
- package/dist/app/bin/build-packages.js +149 -0
- package/dist/app/bin/build-reconciliation-pages.js +1052 -0
- package/dist/app/bin/build-sitemaps.js +51 -0
- package/dist/app/bin/compliance-report.js +125 -0
- package/dist/app/bin/cross-package-reconciliation.js +92 -0
- package/dist/app/bin/diya-gl-mcp.js +27 -0
- package/dist/app/bin/export.js +318 -0
- package/dist/app/bin/extract-scenarios.js +1348 -0
- package/dist/app/bin/generate.js +353 -0
- package/dist/app/bin/judge-reconciliation.js +649 -0
- package/dist/app/bin/reconcile.js +247 -0
- package/dist/app/bin/report.js +291 -0
- package/dist/app/bin/verify-roundtrip.js +941 -0
- package/dist/app/bin/verify-stability.js +429 -0
- package/dist/app/bin/write-workbook.js +106 -0
- package/dist/app/data/filing/ct-computation-v1.1.toml +1033 -0
- package/dist/app/data/filing/ct600-v3.toml +2329 -0
- package/dist/app/data/filing/frs105-formats.toml +244 -0
- package/dist/app/data/fixture-master-gaps.json +247 -0
- package/dist/app/data/hmrc/SOURCE.md +14 -0
- package/dist/app/data/hmrc/form-layouts/bst.json +116 -0
- package/dist/app/data/hmrc/form-layouts/ltd.json +452 -0
- package/dist/app/data/hmrc/form-layouts/se.json +485 -0
- package/dist/app/data/hmrc/form-layouts/taxi.json +117 -0
- package/dist/app/data/hmrc/sa103-mtd-mapping.json +1078 -0
- package/dist/app/data/hmrc/sa103f_mapping_v3.csv +111 -0
- package/dist/app/data/judge-rubric.md +52 -0
- package/dist/app/data/ltd-2020.toml +49 -0
- package/dist/app/data/ltd-2021.toml +49 -0
- package/dist/app/data/ltd-2022.toml +49 -0
- package/dist/app/data/ltd-2023.toml +49 -0
- package/dist/app/data/ltd-2024.toml +49 -0
- package/dist/app/data/ltd-2025.toml +49 -0
- package/dist/app/data/ltd-2026.toml +53 -0
- package/dist/app/data/ltd-2027.toml +51 -0
- package/dist/app/data/render-unrepresentable/bst.json +24 -0
- package/dist/app/data/render-unrepresentable/ltd.json +863 -0
- package/dist/app/data/render-unrepresentable/se.json +455 -0
- package/dist/app/data/render-unrepresentable/taxi.json +16 -0
- package/dist/app/data/roundtrip-budget.json +38 -0
- package/dist/app/data/roundtrip-matrix-budget.json +26 -0
- package/dist/app/data/roundtrip-unrepresentable.json +401 -0
- package/dist/app/data/se-2020-2021.toml +52 -0
- package/dist/app/data/se-2021-2022.toml +52 -0
- package/dist/app/data/se-2022-2023.toml +55 -0
- package/dist/app/data/se-2023-2024.toml +54 -0
- package/dist/app/data/se-2024-2025.toml +51 -0
- package/dist/app/data/se-2025-2026.toml +51 -0
- package/dist/app/data/se-2026-2027.toml +51 -0
- package/dist/app/data/volatile-cells.json +16 -0
- package/dist/app/lib/anchors/bst.js +79 -0
- package/dist/app/lib/anchors/ltd.js +626 -0
- package/dist/app/lib/anchors/run.js +100 -0
- package/dist/app/lib/anchors/se.js +456 -0
- package/dist/app/lib/anchors/taxi.js +164 -0
- package/dist/app/lib/app-resources.js +80 -0
- package/dist/app/lib/book-checks/ltd.js +408 -0
- package/dist/app/lib/book-checks/se.js +687 -0
- package/dist/app/lib/book-checks/taxi.js +215 -0
- package/dist/app/lib/book-checks.js +872 -0
- package/dist/app/lib/books-engine.js +197 -0
- package/dist/app/lib/books-interchange.js +533 -0
- package/dist/app/lib/calculators/bst.js +381 -0
- package/dist/app/lib/calculators/ltd.js +1938 -0
- package/dist/app/lib/calculators/se.js +1239 -0
- package/dist/app/lib/calculators/shared.js +131 -0
- package/dist/app/lib/calculators/taxi.js +461 -0
- package/dist/app/lib/canonical-report-value.js +77 -0
- package/dist/app/lib/compliance-report.js +341 -0
- package/dist/app/lib/diya-gl-calculator.js +52 -0
- package/dist/app/lib/diya-gl-canonical.js +355 -0
- package/dist/app/lib/diya-gl-edits-ltd.js +144 -0
- package/dist/app/lib/diya-gl-edits.js +361 -0
- package/dist/app/lib/diya-gl-loader.js +774 -0
- package/dist/app/lib/diya-gl-schema.js +212 -0
- package/dist/app/lib/generator.js +1912 -0
- package/dist/app/lib/guide.js +33 -0
- package/dist/app/lib/headlines.js +293 -0
- package/dist/app/lib/link-caches.js +421 -0
- package/dist/app/lib/ltd-layout.js +139 -0
- package/dist/app/lib/mcp/diya-gl-tools.js +330 -0
- package/dist/app/lib/mcp/jsonrpc-stdio.js +66 -0
- package/dist/app/lib/mcp/server.js +65 -0
- package/dist/app/lib/overtype-sidecar.js +180 -0
- package/dist/app/lib/package-builder.js +202 -0
- package/dist/app/lib/payslips-date-chain.js +481 -0
- package/dist/app/lib/payslips-layout.js +235 -0
- package/dist/app/lib/period-shift.js +111 -0
- package/dist/app/lib/product-workbook.js +317 -0
- package/dist/app/lib/products.js +27 -0
- package/dist/app/lib/provenance-data.js +36 -0
- package/dist/app/lib/provenance.js +92 -0
- package/dist/app/lib/report-generator.js +374 -0
- package/dist/app/lib/report-indicators.js +468 -0
- package/dist/app/lib/report-serializer.js +392 -0
- package/dist/app/lib/scenario-extractor.js +1403 -0
- package/dist/app/lib/scenario-loader.js +84 -0
- package/dist/app/lib/sitemap-builder.js +59 -0
- package/dist/app/lib/spreadsheet-runner.js +566 -0
- package/dist/app/lib/tax/capital-allowances.js +77 -0
- package/dist/app/lib/tax/corporation-tax.js +144 -0
- package/dist/app/lib/tax/income-tax.js +122 -0
- package/dist/app/lib/tax/mileage.js +69 -0
- package/dist/app/lib/tax/national-insurance.js +31 -0
- package/dist/app/lib/tax/vat.js +188 -0
- package/dist/app/lib/tax-year.js +31 -0
- package/dist/app/lib/template-formula-map.js +253 -0
- package/dist/app/lib/workbook-set.js +153 -0
- package/dist/app/lib/xlsx-exporter.js +2815 -0
- package/dist/app/lib/xlsx-parts.js +111 -0
- package/dist/app/lib/xlsx-reader.js +84 -0
- package/dist/app/products/bst.js +727 -0
- package/dist/app/products/ltd.js +4270 -0
- package/dist/app/products/se.js +3403 -0
- package/dist/app/products/taxi.js +912 -0
- package/dist/app/templates/bst/bst-excel.xlsx +0 -0
- package/dist/app/templates/bst/meta.toml +27 -0
- package/dist/app/templates/ltd/Cashaccount.xlsx +0 -0
- package/dist/app/templates/ltd/Companysecretary.xlsx +0 -0
- package/dist/app/templates/ltd/Creditcardaccount.xlsx +0 -0
- package/dist/app/templates/ltd/Currentaccount.xlsx +0 -0
- package/dist/app/templates/ltd/Dividend Voucher.docx +0 -0
- package/dist/app/templates/ltd/Financialaccounts.xlsx +0 -0
- package/dist/app/templates/ltd/Fixedassets.xlsx +0 -0
- package/dist/app/templates/ltd/Payslips.xlsx +0 -0
- package/dist/app/templates/ltd/Purchases.xlsx +0 -0
- package/dist/app/templates/ltd/Sales.xlsx +0 -0
- package/dist/app/templates/ltd/Salesinvoice.xlsx +0 -0
- package/dist/app/templates/ltd/Savingaccount.xlsx +0 -0
- package/dist/app/templates/ltd/Vatreturns.xlsx +0 -0
- package/dist/app/templates/ltd/expensesform.xlsx +0 -0
- package/dist/app/templates/ltd/meta.toml +54 -0
- package/dist/app/templates/meta.toml +12 -0
- package/dist/app/templates/se/Bank.xlsx +0 -0
- package/dist/app/templates/se/Cash.xlsx +0 -0
- package/dist/app/templates/se/Financialaccounts.xlsx +0 -0
- package/dist/app/templates/se/Fixedassets.xlsx +0 -0
- package/dist/app/templates/se/Payslips.xlsx +0 -0
- package/dist/app/templates/se/Purchases.xlsx +0 -0
- package/dist/app/templates/se/Sales.xlsx +0 -0
- package/dist/app/templates/se/Salesinvoice.xlsx +0 -0
- package/dist/app/templates/se/Vat.xlsx +0 -0
- package/dist/app/templates/se/meta.toml +43 -0
- package/dist/app/templates/taxi/meta.toml +43 -0
- package/dist/app/templates/taxi/taxi-excel.xlsx +0 -0
- package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-book-v2.schema.json +713 -0
- package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-docs.md +930 -0
- package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-lines-v2.schema.json +220 -0
- package/package.json +37 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
// Copyright (C) 2026 DIY Accounting Ltd
|
|
4
|
+
//
|
|
5
|
+
// reconcile.js — Run test scenarios against generated packages, compare
|
|
6
|
+
// computed results to expected values, generate compliance reports.
|
|
7
|
+
// Dispatches to product modules in app/products/ for cell writes, reads,
|
|
8
|
+
// and compliance checks.
|
|
9
|
+
//
|
|
10
|
+
// Usage:
|
|
11
|
+
// node app/bin/reconcile.js # all scenarios, all packages
|
|
12
|
+
// node app/bin/reconcile.js --package bst # BST only
|
|
13
|
+
// node app/bin/reconcile.js --years se-2025-2026 # specific year
|
|
14
|
+
|
|
15
|
+
import { parse as parseTOML } from "smol-toml";
|
|
16
|
+
import { readFileSync, writeFileSync, mkdirSync, readdirSync, existsSync } from "fs";
|
|
17
|
+
import { resolve, dirname, basename } from "path";
|
|
18
|
+
import { fileURLToPath } from "url";
|
|
19
|
+
import { runSpreadsheet, runMultiFileSpreadsheet } from "../lib/spreadsheet-runner.js";
|
|
20
|
+
import { loadScenario } from "../lib/scenario-loader.js";
|
|
21
|
+
import { generateReport } from "../lib/report-generator.js";
|
|
22
|
+
import { calculateExpectedTax } from "../lib/tax/income-tax.js";
|
|
23
|
+
import * as bst from "../products/bst.js";
|
|
24
|
+
import * as taxi from "../products/taxi.js";
|
|
25
|
+
import * as se from "../products/se.js";
|
|
26
|
+
import * as ltd from "../products/ltd.js";
|
|
27
|
+
|
|
28
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
const APP_DIR = resolve(__dirname, "..");
|
|
30
|
+
const ROOT = resolve(APP_DIR, "..");
|
|
31
|
+
const PACKAGES_DIR = resolve(ROOT, "packages");
|
|
32
|
+
const FIXTURES_DIR = resolve(APP_DIR, "test", "fixtures");
|
|
33
|
+
const REPORTS_DIR = resolve(ROOT, "reports");
|
|
34
|
+
|
|
35
|
+
// Each product module owns its own prefix, cell writes, reads, and compliance checks.
|
|
36
|
+
const PRODUCTS = {
|
|
37
|
+
bst,
|
|
38
|
+
taxi,
|
|
39
|
+
se,
|
|
40
|
+
ltd: ltd,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function findXlsx(packageDir) {
|
|
44
|
+
const files = readdirSync(packageDir);
|
|
45
|
+
return files.find((f) => f.endsWith(".xlsx"));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The package directory name carries this run's own year-end date
|
|
49
|
+
// (YYYY-MM-DD). checkCompliance needs it to anchor date checks against the
|
|
50
|
+
// run's actual configuration rather than a value the sheet derives itself.
|
|
51
|
+
export function packageYearEnd(pkgDir) {
|
|
52
|
+
const match = pkgDir.match(/(\d{4})-(\d{2})-(\d{2})/);
|
|
53
|
+
return match ? `${match[1]}-${match[2]}-${match[3]}` : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function main() {
|
|
57
|
+
console.log("=== reconcile.js ===");
|
|
58
|
+
|
|
59
|
+
const args = process.argv.slice(2);
|
|
60
|
+
|
|
61
|
+
// Find scenario fixtures
|
|
62
|
+
const fixtures = readdirSync(FIXTURES_DIR)
|
|
63
|
+
.filter((f) => f.endsWith(".toml"))
|
|
64
|
+
.map((f) => resolve(FIXTURES_DIR, f));
|
|
65
|
+
|
|
66
|
+
console.log(
|
|
67
|
+
"Scenarios:",
|
|
68
|
+
fixtures.map((f) => basename(f)),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Find generated packages
|
|
72
|
+
if (!existsSync(PACKAGES_DIR)) {
|
|
73
|
+
console.error("No packages/ directory. Run 'npm run build' first.");
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Package filter
|
|
78
|
+
const pkgIdx = args.indexOf("--package");
|
|
79
|
+
const packageFilter = pkgIdx !== -1 && args[pkgIdx + 1] ? args[pkgIdx + 1] : "all";
|
|
80
|
+
|
|
81
|
+
// Discover packages for each product
|
|
82
|
+
const allPackageDirs = readdirSync(PACKAGES_DIR).sort();
|
|
83
|
+
let packageDirs;
|
|
84
|
+
if (packageFilter === "all") {
|
|
85
|
+
packageDirs = allPackageDirs.filter((d) => Object.values(PRODUCTS).some((mod) => d.startsWith(mod.PRODUCT.prefix)));
|
|
86
|
+
} else {
|
|
87
|
+
const mod = PRODUCTS[packageFilter];
|
|
88
|
+
if (!mod) {
|
|
89
|
+
console.error(`Unknown package: ${packageFilter}. Available: ${Object.keys(PRODUCTS).join(", ")}`);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
packageDirs = allPackageDirs.filter((d) => d.startsWith(mod.PRODUCT.prefix));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Filter by --years (tax data file names) if specified
|
|
96
|
+
const yearsIdx = args.indexOf("--years");
|
|
97
|
+
if (yearsIdx !== -1) {
|
|
98
|
+
const years = [];
|
|
99
|
+
for (let i = yearsIdx + 1; i < args.length; i++) {
|
|
100
|
+
if (args[i].startsWith("--")) break;
|
|
101
|
+
years.push(args[i]);
|
|
102
|
+
}
|
|
103
|
+
packageDirs = packageDirs.filter((d) =>
|
|
104
|
+
years.some((y) => {
|
|
105
|
+
if (y.startsWith("ltd-")) {
|
|
106
|
+
const fyStart = parseInt(y.replace("ltd-", ""), 10);
|
|
107
|
+
return d.includes(String(fyStart + 1));
|
|
108
|
+
}
|
|
109
|
+
const [, endYear] = y.replace("se-", "").split("-");
|
|
110
|
+
return d.includes(endYear);
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Filter by --year-end (specific year-end dates like 2026-03-31)
|
|
116
|
+
const yeIdx = args.indexOf("--year-end");
|
|
117
|
+
if (yeIdx !== -1) {
|
|
118
|
+
const yearEnds = [];
|
|
119
|
+
for (let i = yeIdx + 1; i < args.length; i++) {
|
|
120
|
+
if (args[i].startsWith("--")) break;
|
|
121
|
+
yearEnds.push(args[i]);
|
|
122
|
+
}
|
|
123
|
+
packageDirs = packageDirs.filter((d) => yearEnds.some((ye) => d.includes(ye)));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Filter by --scenario (e.g. --scenario basic, --scenario extended, --scenario full)
|
|
127
|
+
const scIdx = args.indexOf("--scenario");
|
|
128
|
+
const scenarioFilter = scIdx !== -1 && args[scIdx + 1] ? args[scIdx + 1] : null;
|
|
129
|
+
|
|
130
|
+
console.log("Packages:", packageDirs.length);
|
|
131
|
+
mkdirSync(REPORTS_DIR, { recursive: true });
|
|
132
|
+
|
|
133
|
+
let totalCompliant = 0;
|
|
134
|
+
let totalNonCompliant = 0;
|
|
135
|
+
|
|
136
|
+
for (const fixture of fixtures) {
|
|
137
|
+
const scenario = loadScenario(fixture);
|
|
138
|
+
const scenarioName = basename(fixture, ".toml");
|
|
139
|
+
const scenarioProduct = scenario.metadata?.product || "bst";
|
|
140
|
+
const productMod = PRODUCTS[scenarioProduct];
|
|
141
|
+
if (!productMod) continue;
|
|
142
|
+
if (scenarioFilter && !scenarioName.includes(scenarioFilter)) continue;
|
|
143
|
+
|
|
144
|
+
console.log(`\nScenario: ${scenarioName} (${scenario.metadata.description}) [${scenarioProduct}]`);
|
|
145
|
+
|
|
146
|
+
// Only run scenario against matching product packages
|
|
147
|
+
const matchingDirs = packageDirs.filter((d) => d.startsWith(productMod.PRODUCT.prefix));
|
|
148
|
+
|
|
149
|
+
for (const pkgDir of matchingDirs) {
|
|
150
|
+
console.log(` Testing: ${pkgDir}...`);
|
|
151
|
+
|
|
152
|
+
// Extract the year-end date from the package directory name
|
|
153
|
+
const yearEndMatch = pkgDir.match(/(\d{4})-(\d{2})-(\d{2})/);
|
|
154
|
+
const endYear = yearEndMatch ? parseInt(yearEndMatch[1], 10) : null;
|
|
155
|
+
const endMonth = yearEndMatch ? parseInt(yearEndMatch[2], 10) : null;
|
|
156
|
+
const startYear = endYear ? endYear - 1 : null;
|
|
157
|
+
|
|
158
|
+
// Product module owns cell writes and reads
|
|
159
|
+
const writes = productMod.cellWrites(scenario, startYear, endMonth);
|
|
160
|
+
const reads = productMod.standardReads();
|
|
161
|
+
|
|
162
|
+
let results;
|
|
163
|
+
const pkgSlug = pkgDir.replace(/[^a-zA-Z0-9]/g, "_");
|
|
164
|
+
const populatedDir = resolve(REPORTS_DIR, "populated");
|
|
165
|
+
|
|
166
|
+
if (productMod.MULTI_FILE) {
|
|
167
|
+
// Multi-file product (SE): load all xlsx files, use cross-file runner
|
|
168
|
+
const pkgPath = resolve(PACKAGES_DIR, pkgDir);
|
|
169
|
+
const xlsxFiles = readdirSync(pkgPath).filter((f) => f.endsWith(".xlsx"));
|
|
170
|
+
if (xlsxFiles.length === 0) {
|
|
171
|
+
console.log(` Skip ${pkgDir}: no xlsx files found`);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const fileBuffers = {};
|
|
176
|
+
for (const f of xlsxFiles) {
|
|
177
|
+
fileBuffers[f] = readFileSync(resolve(pkgPath, f));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const saveDir = resolve(populatedDir, `${pkgSlug}_${scenarioName}`);
|
|
181
|
+
results = await runMultiFileSpreadsheet(fileBuffers, writes, reads, "Financialaccounts.xlsx", {
|
|
182
|
+
...(productMod.multiFileOptions ? productMod.multiFileOptions(endMonth) : {}),
|
|
183
|
+
saveRecalculatedTo: saveDir,
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
// Single-file product (BST, Taxi)
|
|
187
|
+
const xlsxFile = findXlsx(resolve(PACKAGES_DIR, pkgDir));
|
|
188
|
+
if (!xlsxFile) {
|
|
189
|
+
console.log(` Skip ${pkgDir}: no xlsx found`);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const xlsxBuffer = readFileSync(resolve(PACKAGES_DIR, pkgDir, xlsxFile));
|
|
194
|
+
const savePath = resolve(populatedDir, `${pkgSlug}_${scenarioName}.xlsx`);
|
|
195
|
+
results = await runSpreadsheet(xlsxBuffer, writes, reads, {
|
|
196
|
+
saveRecalculatedTo: savePath,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Find the tax-data TOML for this package's year
|
|
201
|
+
let taxData = null;
|
|
202
|
+
if (startYear) {
|
|
203
|
+
const regime = productMod.PRODUCT.taxRegime;
|
|
204
|
+
// A Jan-Mar year-end belongs to the financial year that started the
|
|
205
|
+
// previous April, which is the TOML the generator built it from.
|
|
206
|
+
const ltdFinancialYear = endMonth <= 3 ? endYear - 1 : endYear;
|
|
207
|
+
const taxDataName = regime === "ltd" ? `ltd-${ltdFinancialYear}.toml` : `se-${startYear}-${endYear}.toml`;
|
|
208
|
+
const taxDataFile = resolve(APP_DIR, "data", taxDataName);
|
|
209
|
+
if (existsSync(taxDataFile)) {
|
|
210
|
+
taxData = parseTOML(readFileSync(taxDataFile, "utf8"));
|
|
211
|
+
console.log(` Tax data: ${taxDataName}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Product module owns compliance checks. Fixture anchors (opening_debtors,
|
|
216
|
+
// closing_creditors, ...) are top-level scenario tables, not [expected] keys,
|
|
217
|
+
// so checks that anchor against fixtures need the whole scenario merged in.
|
|
218
|
+
const mergedScenario = { ...scenario, ...scenario.expected };
|
|
219
|
+
const checks = productMod.checkCompliance({ ...results }, mergedScenario, taxData, calculateExpectedTax, packageYearEnd(pkgDir));
|
|
220
|
+
const { content, compliant } = generateReport(pkgDir, scenarioName, results, checks, productMod, mergedScenario);
|
|
221
|
+
|
|
222
|
+
// Report naming: <product>_<scenario>.md
|
|
223
|
+
const reportFile = `${pkgSlug}_${scenarioName}.md`;
|
|
224
|
+
writeFileSync(resolve(REPORTS_DIR, reportFile), content);
|
|
225
|
+
console.log(` Report: reports/${reportFile}`);
|
|
226
|
+
console.log(
|
|
227
|
+
` Status: ${compliant ? "RECONCILES" : "ANOMALYDETECTED"} (${checks.filter((c) => c.pass).length}/${checks.length} checks passed)`,
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
if (compliant) totalCompliant++;
|
|
231
|
+
else totalNonCompliant++;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
console.log(`\n=== Summary: ${totalCompliant} reconciled, ${totalNonCompliant} anomalies ===`);
|
|
236
|
+
|
|
237
|
+
if (totalNonCompliant > 0) {
|
|
238
|
+
process.exit(1);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
243
|
+
main().catch((err) => {
|
|
244
|
+
console.error(err);
|
|
245
|
+
process.exit(1);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
// Copyright (C) 2026 DIY Accounting Ltd
|
|
4
|
+
//
|
|
5
|
+
// report.js — Extract financial reports from a populated Excel package.
|
|
6
|
+
//
|
|
7
|
+
// Usage:
|
|
8
|
+
// node app/bin/report.js --package bst --source-dir examples/bst-latest --output-dir /tmp/reports
|
|
9
|
+
// node app/bin/report.js --package bst --source-dir examples/bst-latest --output-dir /tmp/reports --mode recalculate
|
|
10
|
+
// node app/bin/report.js --package bst --data examples/precision-code-ltd/bst --output-dir /tmp/reports
|
|
11
|
+
//
|
|
12
|
+
// Modes:
|
|
13
|
+
// --mode saved (default) Read xlsx cell values as-is from XML. No LibreOffice needed.
|
|
14
|
+
// --mode recalculate Run xls roundtrip first, then read. Requires LibreOffice.
|
|
15
|
+
// --data <dir> Compute reports from diya-gl data via JS engine. No Excel needed.
|
|
16
|
+
//
|
|
17
|
+
// Either mode writes report.json beside the markdown: R, the canonical
|
|
18
|
+
// document verify-roundtrip.js scores. --data also writes its own input to
|
|
19
|
+
// data/ in canonical form, so the output directory is a whole (data, report)
|
|
20
|
+
// tuple. Passing --data alongside --source-dir gives the Excel run the same
|
|
21
|
+
// scenario the JS run has, which is what lets it publish compliance verdicts
|
|
22
|
+
// the two sides can be compared on.
|
|
23
|
+
|
|
24
|
+
import { parse as parseTOML } from "smol-toml";
|
|
25
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
|
|
26
|
+
import { resolve, dirname, basename } from "path";
|
|
27
|
+
import { fileURLToPath } from "url";
|
|
28
|
+
import { readXlsxCellValues, readMultiFileXlsxCellValues, readMultiFileAdditionalXlsxCellValues, findXlsx } from "../lib/xlsx-reader.js";
|
|
29
|
+
import { runSpreadsheet, runMultiFileSpreadsheet } from "../lib/spreadsheet-runner.js";
|
|
30
|
+
import { generateSectionReports } from "../lib/report-generator.js";
|
|
31
|
+
import { buildReportDocument, serializeReportDocument } from "../lib/report-serializer.js";
|
|
32
|
+
import { loadDiyaGlData, extractTaxDataFromBook, diyaGlToScenario } from "../lib/diya-gl-loader.js";
|
|
33
|
+
import { canonicalBookToml, canonicalLinesJsonl } from "../lib/diya-gl-canonical.js";
|
|
34
|
+
import { calculateFromDiyaGl } from "../lib/diya-gl-calculator.js";
|
|
35
|
+
import { calculateExpectedTax } from "../lib/tax/income-tax.js";
|
|
36
|
+
import * as bst from "../products/bst.js";
|
|
37
|
+
import * as taxi from "../products/taxi.js";
|
|
38
|
+
import * as se from "../products/se.js";
|
|
39
|
+
import * as ltd from "../products/ltd.js";
|
|
40
|
+
|
|
41
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
42
|
+
|
|
43
|
+
const PRODUCTS = { bst, taxi, se, ltd };
|
|
44
|
+
|
|
45
|
+
// The package directory name carries this run's own year-end date
|
|
46
|
+
// (YYYY-MM-DD), the same convention reconcile.js reads. --year-end overrides
|
|
47
|
+
// it for a --source-dir whose own name does not carry a date.
|
|
48
|
+
function packageYearEnd(dirName) {
|
|
49
|
+
const match = dirName.match(/(\d{4})-(\d{2})-(\d{2})/);
|
|
50
|
+
return match ? `${match[1]}-${match[2]}-${match[3]}` : null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseArgs(argv) {
|
|
54
|
+
const args = argv.slice(2);
|
|
55
|
+
const getArg = (name) => {
|
|
56
|
+
const idx = args.indexOf(name);
|
|
57
|
+
return idx !== -1 && args[idx + 1] ? args[idx + 1] : null;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const packageName = getArg("--package");
|
|
61
|
+
const sourceDir = getArg("--source-dir");
|
|
62
|
+
const outputDir = getArg("--output-dir");
|
|
63
|
+
const mode = getArg("--mode") || "saved";
|
|
64
|
+
const dataDir = getArg("--data");
|
|
65
|
+
const offset = getArg("--offset");
|
|
66
|
+
const years = getArg("--years");
|
|
67
|
+
const yearEndArg = getArg("--year-end");
|
|
68
|
+
|
|
69
|
+
if (!packageName) {
|
|
70
|
+
console.error("Error: --package is required (bst, taxi, se, ltd)");
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
if (!sourceDir && !dataDir) {
|
|
74
|
+
console.error("Error: --source-dir or --data is required");
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
if (!outputDir) {
|
|
78
|
+
console.error("Error: --output-dir is required");
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { packageName, sourceDir, outputDir, mode, dataDir, offset, years, yearEndArg };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// R, the canonical report document, beside the markdown. Both engines write
|
|
86
|
+
// it through the one serializer, so verify-roundtrip.js joins them on a key
|
|
87
|
+
// rather than on a rendered table row.
|
|
88
|
+
function writeReportJson(outputDir, options) {
|
|
89
|
+
const document = buildReportDocument(options);
|
|
90
|
+
writeFileSync(resolve(outputDir, "report.json"), serializeReportDocument(document));
|
|
91
|
+
console.log(` Written: report.json (${document.values.length} values)`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function main() {
|
|
95
|
+
const { packageName, sourceDir, outputDir, mode, dataDir, offset, years, yearEndArg } = parseArgs(process.argv);
|
|
96
|
+
|
|
97
|
+
const productMod = PRODUCTS[packageName];
|
|
98
|
+
if (!productMod) {
|
|
99
|
+
console.error(`Unknown package: ${packageName}. Available: ${Object.keys(PRODUCTS).join(", ")}`);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --data alone computes the report; --data beside --source-dir only lends
|
|
104
|
+
// the Excel run its scenario, so the workbook stays the source of values.
|
|
105
|
+
if (dataDir && !sourceDir) {
|
|
106
|
+
console.log(`=== report.js (diya-gl mode) ===`);
|
|
107
|
+
console.log(`Package: ${packageName}`);
|
|
108
|
+
console.log(`Data: ${resolve(dataDir)}`);
|
|
109
|
+
console.log(`Output: ${outputDir}`);
|
|
110
|
+
|
|
111
|
+
const { book, lines } = loadDiyaGlData(resolve(dataDir), offset);
|
|
112
|
+
|
|
113
|
+
// Load tax data: prefer --years (from app/data/*.toml) over book.toml extraction
|
|
114
|
+
let taxData;
|
|
115
|
+
if (years) {
|
|
116
|
+
const taxDataPath = resolve(__dirname, "..", "data", `${years}.toml`);
|
|
117
|
+
if (!existsSync(taxDataPath)) {
|
|
118
|
+
console.error(`Tax data file not found: ${taxDataPath}`);
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
taxData = parseTOML(readFileSync(taxDataPath, "utf8"));
|
|
122
|
+
console.log(`Tax data: ${years}.toml`);
|
|
123
|
+
} else {
|
|
124
|
+
taxData = extractTaxDataFromBook(book, packageName);
|
|
125
|
+
console.log(`Tax data: extracted from book.toml (use --years for precise rates)`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// diyaGlToScenario() builds the same scenario shape cellWrites() consumes
|
|
129
|
+
// on the Excel side: opening balances, stock, debtors, creditors and
|
|
130
|
+
// business details. Passing it through lights those up on the JS side too.
|
|
131
|
+
const scenario = diyaGlToScenario(book, lines, packageName);
|
|
132
|
+
const results = calculateFromDiyaGl(book, lines, packageName, taxData, scenario);
|
|
133
|
+
|
|
134
|
+
// Fixture anchors (opening_debtors, closing_creditors, ...) are top-level
|
|
135
|
+
// scenario tables, not [expected] keys, so checks that anchor against
|
|
136
|
+
// them need the whole scenario merged in — the same shape reconcile.js
|
|
137
|
+
// builds for the Excel side.
|
|
138
|
+
const mergedScenario = { ...scenario, ...scenario.expected };
|
|
139
|
+
const periodEnd = book.documentInfo?.periodCoveredEnd;
|
|
140
|
+
const yearEnd = yearEndArg || (periodEnd ? new Date(periodEnd).toISOString().slice(0, 10) : null);
|
|
141
|
+
const checks =
|
|
142
|
+
typeof productMod.checkCompliance === "function"
|
|
143
|
+
? productMod.checkCompliance({ ...results }, mergedScenario, taxData, calculateExpectedTax, yearEnd)
|
|
144
|
+
: [];
|
|
145
|
+
|
|
146
|
+
const resolvedOutputDir = resolve(outputDir);
|
|
147
|
+
mkdirSync(resolvedOutputDir, { recursive: true });
|
|
148
|
+
|
|
149
|
+
const sectionReports = generateSectionReports(results, productMod, mergedScenario, checks);
|
|
150
|
+
for (const [filename, content] of Object.entries(sectionReports)) {
|
|
151
|
+
writeFileSync(resolve(resolvedOutputDir, filename), content);
|
|
152
|
+
console.log(` Written: ${filename}`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
writeReportJson(resolvedOutputDir, {
|
|
156
|
+
packageName,
|
|
157
|
+
engine: "js",
|
|
158
|
+
results,
|
|
159
|
+
productMod,
|
|
160
|
+
scenario: mergedScenario,
|
|
161
|
+
checks,
|
|
162
|
+
scenarioName: book.documentInfo?.entriesComment,
|
|
163
|
+
yearEnd,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// The JS engine never lets D out of memory, so the data half of its
|
|
167
|
+
// tuple is an identity. Writing it here is not evidence: it is the
|
|
168
|
+
// canonical form of the fixture, which is the side the Excel export
|
|
169
|
+
// gets measured against.
|
|
170
|
+
const dataDirOut = resolve(resolvedOutputDir, "data");
|
|
171
|
+
mkdirSync(dataDirOut, { recursive: true });
|
|
172
|
+
writeFileSync(resolve(dataDirOut, "book.toml"), canonicalBookToml(book));
|
|
173
|
+
writeFileSync(resolve(dataDirOut, "lines.jsonl"), canonicalLinesJsonl(lines));
|
|
174
|
+
console.log(` Written: data/book.toml, data/lines.jsonl (${lines.length} lines)`);
|
|
175
|
+
|
|
176
|
+
console.log(`\n${Object.keys(sectionReports).length} report files written to ${resolvedOutputDir}`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const cellReads = productMod.standardReads();
|
|
181
|
+
const resolvedSourceDir = resolve(sourceDir);
|
|
182
|
+
|
|
183
|
+
// The year-end date names the tab layout (non-March year ends rename the
|
|
184
|
+
// month tabs) that multiFileOptions() reads against. --year-end overrides
|
|
185
|
+
// it; failing that, the source directory's own name carries it the same
|
|
186
|
+
// way a packages/ directory does.
|
|
187
|
+
const yearEnd = yearEndArg || packageYearEnd(basename(resolvedSourceDir));
|
|
188
|
+
const yearEndMonth = yearEnd ? parseInt(yearEnd.split("-")[1], 10) : undefined;
|
|
189
|
+
const multiOpts =
|
|
190
|
+
productMod.MULTI_FILE && typeof productMod.multiFileOptions === "function" ? productMod.multiFileOptions(yearEndMonth) : {};
|
|
191
|
+
|
|
192
|
+
console.log(`=== report.js ===`);
|
|
193
|
+
console.log(`Package: ${packageName}`);
|
|
194
|
+
console.log(`Source: ${resolvedSourceDir}`);
|
|
195
|
+
console.log(`Mode: ${mode}`);
|
|
196
|
+
console.log(`Output: ${outputDir}`);
|
|
197
|
+
|
|
198
|
+
let results;
|
|
199
|
+
|
|
200
|
+
if (mode === "saved") {
|
|
201
|
+
if (productMod.MULTI_FILE) {
|
|
202
|
+
results = await readMultiFileXlsxCellValues(resolvedSourceDir, "Financialaccounts.xlsx", cellReads);
|
|
203
|
+
// The VAT Returns and Fixed Asset Schedule sections live on leaf files
|
|
204
|
+
// (Vatreturns.xlsx, Fixedassets.xlsx, ...), not the hub, so the hub-only
|
|
205
|
+
// read above never carries them. additionalReads names every cell
|
|
206
|
+
// reportSections() needs from those files, keyed "<filename>!<sheetName>".
|
|
207
|
+
Object.assign(results, await readMultiFileAdditionalXlsxCellValues(resolvedSourceDir, multiOpts.additionalReads));
|
|
208
|
+
} else {
|
|
209
|
+
const xlsxFile = findXlsx(resolvedSourceDir);
|
|
210
|
+
if (!xlsxFile) {
|
|
211
|
+
console.error(`No xlsx file found in ${resolvedSourceDir}`);
|
|
212
|
+
process.exit(1);
|
|
213
|
+
}
|
|
214
|
+
const xlsxBuffer = readFileSync(resolve(resolvedSourceDir, xlsxFile));
|
|
215
|
+
results = await readXlsxCellValues(xlsxBuffer, cellReads);
|
|
216
|
+
}
|
|
217
|
+
} else if (mode === "recalculate") {
|
|
218
|
+
if (productMod.MULTI_FILE) {
|
|
219
|
+
const { readdirSync } = await import("fs");
|
|
220
|
+
const xlsxFiles = readdirSync(resolvedSourceDir).filter((f) => f.endsWith(".xlsx"));
|
|
221
|
+
const fileBuffers = {};
|
|
222
|
+
for (const f of xlsxFiles) {
|
|
223
|
+
fileBuffers[f] = readFileSync(resolve(resolvedSourceDir, f));
|
|
224
|
+
}
|
|
225
|
+
results = await runMultiFileSpreadsheet(fileBuffers, {}, cellReads, "Financialaccounts.xlsx", multiOpts);
|
|
226
|
+
} else {
|
|
227
|
+
const xlsxFile = findXlsx(resolvedSourceDir);
|
|
228
|
+
if (!xlsxFile) {
|
|
229
|
+
console.error(`No xlsx file found in ${resolvedSourceDir}`);
|
|
230
|
+
process.exit(1);
|
|
231
|
+
}
|
|
232
|
+
const xlsxBuffer = readFileSync(resolve(resolvedSourceDir, xlsxFile));
|
|
233
|
+
results = await runSpreadsheet(xlsxBuffer, {}, cellReads);
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
console.error(`Unknown mode: ${mode}. Use 'saved' or 'recalculate'.`);
|
|
237
|
+
process.exit(1);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Generate individual report files
|
|
241
|
+
const resolvedOutputDir = resolve(outputDir);
|
|
242
|
+
mkdirSync(resolvedOutputDir, { recursive: true });
|
|
243
|
+
|
|
244
|
+
// The Excel read set on its own carries no scenario, so a run given one
|
|
245
|
+
// publishes the same compliance verdicts the JS run does, against the
|
|
246
|
+
// sheet's own figures. Without it the report is the values alone.
|
|
247
|
+
let excelScenario;
|
|
248
|
+
let excelChecks = [];
|
|
249
|
+
if (dataDir) {
|
|
250
|
+
const { book, lines } = loadDiyaGlData(resolve(dataDir), offset);
|
|
251
|
+
const scenario = diyaGlToScenario(book, lines, packageName);
|
|
252
|
+
excelScenario = { ...scenario, ...scenario.expected };
|
|
253
|
+
let taxData;
|
|
254
|
+
if (years) {
|
|
255
|
+
const taxDataPath = resolve(__dirname, "..", "data", `${years}.toml`);
|
|
256
|
+
if (!existsSync(taxDataPath)) {
|
|
257
|
+
console.error(`Tax data file not found: ${taxDataPath}`);
|
|
258
|
+
process.exit(1);
|
|
259
|
+
}
|
|
260
|
+
taxData = parseTOML(readFileSync(taxDataPath, "utf8"));
|
|
261
|
+
} else {
|
|
262
|
+
taxData = extractTaxDataFromBook(book, packageName);
|
|
263
|
+
}
|
|
264
|
+
if (typeof productMod.checkCompliance === "function") {
|
|
265
|
+
excelChecks = productMod.checkCompliance({ ...results }, excelScenario, taxData, calculateExpectedTax, yearEnd);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const sectionReports = generateSectionReports(results, productMod, excelScenario, excelChecks);
|
|
270
|
+
for (const [filename, content] of Object.entries(sectionReports)) {
|
|
271
|
+
writeFileSync(resolve(resolvedOutputDir, filename), content);
|
|
272
|
+
console.log(` Written: ${filename}`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
writeReportJson(resolvedOutputDir, {
|
|
276
|
+
packageName,
|
|
277
|
+
engine: "excel",
|
|
278
|
+
results,
|
|
279
|
+
productMod,
|
|
280
|
+
scenario: excelScenario,
|
|
281
|
+
checks: excelChecks,
|
|
282
|
+
yearEnd,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
console.log(`\n${Object.keys(sectionReports).length} report files written to ${resolvedOutputDir}`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
main().catch((err) => {
|
|
289
|
+
console.error(err);
|
|
290
|
+
process.exit(1);
|
|
291
|
+
});
|