thatcher 1.0.88 → 1.0.89

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thatcher",
3
- "version": "1.0.88",
3
+ "version": "1.0.89",
4
4
  "description": "A config-driven application framework for building data-intensive web apps without code.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -9,7 +9,7 @@ import { renderEngagementGrid } from '@/ui/engagement-grid-renderer.js';
9
9
  import { renderBoardView } from '@/ui/board-view-renderer.js';
10
10
  import { renderGridView } from '@/ui/grid-view-renderer.js';
11
11
  import { renderCalendarView, renderTimelineView } from '@/ui/calendar-view-renderer.js';
12
- import { renderCountByFieldReport, renderCountOverTimeReport, renderSumByFieldReport, renderRollupReport, renderInventoryForecastReport, renderDemandForecastReport } from '@/ui/report-renderer.js';
12
+ import { renderCountByFieldReport, renderCountOverTimeReport, renderSumByFieldReport, renderRollupReport, renderInventoryForecastReport, renderDemandForecastReport, renderPivotReport } from '@/ui/report-renderer.js';
13
13
  import { renderClientProgress } from '@/ui/client-progress-renderer.js';
14
14
  import { renderLetterWorkflow } from '@/ui/letter-workflow-renderer.js';
15
15
  import { renderAdvancedSearch } from '@/ui/advanced-search-renderer.js';
@@ -370,6 +370,13 @@ export async function handlePage(pathname, req, res) {
370
370
  const groupBy = params.get('group_by') || '';
371
371
  return renderSumByFieldReport(user, entityName, spec, items, field, groupBy);
372
372
  }
