@kruschdev/pascal-plugin-plumbing 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/ipc/pricing.ts +125 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kruschdev/pascal-plugin-plumbing",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Residential & Light-Commercial IPC/UPC/IFGC plumbing authoring, DFU/WSFU/gas load calculations, gravity-sloped DWV & PEX manifold routing, and code audit plugin for Pascal Editor.",
5
5
  "author": "Kevin Ruschman <kruschdev@users.noreply.github.com>",
6
6
  "license": "MIT",
@@ -44,7 +44,7 @@
44
44
  "check": "bun run check-types && bun test && bun run check:render && bun run build"
45
45
  },
46
46
  "dependencies": {
47
- "@kruschdev/pascal-mep-kernel": "^0.2.0"
47
+ "@kruschdev/pascal-mep-kernel": "^0.3.0"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "@pascal-app/core": ">=1.0.0 <2",
@@ -8,6 +8,8 @@ import {
8
8
  type PascalBomFragment,
9
9
  createBomLine,
10
10
  slugifyBomSku,
11
+ applyCallerInjectedPartsToFragment,
12
+ type CallerInjectedPart,
11
13
  } from '@kruschdev/pascal-mep-kernel/bom'
12
14
  import type { SceneNodeLike } from './framing-interop'
13
15
  import { type PlumbingSchedule, buildPlumbingSchedule } from './schedule'
@@ -45,6 +47,7 @@ export type PlumbingBomLine = {
45
47
  laborHours: number
46
48
  laborUsd: number
47
49
  totalUsd: number
50
+ nodeIds?: string[]
48
51
  }
49
52
 
50
53
  export type PlumbingBomResult = {
@@ -260,9 +263,23 @@ export function buildPlumbingBom(
260
263
  occupancy,
261
264
  ...(supplyMaterial ? { supplyMaterial } : {}),
262
265
  })
263
- const all = Object.values(nodes)
266
+ const allEntries = Object.entries(nodes)
264
267
  const lines: PlumbingBomLine[] = []
265
268
 
269
+ const normalizeNodeIds = (ids: Iterable<string | undefined>): string[] => {
270
+ const seen = new Set<string>()
271
+ const out: string[] = []
272
+ for (const raw of ids) {
273
+ if (typeof raw !== 'string' || raw.length === 0 || seen.has(raw)) continue
274
+ seen.add(raw)
275
+ out.push(raw)
276
+ }
277
+ return out
278
+ }
279
+
280
+ const resolveEntryNodeId = (key: string, node: SceneNodeLike): string =>
281
+ typeof node.id === 'string' && node.id.length > 0 ? node.id : key
282
+
266
283
  const addLine = (
267
284
  category: PlumbingBomLine['category'],
268
285
  item: string,
@@ -270,11 +287,13 @@ export function buildPlumbingBom(
270
287
  quantity: number,
271
288
  unitMaterialUsd: number,
272
289
  laborHours: number,
290
+ rawNodeIds?: Iterable<string | undefined>,
273
291
  ) => {
274
292
  if (quantity <= 0) return
275
293
  const mat = round2(quantity * unitMaterialUsd)
276
294
  const hrs = round2(laborHours)
277
295
  const lab = round2(hrs * laborRateUsd)
296
+ const dedupedIds = rawNodeIds ? normalizeNodeIds(rawNodeIds) : []
278
297
  lines.push({
279
298
  category,
280
299
  item,
@@ -285,71 +304,117 @@ export function buildPlumbingBom(
285
304
  laborHours: hrs,
286
305
  laborUsd: lab,
287
306
  totalUsd: round2(mat + lab),
307
+ ...(dedupedIds.length > 0 ? { nodeIds: dedupedIds } : {}),
288
308
  })
289
309
  }
290
310
 
291
311
  // 1. Equipment (separate roughInOnly SKU-linked units so real-world water heater SKUs aren't double-billed)
292
312
  const eqFullCounts = new Map<EquipmentKind, number>()
293
313
  const eqRoughOnlyCounts = new Map<EquipmentKind, number>()
294
- for (const n of all) {
314
+ const eqFullNodeIds = new Map<EquipmentKind, string[]>()
315
+ const eqRoughOnlyNodeIds = new Map<EquipmentKind, string[]>()
316
+ for (const [key, n] of allEntries) {
295
317
  if (n.type !== 'plumbing:equipment') continue
318
+ const nid = resolveEntryNodeId(key, n)
296
319
  const k = (n.equipmentKind as EquipmentKind) || 'water-heater-tank-electric'
297
320
  if (n.roughInOnly === true) {
298
321
  eqRoughOnlyCounts.set(k, (eqRoughOnlyCounts.get(k) ?? 0) + 1)
322
+ const list = eqRoughOnlyNodeIds.get(k) ?? []
323
+ list.push(nid)
324
+ eqRoughOnlyNodeIds.set(k, list)
299
325
  } else {
300
326
  eqFullCounts.set(k, (eqFullCounts.get(k) ?? 0) + 1)
327
+ const list = eqFullNodeIds.get(k) ?? []
328
+ list.push(nid)
329
+ eqFullNodeIds.set(k, list)
301
330
  }
302
331
  }
303
332
  for (const [k, qty] of eqFullCounts) {
304
333
  const p = EQUIPMENT_PRICES[k] ?? { mat: 350, hrs: 2.5, label: k }
305
- addLine('Equipment', p.label, 'ea', qty, p.mat, qty * p.hrs)
334
+ addLine('Equipment', p.label, 'ea', qty, p.mat, qty * p.hrs, eqFullNodeIds.get(k))
306
335
  }
307
336
  for (const [k, qty] of eqRoughOnlyCounts) {
308
337
  const p = EQUIPMENT_PRICES[k] ?? { mat: 350, hrs: 2.5, label: k }
309
- addLine('Equipment', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.25), round2(qty * p.hrs * 0.55))
338
+ addLine('Equipment', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.25), round2(qty * p.hrs * 0.55), eqRoughOnlyNodeIds.get(k))
310
339
  }
311
340
 
312
341
  // 2. Manifolds (separate roughInOnly SKU-linked manifolds so catalog SKUs aren't double-billed)
313
342
  const manFullCounts = new Map<ManifoldKind, number>()
314
343
  const manRoughOnlyCounts = new Map<ManifoldKind, number>()
315
- for (const n of all) {
344
+ const manFullNodeIds = new Map<ManifoldKind, string[]>()
345
+ const manRoughOnlyNodeIds = new Map<ManifoldKind, string[]>()
346
+ for (const [key, n] of allEntries) {
316
347
  if (n.type !== 'plumbing:manifold') continue
348
+ const nid = resolveEntryNodeId(key, n)
317
349
  const k = (n.manifoldKind as ManifoldKind) || 'pex-homerun'
318
350
  if (n.roughInOnly === true) {
319
351
  manRoughOnlyCounts.set(k, (manRoughOnlyCounts.get(k) ?? 0) + 1)
352
+ const list = manRoughOnlyNodeIds.get(k) ?? []
353
+ list.push(nid)
354
+ manRoughOnlyNodeIds.set(k, list)
320
355
  } else {
321
356
  manFullCounts.set(k, (manFullCounts.get(k) ?? 0) + 1)
357
+ const list = manFullNodeIds.get(k) ?? []
358
+ list.push(nid)
359
+ manFullNodeIds.set(k, list)
322
360
  }
323
361
  }
324
362
  for (const [k, qty] of manFullCounts) {
325
363
  const p = MANIFOLD_PRICES[k] ?? { mat: 295, hrs: 3.0, label: k }
326
- addLine('Manifolds', p.label, 'ea', qty, p.mat, qty * p.hrs)
364
+ addLine('Manifolds', p.label, 'ea', qty, p.mat, qty * p.hrs, manFullNodeIds.get(k))
327
365
  }
328
366
  for (const [k, qty] of manRoughOnlyCounts) {
329
367
  const p = MANIFOLD_PRICES[k] ?? { mat: 295, hrs: 3.0, label: k }
330
- addLine('Manifolds', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55))
368
+ addLine('Manifolds', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55), manRoughOnlyNodeIds.get(k))
331
369
  }
332
370
 
333
371
  // 3. Fixtures (separate linked real-world SKU/furnishing rough-in-only assemblies so porcelain/trim isn't double-billed)
334
372
  const fixFullCounts = new Map<FixtureKind, number>()
335
373
  const fixRoughOnlyCounts = new Map<FixtureKind, number>()
374
+ const fixFullNodeIds = new Map<FixtureKind, string[]>()
375
+ const fixRoughOnlyNodeIds = new Map<FixtureKind, string[]>()
336
376
  for (const f of schedule.fixtures) {
337
377
  if (f.roughInOnly) {
338
378
  fixRoughOnlyCounts.set(f.fixtureKind, (fixRoughOnlyCounts.get(f.fixtureKind) ?? 0) + 1)
379
+ const list = fixRoughOnlyNodeIds.get(f.fixtureKind) ?? []
380
+ list.push(f.nodeId)
381
+ fixRoughOnlyNodeIds.set(f.fixtureKind, list)
339
382
  } else {
340
383
  fixFullCounts.set(f.fixtureKind, (fixFullCounts.get(f.fixtureKind) ?? 0) + 1)
384
+ const list = fixFullNodeIds.get(f.fixtureKind) ?? []
385
+ list.push(f.nodeId)
386
+ fixFullNodeIds.set(f.fixtureKind, list)
341
387
  }
342
388
  }
343
389
  for (const [k, qty] of fixFullCounts) {
344
390
  const p = FIXTURE_ROUGH_PRICES[k] ?? { mat: 200, hrs: 2.0 }
345
- addLine('Fixtures', `${k} (rough-in + trim)`, 'ea', qty, p.mat, qty * p.hrs)
391
+ addLine('Fixtures', `${k} (rough-in + trim)`, 'ea', qty, p.mat, qty * p.hrs, fixFullNodeIds.get(k))
346
392
  }
347
393
  for (const [k, qty] of fixRoughOnlyCounts) {
348
394
  const p = FIXTURE_ROUGH_PRICES[k] ?? { mat: 200, hrs: 2.0 }
349
- addLine('Fixtures', `${k} (rough-in stub-out, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55))
395
+ addLine('Fixtures', `${k} (rough-in stub-out, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55), fixRoughOnlyNodeIds.get(k))
350
396
  }
351
397
 
352
398
  // 4. Pipe & Tubing (with 10% waste factor)
399
+ const pipeRunEntries: {
400
+ readonly nodeId: string
401
+ readonly system: string
402
+ readonly material: string
403
+ readonly nominalSize: string
404
+ }[] = []
405
+ const pipeRunIdsByKey = new Map<string, string[]>()
406
+ for (const [key, n] of allEntries) {
407
+ if (n.type !== 'plumbing:pipe-run' || n.visible === false) continue
408
+ const nid = resolveEntryNodeId(key, n)
409
+ const system = String(n.system || 'cold-water')
410
+ const material = String(n.material || 'pex-a')
411
+ const nominalSize = String(n.nominalSize || '1/2"')
412
+ pipeRunEntries.push({ nodeId: nid, system, material, nominalSize })
413
+ const mapKey = `${system}|${material}|${nominalSize}`
414
+ const list = pipeRunIdsByKey.get(mapKey) ?? []
415
+ list.push(nid)
416
+ pipeRunIdsByKey.set(mapKey, list)
417
+ }
353
418
  let totalPipeFt = 0
354
419
  let totalDwvFt = 0
355
420
  let totalSupplyFt = 0
@@ -370,12 +435,14 @@ export function buildPlumbingBom(
370
435
  qty,
371
436
  p.matPerUnit,
372
437
  (billedFt / 100) * p.hrsPer100Ft,
438
+ pipeRunIdsByKey.get(`${row.system}|${row.material}|${row.nominalSize}`),
373
439
  )
374
440
  }
