farseer-cli 1.25.0 → 1.26.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 +1 -1
- package/dist/commands/dashboards.js +2 -0
- package/dist/commands/dashboards.js.map +1 -1
- package/dist/commands/login.js +18 -7
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/logout.js +28 -12
- package/dist/commands/logout.js.map +1 -1
- package/dist/commands/run.js +2 -3
- package/dist/commands/run.js.map +1 -1
- package/dist/commands/whoami.js +12 -5
- package/dist/commands/whoami.js.map +1 -1
- package/dist/mcp/tools/authTools.js +2 -1
- package/dist/mcp/tools/authTools.js.map +1 -1
- package/dist/mcp/tools/dashboardTools.js +3 -1
- package/dist/mcp/tools/dashboardTools.js.map +1 -1
- package/dist/mcp/tools/runTools.js +2 -3
- package/dist/mcp/tools/runTools.js.map +1 -1
- package/dist/mcp/utils/helpers.js +8 -6
- package/dist/mcp/utils/helpers.js.map +2 -2
- package/dist/mcp/utils/session.js +6 -2
- package/dist/mcp/utils/session.js.map +1 -1
- package/dist/operations/dashboards.js +1 -1
- package/dist/operations/dashboards.js.map +1 -1
- package/dist/pipeline/generated-reference.json +8 -8
- package/dist/services/codefirst/layoutReadback.js +95 -0
- package/dist/services/codefirst/layoutReadback.js.map +2 -2
- package/dist/services/configService.js +33 -18
- package/dist/services/configService.js.map +1 -1
- package/dist/services/credentials.js +26 -11
- package/dist/services/credentials.js.map +1 -1
- package/dist/services/farseerService.js +34 -32
- package/dist/services/farseerService.js.map +1 -1
- package/dist/services/keycloak.js +44 -0
- package/dist/services/keycloak.js.map +6 -0
- package/dist/services/preflight.js +1 -2
- package/dist/services/preflight.js.map +1 -1
- package/dist/services/realms.js +67 -15
- package/dist/services/realms.js.map +1 -1
- package/dist/utils/serverUrl.js +42 -0
- package/dist/utils/serverUrl.js.map +6 -0
- package/docs/pipeline-reference.md +13 -3
- package/package.json +1 -1
- package/skills/farseer-code-tiles/SKILL.md +153 -0
- package/skills/farseer-code-tiles/anti-patterns.md +183 -0
- package/skills/farseer-code-tiles/api.md +237 -0
- package/skills/farseer-code-tiles/check-tile.mjs +521 -0
- package/skills/farseer-code-tiles/design.md +159 -0
- package/skills/farseer-code-tiles/echarts-look.md +110 -0
- package/skills/farseer-code-tiles/examples/board-deck.js +695 -0
- package/skills/farseer-code-tiles/examples/data-checks.js +475 -0
- package/skills/farseer-code-tiles/examples/kpi-cards.js +374 -0
- package/skills/farseer-code-tiles/examples/map-countries.js +377 -0
- package/skills/farseer-code-tiles/examples/month-plan.js +443 -0
- package/skills/farseer-code-tiles/examples/new-location.js +581 -0
- package/skills/farseer-code-tiles/examples/open-forecast.js +598 -0
- package/skills/farseer-code-tiles/examples/pl-sankey.js +402 -0
- package/skills/farseer-code-tiles/examples/ranked-bars.js +337 -0
- package/skills/farseer-code-tiles/examples/request-form.js +464 -0
- package/skills/farseer-code-tiles/farseer-table.md +98 -0
- package/skills/farseer-code-tiles/known-mistakes.md +178 -0
- package/skills/farseer-code-tiles/maps/europe.geo.json +1 -0
- package/skills/farseer-code-tiles/maps/pick-map.mjs +59 -0
- package/skills/farseer-code-tiles/maps/world.geo.json +1 -0
- package/skills/farseer-dashboards/SKILL.md +3 -2
- package/skills/farseer-dashboards/check.md +3 -2
- package/skills/farseer-dashboards/measures.md +3 -1
- package/skills/farseer-optimize-model/SKILL.md +9 -1
- package/skills/farseer-pnl/SKILL.md +3 -3
- package/skills/pipeline-authoring/SKILL.md +2 -2
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
// Open a forecast: a new version, filled with one company's P&L input (IMPORT_PnL) for the months picked, copied
|
|
2
|
+
// from another version and changed by a percentage. The source is read whenever the form names a new one, so the click only
|
|
3
|
+
// adds the version, writes the copies in parts cut by size and reads the new version back. Tile size: w36 h12.
|
|
4
|
+
export default function (ctx) {
|
|
5
|
+
// readTable: a table of numbers from the model, in the shape ctx.charts.farseer draws
|
|
6
|
+
// ({ columns: [{ label, numberFormat }], rows: [{ label, cells: [{ value }] }] }). Call it in the body.
|
|
7
|
+
// rows: { table: 'Entity' } every member of a table, listed live by name
|
|
8
|
+
// rows: { table, includeDashboardFilters: true } the members the filter bar leaves, even when the numbers ignore it
|
|
9
|
+
// rows: { table: 'Months', members: ['M1', …] } the members named here, in this order
|
|
10
|
+
// rows: [{ label, formula, numberFormat }] one line per formula (a P&L)
|
|
11
|
+
// no rows one row, labelled '', for KPIs
|
|
12
|
+
// columns: [{ label, formula, numberFormat }] with member rows, [{ label, filter }] with formula lines
|
|
13
|
+
// Member rows and every number follow the filter bar and timeline, unless the spec turns them off.
|
|
14
|
+
const quote = (name) => `"${String(name).replace(/(["\\])/g, '\\$1')}"`;
|
|
15
|
+
// a table's members from a DTABLE answer, by name, without its NULL member
|
|
16
|
+
const namesOf = (answer) => (answer?.value ?? []).map(String).filter((name) => !name.startsWith('NULL ('));
|
|
17
|
+
function withFilter(formula, filter) {
|
|
18
|
+
let out = '', depth = [], quoted = false;
|
|
19
|
+
for (let i = 0; i < formula.length; i++) {
|
|
20
|
+
const c = formula[i];
|
|
21
|
+
if (c === '"' && formula[i - 1] !== '\\') quoted = !quoted;
|
|
22
|
+
if (!quoted && c === '(') depth.push(/sum\s*$/i.test(formula.slice(0, i)));
|
|
23
|
+
if (!quoted && c === ')' && depth.pop()) out += `, ${filter}`;
|
|
24
|
+
out += c;
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function readTable(spec) {
|
|
29
|
+
// includeDashboardFilters: false / includeDashboardTimeline: false in the spec leave one out (say so on the tile)
|
|
30
|
+
const options = { includeDashboardFilters: spec.includeDashboardFilters !== false, includeDashboardTimeline: spec.includeDashboardTimeline !== false };
|
|
31
|
+
const { columns } = spec;
|
|
32
|
+
const rows = spec.rows ?? [{ label: '' }];
|
|
33
|
+
const memberRows = !Array.isArray(rows);
|
|
34
|
+
const listOptions = { ...options, ...(memberRows && rows.includeDashboardFilters !== undefined ? { includeDashboardFilters: rows.includeDashboardFilters } : {}) };
|
|
35
|
+
const list = memberRows && !rows.members ? ctx.query([{ type: 'DTABLE', table: rows.table }], listOptions) : null;
|
|
36
|
+
let names = memberRows ? rows.members ?? null : null;
|
|
37
|
+
let listed = null;
|
|
38
|
+
let values = null;
|
|
39
|
+
const open = () => {
|
|
40
|
+
values?.close();
|
|
41
|
+
const formulas = memberRows
|
|
42
|
+
? names.flatMap((name) => columns.map((c) => withFilter(c.formula, `${quote(rows.table)}>${quote(name)}`)))
|
|
43
|
+
: rows.flatMap((r) => columns.map((c) => (r.formula ? withFilter(r.formula, c.filter) : c.formula)));
|
|
44
|
+
values = formulas.length ? ctx.query(formulas.map((formula) => ({ type: 'VARIABLE', formula })), options) : null;
|
|
45
|
+
};
|
|
46
|
+
if (!list) open();
|
|
47
|
+
const refresh = () => {
|
|
48
|
+
if (list.loading && !list.current.length) return;
|
|
49
|
+
const now = namesOf(list.current[0]);
|
|
50
|
+
const key = now.join('\u0001');
|
|
51
|
+
if (key !== listed) {
|
|
52
|
+
listed = key;
|
|
53
|
+
names = now;
|
|
54
|
+
open();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
get loading() {
|
|
59
|
+
if (list) refresh();
|
|
60
|
+
return (list?.loading ?? false) || (values?.loading ?? false);
|
|
61
|
+
},
|
|
62
|
+
get error() {
|
|
63
|
+
return list?.error ?? values?.error ?? null;
|
|
64
|
+
},
|
|
65
|
+
get current() {
|
|
66
|
+
if (list) refresh();
|
|
67
|
+
const cells = values?.current ?? [];
|
|
68
|
+
const labels = memberRows ? names ?? [] : rows.map((r) => r.label);
|
|
69
|
+
return {
|
|
70
|
+
columns: columns.map((c) => ({ label: c.label, numberFormat: c.numberFormat ?? (memberRows ? null : rows[0]?.numberFormat) ?? null })),
|
|
71
|
+
rows: labels.map((label, j) => ({
|
|
72
|
+
label,
|
|
73
|
+
numberFormat: memberRows ? null : rows[j].numberFormat ?? null,
|
|
74
|
+
cells: columns.map((_, i) => ({ value: cells[j * columns.length + i]?.value ?? null })),
|
|
75
|
+
})),
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// picks(['Entity', 'Year to date']): what the filter bar picked on these tables, for a title or a form.
|
|
81
|
+
// current.Entity is { members: ['Acme Croatia'], all: false }; all is true when the bar doesn't narrow that table.
|
|
82
|
+
function picks(tables) {
|
|
83
|
+
const picked = ctx.query(tables.map((table) => ({ type: 'DTABLE', table })));
|
|
84
|
+
const every = ctx.query(tables.map((table) => ({ type: 'DTABLE', table })), { includeDashboardFilters: false, includeDashboardTimeline: false });
|
|
85
|
+
return {
|
|
86
|
+
get loading() {
|
|
87
|
+
return picked.loading || every.loading;
|
|
88
|
+
},
|
|
89
|
+
get error() {
|
|
90
|
+
return picked.error ?? every.error ?? null;
|
|
91
|
+
},
|
|
92
|
+
get current() {
|
|
93
|
+
return Object.fromEntries(tables.map((table, i) => {
|
|
94
|
+
const members = namesOf(picked.current[i]);
|
|
95
|
+
return [table, { members, all: members.length === namesOf(every.current[i]).length }];
|
|
96
|
+
}));
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
// --- Open a forecast: a new member of Versions, filled with one company's P&L input (IMPORT_PnL) for the months
|
|
101
|
+
// picked, copied from a version and changed by a percentage. Year 2026.
|
|
102
|
+
const YEAR = 'Y2026';
|
|
103
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
104
|
+
const lists = ctx.query(
|
|
105
|
+
[{ type: 'DTABLE', table: 'Versions' }, { type: 'DTABLE', table: 'Entity' }, { type: 'DTABLE', table: 'P&L account' }, { type: 'DTABLE', table: 'Cost centre' }],
|
|
106
|
+
{ includeDashboardFilters: false, includeDashboardTimeline: false },
|
|
107
|
+
);
|
|
108
|
+
const ANY = { includeDashboardFilters: false, includeDashboardTimeline: false };
|
|
109
|
+
const cell = (version, f, account, centre, month) =>
|
|
110
|
+
`sum("IMPORT_PnL", "Versions">${quote(version)}, "Years">"${YEAR}", "Months">"${month}", "Entity">${quote(f.entity)}, "Cost centre">${quote(centre)}, "P&L account">${quote(account)})`;
|
|
111
|
+
const monthTotal = (version, f, month) => `sum("IMPORT_PnL", "Versions">${quote(version)}, "Years">"${YEAR}", "Months">"${month}", "Entity">${quote(f.entity)})`;
|
|
112
|
+
// A message between the tile and Farseer holds at most 256 KB, and one it can't hold gets no answer at all. So reads
|
|
113
|
+
// and writes go in parts cut by size, about 200 KB of JSON each, never by count: long names make long formulas.
|
|
114
|
+
const LIMIT = 200000;
|
|
115
|
+
const MAX_PARTS = 8;
|
|
116
|
+
function parts(queries) {
|
|
117
|
+
const out = [];
|
|
118
|
+
let part = [];
|
|
119
|
+
let size = 2;
|
|
120
|
+
for (const query of queries) {
|
|
121
|
+
const length = JSON.stringify(query).length + 1;
|
|
122
|
+
if (part.length && size + length > LIMIT) {
|
|
123
|
+
out.push(part);
|
|
124
|
+
part = [];
|
|
125
|
+
size = 2;
|
|
126
|
+
}
|
|
127
|
+
part.push(query);
|
|
128
|
+
size += length;
|
|
129
|
+
}
|
|
130
|
+
if (part.length) out.push(part);
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
// the parts are read one after another: a tile holds at most ten reads open at once
|
|
134
|
+
async function readAll(queries) {
|
|
135
|
+
const answers = [];
|
|
136
|
+
for (const part of parts(queries)) answers.push(...(await ctx.read(part, ANY)));
|
|
137
|
+
return answers;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// --- The Farseer look (echarts-look.md, design.md): the same block in every tile ---
|
|
141
|
+
// AC: the dashboards' AC colour (SKILL.md section 2, The AC colour): #323232, or the company's
|
|
142
|
+
// darkest brand colour when it is dark enough, never an example's. AC_LIGHT is its PY tint.
|
|
143
|
+
// Everything else below is derived from them, so nothing else needs changing.
|
|
144
|
+
const AC = '#323232';
|
|
145
|
+
const AC_LIGHT = '#A6A6A6';
|
|
146
|
+
// mixes a colour with white: share 1 is the colour itself, 0 is white
|
|
147
|
+
function tint(hex, share) {
|
|
148
|
+
const n = parseInt(hex.slice(1), 16);
|
|
149
|
+
const mix = (c) => Math.round(c * share + 255 * (1 - share)).toString(16).padStart(2, '0');
|
|
150
|
+
return `#${mix(n >> 16)}${mix((n >> 8) & 255)}${mix(n & 255)}`.toUpperCase();
|
|
151
|
+
}
|
|
152
|
+
// a colour with almost no hue (graphite, grey): its ramp would turn a map grey, so magnitude takes Farseer blue
|
|
153
|
+
const isGrey = (hex) => { const n = parseInt(hex.slice(1), 16); const c = [n >> 16, (n >> 8) & 255, n & 255]; return Math.max(...c) - Math.min(...c) < 24; };
|
|
154
|
+
const PALETTE = [AC, '#ED7D16', '#1BB5D7', '#8A2FE5', '#BFA812', '#E52FCD']; // identity: AC first, then distinct hues
|
|
155
|
+
const FILL = [AC_LIGHT, '#F5B87F', '#82D6E9', '#BF8DF1', '#DCCF7D', '#F18DE4']; // the light step, for large areas
|
|
156
|
+
// magnitude (maps, heatmaps), light to dark: steps of AC, or Farseer blue when AC is a grey
|
|
157
|
+
const RAMP = isGrey(AC) ? ['#81A9FF', '#457DFF', '#0D4BC2', '#043288'] : [0.45, 0.65, 0.8, 1].map((s) => tint(AC, s));
|
|
158
|
+
// light tints of AC for large areas that need ink text (treemap groups), clearly apart from each other
|
|
159
|
+
// (from Farseer blue when AC is a grey, as the ramp: grey tints would make a grey block)
|
|
160
|
+
const TINTS = [0.52, 0.37, 0.24, 0.11].map((s) => tint(isGrey(AC) ? '#043288' : AC, s));
|
|
161
|
+
const TRACK = tint(AC, 0.12); // the unfilled part of a gauge
|
|
162
|
+
const PRIMARY = '#1A66FF'; // Farseer's own primary button, not a data colour
|
|
163
|
+
const GOOD = '#0CB955';
|
|
164
|
+
const BAD = '#E21E09';
|
|
165
|
+
const INK = () => ctx.theme.tokens?.text ?? '#020810';
|
|
166
|
+
const MUTED = () => ctx.theme.tokens?.muted ?? '#687C95';
|
|
167
|
+
const LINE = () => ctx.theme.tokens?.border ?? '#E6EBF2';
|
|
168
|
+
const SURFACE = () => ctx.theme.tokens?.background ?? '#FFFFFF';
|
|
169
|
+
const FONT = 'Inter, system-ui, sans-serif';
|
|
170
|
+
let drawnOnce = false;
|
|
171
|
+
// draws one echarts chart in the look; the first chart that draws ends the entrance animation for the rest
|
|
172
|
+
function drawChart(element, option) {
|
|
173
|
+
ctx.charts.echarts(element, option);
|
|
174
|
+
drawnOnce = true;
|
|
175
|
+
}
|
|
176
|
+
// the chart tile's look, measured from it: square bars, 12 px ink labels with a surface-coloured halo, an ink
|
|
177
|
+
// category axis with ticks, no value axis or gridlines, the legend at the bottom, its tooltip and margins
|
|
178
|
+
const LABEL = { show: true, fontFamily: FONT, fontSize: 12, color: INK(), textBorderColor: SURFACE(), textBorderWidth: 4 };
|
|
179
|
+
function look() {
|
|
180
|
+
return {
|
|
181
|
+
animation: !drawnOnce,
|
|
182
|
+
animationDuration: 500,
|
|
183
|
+
textStyle: { fontFamily: FONT, fontSize: 12, color: INK() },
|
|
184
|
+
grid: { left: 10, right: 10, top: 20, bottom: 10, containLabel: true },
|
|
185
|
+
legend: { bottom: 0, textStyle: { fontFamily: FONT, color: INK() } },
|
|
186
|
+
tooltip: {
|
|
187
|
+
trigger: 'item', confine: true, backgroundColor: SURFACE(), borderColor: '#EDF2F7', borderWidth: 1,
|
|
188
|
+
padding: [6, 10], textStyle: { color: INK(), fontSize: 12, fontFamily: FONT },
|
|
189
|
+
extraCssText: 'border-radius:6px;box-shadow:0 4px 16px rgba(107,131,160,0.15)',
|
|
190
|
+
},
|
|
191
|
+
categoryAxis: {
|
|
192
|
+
axisLine: { show: true, lineStyle: { color: INK(), width: 1 } },
|
|
193
|
+
axisTick: { show: false },
|
|
194
|
+
axisLabel: { color: INK(), fontFamily: FONT, fontSize: 12, hideOverlap: true },
|
|
195
|
+
splitLine: { show: false },
|
|
196
|
+
},
|
|
197
|
+
valueAxis: { show: false, splitLine: { show: false } },
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function merge(base, extra) {
|
|
201
|
+
if (Array.isArray(extra) || typeof extra !== 'object' || extra === null) return extra;
|
|
202
|
+
const out = { ...base };
|
|
203
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
204
|
+
out[key] = typeof value === 'object' && value !== null && !Array.isArray(value) && typeof base?.[key] === 'object' ? merge(base[key], value) : value;
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
// form controls and buttons in Farseer's toolbar look: 28 px high, 12 px text, the focus ring drawn inside the
|
|
209
|
+
// control (so a control at the tile's edge never loses it), button text that never wraps, a long choice ending in …
|
|
210
|
+
// Farseer's own form controls and buttons (the app's input, select and button), the same in every tile
|
|
211
|
+
const CONTROL_CSS = `
|
|
212
|
+
.fs-control { font: 400 12px/20px ${FONT}; height: 32px; padding: 0 8px; border: 1px solid #EDF2F7; border-radius: 8px;
|
|
213
|
+
background-color: #EDF2F7; color: var(--code-tile-text); min-width: 0; max-width: 100%; box-sizing: border-box; outline: none;
|
|
214
|
+
overflow: hidden; white-space: nowrap; text-overflow: ellipsis; transition: border-color 100ms; }
|
|
215
|
+
select.fs-control { appearance: none; -webkit-appearance: none; padding-right: 32px; cursor: pointer;
|
|
216
|
+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='%23020810' fill-rule='evenodd' d='M4.17 6.124a.5.5 0 01.706.047L8 9.74l3.124-3.57a.5.5 0 01.752.658l-3.5 4a.5.5 0 01-.752 0l-3.5-4a.5.5 0 01.047-.705z'/%3E%3C/svg%3E");
|
|
217
|
+
background-repeat: no-repeat; background-position: right 8px center; background-size: 16px 16px; }
|
|
218
|
+
.fs-control:hover { border-color: #C1CEDE; }
|
|
219
|
+
.fs-control:focus, .fs-control:focus-visible { border-color: #1966FF; outline: 1px solid #1966FF; outline-offset: 0; }
|
|
220
|
+
.fs-control.fs-empty, .fs-control::placeholder { color: var(--code-tile-muted); }
|
|
221
|
+
.fs-control:disabled { background-color: var(--code-tile-background); color: var(--code-tile-muted); cursor: not-allowed; }
|
|
222
|
+
.fs-button { font: 500 12px/14px ${FONT}; height: 32px; min-width: 32px; padding: 0 12px; white-space: nowrap; flex: none; border: none;
|
|
223
|
+
border-radius: 8px; cursor: pointer; background-color: #1966FF; color: #FFFFFF; transition: background-color 100ms, color 100ms; }
|
|
224
|
+
.fs-button:hover { background-color: #0942B2; }
|
|
225
|
+
.fs-button.fs-secondary { background-color: #E8F0FF; color: #1966FF; }
|
|
226
|
+
.fs-button.fs-secondary:hover { background-color: #C5D9FF; }
|
|
227
|
+
.fs-button.fs-tertiary { background-color: transparent; color: var(--code-tile-text); font-weight: 400; box-shadow: 0 0 0 1px var(--code-tile-border) inset; }
|
|
228
|
+
.fs-button.fs-tertiary:hover { background-color: #EDF2F7; }
|
|
229
|
+
.fs-button:disabled, .fs-button:disabled:hover { background-color: var(--code-tile-border); color: var(--code-tile-muted); box-shadow: none; cursor: not-allowed; }
|
|
230
|
+
.fs-button:focus-visible { outline: 1px solid var(--code-tile-text); outline-offset: 1px; }
|
|
231
|
+
.fs-button { display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
|
|
232
|
+
.fs-chevron { width: 16px; height: 16px; flex: none; background-color: currentColor; -webkit-mask: center / 16px no-repeat; mask: center / 16px no-repeat; }
|
|
233
|
+
.fs-chevron-left { -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M10.376 4.67a.5.5 0 01-.047.706L6.76 8.5l3.57 3.124a.5.5 0 11-.658.752l-4-3.5a.5.5 0 010-.752l4-3.5a.5.5 0 01.705.047z'/%3E%3C/svg%3E"); mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M10.376 4.67a.5.5 0 01-.047.706L6.76 8.5l3.57 3.124a.5.5 0 11-.658.752l-4-3.5a.5.5 0 010-.752l4-3.5a.5.5 0 01.705.047z'/%3E%3C/svg%3E"); }
|
|
234
|
+
.fs-chevron-right { -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M5.624 12.33a.5.5 0 01.047-.706L9.24 8.5 5.67 5.376a.5.5 0 11.658-.752l4 3.5a.5.5 0 010 .752l-4 3.5a.5.5 0 01-.705-.047z'/%3E%3C/svg%3E"); mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M5.624 12.33a.5.5 0 01.047-.706L9.24 8.5 5.67 5.376a.5.5 0 11.658-.752l4 3.5a.5.5 0 010 .752l-4 3.5a.5.5 0 01-.705-.047z'/%3E%3C/svg%3E"); }
|
|
235
|
+
.fs-button.fs-icon-only { padding: 0; width: 32px; }`;
|
|
236
|
+
function control(tag, className = 'fs-control') {
|
|
237
|
+
if (!document.getElementById('fs-control-style')) {
|
|
238
|
+
const style = document.createElement('style');
|
|
239
|
+
style.id = 'fs-control-style';
|
|
240
|
+
style.textContent = CONTROL_CSS;
|
|
241
|
+
document.head.append(style);
|
|
242
|
+
}
|
|
243
|
+
const node = document.createElement(tag);
|
|
244
|
+
node.className = className;
|
|
245
|
+
return node;
|
|
246
|
+
}
|
|
247
|
+
// Farseer's table look (farseer-table.md): the same font, row height, header and rules as a table tile
|
|
248
|
+
const TABLE_CSS = `
|
|
249
|
+
.fs-table-wrap { width: fit-content; max-width: 100%; overflow: hidden; }
|
|
250
|
+
.fs-table { width: max-content; max-width: calc(100% + 12px); border-collapse: separate; border-spacing: 12px 0; margin: 0 0 0 -12px; font: 12px/1.2 ${FONT}; color: var(--code-tile-text); font-variant-numeric: tabular-nums; }
|
|
251
|
+
.fs-table th { height: 24px; padding: 0 0 5px; font-weight: 400; text-align: right; vertical-align: bottom; white-space: nowrap; background: linear-gradient(var(--code-tile-text), var(--code-tile-text)) bottom / 100% 1px no-repeat; }
|
|
252
|
+
.fs-table td { height: 24px; padding: 0; text-align: right; white-space: nowrap; background: linear-gradient(var(--code-tile-border), var(--code-tile-border)) bottom / 100% 1px no-repeat; }
|
|
253
|
+
.fs-table th:first-child, .fs-table td:first-child { text-align: left; }
|
|
254
|
+
.fs-table td:first-child { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
|
|
255
|
+
.fs-table tr.fs-total td { font-weight: 700; background-image: linear-gradient(var(--code-tile-text), var(--code-tile-text)); }
|
|
256
|
+
.fs-table tbody tr:hover td { background-color: color-mix(in srgb, var(--code-tile-muted) 8%, transparent); }
|
|
257
|
+
.fs-table .fs-good { color: ${GOOD}; } .fs-table .fs-bad { color: ${BAD}; } .fs-table .fs-muted { color: var(--code-tile-muted); }
|
|
258
|
+
.fs-table .fs-key { display: inline-block; width: 8px; height: 8px; margin-right: 8px; vertical-align: 0; }
|
|
259
|
+
.fs-table th.fs-AC, .fs-table tr:last-child td.fs-AC { background: linear-gradient(${AC}, ${AC}) bottom / 100% 3px no-repeat; }
|
|
260
|
+
.fs-table th.fs-PY, .fs-table tr:last-child td.fs-PY { background: linear-gradient(${AC_LIGHT}, ${AC_LIGHT}) bottom / 100% 3px no-repeat; }
|
|
261
|
+
.fs-table th.fs-PL, .fs-table tr:last-child td.fs-PL { background: linear-gradient(${AC}, ${AC}) bottom / 100% 1px no-repeat, linear-gradient(${AC}, ${AC}) 0 calc(100% - 3px) / 100% 1px no-repeat; }
|
|
262
|
+
.fs-table th.fs-FC, .fs-table tr:last-child td.fs-FC { background: repeating-linear-gradient(90deg, ${AC} 0 3px, transparent 3px 5px) bottom / 100% 3px no-repeat; }`;
|
|
263
|
+
function farseerTable(head, rows) {
|
|
264
|
+
// head: [{ label, key?, scenario?: 'AC' | 'PL' | 'PY' | 'FC' }] (a scenario column gets the table tile's rule
|
|
265
|
+
// under its header and its last row: AC thick, PL double, PY light, FC dotted);
|
|
266
|
+
// rows: [{ cells: [text or node, …], total?, tone?: [null | 'good' | 'bad' | 'muted', …] }]
|
|
267
|
+
if (!document.getElementById('fs-table-style')) {
|
|
268
|
+
const style = document.createElement('style');
|
|
269
|
+
style.id = 'fs-table-style';
|
|
270
|
+
style.textContent = TABLE_CSS;
|
|
271
|
+
document.head.append(style);
|
|
272
|
+
}
|
|
273
|
+
const table = el('table');
|
|
274
|
+
table.className = 'fs-table';
|
|
275
|
+
const tr = table.createTHead().insertRow();
|
|
276
|
+
for (const h of head) {
|
|
277
|
+
const th = el('th');
|
|
278
|
+
if (h.scenario) th.className = `fs-${h.scenario}`;
|
|
279
|
+
if (h.key) th.append(h.key);
|
|
280
|
+
th.append(h.label);
|
|
281
|
+
tr.append(th);
|
|
282
|
+
}
|
|
283
|
+
const body = table.createTBody();
|
|
284
|
+
for (const row of rows) {
|
|
285
|
+
const line = body.insertRow();
|
|
286
|
+
if (row.total) line.className = 'fs-total';
|
|
287
|
+
row.cells.forEach((cell, i) => {
|
|
288
|
+
const td = line.insertCell();
|
|
289
|
+
const classes = [row.tone?.[i] ? `fs-${row.tone[i]}` : '', head[i]?.scenario ? `fs-${head[i].scenario}` : ''].filter(Boolean);
|
|
290
|
+
if (classes.length) td.className = classes.join(' ');
|
|
291
|
+
if (cell instanceof Node) td.append(cell);
|
|
292
|
+
else td.textContent = cell ?? '';
|
|
293
|
+
if (i === 0 && typeof cell === 'string') td.title = cell;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
// the first column gap reaches 12 px past the left edge, so the text lines up with the title; a clipping box keeps it inside the tile
|
|
297
|
+
const box = el('div');
|
|
298
|
+
box.className = 'fs-table-wrap';
|
|
299
|
+
box.append(table);
|
|
300
|
+
return box;
|
|
301
|
+
}
|
|
302
|
+
// a variance number in a table, as the table tile shows it: red when it hurts, ink when it helps
|
|
303
|
+
const tone = (delta, costLine) => (delta === null || delta === undefined ? 'muted' : (costLine ? delta > 0 : delta < 0) ? 'bad' : null);
|
|
304
|
+
// small HTML helpers for titles, notes and states, all in the theme's ink
|
|
305
|
+
function el(tag, css, text) {
|
|
306
|
+
const node = document.createElement(tag);
|
|
307
|
+
if (css) node.style.cssText = css;
|
|
308
|
+
if (text !== undefined) node.textContent = text;
|
|
309
|
+
return node;
|
|
310
|
+
}
|
|
311
|
+
// the tile header exactly as Farseer draws a chart or table tile's name: 12 px ink, regular, 10 px above the content;
|
|
312
|
+
// a note under it in the footnote style (11 px muted). The frame already sits inside the card's 8 px padding, so the
|
|
313
|
+
// tile's own content has no outer padding of its own
|
|
314
|
+
const TITLE = 'font-size:12px;font-weight:400;line-height:1.25;color:var(--code-tile-text)';
|
|
315
|
+
const NOTE = 'font-size:11px;line-height:1.35;color:var(--code-tile-muted)'; // footnotes and units only
|
|
316
|
+
const PAGE = 'display:grid;gap:10px;height:100%;padding:0;align-content:start';
|
|
317
|
+
const MESSAGE = 'display:grid;place-items:center;height:100%;font-size:13px;color:var(--code-tile-muted)';
|
|
318
|
+
// one title line, the way Farseer names tiles ("Net sales by region, year to date (kEUR)"); the scope only when the
|
|
319
|
+
// filter bar narrows it. A note the reader needs goes to the bottom in the footnote style, never under the title
|
|
320
|
+
// a value that rounds to zero carries no sign: "-0,0%" and "+0" become "0,0%" and "0"
|
|
321
|
+
const unsigned0 = (text) => String(text).replace(/^[+\-−](?=0(?:[.,]0+)?(?:\s?%|\s?pp)?$)/, '');
|
|
322
|
+
function heading(wrap, title) {
|
|
323
|
+
wrap.append(el('div', TITLE, title));
|
|
324
|
+
}
|
|
325
|
+
function footnote(wrap, text) {
|
|
326
|
+
wrap.append(el('div', `${NOTE};padding-top:2px`, text));
|
|
327
|
+
}
|
|
328
|
+
// --- end of the look ---
|
|
329
|
+
const form = ctx.snapshot('form', { name: '', source: '', entity: '', from: '', to: '', change: '0' });
|
|
330
|
+
// versions this tile added and hasn't filled yet (the write failed or the viewer left): the next click fills them
|
|
331
|
+
const unfilled = ctx.state('unfilled', []);
|
|
332
|
+
// prepared: what the click wrote, { f, factor, count, rows: [{ month, source, written }] }
|
|
333
|
+
let prepared = null;
|
|
334
|
+
// the run: { step: 'adding' | 'writing' | 'checking' | 'done' | 'failed', … }
|
|
335
|
+
let run = null;
|
|
336
|
+
let refresh = () => {};
|
|
337
|
+
let timer = null;
|
|
338
|
+
ctx.onCleanup(() => clearTimeout(timer));
|
|
339
|
+
function parseNumber(text) {
|
|
340
|
+
const parts = new Intl.NumberFormat(ctx.theme.numberLocale || 'en-US').formatToParts(12345.6);
|
|
341
|
+
const group = parts.find((p) => p.type === 'group')?.value ?? ',';
|
|
342
|
+
const decimal = parts.find((p) => p.type === 'decimal')?.value ?? '.';
|
|
343
|
+
const clean = String(text).trim().split(group).join('').replace(/\s/g, '').replace(decimal, '.');
|
|
344
|
+
return clean === '' ? null : Number(clean);
|
|
345
|
+
}
|
|
346
|
+
const versions = () => namesOf(lists.current[0]);
|
|
347
|
+
const isUnfilled = (name) => unfilled.value.includes(name.trim());
|
|
348
|
+
const monthRange = (f) => {
|
|
349
|
+
const a = Number(f.from.slice(1)), b = Number(f.to.slice(1));
|
|
350
|
+
return Array.from({ length: b - a + 1 }, (_, i) => `M${a + i}`);
|
|
351
|
+
};
|
|
352
|
+
function problem(f) {
|
|
353
|
+
if (!f.name.trim()) return 'Name the new version';
|
|
354
|
+
if (versions().some((v) => v.toLowerCase() === f.name.trim().toLowerCase()) && !isUnfilled(f.name)) return 'A version with this name exists';
|
|
355
|
+
if (!f.source) return 'Choose the version to copy from';
|
|
356
|
+
if (!f.entity) return 'Choose the company';
|
|
357
|
+
if (!f.from || !f.to) return 'Choose the months';
|
|
358
|
+
if (Number(f.from.slice(1)) > Number(f.to.slice(1))) return 'The first month is after the last';
|
|
359
|
+
const change = parseNumber(f.change);
|
|
360
|
+
if (change === null || !Number.isFinite(change) || change <= -100) return 'Enter the change in %, e.g. 5 or -3';
|
|
361
|
+
if (!source || source.loading) return `Reading ${f.source} of ${f.entity}…`;
|
|
362
|
+
if (source.failed) return source.failed;
|
|
363
|
+
if (!source.leaves.length) return `${f.source} holds no P&L input for ${f.entity} in those months`;
|
|
364
|
+
// writes have their own budget: a burst of ten, then one a second, and the click's right to write lasts about
|
|
365
|
+
// 5 s; adding the member takes one, so one click carries at most eight parts
|
|
366
|
+
const count = parts(source.leaves.map((l) => ({ type: 'VARIABLE', formula: cell(f.name.trim(), f, l.account, l.centre, l.month), value: l.value }))).length;
|
|
367
|
+
if (count > MAX_PARTS) return `Too many numbers for one click (${count} parts): pick fewer months or one company`;
|
|
368
|
+
return '';
|
|
369
|
+
}
|
|
370
|
+
// The source, read whenever the form names a new one, so the click has only to add and write: the click's right
|
|
371
|
+
// to write lasts a few seconds, and reading a large company first would spend them. It reads the company's totals
|
|
372
|
+
// by month for the table, the year per account and cost centre to find the pairs that hold numbers, and those
|
|
373
|
+
// pairs month by month.
|
|
374
|
+
let source = null; // { key, loading, rows: [{ month, value }], leaves: [{ account, centre, month, value }], failed }
|
|
375
|
+
function loadSource() {
|
|
376
|
+
const f = form.value;
|
|
377
|
+
if (!f.source || !f.entity || !f.from || !f.to || Number(f.from.slice(1)) > Number(f.to.slice(1))) {
|
|
378
|
+
source = null;
|
|
379
|
+
return ctx.render();
|
|
380
|
+
}
|
|
381
|
+
const key = [f.source, f.entity, f.from, f.to].join('|');
|
|
382
|
+
if (source?.key === key) return;
|
|
383
|
+
const accounts = namesOf(lists.current[2]);
|
|
384
|
+
const centres = namesOf(lists.current[3]);
|
|
385
|
+
if (!accounts.length || !centres.length) return; // the lists aren't in yet: the draw asks again
|
|
386
|
+
const months = monthRange(f);
|
|
387
|
+
source = { key, loading: true, rows: [], leaves: [] };
|
|
388
|
+
const pairs = accounts.flatMap((account) => centres.map((centre) => ({ account, centre })));
|
|
389
|
+
const yearOf = (p) => `sum("IMPORT_PnL", "Versions">${quote(f.source)}, "Years">"${YEAR}", "Entity">${quote(f.entity)}, "Cost centre">${quote(p.centre)}, "P&L account">${quote(p.account)})`;
|
|
390
|
+
const read = async () => {
|
|
391
|
+
const totals = await readAll(months.map((m) => ({ type: 'VARIABLE', formula: monthTotal(f.source, f, m) })));
|
|
392
|
+
const years = await readAll(pairs.map((p) => ({ type: 'VARIABLE', formula: yearOf(p) })));
|
|
393
|
+
const used = pairs.filter((_, i) => years[i]?.value);
|
|
394
|
+
const leaves = used.flatMap((p) => months.map((month) => ({ ...p, month })));
|
|
395
|
+
const values = await readAll(leaves.map((l) => ({ type: 'VARIABLE', formula: cell(f.source, f, l.account, l.centre, l.month) })));
|
|
396
|
+
return {
|
|
397
|
+
rows: months.map((month, i) => ({ month, value: totals[i]?.value ?? null })),
|
|
398
|
+
leaves: leaves.map((l, i) => ({ ...l, value: values[i]?.value ?? null })).filter((l) => l.value !== null && l.value !== 0),
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
read().then(
|
|
402
|
+
(answer) => {
|
|
403
|
+
if (source?.key === key) source = { key, ...answer };
|
|
404
|
+
ctx.render();
|
|
405
|
+
},
|
|
406
|
+
(failure) => {
|
|
407
|
+
if (source?.key === key) source = { key, rows: [], leaves: [], failed: `The source could not be read: ${failure.message}` };
|
|
408
|
+
ctx.render();
|
|
409
|
+
},
|
|
410
|
+
);
|
|
411
|
+
ctx.render();
|
|
412
|
+
}
|
|
413
|
+
timer = setTimeout(loadSource, 0);
|
|
414
|
+
// The click: add the version (unless this tile added it and couldn't fill it), then write the copies part by part,
|
|
415
|
+
// one part per tick so each goes as its own request, then read the new totals back from the model.
|
|
416
|
+
function open() {
|
|
417
|
+
const f = { ...form.value, name: form.value.name.trim() };
|
|
418
|
+
const factor = 1 + parseNumber(f.change) / 100;
|
|
419
|
+
const months = monthRange(f);
|
|
420
|
+
const cells = source.leaves.map((l) => ({ type: 'VARIABLE', formula: cell(f.name, f, l.account, l.centre, l.month), value: Math.round(l.value * factor * 100) / 100 }));
|
|
421
|
+
prepared = { f, factor, count: cells.length, rows: source.rows.map((r) => ({ month: r.month, source: r.value })) };
|
|
422
|
+
const handles = [];
|
|
423
|
+
const writeParts = () => {
|
|
424
|
+
run = { step: 'writing' };
|
|
425
|
+
const queue = parts(cells);
|
|
426
|
+
const next = () => {
|
|
427
|
+
handles.push(ctx.write(queue.shift(), ANY));
|
|
428
|
+
timer = queue.length ? setTimeout(next, 0) : setTimeout(check, 200);
|
|
429
|
+
};
|
|
430
|
+
next();
|
|
431
|
+
};
|
|
432
|
+
const check = () => {
|
|
433
|
+
if (handles.some((h) => h.loading)) return void (timer = setTimeout(check, 200));
|
|
434
|
+
const failed = handles.find((h) => h.error);
|
|
435
|
+
if (failed) {
|
|
436
|
+
run = { step: 'failed', message: `${f.name} was added, but not all its numbers were written: ${failed.error.message}. Click Open forecast again to fill it.` };
|
|
437
|
+
return ctx.render();
|
|
438
|
+
}
|
|
439
|
+
unfilled.set(unfilled.value.filter((name) => name !== f.name));
|
|
440
|
+
run = { step: 'checking' };
|
|
441
|
+
ctx.render();
|
|
442
|
+
ctx.read(months.map((m) => ({ type: 'VARIABLE', formula: monthTotal(f.name, f, m) })), ANY).then(
|
|
443
|
+
(answer) => {
|
|
444
|
+
prepared.rows = prepared.rows.map((r, i) => ({ ...r, written: answer[i]?.value ?? null }));
|
|
445
|
+
run = { step: 'done' };
|
|
446
|
+
ctx.render();
|
|
447
|
+
},
|
|
448
|
+
(failure) => {
|
|
449
|
+
run = { step: 'failed', message: `Written, but the new version could not be read back: ${failure.message}` };
|
|
450
|
+
ctx.render();
|
|
451
|
+
},
|
|
452
|
+
);
|
|
453
|
+
};
|
|
454
|
+
if (isUnfilled(f.name)) {
|
|
455
|
+
writeParts();
|
|
456
|
+
return ctx.render();
|
|
457
|
+
}
|
|
458
|
+
run = { step: 'adding', member: ctx.addMembers('Versions', [{ name: f.name }]) };
|
|
459
|
+
let tries = 0;
|
|
460
|
+
const whenAdded = () => {
|
|
461
|
+
if (run.member.loading && ++tries < 40) return void (timer = setTimeout(whenAdded, 100));
|
|
462
|
+
if (run.member.error || run.member.loading) {
|
|
463
|
+
run = { step: 'failed', message: `The version was not added: ${run.member.error?.message ?? 'no answer'}` };
|
|
464
|
+
return ctx.render();
|
|
465
|
+
}
|
|
466
|
+
// remembered until its numbers are written, so a failed write can be finished by the next click
|
|
467
|
+
unfilled.set([...unfilled.value, f.name]);
|
|
468
|
+
writeParts();
|
|
469
|
+
};
|
|
470
|
+
timer = setTimeout(whenAdded, 100);
|
|
471
|
+
ctx.render();
|
|
472
|
+
}
|
|
473
|
+
function said(f) {
|
|
474
|
+
if (run?.step === 'adding') return `Adding ${prepared.f.name}…`;
|
|
475
|
+
if (run?.step === 'writing') return `Writing ${ctx.format(prepared.count, '#,##0')} numbers…`;
|
|
476
|
+
if (run?.step === 'checking') return 'Reading the new version back…';
|
|
477
|
+
if (run?.step === 'failed') return run.message;
|
|
478
|
+
if (run?.step === 'done') return `${prepared.f.name} is open: ${ctx.format(prepared.count, '#,##0')} numbers of ${prepared.f.entity}, copied from ${prepared.f.source} ${changeText(prepared.factor)}.`;
|
|
479
|
+
const wrong = problem(f);
|
|
480
|
+
if (wrong) return wrong;
|
|
481
|
+
const what = `${ctx.format(source.leaves.length, '#,##0')} numbers ${changeText(1 + parseNumber(f.change) / 100)}`;
|
|
482
|
+
return isUnfilled(f.name) ? `Open forecast fills ${f.name.trim()}, added earlier and still empty, with ${what}.` : `Open forecast adds ${f.name.trim()} and copies ${what}.`;
|
|
483
|
+
}
|
|
484
|
+
const changeText = (factor) => (factor === 1 ? 'unchanged' : `${factor > 1 ? '+' : '−'}${ctx.format(Math.abs(factor - 1) * 100, Number.isInteger(Math.round(Math.abs(factor - 1) * 1e4) / 100) ? '0' : '0.0#')}%`);
|
|
485
|
+
|
|
486
|
+
const FORM_CSS = `
|
|
487
|
+
.fs-form { display: grid; grid-template-columns: 1.6fr 1.2fr 1.3fr 0.9fr 0.9fr 0.8fr; gap: 8px 12px; align-items: end; }
|
|
488
|
+
@media (max-width: 760px) { .fs-form { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); } }
|
|
489
|
+
.fs-form label { display: grid; gap: 4px; font-size: 12px; font-weight: 400; color: var(--code-tile-text); min-width: 0; }
|
|
490
|
+
.fs-form .fs-control { width: 100%; }
|
|
491
|
+
.fs-actions { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 12px; }
|
|
492
|
+
.fs-buttons { display: flex; gap: 8px; }`;
|
|
493
|
+
function style() {
|
|
494
|
+
if (document.getElementById('fs-form-style')) return;
|
|
495
|
+
const node = document.createElement('style');
|
|
496
|
+
node.id = 'fs-form-style';
|
|
497
|
+
node.textContent = FORM_CSS;
|
|
498
|
+
document.head.append(node);
|
|
499
|
+
}
|
|
500
|
+
function field(label, node) {
|
|
501
|
+
const box = el('label');
|
|
502
|
+
box.append(label, node);
|
|
503
|
+
return box;
|
|
504
|
+
}
|
|
505
|
+
function select(options, key, prompt) {
|
|
506
|
+
const s = control('select');
|
|
507
|
+
s.add(new Option(prompt, ''));
|
|
508
|
+
for (const [value, text] of options) s.add(new Option(text, value, false, value === form.value[key]));
|
|
509
|
+
s.classList.toggle('fs-empty', !form.value[key]);
|
|
510
|
+
s.onchange = () => {
|
|
511
|
+
form.set({ ...form.value, [key]: s.value });
|
|
512
|
+
if (run?.step === 'done' || run?.step === 'failed') run = null;
|
|
513
|
+
loadSource();
|
|
514
|
+
ctx.render();
|
|
515
|
+
};
|
|
516
|
+
return s;
|
|
517
|
+
}
|
|
518
|
+
function input(key, placeholder, mode) {
|
|
519
|
+
const i = control('input');
|
|
520
|
+
i.value = form.value[key];
|
|
521
|
+
i.placeholder = placeholder;
|
|
522
|
+
if (mode) i.inputMode = mode;
|
|
523
|
+
i.oninput = () => {
|
|
524
|
+
form.set({ ...form.value, [key]: i.value });
|
|
525
|
+
if (run?.step === 'done' || run?.step === 'failed') run = null;
|
|
526
|
+
refresh();
|
|
527
|
+
};
|
|
528
|
+
return i;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return (element) => {
|
|
532
|
+
style();
|
|
533
|
+
ctx.loading(lists.loading && !lists.current.length);
|
|
534
|
+
const wrap = el('div', PAGE);
|
|
535
|
+
heading(wrap, 'Open a forecast, 2026 (EUR)');
|
|
536
|
+
const grid = el('div');
|
|
537
|
+
grid.className = 'fs-form';
|
|
538
|
+
const monthOptions = MONTHS.map((m, i) => [`M${i + 1}`, `${m} 2026`]);
|
|
539
|
+
grid.append(
|
|
540
|
+
field('New version', input('name', 'e.g. FC 9+3')),
|
|
541
|
+
field('Copy from', select(versions().map((v) => [v, v]), 'source', 'Choose a version')),
|
|
542
|
+
field('Company', select(namesOf(lists.current[1]).map((c) => [c, c]), 'entity', 'Choose a company')),
|
|
543
|
+
field('From', select(monthOptions, 'from', 'Month')),
|
|
544
|
+
field('To', select(monthOptions, 'to', 'Month')),
|
|
545
|
+
field('Change (%)', input('change', 'e.g. 5', 'decimal')),
|
|
546
|
+
);
|
|
547
|
+
const busy = Boolean(run && !['done', 'failed'].includes(run.step));
|
|
548
|
+
// the lists arrived after the form named its source: read it now
|
|
549
|
+
if (!source && form.value.source && form.value.entity && form.value.from && form.value.to && namesOf(lists.current[2]).length) timer = setTimeout(loadSource, 0);
|
|
550
|
+
const status = el('span', 'font-size:12px;color:var(--code-tile-muted)');
|
|
551
|
+
status.setAttribute('role', 'status');
|
|
552
|
+
const openButton = control('button', 'fs-button');
|
|
553
|
+
openButton.textContent = 'Open forecast';
|
|
554
|
+
refresh = () => {
|
|
555
|
+
const f = form.value;
|
|
556
|
+
openButton.disabled = busy || Boolean(problem(f)) || run?.step === 'done';
|
|
557
|
+
status.textContent = said(f);
|
|
558
|
+
status.style.color = run?.step === 'done' ? GOOD : run?.step === 'failed' ? BAD : 'var(--code-tile-muted)';
|
|
559
|
+
};
|
|
560
|
+
refresh();
|
|
561
|
+
openButton.onclick = () => {
|
|
562
|
+
if (!problem(form.value)) open();
|
|
563
|
+
};
|
|
564
|
+
const buttons = el('div');
|
|
565
|
+
buttons.className = 'fs-buttons';
|
|
566
|
+
buttons.append(openButton);
|
|
567
|
+
const actions = el('div');
|
|
568
|
+
actions.className = 'fs-actions';
|
|
569
|
+
actions.append(status, buttons);
|
|
570
|
+
wrap.append(grid, actions);
|
|
571
|
+
|
|
572
|
+
// the company's P&L input by month. Before the write only the source; the new version and the change
|
|
573
|
+
// appear once it is written, read back from the model
|
|
574
|
+
const done = run?.step === 'done';
|
|
575
|
+
const f = form.value;
|
|
576
|
+
const rows = done ? prepared.rows : source?.rows?.map((r) => ({ month: r.month, source: r.value })) ?? null;
|
|
577
|
+
if (rows?.length && (done || !source?.loading)) {
|
|
578
|
+
const from = done ? prepared.f : f;
|
|
579
|
+
const head = [{ label: 'Month' }, { label: from.source, scenario: 'PL' }];
|
|
580
|
+
if (done) head.push({ label: from.name, scenario: 'FC' }, { label: `Change ${changeText(prepared.factor)}` });
|
|
581
|
+
wrap.append(
|
|
582
|
+
el('div', TITLE, `${from.entity}, P&L input by month (EUR)`),
|
|
583
|
+
farseerTable(
|
|
584
|
+
head,
|
|
585
|
+
rows.map((r) => ({
|
|
586
|
+
cells: [
|
|
587
|
+
`${MONTHS[Number(r.month.slice(1)) - 1]} 2026`,
|
|
588
|
+
ctx.format(r.source, '#,##0'),
|
|
589
|
+
...(done ? [ctx.format(r.written, '#,##0'), unsigned0(ctx.format(r.written === null || r.source === null ? null : r.written - r.source, '+#,##0;-#,##0;0'))] : []),
|
|
590
|
+
],
|
|
591
|
+
})),
|
|
592
|
+
),
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
footnote(wrap, 'Copies the P&L input of one company at a time. Copying a whole version runs through an App for now.');
|
|
596
|
+
element.replaceChildren(wrap);
|
|
597
|
+
};
|
|
598
|
+
}
|