@kruschdev/pascal-plugin-furnishings 0.0.0-stage → 0.2.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.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Framework-free Furnishings, Electronics & Botanicals Engine
3
+ * (`@kruschdev/pascal-plugin-furnishings/furnishings`).
4
+ *
5
+ * Strictly pure TypeScript: zero `three`, `react`, `@pascal-app/*`, or `next` imports.
6
+ */
7
+
8
+ export * from './types'
9
+ export * from './catalog'
10
+ export * from './geometry'
11
+ export * from './autopopulate'
12
+ export * from './audit'
13
+ export * from './schedule'
14
+ export * from './spatial-grounding'
@@ -0,0 +1,503 @@
1
+ /**
2
+ * FF&E Room Staging Schedule, Electronics Electrical-Load Handoff, Botanical Schedule &
3
+ * CSI Division 12 / 23 / 26 / 27 / 32 Priced BOM + Formula-Safe CSV Exports (`src/furnishings/schedule.ts`).
4
+ *
5
+ * Pure framework-free TypeScript: zero `three`, `react`, `@pascal-app/*`, or `next` imports.
6
+ */
7
+
8
+ import {
9
+ type PascalBomFragment,
10
+ createBomLine,
11
+ } from '@kruschdev/pascal-mep-kernel/bom'
12
+ import { csvCell, deriveDisplayDimensionsM, getFurnishingSpec } from './catalog'
13
+ import {
14
+ BUDGET_TIER_MULTIPLIER,
15
+ type BudgetTier,
16
+ DOMAIN_LABELS,
17
+ type FurnishingDomain,
18
+ PLANT_MATURITY_SCALE,
19
+ type PlantMaturity,
20
+ type PlantSeason,
21
+ type SceneNodeLike,
22
+ type StylePreset,
23
+ round2,
24
+ } from './types'
25
+
26
+ export type FurnishingScheduleRow = {
27
+ readonly nodeId: string
28
+ readonly nodeType: 'furnishings:item' | 'furnishings:electronics' | 'furnishings:plant'
29
+ readonly kind: string
30
+ readonly label: string
31
+ readonly domain: FurnishingDomain
32
+ readonly domainLabel: string
33
+ readonly csiDivision: string
34
+ readonly roomName: string
35
+ readonly finishSummary: string
36
+ readonly dimensionsLabel: string
37
+ readonly quantity: number
38
+ readonly unitPriceUsd: number
39
+ readonly extendedPriceUsd: number
40
+ readonly wattageW: number
41
+ readonly brand: string
42
+ readonly sku: string
43
+ readonly species?: string
44
+ readonly maturity?: string
45
+ readonly placement?: string
46
+ readonly notes: string
47
+ }
48
+
49
+ export type RoomStagingSummary = {
50
+ readonly roomName: string
51
+ readonly itemCount: number
52
+ readonly totalWattageW: number
53
+ readonly recommendedAmps120V: number
54
+ readonly subtotalUsd: number
55
+ }
56
+
57
+ export type ElectricalDemandCoordination = {
58
+ readonly totalPlugInWattageW: number
59
+ readonly estimatedAmps120V: number
60
+ readonly recommended15ACircuits: number
61
+ readonly byRoom: readonly {
62
+ readonly roomName: string
63
+ readonly wattageW: number
64
+ readonly amps120V: number
65
+ readonly deviceCount: number
66
+ }[]
67
+ }
68
+
69
+ export type FurnishingsSchedule = {
70
+ readonly stylePreset: string
71
+ readonly budgetTier: BudgetTier
72
+ readonly season: string
73
+ readonly totalItems: number
74
+ readonly totalUnits: number
75
+ readonly rows: readonly FurnishingScheduleRow[]
76
+ readonly rooms: readonly RoomStagingSummary[]
77
+ readonly electricalDemand: ElectricalDemandCoordination
78
+ readonly botanicalCount: number
79
+ readonly electronicsCount: number
80
+ }
81
+
82
+ export type FurnishingsBomLine = {
83
+ readonly csiDivision: string
84
+ readonly domain: FurnishingDomain
85
+ readonly domainLabel: string
86
+ readonly kind: string
87
+ readonly label: string
88
+ readonly finishSummary: string
89
+ readonly sku: string
90
+ readonly brand: string
91
+ readonly quantity: number
92
+ readonly unitPriceUsd: number
93
+ readonly extendedUsd: number
94
+ }
95
+
96
+ export type FurnishingsBomResult = {
97
+ readonly lines: readonly FurnishingsBomLine[]
98
+ readonly itemCount: number
99
+ readonly subtotalUsd: number
100
+ readonly markupPct: number
101
+ readonly markupUsd: number
102
+ readonly salesTaxPct: number
103
+ readonly salesTaxUsd: number
104
+ readonly grandTotalUsd: number
105
+ readonly byCsiDivision: Readonly<Record<string, number>>
106
+ }
107
+
108
+ function safeNum(v: unknown, fallback = 0): number {
109
+ return typeof v === 'number' && Number.isFinite(v) ? v : fallback
110
+ }
111
+
112
+ export type FurnishingsScheduleOptions = {
113
+ readonly budgetTier?: BudgetTier
114
+ readonly stylePreset?: StylePreset | string
115
+ readonly season?: PlantSeason | string
116
+ }
117
+
118
+ function resolveAssemblySettings(
119
+ nodes: Readonly<Record<string, SceneNodeLike>>,
120
+ options: FurnishingsScheduleOptions = {},
121
+ ): {
122
+ stylePreset: string
123
+ budgetTier: BudgetTier
124
+ season: string
125
+ } {
126
+ for (const n of Object.values(nodes)) {
127
+ if (n.type === 'furnishings:assembly') {
128
+ const tier = String(options.budgetTier ?? n.budgetTier ?? 'mid-tier')
129
+ return {
130
+ stylePreset: String(options.stylePreset ?? n.stylePreset ?? 'mid-century'),
131
+ budgetTier: tier === 'value' || tier === 'designer' ? tier : 'mid-tier',
132
+ season: String(options.season ?? n.season ?? 'summer'),
133
+ }
134
+ }
135
+ }
136
+ const fallbackTier = options.budgetTier
137
+ return {
138
+ stylePreset: String(options.stylePreset ?? 'mid-century'),
139
+ budgetTier: fallbackTier === 'value' || fallbackTier === 'designer' ? fallbackTier : 'mid-tier',
140
+ season: String(options.season ?? 'summer'),
141
+ }
142
+ }
143
+
144
+ export function buildFurnishingsSchedule(
145
+ nodes: Readonly<Record<string, SceneNodeLike>>,
146
+ options: FurnishingsScheduleOptions = {},
147
+ ): FurnishingsSchedule {
148
+ const settings = resolveAssemblySettings(nodes, options)
149
+ const tierMult = BUDGET_TIER_MULTIPLIER[settings.budgetTier]
150
+ const rows: FurnishingScheduleRow[] = []
151
+
152
+ let totalUnits = 0
153
+ let botanicalCount = 0
154
+ let electronicsCount = 0
155
+ const plugInWattageByNode = new Map<string, number>()
156
+
157
+ for (const node of Object.values(nodes)) {
158
+ if (node.visible === false) continue
159
+ if (
160
+ node.type !== 'furnishings:item' &&
161
+ node.type !== 'furnishings:electronics' &&
162
+ node.type !== 'furnishings:plant'
163
+ ) {
164
+ continue
165
+ }
166
+ const kind = String(node.itemKind ?? node.plantKind ?? '')
167
+ const spec = getFurnishingSpec(kind)
168
+ const domain: FurnishingDomain = spec?.domain ?? (node.type === 'furnishings:plant' ? 'trees-plants' : node.type === 'furnishings:electronics' ? 'electronics-av' : 'seating')
169
+ const qty = Math.max(1, Math.round(safeNum(node.quantity, 1)))
170
+ const basePrice = spec?.estUnitPriceUsd ?? 250
171
+ const overridePrice =
172
+ typeof node.unitPriceOverrideUsd === 'number' && Number.isFinite(node.unitPriceOverrideUsd) && node.unitPriceOverrideUsd >= 0
173
+ ? node.unitPriceOverrideUsd
174
+ : null
175
+ const unitPriceUsd = round2(overridePrice !== null ? overridePrice : basePrice * tierMult)
176
+ const extendedPriceUsd = round2(unitPriceUsd * qty)
177
+ const wattageW = round2(Math.max(0, safeNum(node.wattageW, spec?.wattageW ?? 0)) * qty)
178
+ const plugInWattageW = spec?.isHardwiredLight ? 0 : wattageW
179
+ plugInWattageByNode.set(node.id, plugInWattageW)
180
+ const roomName = String(node.roomName ?? node.zoneName ?? 'Unassigned')
181
+
182
+ let wM = Math.max(0.01, safeNum(node.widthM, spec?.widthM ?? 0.8))
183
+ let dM = Math.max(0.01, safeNum(node.depthM, spec?.depthM ?? 0.8))
184
+ let hM = Math.max(0.01, safeNum(node.heightM, spec?.heightM ?? 0.8))
185
+
186
+ if (node.type === 'furnishings:electronics' && typeof node.screenDiagonalIn === 'number') {
187
+ const derived = deriveDisplayDimensionsM(kind, node.screenDiagonalIn)
188
+ const usesCatalogDefaults =
189
+ (node.widthM === undefined || node.widthM === spec?.widthM) &&
190
+ (node.heightM === undefined || node.heightM === spec?.heightM)
191
+ if (derived && usesCatalogDefaults) {
192
+ wM = derived.widthM
193
+ hM = derived.heightM
194
+ }
195
+ }
196
+
197
+ if (node.type === 'furnishings:plant') {
198
+ const matKey = String(node.maturity ?? 'mature') as PlantMaturity
199
+ const scale = Object.hasOwn(PLANT_MATURITY_SCALE, matKey) ? PLANT_MATURITY_SCALE[matKey] : 1.0
200
+ const canopyR = Math.max(0.05, safeNum(node.canopyRadiusM, (spec?.widthM ?? 1.0) / 2))
201
+ wM = canopyR * 2 * scale
202
+ const isLinearPlant = kind === 'bush-hedge-row' || kind === 'plant-succulent-trough'
203
+ dM = isLinearPlant ? (spec?.depthM ?? 0.5) * scale : canopyR * 2 * scale
204
+ hM = Math.max(0.1, safeNum(node.heightM, spec?.heightM ?? 1.2)) * scale
205
+ }
206
+
207
+ let finishSummary = ''
208
+ if (node.type === 'furnishings:plant') {
209
+ botanicalCount += qty
210
+ finishSummary = `${String(node.maturity ?? 'mature')} · ${String(node.season ?? settings.season)} · ${String(node.potStyle ?? 'none')}`
211
+ } else if (node.type === 'furnishings:electronics') {
212
+ electronicsCount += qty
213
+ const diag = typeof node.screenDiagonalIn === 'number' ? `${node.screenDiagonalIn}" · ` : ''
214
+ finishSummary = `${diag}${String(node.mountStyle ?? 'stand')} · ${String(node.finish ?? 'matte-black')}`
215
+ } else {
216
+ const isHvacGrate = kind.startsWith('vent-') || Boolean(spec?.swapGroup?.startsWith('hvac-'))
217
+ if (isHvacGrate || spec?.isHardwiredLight) {
218
+ const defaultMetal = kind === 'vent-grate-floor-brass' ? 'brushed-brass' : 'matte-black'
219
+ const metal = String(node.metalFinish ?? defaultMetal)
220
+ finishSummary =
221
+ kind === 'light-ceiling-fan-modern'
222
+ ? `${String(node.woodFinish ?? 'natural-oak')} / ${metal}`
223
+ : metal
224
+ } else {
225
+ finishSummary = `${String(node.woodFinish ?? 'natural-oak')} / ${String(node.fabricFinish ?? 'warm-linen')}`
226
+ }
227
+ }
228
+
229
+ totalUnits += qty
230
+ rows.push({
231
+ nodeId: node.id,
232
+ nodeType: node.type,
233
+ kind,
234
+ label: typeof node.name === 'string' && node.name.trim() ? node.name.trim() : (spec?.label ?? kind),
235
+ domain,
236
+ domainLabel: DOMAIN_LABELS[domain],
237
+ csiDivision: spec?.csiDivision ?? '12 52 00',
238
+ roomName,
239
+ finishSummary,
240
+ dimensionsLabel: `${round2(wM)}×${round2(dM)}×${round2(hM)}m`,
241
+ quantity: qty,
242
+ unitPriceUsd,
243
+ extendedPriceUsd,
244
+ wattageW,
245
+ brand: typeof node.customBrand === 'string' && node.customBrand.trim() ? node.customBrand.trim() : 'Parametric Spec',
246
+ sku: typeof node.customSku === 'string' && node.customSku.trim() ? node.customSku.trim() : `FRN-${kind.toUpperCase()}`,
247
+ ...(node.type === 'furnishings:plant'
248
+ ? {
249
+ species: String(node.species ?? spec?.defaultSpecies ?? spec?.label ?? kind),
250
+ maturity: String(node.maturity ?? 'mature'),
251
+ placement: String(node.placement ?? spec?.defaultPlacement ?? 'in-ground'),
252
+ }
253
+ : {}),
254
+ notes: typeof node.notes === 'string' ? node.notes : '',
255
+ })
256
+ }
257
+
258
+ rows.sort((a, b) => a.roomName.localeCompare(b.roomName) || a.domain.localeCompare(b.domain) || a.label.localeCompare(b.label))
259
+
260
+ const roomMap = new Map<
261
+ string,
262
+ { itemCount: number; watts: number; plugInWatts: number; subtotal: number; plugInDevices: number }
263
+ >()
264
+ for (const r of rows) {
265
+ const cur = roomMap.get(r.roomName) ?? { itemCount: 0, watts: 0, plugInWatts: 0, subtotal: 0, plugInDevices: 0 }
266
+ const plugInW = plugInWattageByNode.get(r.nodeId) ?? r.wattageW
267
+ cur.itemCount += r.quantity
268
+ cur.watts += r.wattageW
269
+ cur.plugInWatts += plugInW
270
+ cur.subtotal += r.extendedPriceUsd
271
+ if (plugInW > 0) cur.plugInDevices += r.quantity
272
+ roomMap.set(r.roomName, cur)
273
+ }
274
+
275
+ const rooms: RoomStagingSummary[] = [...roomMap.entries()]
276
+ .map(([roomName, agg]) => ({
277
+ roomName,
278
+ itemCount: agg.itemCount,
279
+ totalWattageW: round2(agg.watts),
280
+ recommendedAmps120V: round2(agg.watts / 120),
281
+ subtotalUsd: round2(agg.subtotal),
282
+ }))
283
+ .sort((a, b) => a.roomName.localeCompare(b.roomName))
284
+
285
+ const totalPlugInWattageW = round2(
286
+ rows.reduce((s, r) => s + (plugInWattageByNode.get(r.nodeId) ?? r.wattageW), 0),
287
+ )
288
+ const estimatedAmps120V = round2(totalPlugInWattageW / 120)
289
+ const electricalDemand: ElectricalDemandCoordination = {
290
+ totalPlugInWattageW,
291
+ estimatedAmps120V,
292
+ recommended15ACircuits: totalPlugInWattageW > 0 ? Math.max(1, Math.ceil(totalPlugInWattageW / 1440)) : 0,
293
+ byRoom: [...roomMap.entries()]
294
+ .filter(([, agg]) => agg.plugInWatts > 0)
295
+ .map(([roomName, agg]) => ({
296
+ roomName,
297
+ wattageW: round2(agg.plugInWatts),
298
+ amps120V: round2(agg.plugInWatts / 120),
299
+ deviceCount: agg.plugInDevices,
300
+ }))
301
+ .sort((a, b) => b.wattageW - a.wattageW),
302
+ }
303
+
304
+ return {
305
+ stylePreset: settings.stylePreset,
306
+ budgetTier: settings.budgetTier,
307
+ season: settings.season,
308
+ totalItems: rows.length,
309
+ totalUnits,
310
+ rows,
311
+ rooms,
312
+ electricalDemand,
313
+ botanicalCount,
314
+ electronicsCount,
315
+ }
316
+ }
317
+
318
+ export function buildFurnishingsBom(
319
+ nodes: Readonly<Record<string, SceneNodeLike>>,
320
+ options: { markupPct?: number; salesTaxPct?: number; budgetTier?: BudgetTier; schedule?: FurnishingsSchedule } = {},
321
+ ): FurnishingsBomResult {
322
+ const schedule = options.schedule ?? buildFurnishingsSchedule(nodes, { budgetTier: options.budgetTier })
323
+ const markupPct = Math.min(200, Math.max(0, safeNum(options.markupPct, 0)))
324
+ const salesTaxPct = Math.min(25, Math.max(0, safeNum(options.salesTaxPct, 0)))
325
+
326
+ // Track distinct finishes/sizes per kind so BOM line labels disambiguate when multiple variants appear
327
+ const finishesByKind = new Map<string, Set<string>>()
328
+ for (const r of schedule.rows) {
329
+ const set = finishesByKind.get(r.kind) ?? new Set<string>()
330
+ if (r.finishSummary) set.add(r.finishSummary)
331
+ finishesByKind.set(r.kind, set)
332
+ }
333
+
334
+ const grouped = new Map<string, FurnishingsBomLine>()
335
+ for (const r of schedule.rows) {
336
+ const key = `${r.csiDivision}|${r.kind}|${r.sku}|${r.brand}|${r.finishSummary}|${r.unitPriceUsd}`
337
+ const prev = grouped.get(key)
338
+ if (prev) {
339
+ const nextQty = prev.quantity + r.quantity
340
+ grouped.set(key, {
341
+ ...prev,
342
+ quantity: nextQty,
343
+ extendedUsd: round2(nextQty * prev.unitPriceUsd),
344
+ })
345
+ } else {
346
+ const hasDistinctFinishes = (finishesByKind.get(r.kind)?.size ?? 0) > 1
347
+ const label = hasDistinctFinishes && r.finishSummary ? `${r.label} (${r.finishSummary})` : r.label
348
+ grouped.set(key, {
349
+ csiDivision: r.csiDivision,
350
+ domain: r.domain,
351
+ domainLabel: r.domainLabel,
352
+ kind: r.kind,
353
+ label,
354
+ finishSummary: r.finishSummary,
355
+ sku: r.sku,
356
+ brand: r.brand,
357
+ quantity: r.quantity,
358
+ unitPriceUsd: r.unitPriceUsd,
359
+ extendedUsd: r.extendedPriceUsd,
360
+ })
361
+ }
362
+ }
363
+
364
+ const lines = [...grouped.values()].sort(
365
+ (a, b) => a.csiDivision.localeCompare(b.csiDivision) || a.label.localeCompare(b.label),
366
+ )
367
+
368
+ const byCsiDivision: Record<string, number> = {}
369
+ let subtotalUsd = 0
370
+ let itemCount = 0
371
+ for (const line of lines) {
372
+ subtotalUsd += line.extendedUsd
373
+ itemCount += line.quantity
374
+ byCsiDivision[line.csiDivision] = round2((byCsiDivision[line.csiDivision] ?? 0) + line.extendedUsd)
375
+ }
376
+ subtotalUsd = round2(subtotalUsd)
377
+ const markupUsd = round2(subtotalUsd * (markupPct / 100))
378
+ const preTax = subtotalUsd + markupUsd
379
+ const salesTaxUsd = round2(preTax * (salesTaxPct / 100))
380
+ const grandTotalUsd = round2(preTax + salesTaxUsd)
381
+
382
+ return {
383
+ lines,
384
+ itemCount,
385
+ subtotalUsd,
386
+ markupPct,
387
+ markupUsd,
388
+ salesTaxPct,
389
+ salesTaxUsd,
390
+ grandTotalUsd,
391
+ byCsiDivision,
392
+ }
393
+ }
394
+
395
+ export function formatFurnishingsScheduleCsv(schedule: FurnishingsSchedule): string {
396
+ const header = [
397
+ 'Node ID',
398
+ 'Room / Zone',
399
+ 'CSI Division',
400
+ 'Domain',
401
+ 'Item',
402
+ 'SKU',
403
+ 'Brand',
404
+ 'Finish / Spec',
405
+ 'Dimensions',
406
+ 'Qty',
407
+ 'Wattage (W)',
408
+ 'Unit Price ($)',
409
+ 'Extended ($)',
410
+ 'Notes',
411
+ ]
412
+ const lines = [header.map(csvCell).join(',')]
413
+ for (const r of schedule.rows) {
414
+ lines.push(
415
+ [
416
+ r.nodeId,
417
+ r.roomName,
418
+ r.csiDivision,
419
+ r.domainLabel,
420
+ r.label,
421
+ r.sku,
422
+ r.brand,
423
+ r.finishSummary,
424
+ r.dimensionsLabel,
425
+ r.quantity,
426
+ r.wattageW,
427
+ r.unitPriceUsd.toFixed(2),
428
+ r.extendedPriceUsd.toFixed(2),
429
+ r.notes,
430
+ ]
431
+ .map(csvCell)
432
+ .join(','),
433
+ )
434
+ }
435
+ return lines.join('\n')
436
+ }
437
+
438
+ export function formatFurnishingsBomCsv(bom: FurnishingsBomResult): string {
439
+ const header = ['CSI Division', 'Domain', 'SKU', 'Brand', 'Description', 'Qty', 'Unit Price ($)', 'Extended ($)']
440
+ const lines = [header.map(csvCell).join(',')]
441
+ for (const line of bom.lines) {
442
+ lines.push(
443
+ [
444
+ line.csiDivision,
445
+ line.domainLabel,
446
+ line.sku,
447
+ line.brand,
448
+ line.label,
449
+ line.quantity,
450
+ line.unitPriceUsd.toFixed(2),
451
+ line.extendedUsd.toFixed(2),
452
+ ]
453
+ .map(csvCell)
454
+ .join(','),
455
+ )
456
+ }
457
+ lines.push(['', '', '', '', 'Subtotal', bom.itemCount, '', bom.subtotalUsd.toFixed(2)].map(csvCell).join(','))
458
+ if (bom.markupUsd > 0) {
459
+ lines.push(['', '', '', '', `Markup (${bom.markupPct}%)`, '', '', bom.markupUsd.toFixed(2)].map(csvCell).join(','))
460
+ }
461
+ if (bom.salesTaxUsd > 0) {
462
+ lines.push(['', '', '', '', `Sales Tax (${bom.salesTaxPct}%)`, '', '', bom.salesTaxUsd.toFixed(2)].map(csvCell).join(','))
463
+ }
464
+ lines.push(['', '', '', '', 'Grand Total', '', '', bom.grandTotalUsd.toFixed(2)].map(csvCell).join(','))
465
+ return lines.join('\n')
466
+ }
467
+
468
+ /**
469
+ * Convert a `FurnishingsBomResult` into a canonical `@kruschdev/pascal-mep-kernel/bom` fragment (`pascal-bom/1.0`).
470
+ */
471
+ export function toFurnishingsPascalBom(bom: FurnishingsBomResult): PascalBomFragment {
472
+ const lines = bom.lines.map((line) =>
473
+ createBomLine({
474
+ sourcePlugin: 'pascal-plugin-furnishings',
475
+ trade: 'furnishings',
476
+ category: line.domain,
477
+ costScope: 'finish-sku',
478
+ csiMasterFormat: line.csiDivision,
479
+ sku: line.sku,
480
+ manufacturer: line.brand,
481
+ name: line.label,
482
+ nominalCallout: line.finishSummary || line.domainLabel,
483
+ quantity: line.quantity,
484
+ unit: 'ea',
485
+ unitMaterialUsd: line.unitPriceUsd,
486
+ extendedMaterialUsd: line.extendedUsd,
487
+ }),
488
+ )
489
+
490
+ return {
491
+ plugin: 'pascal-plugin-furnishings',
492
+ pluginVersion: '0.1.0',
493
+ laborIncluded: false,
494
+ lines,
495
+ }
496
+ }
497
+
498
+ export function buildFurnishingsPascalBom(
499
+ nodes: Readonly<Record<string, SceneNodeLike>>,
500
+ options: Parameters<typeof buildFurnishingsBom>[1] = {},
501
+ ): PascalBomFragment {
502
+ return toFurnishingsPascalBom(buildFurnishingsBom(nodes, options))
503
+ }