373
+ if (report === 'pivot') {
374
+ const rowField = params.get('row_field') || '';
375
+ const colField = params.get('col_field') || '';
376
+ const valueField = params.get('value_field') || '';
377
+ const agg = params.get('agg') || 'count';
378
+ return renderPivotReport(user, entityName, spec, items, rowField, colField, valueField, agg);
379
+ }
373
380
  if (report === 'rollup') {
374
381
  const refField = params.get('ref_field') || '';
375
382
  const rollupField = params.get('rollup_field') || '';
@@ -1,259 +1,345 @@
1
- import { page } from '@/ui/layout.js';
2
- import { esc } from '@/ui/render-helpers.js';
3
- import { emptyState } from '@/ui/format-helpers.js';
4
- import { getEntityLabel } from '@/config/spec-helpers.js';
5
-
6
- const MAX_BUCKETS = 15;
7
-
8
- function bucketLabel(spec, field, rawValue) {
9
- if (rawValue === null || rawValue === undefined || rawValue === '') return '(empty)';
10
- const fieldDef = spec.fields?.[field];
11
- if (fieldDef?.type === 'enum' && Array.isArray(fieldDef.options)) {
12
- const opt = fieldDef.options.find(o => String(o.value ?? o) === String(rawValue));
13
- return opt ? String(opt.label || opt.value || opt) : String(rawValue);
14
- }
15
- return String(rawValue);
16
- }
17
-
18
- export function countByField(records, spec, field) {
19
- const counts = new Map();
20
- for (const r of records) {
21
- const label = bucketLabel(spec, field, r[field]);
22
- counts.set(label, (counts.get(label) || 0) + 1);
23
- }
24
- const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]);
25
- if (sorted.length <= MAX_BUCKETS) return sorted;
26
- const top = sorted.slice(0, MAX_BUCKETS - 1);
27
- const otherCount = sorted.slice(MAX_BUCKETS - 1).reduce((sum, [, c]) => sum + c, 0);
28
- return [...top, ['(other)', otherCount]];
29
- }
30
-
31
- function barChart(buckets) {
32
- if (!buckets.length) return emptyState('No data to report on', 'bar-chart');
33
- const max = Math.max(...buckets.map(([, c]) => c), 1);
34
- const rows = buckets.map(([label, count]) => {
35
- const pct = Math.round((count / max) * 100);
36
- return `<div class="report-bar-row" style="display:flex;align-items:center;gap:12px;margin-bottom:8px">
37
- <div style="min-width:140px;max-width:220px;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="${esc(label)}">${esc(label)}</div>
38
- <div style="flex:1;height:20px;background:var(--color-border,#eee);border-radius:4px;overflow:hidden">
39
- <div style="height:100%;width:${pct}%;background:var(--color-primary,#3b82f6);border-radius:4px"></div>
40
- </div>
41
- <div style="min-width:40px;text-align:right;font-size:13px;font-weight:600">${count}</div>
42
- </div>`;
43
- }).join('');
44
- return `<div class="report-bar-chart">${rows}</div>`;
45
- }
46
-
47
- function bucketDateKey(dateValue, granularity) {
48
- if (dateValue === null || dateValue === undefined || dateValue === '') return null;
49
- const num = Number(dateValue);
50
- const d = !isNaN(num) && num > 0 ? new Date(num * 1000) : new Date(dateValue);
51
- if (isNaN(d.getTime())) return null;
52
- const y = d.getFullYear();
53
- const m = String(d.getMonth() + 1).padStart(2, '0');
54
- const day = String(d.getDate()).padStart(2, '0');
55
- if (granularity === 'day') return `${y}-${m}-${day}`;
56
- if (granularity === 'week') {
57
- const onejan = new Date(y, 0, 1);
58
- const week = Math.ceil((((d - onejan) / 86400000) + onejan.getDay() + 1) / 7);
59
- return `${y}-W${String(week).padStart(2, '0')}`;
60
- }
61
- return `${y}-${m}`;
62
- }
63
-
64
- export function countOverTime(records, dateField, granularity = 'month') {
65
- const counts = new Map();
66
- let unresolved = 0;
67
- for (const r of records) {
68
- const key = bucketDateKey(r[dateField], granularity);
69
- if (key === null) { unresolved++; continue; }
70
- counts.set(key, (counts.get(key) || 0) + 1);
71
- }
72
- const sorted = [...counts.entries()].sort((a, b) => a[0].localeCompare(b[0]));
73
- return { buckets: sorted.length > MAX_BUCKETS ? sorted.slice(-MAX_BUCKETS) : sorted, unresolved, truncated: sorted.length > MAX_BUCKETS };
74
- }
75
-
76
- export function renderCountByFieldReport(user, entityName, spec, records, field) {
77
- const label = getEntityLabel(spec, true) || entityName;
78
- const fieldDef = spec.fields?.[field];
79
- if (!fieldDef) {
80
- return page(user, `${label} | Report`, null,
81
- `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
82
- <div class="report-empty-state">Unknown field "${esc(field)}" for this entity.</div>`);
83
- }
84
- const buckets = countByField(records, spec, field);
85
- const content = `<div class="page-header">
86
- <div><h1 class="page-title">${esc(label)} by ${esc(fieldDef.label || field)}</h1><p class="page-subtitle">${records.length} total records</p></div>
87
- </div>
88
- ${barChart(buckets)}`;
89
- return page(user, `${label} Report | Thatcher`, null, content);
90
- }
91
-
92
- export function sumByField(records, spec, field, groupBy) {
93
- const sums = new Map();
94
- for (const r of records) {
95
- const raw = r[field];
96
- const num = typeof raw === 'string' ? Number(raw) : raw;
97
- if (typeof num !== 'number' || isNaN(num)) continue;
98
- const label = bucketLabel(spec, groupBy, r[groupBy]);
99
- sums.set(label, (sums.get(label) || 0) + num);
100
- }
101
- const sorted = [...sums.entries()].sort((a, b) => b[1] - a[1]);
102
- if (sorted.length <= MAX_BUCKETS) return sorted;
103
- const top = sorted.slice(0, MAX_BUCKETS - 1);
104
- const otherSum = sorted.slice(MAX_BUCKETS - 1).reduce((sum, [, v]) => sum + v, 0);
105
- return [...top, ['(other)', otherSum]];
106
- }
107
-
108
- function formatSumValue(value, fieldDef) {
109
- if (fieldDef?.type === 'currency') return (fieldDef.currency_symbol || '$') + (value / 100).toFixed(2);
110
- return String(Math.round(value * 100) / 100);
111
- }
112
-
113
- function sumBarChart(buckets, fieldDef) {
114
- if (!buckets.length) return emptyState('No data to report on', 'bar-chart');
115
- const max = Math.max(...buckets.map(([, v]) => Math.abs(v)), 1);
116
- const rows = buckets.map(([label, value]) => {
117
- const pct = Math.round((Math.abs(value) / max) * 100);
118
- return `<div class="report-bar-row" style="display:flex;align-items:center;gap:12px;margin-bottom:8px">
119
- <div style="min-width:140px;max-width:220px;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="${esc(label)}">${esc(label)}</div>
120
- <div style="flex:1;height:20px;background:var(--color-border,#eee);border-radius:4px;overflow:hidden">
121
- <div style="height:100%;width:${pct}%;background:var(--color-primary,#3b82f6);border-radius:4px"></div>
122
- </div>
123
- <div style="min-width:80px;text-align:right;font-size:13px;font-weight:600">${esc(formatSumValue(value, fieldDef))}</div>
124
- </div>`;
125
- }).join('');
126
- return `<div class="report-bar-chart">${rows}</div>`;
127
- }
128
-
129
- export function renderSumByFieldReport(user, entityName, spec, records, field, groupBy) {
130
- const label = getEntityLabel(spec, true) || entityName;
131
- const fieldDef = spec.fields?.[field];
132
- const groupByDef = spec.fields?.[groupBy];
133
- if (!fieldDef || !groupByDef) {
134
- return page(user, `${label} | Report`, null,
135
- `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
136
- <div class="report-empty-state">Unknown field "${esc(!fieldDef ? field : groupBy)}" for this entity.</div>`);
137
- }
138
- const buckets = sumByField(records, spec, field, groupBy);
139
- const content = `<div class="page-header">
140
- <div><h1 class="page-title">${esc(label)}: ${esc(fieldDef.label || field)} by ${esc(groupByDef.label || groupBy)}</h1><p class="page-subtitle">${records.length} total records</p></div>
141
- </div>
142
- ${sumBarChart(buckets, fieldDef)}`;
143
- return page(user, `${label} Report | Thatcher`, null, content);
144
- }
145
-
146
- // Cross-entity rollup: entity A's records are grouped by a field on related
147
- // entity B, joined through A's ref field. relatedRecords must already be the
148
- // CALLER's row/org-scoped list() result for B -- this function only joins
149
- // and counts, it enforces no access itself (the access decision -- can this
150
- // user list B at all -- is made by the caller before relatedRecords exists).
151
- export function rollupByRelatedField(records, refField, relatedRecords, relatedSpec, rollupField) {
152
- const relatedById = new Map(relatedRecords.map(r => [String(r.id), r]));
153
- const counts = new Map();
154
- let unmatched = 0;
155
- for (const r of records) {
156
- const refId = r[refField];
157
- const related = refId != null ? relatedById.get(String(refId)) : null;
158
- if (!related) { unmatched++; continue; }
159
- const label = bucketLabel(relatedSpec, rollupField, related[rollupField]);
160
- counts.set(label, (counts.get(label) || 0) + 1);
161
- }
162
- const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]);
163
- return { buckets: sorted.length > MAX_BUCKETS ? sorted.slice(0, MAX_BUCKETS) : sorted, unmatched };
164
- }
165
-
166
- export function renderRollupReport(user, entityName, spec, records, refField, relatedEntity, relatedSpec, relatedRecords, rollupField) {
167
- const label = getEntityLabel(spec, true) || entityName;
168
- const relatedLabel = getEntityLabel(relatedSpec, true) || relatedEntity;
169
- const rollupFieldDef = relatedSpec?.fields?.[rollupField];
170
- if (!spec.fields?.[refField] || !rollupFieldDef) {
171
- return page(user, `${label} | Report`, null,
172
- `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
173
- <div class="report-empty-state">Unknown field for this rollup.</div>`);
174
- }
175
- const { buckets, unmatched } = rollupByRelatedField(records, refField, relatedRecords, relatedSpec, rollupField);
176
- const notice = unmatched
177
- ? `<div class="report-notice">${unmatched} record${unmatched === 1 ? '' : 's'} without a resolvable ${esc(relatedLabel)} excluded</div>`
178
- : '';
179
- const content = `<div class="page-header">
180
- <div><h1 class="page-title">${esc(label)} by ${esc(relatedLabel)}.${esc(rollupFieldDef.label || rollupField)}</h1><p class="page-subtitle">${records.length} total records</p></div>
181
- </div>
182
- ${notice}
183
- ${barChart(buckets)}`;
184
- return page(user, `${label} Report | Thatcher`, null, content);
185
- }
186
-
187
- export function renderCountOverTimeReport(user, entityName, spec, records, dateField, granularity) {
188
- const label = getEntityLabel(spec, true) || entityName;
189
- const fieldDef = spec.fields?.[dateField];
190
- if (!fieldDef) {
191
- return page(user, `${label} | Report`, null,
192
- `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
193
- <div class="report-empty-state">Unknown date field "${esc(dateField)}" for this entity.</div>`);
194
- }
195
- const { buckets, unresolved, truncated } = countOverTime(records, dateField, granularity);
196
- const notice = unresolved
197
- ? `<div class="report-notice">${unresolved} record${unresolved === 1 ? '' : 's'} without a resolvable date excluded</div>`
198
- : '';
199
- const truncatedNotice = truncated
200
- ? `<div class="report-notice">Showing the most recent ${buckets.length} periods only</div>`
201
- : '';
202
- const content = `<div class="page-header">
203
- <div><h1 class="page-title">${esc(label)} over time</h1><p class="page-subtitle">${records.length} total records, grouped by ${esc(granularity)}</p></div>
204
- </div>
205
- ${notice}${truncatedNotice}
206
- ${barChart(buckets)}`;
207
- return page(user, `${label} Report | Thatcher`, null, content);
208
- }
209
-
210
- // One table per-product rather than a bar chart: unlike count-by-field/
211
- // sum-by-field's single grouped metric, a forecast is inherently multiple
212
- // values per row (stock, consumption rate, days-out, reorder date) -- a bar
213
- // chart of any single one of those would discard the others, so a table is
214
- // the correct shape here, reusing the same page()/esc() rendering discipline
215
- // as every other report rather than inventing a new chart type.
216
- export function renderInventoryForecastReport(user, spec, forecastRows) {
217
- const label = getEntityLabel(spec, true) || 'Products';
218
- const rows = forecastRows.map(p => {
219
- const daysLabel = p.days_until_stockout == null ? 'Unknown' : String(Math.round(p.days_until_stockout))
220
- const reorderLabel = p.reorder_date == null ? '-' : new Date(p.reorder_date * 1000).toISOString().slice(0, 10)
221
- const rowCls = p.reorder_due ? 'style="background:var(--color-danger-bg,#fee2e2)"' : ''
222
- return `<tr ${rowCls}>
223
- <td>${esc(p.name || p.id)}</td>
224
- <td>${esc(String(p.current_stock))}</td>
225
- <td>${esc((p.avg_daily_consumption || 0).toFixed(2))}</td>
226
- <td>${esc(daysLabel)}</td>
227
- <td>${esc(reorderLabel)}${p.reorder_due ? ' <span class="pill pill-danger">Reorder Now</span>' : ''}</td>
228
- </tr>`;
229
- }).join('') || emptyState('No products to forecast', 'bar-chart');
230
-
231
- const content = `<div class="page-header">
232
- <div><h1 class="page-title">${esc(label)}: Inventory Forecast</h1><p class="page-subtitle">${forecastRows.length} products</p></div>
233
- </div>
234
- <div class="table-wrap"><table class="data-table">
235
- <thead><tr><th>Product</th><th>Current Stock</th><th>Avg Daily Use</th><th>Days Until Stockout</th><th>Suggested Reorder</th></tr></thead>
236
- <tbody>${rows}</tbody>
237
- </table></div>`;
238
- return page(user, `Inventory Forecast | Thatcher`, null, content);
239
- }
240
-
241
- // Reuses sumBarChart (already built for sum-by-field's currency-aware bar
242
- // rendering) rather than inventing a third chart type -- a demand-by-month
243
- // bucket is the exact same [label, numericValue] shape sum-by-field already
244
- // renders, just currency-formatted since weighted_value derives from a
245
- // currency field.
246
- export function renderDemandForecastReport(user, spec, buckets, totalOpportunities) {
247
- const label = getEntityLabel(spec, true) || 'Opportunities';
248
- const valueFieldDef = spec.fields?.value || { type: 'currency' };
249
- const totalProjected = buckets.reduce((sum, [, v]) => sum + v, 0);
250
- const notice = !buckets.length
251
- ? `<div class="report-notice">No open opportunities with a future expected close date</div>`
252
- : '';
253
- const content = `<div class="page-header">
254
- <div><h1 class="page-title">${esc(label)}: Demand Forecast</h1><p class="page-subtitle">${totalOpportunities} total opportunities, ${esc(formatSumValue(totalProjected, valueFieldDef))} projected across ${buckets.length} future month${buckets.length === 1 ? '' : 's'}</p></div>
255
- </div>
256
- ${notice}
257
- ${sumBarChart(buckets, valueFieldDef)}`;
258
- return page(user, `Demand Forecast | Thatcher`, null, content);
259
- }
1
+ import { page } from '@/ui/layout.js';
2
+ import { esc } from '@/ui/render-helpers.js';
3
+ import { emptyState } from '@/ui/format-helpers.js';
4
+ import { getEntityLabel } from '@/config/spec-helpers.js';
5
+
6
+ const MAX_BUCKETS = 15;
7
+
8
+ function bucketLabel(spec, field, rawValue) {
9
+ if (rawValue === null || rawValue === undefined || rawValue === '') return '(empty)';
10
+ const fieldDef = spec.fields?.[field];
11
+ if (fieldDef?.type === 'enum' && Array.isArray(fieldDef.options)) {
12
+ const opt = fieldDef.options.find(o => String(o.value ?? o) === String(rawValue));
13
+ return opt ? String(opt.label || opt.value || opt) : String(rawValue);
14
+ }
15
+ return String(rawValue);
16
+ }
17
+
18
+ export function countByField(records, spec, field) {
19
+ const counts = new Map();
20
+ for (const r of records) {
21
+ const label = bucketLabel(spec, field, r[field]);
22
+ counts.set(label, (counts.get(label) || 0) + 1);
23
+ }
24
+ const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]);
25
+ if (sorted.length <= MAX_BUCKETS) return sorted;
26
+ const top = sorted.slice(0, MAX_BUCKETS - 1);
27
+ const otherCount = sorted.slice(MAX_BUCKETS - 1).reduce((sum, [, c]) => sum + c, 0);
28
+ return [...top, ['(other)', otherCount]];
29
+ }
30
+
31
+ function barChart(buckets) {
32
+ if (!buckets.length) return emptyState('No data to report on', 'bar-chart');
33
+ const max = Math.max(...buckets.map(([, c]) => c), 1);
34
+ const rows = buckets.map(([label, count]) => {
35
+ const pct = Math.round((count / max) * 100);
36
+ return `<div class="report-bar-row" style="display:flex;align-items:center;gap:12px;margin-bottom:8px">
37
+ <div style="min-width:140px;max-width:220px;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="${esc(label)}">${esc(label)}</div>
38
+ <div style="flex:1;height:20px;background:var(--color-border,#eee);border-radius:4px;overflow:hidden">
39
+ <div style="height:100%;width:${pct}%;background:var(--color-primary,#3b82f6);border-radius:4px"></div>
40
+ </div>
41
+ <div style="min-width:40px;text-align:right;font-size:13px;font-weight:600">${count}</div>
42
+ </div>`;
43
+ }).join('');
44
+ return `<div class="report-bar-chart">${rows}</div>`;
45
+ }
46
+
47
+ function bucketDateKey(dateValue, granularity) {
48
+ if (dateValue === null || dateValue === undefined || dateValue === '') return null;
49
+ const num = Number(dateValue);
50
+ const d = !isNaN(num) && num > 0 ? new Date(num * 1000) : new Date(dateValue);
51
+ if (isNaN(d.getTime())) return null;
52
+ const y = d.getFullYear();
53
+ const m = String(d.getMonth() + 1).padStart(2, '0');
54
+ const day = String(d.getDate()).padStart(2, '0');
55
+ if (granularity === 'day') return `${y}-${m}-${day}`;
56
+ if (granularity === 'week') {
57
+ const onejan = new Date(y, 0, 1);
58
+ const week = Math.ceil((((d - onejan) / 86400000) + onejan.getDay() + 1) / 7);
59
+ return `${y}-W${String(week).padStart(2, '0')}`;
60
+ }
61
+ return `${y}-${m}`;
62
+ }
63
+
64
+ export function countOverTime(records, dateField, granularity = 'month') {
65
+ const counts = new Map();
66
+ let unresolved = 0;
67
+ for (const r of records) {
68
+ const key = bucketDateKey(r[dateField], granularity);
69
+ if (key === null) { unresolved++; continue; }
70
+ counts.set(key, (counts.get(key) || 0) + 1);
71
+ }
72
+ const sorted = [...counts.entries()].sort((a, b) => a[0].localeCompare(b[0]));
73
+ return { buckets: sorted.length > MAX_BUCKETS ? sorted.slice(-MAX_BUCKETS) : sorted, unresolved, truncated: sorted.length > MAX_BUCKETS };
74
+ }
75
+
76
+ export function renderCountByFieldReport(user, entityName, spec, records, field) {
77
+ const label = getEntityLabel(spec, true) || entityName;
78
+ const fieldDef = spec.fields?.[field];
79
+ if (!fieldDef) {
80
+ return page(user, `${label} | Report`, null,
81
+ `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
82
+ <div class="report-empty-state">Unknown field "${esc(field)}" for this entity.</div>`);
83
+ }
84
+ const buckets = countByField(records, spec, field);
85
+ const content = `<div class="page-header">
86
+ <div><h1 class="page-title">${esc(label)} by ${esc(fieldDef.label || field)}</h1><p class="page-subtitle">${records.length} total records</p></div>
87
+ </div>
88
+ ${barChart(buckets)}`;
89
+ return page(user, `${label} Report | Thatcher`, null, content);
90
+ }
91
+
92
+ export function sumByField(records, spec, field, groupBy) {
93
+ const sums = new Map();
94
+ for (const r of records) {
95
+ const raw = r[field];
96
+ const num = typeof raw === 'string' ? Number(raw) : raw;
97
+ if (typeof num !== 'number' || isNaN(num)) continue;
98
+ const label = bucketLabel(spec, groupBy, r[groupBy]);
99
+ sums.set(label, (sums.get(label) || 0) + num);
100
+ }
101
+ const sorted = [...sums.entries()].sort((a, b) => b[1] - a[1]);
102
+ if (sorted.length <= MAX_BUCKETS) return sorted;
103
+ const top = sorted.slice(0, MAX_BUCKETS - 1);
104
+ const otherSum = sorted.slice(MAX_BUCKETS - 1).reduce((sum, [, v]) => sum + v, 0);
105
+ return [...top, ['(other)', otherSum]];
106
+ }
107
+
108
+ function formatSumValue(value, fieldDef) {
109
+ if (fieldDef?.type === 'currency') return (fieldDef.currency_symbol || '$') + (value / 100).toFixed(2);
110
+ return String(Math.round(value * 100) / 100);
111
+ }
112
+
113
+ function sumBarChart(buckets, fieldDef) {
114
+ if (!buckets.length) return emptyState('No data to report on', 'bar-chart');
115
+ const max = Math.max(...buckets.map(([, v]) => Math.abs(v)), 1);
116
+ const rows = buckets.map(([label, value]) => {
117
+ const pct = Math.round((Math.abs(value) / max) * 100);
118
+ return `<div class="report-bar-row" style="display:flex;align-items:center;gap:12px;margin-bottom:8px">
119
+ <div style="min-width:140px;max-width:220px;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="${esc(label)}">${esc(label)}</div>
120
+ <div style="flex:1;height:20px;background:var(--color-border,#eee);border-radius:4px;overflow:hidden">
121
+ <div style="height:100%;width:${pct}%;background:var(--color-primary,#3b82f6);border-radius:4px"></div>
122
+ </div>
123
+ <div style="min-width:80px;text-align:right;font-size:13px;font-weight:600">${esc(formatSumValue(value, fieldDef))}</div>
124
+ </div>`;
125
+ }).join('');
126
+ return `<div class="report-bar-chart">${rows}</div>`;
127
+ }
128
+
129
+ export function renderSumByFieldReport(user, entityName, spec, records, field, groupBy) {
130
+ const label = getEntityLabel(spec, true) || entityName;
131
+ const fieldDef = spec.fields?.[field];
132
+ const groupByDef = spec.fields?.[groupBy];
133
+ if (!fieldDef || !groupByDef) {
134
+ return page(user, `${label} | Report`, null,
135
+ `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
136
+ <div class="report-empty-state">Unknown field "${esc(!fieldDef ? field : groupBy)}" for this entity.</div>`);
137
+ }
138
+ const buckets = sumByField(records, spec, field, groupBy);
139
+ const content = `<div class="page-header">
140
+ <div><h1 class="page-title">${esc(label)}: ${esc(fieldDef.label || field)} by ${esc(groupByDef.label || groupBy)}</h1><p class="page-subtitle">${records.length} total records</p></div>
141
+ </div>
142
+ ${sumBarChart(buckets, fieldDef)}`;
143
+ return page(user, `${label} Report | Thatcher`, null, content);
144
+ }
145
+
146
+ // Cross-entity rollup: entity A's records are grouped by a field on related
147
+ // entity B, joined through A's ref field. relatedRecords must already be the
148
+ // CALLER's row/org-scoped list() result for B -- this function only joins
149
+ // and counts, it enforces no access itself (the access decision -- can this
150
+ // user list B at all -- is made by the caller before relatedRecords exists).
151
+ export function rollupByRelatedField(records, refField, relatedRecords, relatedSpec, rollupField) {
152
+ const relatedById = new Map(relatedRecords.map(r => [String(r.id), r]));
153
+ const counts = new Map();
154
+ let unmatched = 0;
155
+ for (const r of records) {
156
+ const refId = r[refField];
157
+ const related = refId != null ? relatedById.get(String(refId)) : null;
158
+ if (!related) { unmatched++; continue; }
159
+ const label = bucketLabel(relatedSpec, rollupField, related[rollupField]);
160
+ counts.set(label, (counts.get(label) || 0) + 1);
161
+ }
162
+ const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]);
163
+ return { buckets: sorted.length > MAX_BUCKETS ? sorted.slice(0, MAX_BUCKETS) : sorted, unmatched };
164
+ }
165
+
166
+ export function renderRollupReport(user, entityName, spec, records, refField, relatedEntity, relatedSpec, relatedRecords, rollupField) {
167
+ const label = getEntityLabel(spec, true) || entityName;
168
+ const relatedLabel = getEntityLabel(relatedSpec, true) || relatedEntity;
169
+ const rollupFieldDef = relatedSpec?.fields?.[rollupField];
170
+ if (!spec.fields?.[refField] || !rollupFieldDef) {
171
+ return page(user, `${label} | Report`, null,
172
+ `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
173
+ <div class="report-empty-state">Unknown field for this rollup.</div>`);
174
+ }
175
+ const { buckets, unmatched } = rollupByRelatedField(records, refField, relatedRecords, relatedSpec, rollupField);
176
+ const notice = unmatched
177
+ ? `<div class="report-notice">${unmatched} record${unmatched === 1 ? '' : 's'} without a resolvable ${esc(relatedLabel)} excluded</div>`
178
+ : '';
179
+ const content = `<div class="page-header">
180
+ <div><h1 class="page-title">${esc(label)} by ${esc(relatedLabel)}.${esc(rollupFieldDef.label || rollupField)}</h1><p class="page-subtitle">${records.length} total records</p></div>
181
+ </div>
182
+ ${notice}
183
+ ${barChart(buckets)}`;
184
+ return page(user, `${label} Report | Thatcher`, null, content);
185
+ }
186
+
187
+ export function renderCountOverTimeReport(user, entityName, spec, records, dateField, granularity) {
188
+ const label = getEntityLabel(spec, true) || entityName;
189
+ const fieldDef = spec.fields?.[dateField];
190
+ if (!fieldDef) {
191
+ return page(user, `${label} | Report`, null,
192
+ `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
193
+ <div class="report-empty-state">Unknown date field "${esc(dateField)}" for this entity.</div>`);
194
+ }
195
+ const { buckets, unresolved, truncated } = countOverTime(records, dateField, granularity);
196
+ const notice = unresolved
197
+ ? `<div class="report-notice">${unresolved} record${unresolved === 1 ? '' : 's'} without a resolvable date excluded</div>`
198
+ : '';
199
+ const truncatedNotice = truncated
200
+ ? `<div class="report-notice">Showing the most recent ${buckets.length} periods only</div>`
201
+ : '';
202
+ const content = `<div class="page-header">
203
+ <div><h1 class="page-title">${esc(label)} over time</h1><p class="page-subtitle">${records.length} total records, grouped by ${esc(granularity)}</p></div>
204
+ </div>
205
+ ${notice}${truncatedNotice}
206
+ ${barChart(buckets)}`;
207
+ return page(user, `${label} Report | Thatcher`, null, content);
208
+ }
209
+
210
+ // One table per-product rather than a bar chart: unlike count-by-field/
211
+ // sum-by-field's single grouped metric, a forecast is inherently multiple
212
+ // values per row (stock, consumption rate, days-out, reorder date) -- a bar
213
+ // chart of any single one of those would discard the others, so a table is
214
+ // the correct shape here, reusing the same page()/esc() rendering discipline
215
+ // as every other report rather than inventing a new chart type.
216
+ export function renderInventoryForecastReport(user, spec, forecastRows) {
217
+ const label = getEntityLabel(spec, true) || 'Products';
218
+ const rows = forecastRows.map(p => {
219
+ const daysLabel = p.days_until_stockout == null ? 'Unknown' : String(Math.round(p.days_until_stockout))
220
+ const reorderLabel = p.reorder_date == null ? '-' : new Date(p.reorder_date * 1000).toISOString().slice(0, 10)
221
+ const rowCls = p.reorder_due ? 'style="background:var(--color-danger-bg,#fee2e2)"' : ''
222
+ return `<tr ${rowCls}>
223
+ <td>${esc(p.name || p.id)}</td>
224
+ <td>${esc(String(p.current_stock))}</td>
225
+ <td>${esc((p.avg_daily_consumption || 0).toFixed(2))}</td>
226
+ <td>${esc(daysLabel)}</td>
227
+ <td>${esc(reorderLabel)}${p.reorder_due ? ' <span class="pill pill-danger">Reorder Now</span>' : ''}</td>
228
+ </tr>`;
229
+ }).join('') || emptyState('No products to forecast', 'bar-chart');
230
+
231
+ const content = `<div class="page-header">
232
+ <div><h1 class="page-title">${esc(label)}: Inventory Forecast</h1><p class="page-subtitle">${forecastRows.length} products</p></div>
233
+ </div>
234
+ <div class="table-wrap"><table class="data-table">
235
+ <thead><tr><th>Product</th><th>Current Stock</th><th>Avg Daily Use</th><th>Days Until Stockout</th><th>Suggested Reorder</th></tr></thead>
236
+ <tbody>${rows}</tbody>
237
+ </table></div>`;
238
+ return page(user, `Inventory Forecast | Thatcher`, null, content);
239
+ }
240
+
241
+ // Reuses sumBarChart (already built for sum-by-field's currency-aware bar
242
+ // rendering) rather than inventing a third chart type -- a demand-by-month
243
+ // bucket is the exact same [label, numericValue] shape sum-by-field already
244
+ // renders, just currency-formatted since weighted_value derives from a
245
+ // currency field.
246
+ // Two-dimensional cross-tab: row_field x col_field, cell = aggregated
247
+ // value_field. Distinct from countByField/sumByField (single-dimension
248
+ // group-bys) -- this is the only report shape that can answer "how does X
249
+ // break down by Y AND Z simultaneously". Both axes independently collapse
250
+ // past MAX_BUCKETS into an '(other)' bucket the same way countByField does,
251
+ // so a high-cardinality field on either axis can't blow up the table.
252
+ const PIVOT_AGGREGATORS = {
253
+ sum: (values) => values.reduce((a, b) => a + b, 0),
254
+ count: (values) => values.length,
255
+ avg: (values) => values.length ? values.reduce((a, b) => a + b, 0) / values.length : 0,
256
+ };
257
+
258
+ function topBucketLabels(records, spec, field) {
259
+ const counts = new Map();
260
+ for (const r of records) {
261
+ const label = bucketLabel(spec, field, r[field]);
262
+ counts.set(label, (counts.get(label) || 0) + 1);
263
+ }
264
+ const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]).map(([label]) => label);
265
+ if (sorted.length <= MAX_BUCKETS) return new Set(sorted);
266
+ return new Set(sorted.slice(0, MAX_BUCKETS - 1));
267
+ }
268
+
269
+ export function pivotByFields(records, spec, rowField, colField, valueField, agg = 'count') {
270
+ const aggregator = PIVOT_AGGREGATORS[agg] || PIVOT_AGGREGATORS.count;
271
+ const rowKeep = topBucketLabels(records, spec, rowField);
272
+ const colKeep = topBucketLabels(records, spec, colField);
273
+ const cellValues = new Map();
274
+ const rowLabels = new Set();
275
+ const colLabels = new Set();
276
+
277
+ for (const r of records) {
278
+ const rawRow = bucketLabel(spec, rowField, r[rowField]);
279
+ const rawCol = bucketLabel(spec, colField, r[colField]);
280
+ const rowLabel = rowKeep.has(rawRow) ? rawRow : '(other)';
281
+ const colLabel = colKeep.has(rawCol) ? rawCol : '(other)';
282
+ rowLabels.add(rowLabel);
283
+ colLabels.add(colLabel);
284
+ const key = rowLabel + '' + colLabel;
285
+ const raw = agg === 'count' ? 1 : (typeof r[valueField] === 'string' ? Number(r[valueField]) : r[valueField]);
286
+ if (agg !== 'count' && (typeof raw !== 'number' || isNaN(raw))) continue;
287
+ if (!cellValues.has(key)) cellValues.set(key, []);
288
+ cellValues.get(key).push(agg === 'count' ? 1 : raw);
289
+ }
290
+
291
+ const rows = [...rowLabels].sort((a, b) => a === '(other)' ? 1 : b === '(other)' ? -1 : a.localeCompare(b));
292
+ const cols = [...colLabels].sort((a, b) => a === '(other)' ? 1 : b === '(other)' ? -1 : a.localeCompare(b));
293
+ const cells = rows.map(rowLabel =>
294
+ cols.map(colLabel => {
295
+ const values = cellValues.get(rowLabel + '' + colLabel);
296
+ return values ? aggregator(values) : 0;
297
+ })
298
+ );
299
+
300
+ return { rows, cols, cells };
301
+ }
302
+
303
+ export function renderPivotReport(user, entityName, spec, records, rowField, colField, valueField, agg) {
304
+ const label = getEntityLabel(spec, true) || entityName;
305
+ const rowFieldDef = spec.fields?.[rowField];
306
+ const colFieldDef = spec.fields?.[colField];
307
+ const validAgg = PIVOT_AGGREGATORS[agg] ? agg : 'count';
308
+ const valueFieldDef = validAgg === 'count' ? null : spec.fields?.[valueField];
309
+ if (!rowFieldDef || !colFieldDef || (validAgg !== 'count' && !valueFieldDef)) {
310
+ const badField = !rowFieldDef ? rowField : !colFieldDef ? colField : valueField;
311
+ return page(user, `${label} | Report`, null,
312
+ `<div class="page-header"><h1 class="page-title">${esc(label)}</h1></div>
313
+ <div class="report-empty-state">Unknown field "${esc(badField)}" for this entity.</div>`);
314
+ }
315
+ const { rows, cols, cells } = pivotByFields(records, spec, rowField, colField, valueField, validAgg);
316
+ const formatCell = (v) => validAgg === 'avg' ? String(Math.round(v * 100) / 100) : (valueFieldDef ? formatSumValue(v, valueFieldDef) : String(v));
317
+ const headerCells = cols.map(c => `<th>${esc(c)}</th>`).join('');
318
+ const bodyRows = rows.map((r, i) => {
319
+ const dataCells = cells[i].map(v => `<td style="text-align:right">${esc(formatCell(v))}</td>`).join('');
320
+ return `<tr><td>${esc(r)}</td>${dataCells}</tr>`;
321
+ }).join('') || emptyState('No data to report on', 'bar-chart');
322
+ const content = `<div class="page-header">
323
+ <div><h1 class="page-title">${esc(label)}: ${esc(rowFieldDef.label || rowField)} &times; ${esc(colFieldDef.label || colField)} (${esc(validAgg)}${valueFieldDef ? ' of ' + esc(valueFieldDef.label || valueField) : ''})</h1><p class="page-subtitle">${records.length} total records</p></div>
324
+ </div>
325
+ <div class="table-wrap"><table class="data-table">
326
+ <thead><tr><th></th>${headerCells}</tr></thead>
327
+ <tbody>${bodyRows}</tbody>
328
+ </table></div>`;
329
+ return page(user, `${label} Report | Thatcher`, null, content);
330
+ }
331
+
332
+ export function renderDemandForecastReport(user, spec, buckets, totalOpportunities) {
333
+ const label = getEntityLabel(spec, true) || 'Opportunities';
334
+ const valueFieldDef = spec.fields?.value || { type: 'currency' };
335
+ const totalProjected = buckets.reduce((sum, [, v]) => sum + v, 0);
336
+ const notice = !buckets.length
337
+ ? `<div class="report-notice">No open opportunities with a future expected close date</div>`
338
+ : '';
339
+ const content = `<div class="page-header">
340
+ <div><h1 class="page-title">${esc(label)}: Demand Forecast</h1><p class="page-subtitle">${totalOpportunities} total opportunities, ${esc(formatSumValue(totalProjected, valueFieldDef))} projected across ${buckets.length} future month${buckets.length === 1 ? '' : 's'}</p></div>
341
+ </div>
342
+ ${notice}
343
+ ${sumBarChart(buckets, valueFieldDef)}`;
344
+ return page(user, `Demand Forecast | Thatcher`, null, content);
345
+ }