@kruschdev/pascal-plugin-plumbing 0.0.0-stage → 0.2.1

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,9 @@
1
+ export * from './tables'
2
+ export * from './rules'
3
+ export * from './sizing'
4
+ export * from './framing-interop'
5
+ export * from './routing'
6
+ export * from './autopopulate'
7
+ export * from './schedule'
8
+ export * from './audit'
9
+ export * from './pricing'
@@ -0,0 +1,473 @@
1
+ /**
2
+ * Pure Priced Plumbing BOM Takeoff & CSV Export (`src/ipc/pricing.ts`).
3
+ * Framework-free: zero React, Three.js, or `@pascal-app/*` imports.
4
+ */
5
+
6
+ import type { SceneNodeLike } from './framing-interop'
7
+ import { type PlumbingSchedule, buildPlumbingSchedule } from './schedule'
8
+ import {
9
+ type CodeEdition,
10
+ type EquipmentKind,
11
+ FIXTURE_SPECS,
12
+ type FixtureKind,
13
+ type ManifoldKind,
14
+ type OccupancyType,
15
+ type PipeMaterial,
16
+ type PipeNominalSize,
17
+ csvEscape,
18
+ round2,
19
+ } from './tables'
20
+
21
+ export type PlumbingPricingOptions = {
22
+ laborRateUsd?: number
23
+ materialMarkupPct?: number
24
+ salesTaxPct?: number
25
+ codeEdition?: CodeEdition
26
+ occupancy?: OccupancyType
27
+ supplyMaterial?: 'pex-a' | 'pex-b' | 'copper-l' | 'copper-m' | 'cpvc'
28
+ /** Optional precomputed schedule to avoid redundant scene scans. */
29
+ schedule?: PlumbingSchedule
30
+ }
31
+
32
+ export type PlumbingBomLine = {
33
+ category: 'Equipment' | 'Manifolds' | 'Fixtures' | 'Pipe & Tubing' | 'Fittings & Accessories'
34
+ item: string
35
+ unit: string
36
+ quantity: number
37
+ unitMaterialUsd: number
38
+ materialUsd: number
39
+ laborHours: number
40
+ laborUsd: number
41
+ totalUsd: number
42
+ }
43
+
44
+ export type PlumbingBomResult = {
45
+ lines: PlumbingBomLine[]
46
+ subtotalMaterialUsd: number
47
+ markupUsd: number
48
+ taxUsd: number
49
+ subtotalLaborUsd: number
50
+ totalLaborHours: number
51
+ grandTotalUsd: number
52
+ }
53
+
54
+ const EQUIPMENT_PRICES: Readonly<Record<EquipmentKind, { mat: number; hrs: number; label: string }>> = {
55
+ 'water-heater-tank-gas': { mat: 980, hrs: 5.0, label: 'Gas Tank Water Heater (50/75 gal)' },
56
+ 'water-heater-tank-electric': { mat: 780, hrs: 4.0, label: 'Electric Tank Water Heater (50/75 gal)' },
57
+ 'water-heater-tankless-gas': { mat: 1650, hrs: 7.5, label: 'Tankless Condensing Gas Water Heater' },
58
+ 'water-heater-hpwh': { mat: 1890, hrs: 5.5, label: 'Hybrid Heat Pump Water Heater' },
59
+ 'expansion-tank': { mat: 68, hrs: 0.75, label: 'Thermal Expansion Tank (IPC 607.3)' },
60
+ 'main-shutoff-prv': { mat: 215, hrs: 2.0, label: 'Main Shutoff + PRV + Check Valve Assembly' },
61
+ 'water-softener': { mat: 890, hrs: 3.5, label: 'Whole-House Water Softener + Brine Tank' },
62
+ 'sump-pump': { mat: 460, hrs: 4.0, label: 'Cast-Iron Sump Pump + Basin + Check Valve' },
63
+ 'sewage-ejector': { mat: 840, hrs: 5.5, label: 'Sewage Ejector Pump + Sealed Basin' },
64
+ 'recirc-pump': { mat: 295, hrs: 2.0, label: 'Hot Water Recirculation Pump + Aquastat' },
65
+ 'gas-meter': { mat: 320, hrs: 2.5, label: 'Gas Service Meter Bar & Regulator Assembly' },
66
+ 'backwater-valve': { mat: 195, hrs: 2.0, label: 'Mainline Backwater Valve (IPC 714)' },
67
+ }
68
+
69
+ const MANIFOLD_PRICES: Readonly<Record<ManifoldKind, { mat: number; hrs: number; label: string }>> = {
70
+ 'pex-homerun': { mat: 345, hrs: 3.5, label: 'PEX Home-Run Manifold w/ Ball Valves' },
71
+ 'copper-header': { mat: 265, hrs: 3.0, label: 'Type L Copper Distribution Header' },
72
+ 'gas-csst': { mat: 185, hrs: 2.0, label: 'CSST Fuel Gas Distribution Manifold' },
73
+ }
74
+
75
+ const FIXTURE_ROUGH_PRICES: Readonly<Record<FixtureKind, { mat: number; hrs: number }>> = {
76
+ 'water-closet': { mat: 290, hrs: 2.5 },
77
+ lavatory: { mat: 240, hrs: 2.0 },
78
+ 'kitchen-sink': { mat: 380, hrs: 2.5 },
79
+ bathtub: { mat: 520, hrs: 4.0 },
80
+ shower: { mat: 640, hrs: 4.5 },
81
+ 'tub-shower-combo': { mat: 680, hrs: 5.0 },
82
+ dishwasher: { mat: 65, hrs: 1.0 },
83
+ 'clothes-washer': { mat: 95, hrs: 1.5 },
84
+ 'laundry-sink': { mat: 210, hrs: 2.0 },
85
+ 'bar-sink': { mat: 190, hrs: 1.8 },
86
+ 'floor-drain': { mat: 85, hrs: 1.5 },
87
+ 'hose-bibb': { mat: 58, hrs: 1.0 },
88
+ bidet: { mat: 310, hrs: 2.0 },
89
+ 'garbage-disposal': { mat: 185, hrs: 1.2 },
90
+ urinal: { mat: 420, hrs: 3.0 },
91
+ 'mop-sink': { mat: 360, hrs: 2.8 },
92
+ 'drinking-fountain': { mat: 680, hrs: 3.0 },
93
+ 'grease-interceptor': { mat: 1150, hrs: 6.0 },
94
+ }
95
+
96
+ const PEX_ROLL_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
97
+ '3/8"': 52,
98
+ '1/2"': 62,
99
+ '3/4"': 95,
100
+ '1"': 145,
101
+ '1-1/4"': 215,
102
+ '1-1/2"': 295,
103
+ '2"': 440,
104
+ '2-1/2"': 620,
105
+ '3"': 840,
106
+ '4"': 1250,
107
+ '6"': 2100,
108
+ }
109
+
110
+ const COPPER_STICK_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
111
+ '3/8"': 42,
112
+ '1/2"': 54,
113
+ '3/4"': 86,
114
+ '1"': 128,
115
+ '1-1/4"': 176,
116
+ '1-1/2"': 235,
117
+ '2"': 345,
118
+ '2-1/2"': 490,
119
+ '3"': 660,
120
+ '4"': 1020,
121
+ '6"': 1850,
122
+ }
123
+
124
+ const DWV_PLASTIC_STICK_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
125
+ '3/8"': 8,
126
+ '1/2"': 9,
127
+ '3/4"': 10,
128
+ '1"': 11,
129
+ '1-1/4"': 11.5,
130
+ '1-1/2"': 12,
131
+ '2"': 16,
132
+ '2-1/2"': 22,
133
+ '3"': 28,
134
+ '4"': 42,
135
+ '6"': 86,
136
+ }
137
+
138
+ const CAST_IRON_STICK_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
139
+ '3/8"': 48,
140
+ '1/2"': 52,
141
+ '3/4"': 56,
142
+ '1"': 60,
143
+ '1-1/4"': 64,
144
+ '1-1/2"': 68,
145
+ '2"': 74,
146
+ '2-1/2"': 84,
147
+ '3"': 92,
148
+ '4"': 125,
149
+ '6"': 215,
150
+ }
151
+
152
+ const BLACK_IRON_STICK_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
153
+ '3/8"': 28,
154
+ '1/2"': 36,
155
+ '3/4"': 48,
156
+ '1"': 68,
157
+ '1-1/4"': 94,
158
+ '1-1/2"': 118,
159
+ '2"': 162,
160
+ '2-1/2"': 225,
161
+ '3"': 295,
162
+ '4"': 420,
163
+ '6"': 720,
164
+ }
165
+
166
+ const CSST_ROLL_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
167
+ '3/8"': 135,
168
+ '1/2"': 165,
169
+ '3/4"': 225,
170
+ '1"': 310,
171
+ '1-1/4"': 420,
172
+ '1-1/2"': 540,
173
+ '2"': 720,
174
+ '2-1/2"': 920,
175
+ '3"': 1150,
176
+ '4"': 1550,
177
+ '6"': 2400,
178
+ }
179
+
180
+ const CPVC_STICK_PRICE_BY_SIZE: Readonly<Record<PipeNominalSize, number>> = {
181
+ '3/8"': 12,
182
+ '1/2"': 14,
183
+ '3/4"': 20,
184
+ '1"': 30,
185
+ '1-1/4"': 42,
186
+ '1-1/2"': 56,
187
+ '2"': 78,
188
+ '2-1/2"': 115,
189
+ '3"': 155,
190
+ '4"': 225,
191
+ '6"': 390,
192
+ }
193
+
194
+ function pipeUnitPricing(material: PipeMaterial, size: PipeNominalSize): {
195
+ unit: string
196
+ unitFt: number
197
+ matPerUnit: number
198
+ hrsPer100Ft: number
199
+ } {
200
+ if (material === 'pex-a' || material === 'pex-b') {
201
+ const mat = PEX_ROLL_PRICE_BY_SIZE[size] ?? 95
202
+ return { unit: '100 ft roll', unitFt: 100, matPerUnit: mat, hrsPer100Ft: 3.2 }
203
+ }
204
+ if (material === 'copper-l' || material === 'copper-m') {
205
+ const mat = COPPER_STICK_PRICE_BY_SIZE[size] ?? 86
206
+ return { unit: '20 ft stick', unitFt: 20, matPerUnit: mat, hrsPer100Ft: 7.5 }
207
+ }
208
+ if (material === 'pvc-dwv' || material === 'abs-dwv') {
209
+ const mat = DWV_PLASTIC_STICK_PRICE_BY_SIZE[size] ?? 28
210
+ return { unit: '10 ft stick', unitFt: 10, matPerUnit: mat, hrsPer100Ft: 5.5 }
211
+ }
212
+ if (material === 'cast-iron') {
213
+ const mat = CAST_IRON_STICK_PRICE_BY_SIZE[size] ?? 92
214
+ return { unit: '10 ft stick', unitFt: 10, matPerUnit: mat, hrsPer100Ft: 9.0 }
215
+ }
216
+ if (material === 'black-iron' || material === 'csst') {
217
+ const mat =
218
+ material === 'csst'
219
+ ? (CSST_ROLL_PRICE_BY_SIZE[size] ?? 225)
220
+ : (BLACK_IRON_STICK_PRICE_BY_SIZE[size] ?? 48)
221
+ const unitFt = material === 'csst' ? 50 : 10
222
+ return { unit: `${unitFt} ft`, unitFt, matPerUnit: mat, hrsPer100Ft: material === 'csst' ? 4.0 : 8.5 }
223
+ }
224
+ if (material === 'cpvc') {
225
+ const mat = CPVC_STICK_PRICE_BY_SIZE[size] ?? 24
226
+ return { unit: '10 ft stick', unitFt: 10, matPerUnit: mat, hrsPer100Ft: 4.5 }
227
+ }
228
+ const rigidMat = size === '6"' ? 42 : size === '4"' ? 28 : 22
229
+ return { unit: '10 ft stick', unitFt: 10, matPerUnit: rigidMat, hrsPer100Ft: 4.5 }
230
+ }
231
+
232
+ /**
233
+ * Generate a complete priced plumbing BOM takeoff for the scene.
234
+ */
235
+ export function buildPlumbingBom(
236
+ nodes: Readonly<Record<string, SceneNodeLike>>,
237
+ options: PlumbingPricingOptions = {},
238
+ ): PlumbingBomResult {
239
+ const {
240
+ laborRateUsd: rawLaborRateUsd = 115,
241
+ materialMarkupPct: rawMaterialMarkupPct = 0,
242
+ salesTaxPct: rawSalesTaxPct = 0,
243
+ codeEdition = 'IPC-2021',
244
+ occupancy = 'residential',
245
+ supplyMaterial,
246
+ } = options
247
+ const laborRateUsd = Number.isFinite(rawLaborRateUsd) ? Math.max(0, rawLaborRateUsd) : 115
248
+ const materialMarkupPct = Number.isFinite(rawMaterialMarkupPct) ? Math.max(0, rawMaterialMarkupPct) : 0
249
+ const salesTaxPct = Number.isFinite(rawSalesTaxPct) ? Math.max(0, rawSalesTaxPct) : 0
250
+ const schedule =
251
+ options.schedule ??
252
+ buildPlumbingSchedule(nodes, {
253
+ codeEdition,
254
+ occupancy,
255
+ ...(supplyMaterial ? { supplyMaterial } : {}),
256
+ })
257
+ const all = Object.values(nodes)
258
+ const lines: PlumbingBomLine[] = []
259
+
260
+ const addLine = (
261
+ category: PlumbingBomLine['category'],
262
+ item: string,
263
+ unit: string,
264
+ quantity: number,
265
+ unitMaterialUsd: number,
266
+ laborHours: number,
267
+ ) => {
268
+ if (quantity <= 0) return
269
+ const mat = round2(quantity * unitMaterialUsd)
270
+ const hrs = round2(laborHours)
271
+ const lab = round2(hrs * laborRateUsd)
272
+ lines.push({
273
+ category,
274
+ item,
275
+ unit,
276
+ quantity,
277
+ unitMaterialUsd: round2(unitMaterialUsd),
278
+ materialUsd: mat,
279
+ laborHours: hrs,
280
+ laborUsd: lab,
281
+ totalUsd: round2(mat + lab),
282
+ })
283
+ }
284
+
285
+ // 1. Equipment (separate roughInOnly SKU-linked units so real-world water heater SKUs aren't double-billed)
286
+ const eqFullCounts = new Map<EquipmentKind, number>()
287
+ const eqRoughOnlyCounts = new Map<EquipmentKind, number>()
288
+ for (const n of all) {
289
+ if (n.type !== 'plumbing:equipment') continue
290
+ const k = (n.equipmentKind as EquipmentKind) || 'water-heater-tank-electric'
291
+ if (n.roughInOnly === true) {
292
+ eqRoughOnlyCounts.set(k, (eqRoughOnlyCounts.get(k) ?? 0) + 1)
293
+ } else {
294
+ eqFullCounts.set(k, (eqFullCounts.get(k) ?? 0) + 1)
295
+ }
296
+ }
297
+ for (const [k, qty] of eqFullCounts) {
298
+ const p = EQUIPMENT_PRICES[k] ?? { mat: 350, hrs: 2.5, label: k }
299
+ addLine('Equipment', p.label, 'ea', qty, p.mat, qty * p.hrs)
300
+ }
301
+ for (const [k, qty] of eqRoughOnlyCounts) {
302
+ const p = EQUIPMENT_PRICES[k] ?? { mat: 350, hrs: 2.5, label: k }
303
+ addLine('Equipment', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.25), round2(qty * p.hrs * 0.55))
304
+ }
305
+
306
+ // 2. Manifolds (separate roughInOnly SKU-linked manifolds so catalog SKUs aren't double-billed)
307
+ const manFullCounts = new Map<ManifoldKind, number>()
308
+ const manRoughOnlyCounts = new Map<ManifoldKind, number>()
309
+ for (const n of all) {
310
+ if (n.type !== 'plumbing:manifold') continue
311
+ const k = (n.manifoldKind as ManifoldKind) || 'pex-homerun'
312
+ if (n.roughInOnly === true) {
313
+ manRoughOnlyCounts.set(k, (manRoughOnlyCounts.get(k) ?? 0) + 1)
314
+ } else {
315
+ manFullCounts.set(k, (manFullCounts.get(k) ?? 0) + 1)
316
+ }
317
+ }
318
+ for (const [k, qty] of manFullCounts) {
319
+ const p = MANIFOLD_PRICES[k] ?? { mat: 295, hrs: 3.0, label: k }
320
+ addLine('Manifolds', p.label, 'ea', qty, p.mat, qty * p.hrs)
321
+ }
322
+ for (const [k, qty] of manRoughOnlyCounts) {
323
+ const p = MANIFOLD_PRICES[k] ?? { mat: 295, hrs: 3.0, label: k }
324
+ addLine('Manifolds', `${p.label} (rough-in connection, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55))
325
+ }
326
+
327
+ // 3. Fixtures (separate linked real-world SKU/furnishing rough-in-only assemblies so porcelain/trim isn't double-billed)
328
+ const fixFullCounts = new Map<FixtureKind, number>()
329
+ const fixRoughOnlyCounts = new Map<FixtureKind, number>()
330
+ for (const f of schedule.fixtures) {
331
+ if (f.roughInOnly) {
332
+ fixRoughOnlyCounts.set(f.fixtureKind, (fixRoughOnlyCounts.get(f.fixtureKind) ?? 0) + 1)
333
+ } else {
334
+ fixFullCounts.set(f.fixtureKind, (fixFullCounts.get(f.fixtureKind) ?? 0) + 1)
335
+ }
336
+ }
337
+ for (const [k, qty] of fixFullCounts) {
338
+ const p = FIXTURE_ROUGH_PRICES[k] ?? { mat: 200, hrs: 2.0 }
339
+ addLine('Fixtures', `${k} (rough-in + trim)`, 'ea', qty, p.mat, qty * p.hrs)
340
+ }
341
+ for (const [k, qty] of fixRoughOnlyCounts) {
342
+ const p = FIXTURE_ROUGH_PRICES[k] ?? { mat: 200, hrs: 2.0 }
343
+ addLine('Fixtures', `${k} (rough-in stub-out, SKU linked)`, 'ea', qty, round2(p.mat * 0.35), round2(qty * p.hrs * 0.55))
344
+ }
345
+
346
+ // 4. Pipe & Tubing (with 10% waste factor)
347
+ let totalPipeFt = 0
348
+ let totalDwvFt = 0
349
+ let totalSupplyFt = 0
350
+ for (const row of schedule.pipeSummary) {
351
+ const billedFt = row.totalLengthFt * 1.1
352
+ if (billedFt <= 0) continue
353
+ totalPipeFt += billedFt
354
+ if (row.system === 'dwv-drain' || row.system === 'dwv-vent') totalDwvFt += billedFt
355
+ if (row.system === 'cold-water' || row.system === 'hot-water' || row.system === 'hot-recirc') {
356
+ totalSupplyFt += billedFt
357
+ }
358
+ const p = pipeUnitPricing(row.material, row.nominalSize)
359
+ const qty = Math.max(1, Math.ceil(billedFt / p.unitFt))
360
+ addLine(
361
+ 'Pipe & Tubing',
362
+ `${row.nominalSize} ${row.material.toUpperCase()} (${row.system})`,
363
+ p.unit,
364
+ qty,
365
+ p.matPerUnit,
366
+ (billedFt / 100) * p.hrsPer100Ft,
367
+ )
368
+ }
369
+
370
+ // 5. Fittings, Hangers, P-Traps, Cleanouts, Angle Stops & Consumables
371
+ const externalSupplyKeys = new Set<string>()
372
+ for (const n of all) {
373
+ if (
374
+ n.type === 'plumbing:pipe-run' &&
375
+ (n.system === 'cold-water' || n.system === 'hot-water') &&
376
+ typeof n.targetNodeId === 'string' &&
377
+ n.targetNodeId
378
+ ) {
379
+ const target = nodes[n.targetNodeId]
380
+ if (target && !target.type.startsWith('plumbing:')) {
381
+ externalSupplyKeys.add(`${n.system}:${n.targetNodeId}`)
382
+ }
383
+ }
384
+ }
385
+ const externalSupplyStops = externalSupplyKeys.size
386
+ const fixTotal = schedule.fixtures.length
387
+ const dwvFixtureCount = schedule.fixtures.filter((f) => f.drainSize !== null || f.dfu > 0).length
388
+ const supplyFixtureCount =
389
+ schedule.fixtures.filter((f) => f.supplySize !== null || f.wsfuTotal > 0).length + externalSupplyStops
390
+ const hasDwvScope = totalDwvFt > 0 || dwvFixtureCount > 0
391
+ const hasSupplyScope = totalSupplyFt > 0 || supplyFixtureCount > 0
392
+ if (fixTotal > 0 || totalPipeFt > 0) {
393
+ const pTraps = schedule.fixtures.filter(
394
+ (f) => FIXTURE_SPECS[f.fixtureKind]?.needsPTrap && nodes[f.nodeId]?.hasPTrap !== false,
395
+ ).length
396
+ const cleanouts = hasDwvScope
397
+ ? Math.max(2, all.filter((n) => n.type === 'plumbing:pipe-run' && n.hasCleanout).length)
398
+ : 0
399
+ const dwvFittings = hasDwvScope ? Math.ceil(dwvFixtureCount * 3 + totalDwvFt / 10) : 0
400
+ const supplyFittings = hasSupplyScope ? Math.ceil(supplyFixtureCount * 4 + totalSupplyFt / 15) : 0
401
+ const hangers = Math.ceil(totalPipeFt / 4)
402
+ const NO_ANGLE_STOP_FIXTURES = new Set<FixtureKind>([
403
+ 'bathtub',
404
+ 'shower',
405
+ 'tub-shower-combo',
406
+ 'floor-drain',
407
+ 'hose-bibb',
408
+ 'garbage-disposal',
409
+ 'grease-interceptor',
410
+ 'urinal',
411
+ 'mop-sink',
412
+ 'drinking-fountain',
413
+ ])
414
+ const angleStops =
415
+ schedule.fixtures.reduce((sum, f) => {
416
+ if (NO_ANGLE_STOP_FIXTURES.has(f.fixtureKind)) return sum
417
+ return sum + (f.wsfuCold > 0 ? 1 : 0) + (f.wsfuHot > 0 ? 1 : 0)
418
+ }, 0) + externalSupplyStops
419
+
420
+ addLine('Fittings & Accessories', 'DWV Fittings (Wyes, Combos, Sweeps, San-Tees)', 'ea', dwvFittings, 7.5, dwvFittings * 0.08)
421
+ addLine('Fittings & Accessories', 'Supply Fittings & Crimp/Expansion Rings', 'ea', supplyFittings, 4.2, supplyFittings * 0.05)
422
+ addLine('Fittings & Accessories', 'P-Trap Assemblies (1-1/2" / 2")', 'ea', pTraps, 16, pTraps * 0.25)
423
+ addLine('Fittings & Accessories', 'Cleanout Tees & Brass/PVC Plugs', 'ea', cleanouts, 24, cleanouts * 0.4)
424
+ addLine('Fittings & Accessories', 'Quarter-Turn Angle Stops & Braided Supply Lines', 'ea', angleStops, 18, angleStops * 0.15)
425
+ addLine('Fittings & Accessories', 'Pipe Hangers, J-Hooks, Nail Plates & Solvent Cement', 'ea', hangers, 2.5, hangers * 0.04)
426
+ }
427
+
428
+ const subtotalMaterialUsd = round2(lines.reduce((s, l) => s + l.materialUsd, 0))
429
+ const subtotalLaborUsd = round2(lines.reduce((s, l) => s + l.laborUsd, 0))
430
+ const totalLaborHours = round2(lines.reduce((s, l) => s + l.laborHours, 0))
431
+ const markupUsd = round2(subtotalMaterialUsd * (materialMarkupPct / 100))
432
+ const taxUsd = round2((subtotalMaterialUsd + markupUsd) * (salesTaxPct / 100))
433
+ const grandTotalUsd = round2(subtotalMaterialUsd + markupUsd + taxUsd + subtotalLaborUsd)
434
+
435
+ return {
436
+ lines,
437
+ subtotalMaterialUsd,
438
+ markupUsd,
439
+ taxUsd,
440
+ subtotalLaborUsd,
441
+ totalLaborHours,
442
+ grandTotalUsd,
443
+ }
444
+ }
445
+
446
+ export function formatPlumbingBomCsv(bom: PlumbingBomResult): string {
447
+ const header = ['Category', 'Item', 'Unit', 'Qty', 'Unit Material ($)', 'Material ($)', 'Labor (hrs)', 'Labor ($)', 'Total ($)']
448
+ const rows = bom.lines.map((l) =>
449
+ [
450
+ l.category,
451
+ l.item,
452
+ l.unit,
453
+ l.quantity,
454
+ l.unitMaterialUsd.toFixed(2),
455
+ l.materialUsd.toFixed(2),
456
+ l.laborHours.toFixed(2),
457
+ l.laborUsd.toFixed(2),
458
+ l.totalUsd.toFixed(2),
459
+ ]
460
+ .map(csvEscape)
461
+ .join(','),
462
+ )
463
+ rows.push(['Summary', 'Material Subtotal', '', '', '', bom.subtotalMaterialUsd.toFixed(2), '', '', bom.subtotalMaterialUsd.toFixed(2)].join(','))
464
+ if (bom.markupUsd > 0) {
465
+ rows.push(['Summary', 'Material Markup', '', '', '', bom.markupUsd.toFixed(2), '', '', bom.markupUsd.toFixed(2)].join(','))
466
+ }
467
+ if (bom.taxUsd > 0) {
468
+ rows.push(['Summary', 'Sales Tax', '', '', '', bom.taxUsd.toFixed(2), '', '', bom.taxUsd.toFixed(2)].join(','))
469
+ }
470
+ rows.push(['Summary', 'Labor Subtotal', '', '', '', '', bom.totalLaborHours.toFixed(2), bom.subtotalLaborUsd.toFixed(2), bom.subtotalLaborUsd.toFixed(2)].join(','))
471
+ rows.push(['Summary', 'Grand Total', '', '', '', round2(bom.subtotalMaterialUsd + bom.markupUsd + bom.taxUsd).toFixed(2), bom.totalLaborHours.toFixed(2), bom.subtotalLaborUsd.toFixed(2), bom.grandTotalUsd.toFixed(2)].join(','))
472
+ return [header.join(','), ...rows].join('\n')
473
+ }