375
441
 
376
442
  // 5. Fittings, Hangers, P-Traps, Cleanouts, Angle Stops & Consumables
377
443
  const externalSupplyKeys = new Set<string>()
378
- for (const n of all) {
444
+ const externalSupplyNodeIds: string[] = []
445
+ for (const [key, n] of allEntries) {
379
446
  if (
380
447
  n.type === 'plumbing:pipe-run' &&
381
448
  (n.system === 'cold-water' || n.system === 'hot-water') &&
@@ -385,22 +452,33 @@ export function buildPlumbingBom(
385
452
  const target = nodes[n.targetNodeId]
386
453
  if (target && !target.type.startsWith('plumbing:')) {
387
454
  externalSupplyKeys.add(`${n.system}:${n.targetNodeId}`)
455
+ externalSupplyNodeIds.push(resolveEntryNodeId(key, n), resolveEntryNodeId(n.targetNodeId, target))
388
456
  }
389
457
  }
390
458
  }
391
459
  const externalSupplyStops = externalSupplyKeys.size
392
460
  const fixTotal = schedule.fixtures.length
393
- const dwvFixtureCount = schedule.fixtures.filter((f) => f.drainSize !== null || f.dfu > 0).length
461
+ const dwvFixtures = schedule.fixtures.filter((f) => f.drainSize !== null || f.dfu > 0)
462
+ const dwvFixtureCount = dwvFixtures.length
463
+ const supplyFixtures = schedule.fixtures.filter((f) => f.supplySize !== null || f.wsfuTotal > 0)
394
464
  const supplyFixtureCount =
395
- schedule.fixtures.filter((f) => f.supplySize !== null || f.wsfuTotal > 0).length + externalSupplyStops
465
+ supplyFixtures.length + externalSupplyStops
396
466
  const hasDwvScope = totalDwvFt > 0 || dwvFixtureCount > 0
397
467
  const hasSupplyScope = totalSupplyFt > 0 || supplyFixtureCount > 0
398
468
  if (fixTotal > 0 || totalPipeFt > 0) {
399
- const pTraps = schedule.fixtures.filter(
469
+ const pTrapFixtures = schedule.fixtures.filter(
400
470
  (f) => FIXTURE_SPECS[f.fixtureKind]?.needsPTrap && nodes[f.nodeId]?.hasPTrap !== false,
401
- ).length
471
+ )
472
+ const pTraps = pTrapFixtures.length
473
+ const cleanoutEntries = allEntries.filter(([, n]) => n.type === 'plumbing:pipe-run' && n.hasCleanout)
474
+ const dwvRunNodeIds = pipeRunEntries
475
+ .filter((r) => r.system === 'dwv-drain' || r.system === 'dwv-vent')
476
+ .map((r) => r.nodeId)
477
+ const supplyRunNodeIds = pipeRunEntries
478
+ .filter((r) => r.system === 'cold-water' || r.system === 'hot-water' || r.system === 'hot-recirc')
479
+ .map((r) => r.nodeId)
402
480
  const cleanouts = hasDwvScope
403
- ? Math.max(2, all.filter((n) => n.type === 'plumbing:pipe-run' && n.hasCleanout).length)
481
+ ? Math.max(2, cleanoutEntries.length)
404
482
  : 0
405
483
  const dwvFittings = hasDwvScope ? Math.ceil(dwvFixtureCount * 3 + totalDwvFt / 10) : 0
406
484
  const supplyFittings = hasSupplyScope ? Math.ceil(supplyFixtureCount * 4 + totalSupplyFt / 15) : 0
@@ -417,18 +495,33 @@ export function buildPlumbingBom(
417
495
  'mop-sink',
418
496
  'drinking-fountain',
419
497
  ])
498
+ const angleStopNodeIds: string[] = []
420
499
  const angleStops =
421
500
  schedule.fixtures.reduce((sum, f) => {
422
501
  if (NO_ANGLE_STOP_FIXTURES.has(f.fixtureKind)) return sum
423
- return sum + (f.wsfuCold > 0 ? 1 : 0) + (f.wsfuHot > 0 ? 1 : 0)
502
+ const stops = (f.wsfuCold > 0 ? 1 : 0) + (f.wsfuHot > 0 ? 1 : 0)
503
+ if (stops > 0) angleStopNodeIds.push(f.nodeId)
504
+ return sum + stops
424
505
  }, 0) + externalSupplyStops
506
+ angleStopNodeIds.push(...externalSupplyNodeIds)
507
+ const cleanoutNodeIds =
508
+ cleanoutEntries.length > 0
509
+ ? cleanoutEntries.map(([key, n]) => resolveEntryNodeId(key, n))
510
+ : [...dwvRunNodeIds, ...dwvFixtures.map((f) => f.nodeId)]
425
511
 
426
- addLine('Fittings & Accessories', 'DWV Fittings (Wyes, Combos, Sweeps, San-Tees)', 'ea', dwvFittings, 7.5, dwvFittings * 0.08)
427
- addLine('Fittings & Accessories', 'Supply Fittings & Crimp/Expansion Rings', 'ea', supplyFittings, 4.2, supplyFittings * 0.05)
428
- addLine('Fittings & Accessories', 'P-Trap Assemblies (1-1/2" / 2")', 'ea', pTraps, 16, pTraps * 0.25)
429
- addLine('Fittings & Accessories', 'Cleanout Tees & Brass/PVC Plugs', 'ea', cleanouts, 24, cleanouts * 0.4)
430
- addLine('Fittings & Accessories', 'Quarter-Turn Angle Stops & Braided Supply Lines', 'ea', angleStops, 18, angleStops * 0.15)
431
- addLine('Fittings & Accessories', 'Pipe Hangers, J-Hooks, Nail Plates & Solvent Cement', 'ea', hangers, 2.5, hangers * 0.04)
512
+ addLine('Fittings & Accessories', 'DWV Fittings (Wyes, Combos, Sweeps, San-Tees)', 'ea', dwvFittings, 7.5, dwvFittings * 0.08, [
513
+ ...dwvFixtures.map((f) => f.nodeId),
514
+ ...dwvRunNodeIds,
515
+ ])
516
+ addLine('Fittings & Accessories', 'Supply Fittings & Crimp/Expansion Rings', 'ea', supplyFittings, 4.2, supplyFittings * 0.05, [
517
+ ...supplyFixtures.map((f) => f.nodeId),
518
+ ...externalSupplyNodeIds,
519
+ ...supplyRunNodeIds,
520
+ ])
521
+ addLine('Fittings & Accessories', 'P-Trap Assemblies (1-1/2" / 2")', 'ea', pTraps, 16, pTraps * 0.25, pTrapFixtures.map((f) => f.nodeId))
522
+ addLine('Fittings & Accessories', 'Cleanout Tees & Brass/PVC Plugs', 'ea', cleanouts, 24, cleanouts * 0.4, cleanoutNodeIds)
523
+ addLine('Fittings & Accessories', 'Quarter-Turn Angle Stops & Braided Supply Lines', 'ea', angleStops, 18, angleStops * 0.15, angleStopNodeIds)
524
+ addLine('Fittings & Accessories', 'Pipe Hangers, J-Hooks, Nail Plates & Solvent Cement', 'ea', hangers, 2.5, hangers * 0.04, pipeRunEntries.map((r) => r.nodeId))
432
525
  }
433
526
 
434
527
  const subtotalMaterialUsd = round2(lines.reduce((s, l) => s + l.materialUsd, 0))
@@ -546,6 +639,7 @@ export function toPlumbingPascalBom(
546
639
  extendedLaborHours: line.laborHours,
547
640
  laborRateUsd: effectiveLaborRate,
548
641
  extendedLaborUsd: line.laborUsd,
642
+ ...(line.nodeIds && line.nodeIds.length > 0 ? { nodeIds: line.nodeIds } : {}),
549
643
  })
