Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/lib/cost/geometry.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/**43 * Moteur de géométrie et de quantités : description du bâtiment → quantités44 * d'assemblages, chaque ligne accompagnée de sa formule. Module PUR.45 *46 * Principe (§193) : tout ce qu'une formule explicite peut calculer est calculé47 * ici, jamais par l'IA. Les constantes (hauteur d'étage 9 pi, rapport de forme48 * 1,5, 1 fenêtre de 10 pi² par 90 pi² de plancher…) sont des HYPOTHÈSES de49 * prise de quantités résidentielle, affichées dans chaque formule.50 *51 * Coût de REMPLACEMENT (et non de reproduction) : le bâtiment est reconstruit52 * avec les composantes modernes équivalentes (2 × 6 R24, R50 au comble,53 * poutrelles ajourées), quel que soit son année de construction.54 */55import type { BuildingSpec, Quality, QuantityLine, AttributeSource } from "./types";56import { KITCHEN_LINEAR_FT } from "./taxonomy";5758export const GEOMETRY_VERSION = "1.0";59const FT3_TO_M3 = 0.0283168;60export const STORY_HEIGHT_FT = 9; // 8 pi de plafond + structure de plancher61export const ASPECT_RATIO = 1.5; // rectangle longueur/largeur62export const WINDOW_SQFT = 10; // fenêtre type ~30 × 48 po63export const WINDOW_PER_SQFT_FLOOR = 0.011; // ≈ 11 % de la surface de plancher vitrée64export const PARTITION_LF_PER_SQFT = 0.09; // pi lin de cloison par pi² de plancher65export const BASEMENT_WALL_FT = 8;66export const GARAGE_WALL_FT = 10;6768export interface Geometry {69 footprintSqft: number;70 widthFt: number;71 lengthFt: number;72 perimeterFt: number;73 exposedPerimeterFt: number; // murs extérieurs non mitoyens74 exposedShare: number;75 wallHeightFt: number;76 grossWallSqft: number;77 gableSqft: number;78 windowCount: number;79 extDoors: number;80 patioDoors: number;81 openingsSqft: number;82 netWallSqft: number;83 roofSlopeFactor: number;84 roofSqft: number;85 basementFloorSqft: number;86 basementFinishedSqft: number;87 garageSqft: number;88 garagePerimeterFt: number;89 garageExposedPerimeterFt: number;90}9192const r1 = (x: number) => Math.round(x * 10) / 10;93const r0 = (x: number) => Math.round(x);9495export function computeGeometry(b: BuildingSpec): Geometry {96 const stories = Math.max(1, b.stories || 1);97 const footprint = b.footprintSqft && b.footprintSqft > 100 ? b.footprintSqft : b.grossFloorAreaSqft / stories;98 const width = Math.sqrt(footprint / ASPECT_RATIO);99 const length = ASPECT_RATIO * width;100 const perimeter = 2 * (width + length);101 const exposedShare = b.type === "semi_detached" ? 1 - length / perimeter : b.type === "row" || b.type === "condo" ? 1 - (2 * length) / perimeter : 1;102 const exposedPerimeter = perimeter * exposedShare;103 const grossWall = exposedPerimeter * STORY_HEIGHT_FT * stories;104 const pitch = b.roofGeometry === "flat" ? 0 : b.roofPitch || 6;105 const gable = b.roofGeometry === "gable" && b.type !== "condo" ? 2 * ((width * width * pitch) / 24) * (b.type === "detached" || b.type === "chalet" ? 1 : 0.5) : 0;106 const windowCount = b.windowCount ?? Math.max(4, Math.round(b.grossFloorAreaSqft * WINDOW_PER_SQFT_FLOOR));107 const extDoors = b.type === "row" || b.type === "condo" ? 1 : 2;108 const patioDoors = b.type === "condo" ? 1 : b.deckSqft > 0 || b.quality !== "economy" ? 1 : 0;109 const openings = windowCount * WINDOW_SQFT + extDoors * 20 + patioDoors * 40;110 const slope = Math.sqrt(1 + (pitch / 12) ** 2);111 const complexity = b.roofGeometry === "hip" ? 1.05 : b.roofGeometry === "complex" ? 1.12 : b.roofGeometry === "mansard" ? 1.15 : 1;112 const garageSqft = b.garage.type === "none" || b.garage.type === "carport" ? 0 : b.garage.areaSqft ?? (b.garage.spaces >= 3 ? 700 : b.garage.spaces === 2 ? 480 : 264);113 const roofFootprint = footprint + (b.garage.type === "attached" ? garageSqft : 0);114 const roofSqft = b.type === "condo" ? 0 : roofFootprint * slope * 1.1 * complexity;115 const basementFloor = b.basement === "none" || b.basement === "crawl" ? 0 : footprint * 0.9;116 const finishedPct = b.basement === "finished" || b.basement === "walkout" ? Math.max(b.basementFinishedPct, 0.9) : b.basement === "partial" ? Math.max(b.basementFinishedPct, 0.5) : b.basementFinishedPct;117 const gw = Math.sqrt(garageSqft / 1.1);118 const garagePerimeter = garageSqft ? 2 * (gw + 1.1 * gw) : 0;119 const garageExposed = b.garage.type === "detached" ? garagePerimeter : b.garage.type === "attached" ? garagePerimeter * 0.75 : b.garage.type === "integrated" ? garagePerimeter * 0.5 : 0;120 return {121 footprintSqft: r0(footprint), widthFt: r1(width), lengthFt: r1(length), perimeterFt: r1(perimeter), exposedPerimeterFt: r1(exposedPerimeter), exposedShare: r1(exposedShare * 100) / 100,122 wallHeightFt: STORY_HEIGHT_FT, grossWallSqft: r0(grossWall), gableSqft: r0(gable), windowCount, extDoors, patioDoors, openingsSqft: r0(openings),123 netWallSqft: r0(Math.max(0, grossWall + gable - openings)), roofSlopeFactor: r1(slope * 100) / 100, roofSqft: r0(roofSqft),124 basementFloorSqft: r0(basementFloor), basementFinishedSqft: r0(basementFloor * finishedPct),125 garageSqft: r0(garageSqft), garagePerimeterFt: r1(garagePerimeter), garageExposedPerimeterFt: r1(garageExposed),126 };127}128129/** Répartition de plancher par défaut selon la qualité (si non fournie). */130export function defaultFlooring(q: Quality): BuildingSpec["flooring"] {131 switch (q) {132 case "economy": return { vinyl_plank: 0.7, laminate: 0.2, ceramic: 0.1 };133 case "standard": return { engineered: 0.5, vinyl_plank: 0.35, ceramic: 0.15 };134 case "superior": return { hardwood: 0.6, ceramic: 0.25, vinyl_plank: 0.15 };135 default: return { hardwood: 0.65, ceramic: 0.35 };136 }137}138139/**140 * Quantités d'assemblages à partir de la description du bâtiment.141 * `overrides` remplace la quantité d'un assemblage (source « user »/« AI »/« listing »).142 */143export function deriveQuantities(b: BuildingSpec, overrides: Record<string, number> = {}, overrideSource: AttributeSource = "user", excluded: string[] = []): { geometry: Geometry; lines: QuantityLine[]; warnings: string[] } {144 const g = computeGeometry(b);145 const warnings: string[] = [];146 const lines: QuantityLine[] = [];147 const stories = Math.max(1, b.stories || 1);148 const GFA = b.grossFloorAreaSqft;149 const F = g.footprintSqft;150 const P = g.perimeterFt;151 const Pe = g.exposedPerimeterFt;152 const units = Math.max(1, b.units || 1);153 const finished = g.basementFinishedSqft;154 const livable = GFA + finished;155156 const add = (code: string, qty: number, formula: string, inputs: Record<string, number | string> = {}, unit: QuantityLine["unit"] = "pi2") => {157 if (excluded.includes(code)) return;158 if (!(qty > 0)) return;159 lines.push({ assemblyCode: code, quantity: Math.round(qty * 100) / 100, unit, source: "derived", formula, inputs });160 };161162 const isCondo = b.type === "condo";163 if (isCondo) warnings.push("condo_common_areas");164165 /* ---------------------------------------------------------- site / fondations */166 if (!isCondo) {167 const hasBasement = g.basementFloorSqft > 0;168 const depthFt = hasBasement ? 9 : b.basement === "crawl" ? 5 : b.foundation === "piers" ? 0 : 4.5;169 if (depthFt > 0) {170 const excM3 = (F + 3 * P) * depthFt * FT3_TO_M3;171 add("SITE-EXCAV-BASEMENT", excM3, "(empreinte + 3 pi × périmètre) × profondeur × 0,0283 m³/pi³", { empreinte_pi2: F, perimetre_pi: P, profondeur_pi: depthFt }, "m3");172 add("SITE-BACKFILL", P * 3 * (depthFt - 1) * FT3_TO_M3, "périmètre × 3 pi × (profondeur − 1 pi) × 0,0283", { perimetre_pi: P, profondeur_pi: depthFt }, "m3");173 add("SITE-DRAIN-FRENCH", P, "périmètre de fondation", { perimetre_pi: P }, "pi_lin");174 }175 add("SITE-SERVICE-CONNECTIONS", 1, "1 branchement par bâtiment", {}, "lump");176 if (b.foundation === "piers") {177 add("FND-PIER", Math.ceil(F / 64), "empreinte ÷ 64 pi² (grille 8 × 8 pi)", { empreinte_pi2: F }, "unit");178 } else {179 add("FND-FOOTING", P, "périmètre de fondation", { perimetre_pi: P }, "pi_lin");180 const wallH = hasBasement ? BASEMENT_WALL_FT : b.basement === "crawl" ? 4 : 4;181 const wallCode = b.foundation === "concrete_block" || b.foundation === "stone" ? "FND-WALL-BLOCK-8IN" : "FND-WALL-POURED-8IN";182 add(wallCode, P * wallH, `périmètre × hauteur de mur (${wallH} pi)`, { perimetre_pi: P, hauteur_pi: wallH });183 add("FND-DAMPPROOF-MEMBRANE", P * Math.max(0, wallH - 1), "périmètre × (hauteur − 1 pi hors sol)", { perimetre_pi: P, hauteur_pi: wallH });184 if (hasBasement) {185 add("FND-SLAB-BASEMENT-4IN", F, "empreinte du bâtiment", { empreinte_pi2: F });186 add("OPN-WINDOW-BASEMENT", Math.max(2, Math.round(P / 25)), "périmètre ÷ 25 pi", { perimetre_pi: P }, "unit");187 } else if (b.basement === "none") {188 add("FND-SLAB-ON-GRADE-INSULATED", F, "empreinte du bâtiment (dalle sur sol)", { empreinte_pi2: F });189 }190 }191 }192193 /* ------------------------------------------------------------- structure */194 const floorsSqft = isCondo ? GFA : b.basement === "none" && b.foundation !== "piers" ? Math.max(0, GFA - F) : GFA;195 add("STR-FLOOR-IJOIST", floorsSqft, isCondo ? "aire de l'unité" : b.basement === "none" ? "aire d'étages − empreinte (rez-de-chaussée sur dalle)" : "aire d'étages (planchers portés)", { aire_etages_pi2: GFA, empreinte_pi2: F });196 add("STR-WALL-2X6-EXT", g.grossWallSqft + g.gableSqft, "périmètre exposé × 9 pi × étages + pignons", { perimetre_expose_pi: Pe, etages: stories, pignons_pi2: g.gableSqft });197 const partitionsLf = livable * PARTITION_LF_PER_SQFT;198 add("STR-WALL-2X4-INT", partitionsLf * STORY_HEIGHT_FT, "(aire habitable × 0,09 pi lin/pi²) × 9 pi", { aire_habitable_pi2: livable, cloisons_pi_lin: Math.round(partitionsLf) });199 if (!isCondo) {200 add("STR-BEAM-LVL", g.lengthFt, "longueur du bâtiment (poutre centrale)", { longueur_pi: g.lengthFt }, "pi_lin");201 add("STR-COLUMN-STEEL", Math.max(2, Math.round(g.lengthFt / 10)), "longueur ÷ 10 pi", { longueur_pi: g.lengthFt }, "unit");202 add("STR-ROOF-TRUSS-OSB", g.roofSqft, "(empreinte + garage attenant) × facteur de pente × 1,10 débords × complexité", { empreinte_pi2: F, facteur_pente: g.roofSlopeFactor, geometrie: b.roofGeometry });203 }204 const stairs = (stories - 1) + (g.basementFloorSqft > 0 ? 1 : 0);205 add("STR-STAIRS-INTERIOR", stairs, "(étages − 1) + 1 si sous-sol", { etages: stories }, "unit");206207 /* -------------------------------------------------------------- enveloppe */208 add("ENV-INSUL-WALL-R24", g.netWallSqft, "mur brut + pignons − ouvertures", { mur_brut_pi2: g.grossWallSqft, ouvertures_pi2: g.openingsSqft });209 if (!isCondo) add("ENV-INSUL-ATTIC-R50", F + (b.garage.type === "attached" ? 0 : 0), "empreinte (plafond du dernier étage)", { empreinte_pi2: F });210 const mix = normalizeMix(b.siding);211 const SIDING: Record<string, string> = { vinyl: "ENV-SIDING-VINYL", brick: "ENV-SIDING-BRICK", fiber_cement: "ENV-SIDING-FIBERCEMENT", wood: "ENV-SIDING-WOOD", stone: "ENV-SIDING-STONE", stucco: "ENV-SIDING-FIBERCEMENT", aluminum: "ENV-SIDING-STEEL", steel: "ENV-SIDING-STEEL" };212 const garageWall = g.garageExposedPerimeterFt * GARAGE_WALL_FT;213 for (const [k, share] of Object.entries(mix)) {214 if (share <= 0) continue;215 add(SIDING[k], (g.netWallSqft + garageWall * 0.85) * share, `(mur net + murs de garage) × ${Math.round(share * 100)} %`, { mur_net_pi2: g.netWallSqft, garage_pi2: Math.round(garageWall), part: share });216 }217 if (!isCondo) {218 add("ENV-SOFFIT-FASCIA", P, "périmètre (débords de toit)", { perimetre_pi: P }, "pi_lin");219 add("EXT-GUTTERS", P * 0.6, "périmètre × 60 % (côtés d'égouttement)", { perimetre_pi: P }, "pi_lin");220 }221222 /* ---------------------------------------------------------------- toiture */223 if (!isCondo) {224 const roofCode = b.roofGeometry === "flat" ? (b.roof === "membrane" || b.roof === "asphalt_shingle" ? "ROOF-MEMBRANE-ELASTO" : "ROOF-EPDM") : b.roof === "metal" ? "ROOF-METAL-STANDING" : b.roof === "membrane" ? "ROOF-MEMBRANE-ELASTO" : "ROOF-ASPHALT-ARCH";225 add(roofCode, g.roofSqft, "surface de toit", { toit_pi2: g.roofSqft });226 }227228 /* ------------------------------------------------------------- ouvertures */229 const WIN: Record<string, string> = { pvc: "OPN-WINDOW-PVC-STD", hybrid: "OPN-WINDOW-HYBRID-STD", aluminum: "OPN-WINDOW-ALU-STD", wood: "OPN-WINDOW-WOOD-STD" };230 add(WIN[b.windows] ?? "OPN-WINDOW-PVC-STD", g.windowCount, b.windowCount != null ? "nombre de fenêtres fourni" : "aire d'étages × 0,011 fenêtre/pi² (≈ 11 % vitré, 10 pi² chacune)", { aire_etages_pi2: GFA, fenetres: g.windowCount }, "unit");231 add(b.quality === "prestige" || b.quality === "superior" ? "OPN-DOOR-EXT-FIBERGLASS" : "OPN-DOOR-EXT-STEEL", g.extDoors, "portes extérieures selon le type de bâtiment", { type: b.type }, "unit");232 add("OPN-DOOR-PATIO-6FT", g.patioDoors, "1 porte-patio si terrasse ou qualité ≥ standard", {}, "unit");233 const intDoors = Math.max(3, Math.round(livable / 180));234 add(b.quality === "superior" || b.quality === "prestige" ? "OPN-DOOR-INT-SOLID" : "OPN-DOOR-INT-HOLLOW", intDoors, "aire habitable ÷ 180 pi²", { aire_habitable_pi2: livable }, "unit");235 add("OPN-DOOR-CLOSET-BIFOLD", Math.max(2, Math.round(livable / 350)), "aire habitable ÷ 350 pi²", { aire_habitable_pi2: livable }, "unit");236 if (g.garageSqft) add(b.garage.spaces >= 2 ? "OPN-DOOR-GARAGE-16X7" : "OPN-DOOR-GARAGE-9X7", 1, "1 porte de garage", {}, "unit");237238 /* ----------------------------------------------------------------- garage */239 if (g.garageSqft && !isCondo) {240 const Ag = g.garageSqft;241 add("FND-SLAB-GARAGE-5IN", Ag, "aire du garage", { garage_pi2: Ag });242 if (b.garage.type !== "integrated") {243 add("FND-FOOTING", g.garagePerimeterFt, "périmètre du garage", { perimetre_garage_pi: g.garagePerimeterFt }, "pi_lin");244 add("FND-WALL-POURED-8IN", g.garageExposedPerimeterFt * 4, "périmètre exposé du garage × 4 pi (mur hors gel)", { perimetre_expose_pi: g.garageExposedPerimeterFt });245 }246 add("STR-WALL-2X6-EXT", garageWall, "périmètre exposé du garage × 10 pi", { perimetre_expose_pi: g.garageExposedPerimeterFt });247 add("ENV-INSUL-WALL-R20", garageWall, "murs de garage", { garage_murs_pi2: Math.round(garageWall) });248 add("INT-DRYWALL-1/2-FINISHED", garageWall, "murs de garage (séparation coupe-feu)", { garage_murs_pi2: Math.round(garageWall) });249 add("INT-DRYWALL-5/8-CEILING", Ag, "plafond du garage", { garage_pi2: Ag });250 add("ELE-LIGHTING-STD", 2, "2 luminaires de garage", {}, "unit");251 if (b.garage.type === "detached") {252 const roofG = Ag * g.roofSlopeFactor * 1.1;253 add("STR-ROOF-TRUSS-OSB", roofG, "aire du garage × facteur de pente × 1,10", { garage_pi2: Ag, facteur_pente: g.roofSlopeFactor });254 add(b.roof === "metal" ? "ROOF-METAL-STANDING" : "ROOF-ASPHALT-ARCH", roofG, "toit du garage détaché", { toit_pi2: Math.round(roofG) });255 }256 }257258 /* -------------------------------------------------------------- intérieur */259 const drywallWalls = g.netWallSqft + partitionsLf * STORY_HEIGHT_FT * 2 + (finished ? P * BASEMENT_WALL_FT * (finished / Math.max(1, g.basementFloorSqft)) : 0);260 add("INT-DRYWALL-1/2-FINISHED", drywallWalls, "face intérieure des murs extérieurs + 2 faces des cloisons + murs du sous-sol fini", { murs_ext_pi2: g.netWallSqft, cloisons_pi2: Math.round(partitionsLf * STORY_HEIGHT_FT * 2), sous_sol_pi2: Math.round(finished ? P * BASEMENT_WALL_FT : 0) });261 add("INT-DRYWALL-5/8-CEILING", livable, "plafonds (aire habitable)", { aire_habitable_pi2: livable });262 add("INT-PAINT-2-COATS", drywallWalls + livable, "murs + plafonds", { murs_pi2: Math.round(drywallWalls), plafonds_pi2: livable });263 const bathFloors = b.bathrooms * (b.bathroomQuality === "superior" || b.bathroomQuality === "prestige" ? 60 : b.bathroomQuality === "standard" ? 45 : 40) + b.powderRooms * 20;264 const floorArea = Math.max(0, GFA - bathFloors);265 const fmix = normalizeMix(Object.keys(b.flooring).length ? b.flooring : defaultFlooring(b.quality));266 const FLOOR: Record<string, string> = { hardwood: "INT-FLOOR-HARDWOOD", engineered: "INT-FLOOR-ENGINEERED", vinyl_plank: "INT-FLOOR-VINYL-PLANK", laminate: "INT-FLOOR-LAMINATE", ceramic: "INT-FLOOR-CERAMIC", carpet: "INT-FLOOR-CARPET" };267 for (const [k, share] of Object.entries(fmix)) if (share > 0 && FLOOR[k]) add(FLOOR[k], floorArea * share, `(aire d'étages − planchers de salles de bain) × ${Math.round(share * 100)} %`, { aire_pi2: floorArea, part: share });268 if (finished) {269 add("BSM-SUBFLOOR-PANELS", finished, "aire finie du sous-sol", { sous_sol_fini_pi2: finished });270 add("INT-FLOOR-VINYL-PLANK", finished, "aire finie du sous-sol (vinyle)", { sous_sol_fini_pi2: finished });271 }272 if (g.basementFloorSqft) add("ENV-INSUL-BASEMENT-R20", P * BASEMENT_WALL_FT, "périmètre × 8 pi (isolation intérieure des murs de sous-sol)", { perimetre_pi: P });273 add("INT-TRIM-BASEBOARD", partitionsLf * 2 + Pe * stories + (finished ? P : 0), "2 × cloisons + périmètre exposé × étages + sous-sol fini", { cloisons_pi_lin: Math.round(partitionsLf), perimetre_expose_pi: Pe }, "pi_lin");274 add("INT-CLOSET-SHELVING", Math.max(8, livable / 60), "aire habitable ÷ 60", { aire_habitable_pi2: livable }, "pi_lin");275276 /* ------------------------------------------------------ cuisine / bains */277 const KIT: Record<Quality, string> = { economy: "KIT-ECONOMY", standard: "KIT-STANDARD", superior: "KIT-SUPERIOR", prestige: "KIT-PRESTIGE" };278 const sizeFactor = Math.min(1.3, Math.max(0.8, GFA / units / 1800));279 add(KIT[b.kitchenQuality], KITCHEN_LINEAR_FT[b.kitchenQuality] * sizeFactor * Math.max(1, b.kitchens), `${KITCHEN_LINEAR_FT[b.kitchenQuality]} pi lin (qualité) × facteur de taille ${sizeFactor.toFixed(2)} × cuisines`, { cuisines: b.kitchens, facteur_taille: Math.round(sizeFactor * 100) / 100 }, "pi_lin");280 const nb = Math.max(0, b.bathrooms);281 if (nb) {282 if (b.bathroomQuality === "prestige") add("BATH-SUPERIOR", nb, "toutes les salles de bain (prestige)", {}, "unit");283 else if (b.bathroomQuality === "superior") {284 add("BATH-SUPERIOR", Math.min(nb, units), "1 salle de bain principale supérieure par logement", {}, "unit");285 add("BATH-STANDARD", Math.max(0, nb - units), "autres salles de bain (standard)", {}, "unit");286 } else if (b.bathroomQuality === "standard") add("BATH-STANDARD", nb, "salles de bain standard", {}, "unit");287 else add("BATH-ECONOMY", nb, "salles de bain économiques", {}, "unit");288 }289 add("BATH-POWDER", b.powderRooms, "salles d'eau", {}, "unit");290291 /* ------------------------------------------------------------- mécanique */292 const fixtures = Math.max(1, b.kitchens) * 2 + nb * 3 + b.powderRooms * 2 + units * 2; // laveuse + chauffe-eau par logement293 add("MEC-PLUMBING-ROUGH-IN", fixtures, "2 par cuisine + 3 par salle de bain + 2 par salle d'eau + 2 par logement (laveuse, chauffe-eau)", { cuisines: b.kitchens, sdb: nb, salles_eau: b.powderRooms, logements: units }, "unit");294 add("MEC-WATER-HEATER-60", units, "1 par logement", { logements: units }, "unit");295 if (!isCondo) add("MEC-DRAINAGE-BASE", 1, "1 par bâtiment", {}, "lump");296 const heated = livable;297 switch (b.heating) {298 case "heat_pump":299 case "geothermal":300 add("MEC-HEATPUMP-CENTRAL", units, "1 par logement", {}, "unit");301 add("MEC-FURNACE-ELECTRIC", units, "fournaise électrique d'appoint", {}, "unit");302 add("MEC-DUCTWORK", heated, "aire chauffée", { aire_pi2: heated });303 if (b.heating === "geothermal") warnings.push("geothermal_as_heatpump");304 break;305 case "furnace_electric":306 case "hydronic":307 add("MEC-FURNACE-ELECTRIC", units, "1 par logement", {}, "unit");308 add("MEC-DUCTWORK", heated, "aire chauffée", { aire_pi2: heated });309 if (b.hasAirConditioning) add("MEC-CENTRAL-AC", units, "climatisation centrale", {}, "unit");310 if (b.heating === "hydronic") warnings.push("hydronic_as_furnace");311 break;312 case "furnace_gas":313 case "furnace_oil":314 add("MEC-FURNACE-GAS", units, "1 par logement", {}, "unit");315 add("MEC-DUCTWORK", heated, "aire chauffée", { aire_pi2: heated });316 if (b.hasAirConditioning) add("MEC-CENTRAL-AC", units, "climatisation centrale", {}, "unit");317 break;318 default: {319 add("MEC-BASEBOARD-ELECTRIC", Math.ceil(heated / 150), "aire chauffée ÷ 150 pi² par plinthe de 1 500 W", { aire_pi2: heated }, "unit");320 if (b.hasAirConditioning) add("MEC-HEATPUMP-WALL", units, "thermopompe murale (climatisation)", {}, "unit");321 if (b.heating === "wood") warnings.push("wood_as_baseboard");322 }323 }324 if (b.hasAirExchanger) add("MEC-HRV", units, "1 par logement", {}, "unit");325326 /* ------------------------------------------------------------ électricité */327 add("ELE-SERVICE-200A", units, "1 entrée par logement", { logements: units }, "unit");328 add("ELE-ROUGH-WIRING", livable, "aire habitable", { aire_habitable_pi2: livable });329 add("ELE-LIGHTING-STD", Math.round(livable / 70), "aire habitable ÷ 70 pi²", { aire_habitable_pi2: livable }, "unit");330 if (!isCondo) add("ELE-LIGHTING-EXT", 3 + (g.garageSqft ? 1 : 0), "3 + 1 si garage", {}, "unit");331332 /* -------------------------------------------------------------- extérieur */333 if (!isCondo) {334 if (b.deckSqft > 0) {335 add("EXT-DECK-TREATED", b.deckSqft, "aire de terrasse fournie", { terrasse_pi2: b.deckSqft });336 const rail = 2.5 * Math.sqrt(b.deckSqft);337 add(b.quality === "superior" || b.quality === "prestige" ? "EXT-DECK-RAILING-ALU" : "EXT-DECK-RAILING-WOOD", rail, "2,5 × √aire de terrasse (3 côtés)", { terrasse_pi2: b.deckSqft }, "pi_lin");338 }339 if (b.drivewaySqft > 0 && b.driveway !== "none") {340 const code = b.driveway === "pavers" ? "EXT-DRIVEWAY-PAVERS" : b.driveway === "gravel" ? "EXT-DRIVEWAY-GRAVEL" : "EXT-DRIVEWAY-ASPHALT";341 add(code, b.drivewaySqft, "aire d'entrée fournie", { entree_pi2: b.drivewaySqft });342 }343 if (b.fenceLinFt > 0) add("EXT-FENCE-WOOD", b.fenceLinFt, "longueur de clôture fournie", {}, "pi_lin");344 if (b.landscapingSqft > 0) add("EXT-LANDSCAPING-SOD", b.landscapingSqft, "aire aménagée fournie", {}, "pi2");345 add("EXT-STEPS-PRECAST", 1, "1 perron", {}, "unit");346 if (b.pool === "above_ground") add("EXT-POOL-ABOVEGROUND", 1, "piscine hors terre", {}, "lump");347 if (b.pool === "inground") add("EXT-POOL-INGROUND", 1, "piscine creusée", {}, "lump");348 }349350 /* ----------------------------------------------------- fusion + surcharges */351 const merged = new Map<string, QuantityLine>();352 for (const l of lines) {353 const cur = merged.get(l.assemblyCode);354 if (cur) {355 cur.quantity = Math.round((cur.quantity + l.quantity) * 100) / 100;356 cur.formula = `${cur.formula} + ${l.formula}`;357 cur.inputs = { ...cur.inputs, ...l.inputs };358 } else merged.set(l.assemblyCode, { ...l });359 }360 for (const [code, qty] of Object.entries(overrides)) {361 if (excluded.includes(code)) continue;362 if (!(qty >= 0)) continue;363 const cur = merged.get(code);364 if (cur) {365 cur.formula = `surcharge (${overrideSource}) — calculé : ${cur.quantity}`;366 cur.quantity = qty;367 cur.source = overrideSource;368 } else if (qty > 0) merged.set(code, { assemblyCode: code, quantity: qty, unit: "pi2", source: overrideSource, formula: `quantité fournie (${overrideSource})`, inputs: {} });369 }370 return { geometry: g, lines: [...merged.values()].filter((l) => l.quantity > 0), warnings };371}372373function normalizeMix(m: object): Record<string, number> {374 const entries = Object.entries(m as Record<string, number | undefined>).filter((e): e is [string, number] => typeof e[1] === "number" && e[1] > 0);375 const total = entries.reduce((s, [, v]) => s + v, 0);376 if (!total) return { vinyl: 1 };377 return Object.fromEntries(entries.map(([k, v]) => [k, v / total]));378}379