@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.
Files changed (157) hide show
  1. package/README.md +63 -0
  2. package/bin/diya-gl-mcp.js +10 -0
  3. package/bin/diya-gl-read-workbook.js +11 -0
  4. package/bin/diya-gl-recalc.js +10 -0
  5. package/bin/diya-gl-write-workbook.js +12 -0
  6. package/bin/diya-gl.js +24 -0
  7. package/bin/run-dist-bin.js +23 -0
  8. package/dist/app/bin/build-diya-gl-spec.js +680 -0
  9. package/dist/app/bin/build-packages.js +149 -0
  10. package/dist/app/bin/build-reconciliation-pages.js +1052 -0
  11. package/dist/app/bin/build-sitemaps.js +51 -0
  12. package/dist/app/bin/compliance-report.js +125 -0
  13. package/dist/app/bin/cross-package-reconciliation.js +92 -0
  14. package/dist/app/bin/diya-gl-mcp.js +27 -0
  15. package/dist/app/bin/export.js +318 -0
  16. package/dist/app/bin/extract-scenarios.js +1348 -0
  17. package/dist/app/bin/generate.js +353 -0
  18. package/dist/app/bin/judge-reconciliation.js +649 -0
  19. package/dist/app/bin/reconcile.js +247 -0
  20. package/dist/app/bin/report.js +291 -0
  21. package/dist/app/bin/verify-roundtrip.js +941 -0
  22. package/dist/app/bin/verify-stability.js +429 -0
  23. package/dist/app/bin/write-workbook.js +106 -0
  24. package/dist/app/data/filing/ct-computation-v1.1.toml +1033 -0
  25. package/dist/app/data/filing/ct600-v3.toml +2329 -0
  26. package/dist/app/data/filing/frs105-formats.toml +244 -0
  27. package/dist/app/data/fixture-master-gaps.json +247 -0
  28. package/dist/app/data/hmrc/SOURCE.md +14 -0
  29. package/dist/app/data/hmrc/form-layouts/bst.json +116 -0
  30. package/dist/app/data/hmrc/form-layouts/ltd.json +452 -0
  31. package/dist/app/data/hmrc/form-layouts/se.json +485 -0
  32. package/dist/app/data/hmrc/form-layouts/taxi.json +117 -0
  33. package/dist/app/data/hmrc/sa103-mtd-mapping.json +1078 -0
  34. package/dist/app/data/hmrc/sa103f_mapping_v3.csv +111 -0
  35. package/dist/app/data/judge-rubric.md +52 -0
  36. package/dist/app/data/ltd-2020.toml +49 -0
  37. package/dist/app/data/ltd-2021.toml +49 -0
  38. package/dist/app/data/ltd-2022.toml +49 -0
  39. package/dist/app/data/ltd-2023.toml +49 -0
  40. package/dist/app/data/ltd-2024.toml +49 -0
  41. package/dist/app/data/ltd-2025.toml +49 -0
  42. package/dist/app/data/ltd-2026.toml +53 -0
  43. package/dist/app/data/ltd-2027.toml +51 -0
  44. package/dist/app/data/render-unrepresentable/bst.json +24 -0
  45. package/dist/app/data/render-unrepresentable/ltd.json +863 -0
  46. package/dist/app/data/render-unrepresentable/se.json +455 -0
  47. package/dist/app/data/render-unrepresentable/taxi.json +16 -0
  48. package/dist/app/data/roundtrip-budget.json +38 -0
  49. package/dist/app/data/roundtrip-matrix-budget.json +26 -0
  50. package/dist/app/data/roundtrip-unrepresentable.json +401 -0
  51. package/dist/app/data/se-2020-2021.toml +52 -0
  52. package/dist/app/data/se-2021-2022.toml +52 -0
  53. package/dist/app/data/se-2022-2023.toml +55 -0
  54. package/dist/app/data/se-2023-2024.toml +54 -0
  55. package/dist/app/data/se-2024-2025.toml +51 -0
  56. package/dist/app/data/se-2025-2026.toml +51 -0
  57. package/dist/app/data/se-2026-2027.toml +51 -0
  58. package/dist/app/data/volatile-cells.json +16 -0
  59. package/dist/app/lib/anchors/bst.js +79 -0
  60. package/dist/app/lib/anchors/ltd.js +626 -0
  61. package/dist/app/lib/anchors/run.js +100 -0
  62. package/dist/app/lib/anchors/se.js +456 -0
  63. package/dist/app/lib/anchors/taxi.js +164 -0
  64. package/dist/app/lib/app-resources.js +80 -0
  65. package/dist/app/lib/book-checks/ltd.js +408 -0
  66. package/dist/app/lib/book-checks/se.js +687 -0
  67. package/dist/app/lib/book-checks/taxi.js +215 -0
  68. package/dist/app/lib/book-checks.js +872 -0
  69. package/dist/app/lib/books-engine.js +197 -0
  70. package/dist/app/lib/books-interchange.js +533 -0
  71. package/dist/app/lib/calculators/bst.js +381 -0
  72. package/dist/app/lib/calculators/ltd.js +1938 -0
  73. package/dist/app/lib/calculators/se.js +1239 -0
  74. package/dist/app/lib/calculators/shared.js +131 -0
  75. package/dist/app/lib/calculators/taxi.js +461 -0
  76. package/dist/app/lib/canonical-report-value.js +77 -0
  77. package/dist/app/lib/compliance-report.js +341 -0
  78. package/dist/app/lib/diya-gl-calculator.js +52 -0
  79. package/dist/app/lib/diya-gl-canonical.js +355 -0
  80. package/dist/app/lib/diya-gl-edits-ltd.js +144 -0
  81. package/dist/app/lib/diya-gl-edits.js +361 -0
  82. package/dist/app/lib/diya-gl-loader.js +774 -0
  83. package/dist/app/lib/diya-gl-schema.js +212 -0
  84. package/dist/app/lib/generator.js +1912 -0
  85. package/dist/app/lib/guide.js +33 -0
  86. package/dist/app/lib/headlines.js +293 -0
  87. package/dist/app/lib/link-caches.js +421 -0
  88. package/dist/app/lib/ltd-layout.js +139 -0
  89. package/dist/app/lib/mcp/diya-gl-tools.js +330 -0
  90. package/dist/app/lib/mcp/jsonrpc-stdio.js +66 -0
  91. package/dist/app/lib/mcp/server.js +65 -0
  92. package/dist/app/lib/overtype-sidecar.js +180 -0
  93. package/dist/app/lib/package-builder.js +202 -0
  94. package/dist/app/lib/payslips-date-chain.js +481 -0
  95. package/dist/app/lib/payslips-layout.js +235 -0
  96. package/dist/app/lib/period-shift.js +111 -0
  97. package/dist/app/lib/product-workbook.js +317 -0
  98. package/dist/app/lib/products.js +27 -0
  99. package/dist/app/lib/provenance-data.js +36 -0
  100. package/dist/app/lib/provenance.js +92 -0
  101. package/dist/app/lib/report-generator.js +374 -0
  102. package/dist/app/lib/report-indicators.js +468 -0
  103. package/dist/app/lib/report-serializer.js +392 -0
  104. package/dist/app/lib/scenario-extractor.js +1403 -0
  105. package/dist/app/lib/scenario-loader.js +84 -0
  106. package/dist/app/lib/sitemap-builder.js +59 -0
  107. package/dist/app/lib/spreadsheet-runner.js +566 -0
  108. package/dist/app/lib/tax/capital-allowances.js +77 -0
  109. package/dist/app/lib/tax/corporation-tax.js +144 -0
  110. package/dist/app/lib/tax/income-tax.js +122 -0
  111. package/dist/app/lib/tax/mileage.js +69 -0
  112. package/dist/app/lib/tax/national-insurance.js +31 -0
  113. package/dist/app/lib/tax/vat.js +188 -0
  114. package/dist/app/lib/tax-year.js +31 -0
  115. package/dist/app/lib/template-formula-map.js +253 -0
  116. package/dist/app/lib/workbook-set.js +153 -0
  117. package/dist/app/lib/xlsx-exporter.js +2815 -0
  118. package/dist/app/lib/xlsx-parts.js +111 -0
  119. package/dist/app/lib/xlsx-reader.js +84 -0
  120. package/dist/app/products/bst.js +727 -0
  121. package/dist/app/products/ltd.js +4270 -0
  122. package/dist/app/products/se.js +3403 -0
  123. package/dist/app/products/taxi.js +912 -0
  124. package/dist/app/templates/bst/bst-excel.xlsx +0 -0
  125. package/dist/app/templates/bst/meta.toml +27 -0
  126. package/dist/app/templates/ltd/Cashaccount.xlsx +0 -0
  127. package/dist/app/templates/ltd/Companysecretary.xlsx +0 -0
  128. package/dist/app/templates/ltd/Creditcardaccount.xlsx +0 -0
  129. package/dist/app/templates/ltd/Currentaccount.xlsx +0 -0
  130. package/dist/app/templates/ltd/Dividend Voucher.docx +0 -0
  131. package/dist/app/templates/ltd/Financialaccounts.xlsx +0 -0
  132. package/dist/app/templates/ltd/Fixedassets.xlsx +0 -0
  133. package/dist/app/templates/ltd/Payslips.xlsx +0 -0
  134. package/dist/app/templates/ltd/Purchases.xlsx +0 -0
  135. package/dist/app/templates/ltd/Sales.xlsx +0 -0
  136. package/dist/app/templates/ltd/Salesinvoice.xlsx +0 -0
  137. package/dist/app/templates/ltd/Savingaccount.xlsx +0 -0
  138. package/dist/app/templates/ltd/Vatreturns.xlsx +0 -0
  139. package/dist/app/templates/ltd/expensesform.xlsx +0 -0
  140. package/dist/app/templates/ltd/meta.toml +54 -0
  141. package/dist/app/templates/meta.toml +12 -0
  142. package/dist/app/templates/se/Bank.xlsx +0 -0
  143. package/dist/app/templates/se/Cash.xlsx +0 -0
  144. package/dist/app/templates/se/Financialaccounts.xlsx +0 -0
  145. package/dist/app/templates/se/Fixedassets.xlsx +0 -0
  146. package/dist/app/templates/se/Payslips.xlsx +0 -0
  147. package/dist/app/templates/se/Purchases.xlsx +0 -0
  148. package/dist/app/templates/se/Sales.xlsx +0 -0
  149. package/dist/app/templates/se/Salesinvoice.xlsx +0 -0
  150. package/dist/app/templates/se/Vat.xlsx +0 -0
  151. package/dist/app/templates/se/meta.toml +43 -0
  152. package/dist/app/templates/taxi/meta.toml +43 -0
  153. package/dist/app/templates/taxi/taxi-excel.xlsx +0 -0
  154. package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-book-v2.schema.json +713 -0
  155. package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-docs.md +930 -0
  156. package/dist/web/spreadsheets.diyaccounting.co.uk/public/schema/diya-gl-lines-v2.schema.json +220 -0
  157. package/package.json +37 -0
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (C) 2026 DIY Accounting Ltd
3
+ //
4
+ // guide.js — Generate PDF user guide from markdown template.
5
+ // Uses pandoc + weasyprint. Install: `brew install pandoc` and `pip install weasyprint`.
6
+ //
7
+ // SOURCE_DATE_EPOCH is set for reproducible PDF output:
8
+ // - Default: mtime of the markdown source file
9
+ // - Override: pass sourceDateEpoch parameter (e.g. git commit timestamp)
10
+
11
+ import { execSync } from "child_process";
12
+ import { dirname, resolve } from "path";
13
+ import { existsSync, statSync } from "fs";
14
+
15
+ export async function generatePdf(markdownPath, outputPath, sourceDateEpoch) {
16
+ if (!existsSync(markdownPath)) {
17
+ throw new Error(`Markdown file not found: ${markdownPath}`);
18
+ }
19
+
20
+ const epoch = sourceDateEpoch ?? Math.floor(statSync(markdownPath).mtimeMs / 1000);
21
+
22
+ // Resolve to absolute paths so they work regardless of cwd
23
+ const absMd = resolve(markdownPath);
24
+ const absOut = resolve(outputPath);
25
+ const mdDir = dirname(absMd);
26
+
27
+ // Run pandoc from the markdown's directory so relative image paths (e.g. screenshots/home.png) resolve naturally
28
+ execSync(`pandoc "${absMd}" -o "${absOut}" --pdf-engine=weasyprint`, {
29
+ cwd: mdDir,
30
+ stdio: "pipe",
31
+ env: { ...process.env, SOURCE_DATE_EPOCH: String(epoch) },
32
+ });
33
+ }
@@ -0,0 +1,293 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (C) 2026 DIY Accounting Ltd
3
+ //
4
+ // headlines.js — the year-at-a-glance strip's own arithmetic: four tiles
5
+ // and two pies, derived once from R (the report document report-serializer.js
6
+ // builds) and a product's own HEADLINES declaration (the key names beside
7
+ // its CELL_MAP), so the Node test and the browser page compute the same
8
+ // figures from the same declared keys.
9
+ //
10
+ // Pure and browser-safe: no imports, no DOM, no formatting. A caller turns
11
+ // the numbers into "£12,000" or an SVG; this module only says what the
12
+ // figures are and which R keys they came from.
13
+
14
+ const OUTGOINGS_SLICE_CAP = 5;
15
+
16
+ // One figure read off R for a single cell key, carrying the key it came
17
+ // from so a caller can trace it back to the cell. A required key that R
18
+ // does not carry is an error naming the key; an optional one reads as zero
19
+ // with an empty trail and missing:true, which is how a book with no
20
+ // assets, stock or debtors reads.
21
+ function readOneCell(report, key, optional) {
22
+ const entry = report.values.find((value) => value.key === key);
23
+ if (!entry) {
24
+ if (optional) return { value: 0, from: [], missing: true };
25
+ throw new Error(`headlinesFromReport: report carries no value for ${key}`);
26
+ }
27
+ const value = Number(entry.value);
28
+ if (!Number.isFinite(value)) {
29
+ throw new Error(`headlinesFromReport: ${key} is "${entry.value}", not a number`);
30
+ }
31
+ return { value, from: [key] };
32
+ }
33
+
34
+ // Add two or more figures, keeping the union of the keys each one traces to.
35
+ function addFigures(...figures) {
36
+ return {
37
+ value: figures.reduce((total, figure) => total + figure.value, 0),
38
+ from: figures.flatMap((figure) => figure.from),
39
+ };
40
+ }
41
+
42
+ // One declared figure: `{key, optional}` reads a single cell; `{keys,
43
+ // optional}` reads several and sums them. This is how a HEADLINES
44
+ // declaration names both a single-cell tile (turnover) and a multi-cell one
45
+ // (BST and SE's cost of sales, which is stock plus direct costs).
46
+ function readKey(report, spec) {
47
+ if (spec.keys) {
48
+ return addFigures(...spec.keys.map((key) => readOneCell(report, key, spec.optional)));
49
+ }
50
+ return readOneCell(report, spec.key, spec.optional);
51
+ }
52
+
53
+ // A declared part of a tile that a product may not carry at all (Taxi
54
+ // declares no stock or debtors part for its assets tile). Undeclared reads
55
+ // the same as a declared-but-absent optional cell: zero, empty trail,
56
+ // missing:true.
57
+ function readDeclaredPart(report, spec) {
58
+ if (!spec) return { value: 0, from: [], missing: true };
59
+ return readKey(report, spec);
60
+ }
61
+
62
+ // A declared list of `{label, key, optional}` items, each resolved to a
63
+ // labelled figure. Used for a tile's second line (SE's grants and
64
+ // interest, shown beside turnover but outside the pie), for the assets
65
+ // total's extra parts (SE's cash at bank and in hand, summed in), and for
66
+ // a turnover-pie's extra bridge slices (Ltd's dividends, optional since a
67
+ // company need not have declared any).
68
+ function resolveLines(report, lines = []) {
69
+ return lines.map(({ label, key, optional }) => {
70
+ const figure = readOneCell(report, key, optional);
71
+ return { label, value: figure.value, from: figure.from };
72
+ });
73
+ }
74
+
75
+ // A tile's own optional second line: one labelled figure shown beside the
76
+ // tile's headline value and excluded from every sum (Ltd's tax-outstanding
77
+ // and net-assets figures). Undeclared reads as no second line at all,
78
+ // unlike `resolveLines`, whose undeclared list reads as empty.
79
+ function resolveSecondLine(report, spec) {
80
+ if (!spec) return undefined;
81
+ const figure = readOneCell(report, spec.key, spec.optional);
82
+ return { label: spec.label, value: figure.value, from: figure.from };
83
+ }
84
+
85
+ // The bridge from turnover to what is left: cost of sales, running costs,
86
+ // tax and NI, whatever extra slices the product declares (Ltd's
87
+ // dividends), then whatever remains. A negative remainder is a loss year;
88
+ // a negative slice is a refund year. Either way a pie cannot show it
89
+ // honestly, so the page draws a bar instead. The cost-of-sales, running-
90
+ // costs and tax slices carry the label their own declaration names (the
91
+ // default "Cost of sales" and "Tax and NI"; Ltd's "Administrative expenses"
92
+ // and "Corporation tax", the words its own sheet uses; Taxi's "vehicle
93
+ // costs", since its cost of sales is the vehicle-cost total).
94
+ function turnoverPie(turnover, costOfSales, costOfSalesLabel, runningCosts, runningCostsLabel, tax, taxLabel, pieExtra = []) {
95
+ const extraTotal = pieExtra.reduce((total, slice) => total + slice.value, 0);
96
+ const kept = turnover.value - costOfSales.value - runningCosts.value - tax.value - extraTotal;
97
+ const slices = [
98
+ { label: costOfSalesLabel, value: costOfSales.value, from: costOfSales.from },
99
+ { label: runningCostsLabel, value: runningCosts.value, from: runningCosts.from },
100
+ { label: taxLabel, value: tax.value, from: tax.from },
101
+ ...pieExtra.map((slice) => ({ label: slice.label, value: slice.value, from: slice.from })),
102
+ {
103
+ label: "Kept",
104
+ value: kept,
105
+ from: [...turnover.from, ...costOfSales.from, ...runningCosts.from, ...tax.from, ...pieExtra.flatMap((slice) => slice.from)],
106
+ },
107
+ ];
108
+ const withShares = slices.map((slice) => ({ ...slice, share: turnover.value === 0 ? 0 : slice.value / turnover.value }));
109
+ const negative = withShares.filter((slice) => slice.value < 0);
110
+ if (negative.length > 0) {
111
+ const reason =
112
+ kept < 0
113
+ ? "the year ran at a loss, so turnover cannot be split into positive slices"
114
+ : "one of the turnover slices is negative, so it cannot be split into positive slices";
115
+ return { mode: "bar", reason, slices: withShares };
116
+ }
117
+ return { mode: "pie", slices: withShares };
118
+ }
119
+
120
+ // A declared list of `[key, label]` pairs, each read as a required cell and
121
+ // labelled for the outgoings pie. Used for a product's `expenseLines` (the
122
+ // pie's own list, added beside its automatic Cost of sales slice) and for
123
+ // `pieLines` (see `outgoingsPie` below).
124
+ function resolveKeyLabelPairs(report, pairs = []) {
125
+ return pairs.map(([key, label]) => {
126
+ const figure = readOneCell(report, key, false);
127
+ return { label, value: figure.value, from: figure.from };
128
+ });
129
+ }
130
+
131
+ // The largest spending categories, up to five, with everything else folded
132
+ // into one "Other" slice. Zero-value categories never reach the candidate
133
+ // list, so a book with nothing in a category never shows an empty slice.
134
+ // `pieLines`, when declared, replaces the whole candidate list -- Taxi's
135
+ // cost of sales is itself a bundle of the sheet's own vehicle-cost lines
136
+ // (fuel, car hire, ...), so a product that wants those shown individually
137
+ // declares `pieLines` with the full breakdown instead of one combined
138
+ // "Cost of sales" slice, and no `expenseLines` at all.
139
+ function outgoingsPie(costOfSales, expenseLines, outgoingsTotal, pieLines) {
140
+ const candidates = (pieLines ?? [{ label: "Cost of sales", value: costOfSales.value, from: costOfSales.from }, ...expenseLines]).filter(
141
+ (candidate) => candidate.value !== 0,
142
+ );
143
+ const ranked = [...candidates].sort((a, b) => b.value - a.value);
144
+ const shown = ranked.slice(0, OUTGOINGS_SLICE_CAP);
145
+ const rest = ranked.slice(OUTGOINGS_SLICE_CAP);
146
+ const slices = [...shown];
147
+ if (rest.length > 0) {
148
+ slices.push({
149
+ label: "Other",
150
+ value: rest.reduce((total, candidate) => total + candidate.value, 0),
151
+ from: rest.flatMap((candidate) => candidate.from),
152
+ });
153
+ }
154
+ return {
155
+ slices: slices
156
+ .filter((slice) => slice.value !== 0)
157
+ .map((slice) => ({ ...slice, share: outgoingsTotal.value === 0 ? 0 : slice.value / outgoingsTotal.value })),
158
+ };
159
+ }
160
+
161
+ // A cell whose value is words, not a number -- Taxi's route cell, which the
162
+ // sheet leaves blank (so R carries no entry for it at all) on the
163
+ // actual-costs route. Read if present; absent reads as no value and an
164
+ // empty trail, never an error, since a blank route cell is what the
165
+ // sheet's own formula produces on that route.
166
+ function readOneTextCell(report, key) {
167
+ const entry = report.values.find((value) => value.key === key);
168
+ return entry ? { value: entry.value, from: [key] } : { value: undefined, from: [] };
169
+ }
170
+
171
+ // Taxi's vehicle-comparison tile: the miles driven, the mileage allowance
172
+ // claimed, what the vehicle actually cost to run, the sheet's own
173
+ // comparison figure and which route it took, plus whichever of vehicle
174
+ // costs or the mileage allowance the P&L actually charged. Present only
175
+ // when the product declares `vehicle` and its miles cell is above zero --
176
+ // a book that keeps no mileage log has nothing to compare.
177
+ function resolveVehicle(report, spec) {
178
+ if (!spec) return undefined;
179
+ const miles = readOneCell(report, spec.miles, false);
180
+ if (miles.value <= 0) return undefined;
181
+ const allowance = readOneCell(report, spec.allowance, false);
182
+ const running = readOneCell(report, spec.running, false);
183
+ const compared = readOneCell(report, spec.compared, false);
184
+ const charged = readOneCell(report, spec.charged, false);
185
+ const routeText = readOneTextCell(report, spec.route);
186
+ const route = routeText.value === "MILEAGE ALLOWANCE" ? "mileage" : "actual";
187
+ return { miles, allowance, running, compared, charged, route };
188
+ }
189
+
190
+ /**
191
+ * The year-at-a-glance strip's tiles and pies, derived from R and a
192
+ * product's HEADLINES declaration (see app/products/bst.js).
193
+ *
194
+ * @param {{values: Array<{key: string, value: string}>}} report - R, as
195
+ * report-serializer.js's buildReportDocument() returns it
196
+ * @param {Object} declaration - the product's HEADLINES export: turnover,
197
+ * costOfSales, runningCosts, tax (each a `{key}` or `{keys}` spec),
198
+ * expenseLines (`[key, label]` pairs) and assets (writtenDown, stock,
199
+ * debtors, each optional). turnover may add `secondLine` (an array of
200
+ * `{label, key}`, shown beside the tile, outside the pie) and `pieExtra`
201
+ * (an array of `{label, key, optional}`, extra bridge slices folded into
202
+ * the turnover pie before "Kept" -- Ltd's dividends). assets may add
203
+ * `extra` (`[{label, key}]`, summed into the assets total). tax and
204
+ * assets may each add `secondLine` (one `{label, key}`, shown beside that
205
+ * tile's own value, outside every sum -- Ltd's tax-outstanding and
206
+ * net-assets figures). costOfSales, runningCosts and tax may each add
207
+ * `label` (a string, the turnover-pie slice's own name -- default "Cost
208
+ * of sales", "Running costs" and "Tax and NI"; Ltd names its sheet's own
209
+ * words, "Administrative expenses" and "Corporation tax"; Taxi names its
210
+ * own, "vehicle costs"). A product may replace `expenseLines` with
211
+ * `pieLines` (the same `[key, label]` shape) to give the outgoings pie
212
+ * its full candidate list directly, with no separate combined Cost of
213
+ * sales slice -- Taxi's vehicle-cost breakdown. A product may also
214
+ * declare `vehicle` (`{miles, allowance, running, compared, route,
215
+ * charged}`, each a cell key) for Taxi's mileage-versus-actual-costs
216
+ * tile. All default to empty/absent.
217
+ * @returns {{tiles: Object, pies: Object, keys: Object}}
218
+ */
219
+ export function headlinesFromReport(report, declaration) {
220
+ if (!declaration) {
221
+ throw new Error("headlinesFromReport: declaration is required");
222
+ }
223
+
224
+ const turnoverFigure = readKey(report, declaration.turnover);
225
+ const turnoverSecondLine = resolveLines(report, declaration.turnover.secondLine);
226
+ const turnover = turnoverSecondLine.length > 0 ? { ...turnoverFigure, secondLine: turnoverSecondLine } : turnoverFigure;
227
+ const turnoverPieExtra = resolveLines(report, declaration.turnover.pieExtra);
228
+
229
+ const costOfSales = readKey(report, declaration.costOfSales);
230
+ const costOfSalesLabel = declaration.costOfSales.label ?? "Cost of sales";
231
+ const runningCosts = readKey(report, declaration.runningCosts);
232
+ const runningCostsLabel = declaration.runningCosts.label ?? "Running costs";
233
+ const outgoingsTotal = addFigures(costOfSales, runningCosts);
234
+
235
+ const assetsDecl = declaration.assets || {};
236
+ const writtenDown = readDeclaredPart(report, assetsDecl.writtenDown);
237
+ const stock = readDeclaredPart(report, assetsDecl.stock);
238
+ // What the business holds is the written-down value of its assets, plus
239
+ // its stock and whatever else the product declares as `extra` (SE sums in
240
+ // cash at bank and in hand). What customers owe is a different kind of
241
+ // figure and is reported beside the total, never inside it: on BST and SE
242
+ // the sheet's "Amount owed by customers" counts every invoiced-and-
243
+ // unsettled sale across the year, so in a book that records few
244
+ // settlements it approaches turnover and a sum that includes it reads as
245
+ // nonsense.
246
+ const debtors = readDeclaredPart(report, assetsDecl.debtors);
247
+ const extra = resolveLines(report, assetsDecl.extra);
248
+ const assetsTotalFigure = addFigures(writtenDown, stock, ...extra);
249
+ const assetsSecondLine = resolveSecondLine(report, assetsDecl.secondLine);
250
+ const assetsTotal = assetsSecondLine ? { ...assetsTotalFigure, secondLine: assetsSecondLine } : assetsTotalFigure;
251
+
252
+ const taxFigure = readKey(report, declaration.tax);
253
+ const taxSecondLine = resolveSecondLine(report, declaration.tax.secondLine);
254
+ const tax = taxSecondLine ? { ...taxFigure, secondLine: taxSecondLine } : taxFigure;
255
+ const taxLabel = declaration.tax.label ?? "Tax and NI";
256
+
257
+ const expenseLines = resolveKeyLabelPairs(report, declaration.expenseLines);
258
+ const pieLines = declaration.pieLines ? resolveKeyLabelPairs(report, declaration.pieLines) : undefined;
259
+
260
+ const vehicle = resolveVehicle(report, declaration.vehicle);
261
+
262
+ const tiles = {
263
+ turnover,
264
+ outgoings: { total: outgoingsTotal, costOfSales, runningCosts },
265
+ assets: { total: assetsTotal, writtenDown, stock, debtors },
266
+ tax,
267
+ };
268
+ if (vehicle) tiles.vehicle = vehicle;
269
+
270
+ const pies = {
271
+ turnover: turnoverPie(
272
+ turnoverFigure,
273
+ costOfSales,
274
+ costOfSalesLabel,
275
+ runningCosts,
276
+ runningCostsLabel,
277
+ taxFigure,
278
+ taxLabel,
279
+ turnoverPieExtra,
280
+ ),
281
+ outgoings: outgoingsPie(costOfSales, expenseLines, outgoingsTotal, pieLines),
282
+ };
283
+
284
+ const keys = {
285
+ "headline/turnover": turnoverFigure.value,
286
+ "headline/outgoings": outgoingsTotal.value,
287
+ "headline/assets": assetsTotalFigure.value,
288
+ "headline/tax": taxFigure.value,
289
+ };
290
+ if (vehicle) keys["headline/vehicle-costs"] = vehicle.charged.value;
291
+
292
+ return { tiles, pies, keys };
293
+ }