550
644
  })
551
645
 
@@ -557,15 +651,23 @@ export function toPlumbingPascalBom(
557
651
  }
558
652
  }
559
653
 
654
+ export interface PascalBomBuildOptions {
655
+ readonly injectedParts?: readonly CallerInjectedPart[]
656
+ }
657
+
560
658
  export function buildPlumbingPascalBom(
561
659
  nodes: Readonly<Record<string, SceneNodeLike>>,
562
660
  options: PlumbingPricingOptions = {},
661
+ bomOptions: PascalBomBuildOptions = {},
563
662
  ): PascalBomFragment {
564
663
  const effectiveLaborRate =
565
664
  typeof options.laborRateUsd === 'number' && Number.isFinite(options.laborRateUsd)
566
665
  ? Math.max(0, options.laborRateUsd)
567
666
  : 125
568
- return toPlumbingPascalBom(buildPlumbingBom(nodes, { ...options, laborRateUsd: effectiveLaborRate }), {
667
+ const fragment = toPlumbingPascalBom(buildPlumbingBom(nodes, { ...options, laborRateUsd: effectiveLaborRate }), {
569
668
  laborRateUsd: effectiveLaborRate,
570
669
  })
670
+ return Array.isArray(bomOptions.injectedParts) && bomOptions.injectedParts.length > 0
671
+ ? applyCallerInjectedPartsToFragment(fragment, bomOptions.injectedParts)
672
+ : fragment
571
673
  }