Repository Public
1// UQO Éval — Métré : méthode du coût — coût de remplacement vs reproduction, améliorations du site, dépréciation physique (âge-vie ou %),2// obsolescence fonctionnelle (curable/incurable, garde anti-double déduction), externe, terrain → valeur indiquée.3"use client";4import { useState } from "react";5import Link from "next/link";6import type { WorkspaceData } from "./ProjectWorkspace";7import { api, Block, Btn, Err, Field, Input, money, Select, Textarea } from "./ui";8import type { ValuationInput } from "@/lib/metre/types";9import type { ValuationCase } from "@/lib/metre/estimates";1011const uuid = () => crypto.randomUUID();1213export default function ValuationPanel({ d, fr, lang, canWrite, reload }: { d: WorkspaceData; fr: boolean; lang: string; canWrite: boolean; reload: () => Promise<void> }) {14 const [cur, setCur] = useState<ValuationCase | null>(d.valuations[0] ?? null);15 const [err, setErr] = useState<string | null>(null);16 const blank = (): { name: string; estimateVersionId: string | null; costNew: number } & ValuationInput => ({ name: fr ? "Méthode du coût" : "Cost approach", estimateVersionId: d.versions[0]?.id ?? null, basis: "replacement", costNew: 0, basisAdjustmentPct: 0, siteImprovements: 0, physical: { method: "age_life", effectiveAge: null, economicLife: 60, percent: null }, functional: [], external: { amount: 0, note: null }, land: { value: 0, source: "user", note: null } });17 const [f, setF] = useState(() => (cur ? { name: cur.name, estimateVersionId: cur.estimateVersionId, ...cur.input } : blank()));18 const setP = (p: Partial<typeof f>) => setF((x) => ({ ...x, ...p }));19 const save = async () => { setErr(null); try { const r = cur ? await api<{ valuation: ValuationCase }>(`/valuations/${cur.id}`, { method: "PATCH", json: f }) : await api<{ valuation: ValuationCase }>(`/projects/${d.project.id}/valuations`, { method: "POST", json: f }); setCur(r.valuation); await reload(); } catch (e) { setErr((e as Error).message); } };20 const res = cur?.result ?? null;21 const vLinked = d.versions.find((v) => v.id === f.estimateVersionId);22 return (23 <div className="space-y-12">24 <Block num="01" title={fr ? "Coût à neuf retenu" : "Cost new retained"} aside={<div className="flex gap-2">{d.valuations.length > 1 && <Select value={cur?.id ?? ""} onChange={(e) => { const c = d.valuations.find((x) => x.id === e.target.value) ?? null; setCur(c); if (c) setF({ name: c.name, estimateVersionId: c.estimateVersionId, ...c.input }); }} className="!min-h-0 !py-1 text-[12px]">{d.valuations.map((v) => <option key={v.id} value={v.id}>{v.name}</option>)}</Select>}{canWrite && <Btn small onClick={() => { setCur(null); setF(blank()); }}>+ {fr ? "nouveau cas" : "new case"}</Btn>}</div>}>25 <p className="text-[13.5px] text-ink-2">{fr ? "Le coût à neuf provient d'une révision figée (sous-total avant taxes). Le coût de remplacement (utilité équivalente, matériaux modernes) diffère du coût de reproduction (réplique) ; l'ajustement de base documente l'écart. Le coût de construction n'est pas la valeur marchande." : "Cost new comes from a frozen revision (subtotal before tax). Replacement cost (equivalent utility, modern materials) differs from reproduction cost (replica); the basis adjustment documents the gap. Construction cost is not market value."}</p>26 <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">27 <Field label={fr ? "Nom du cas" : "Case name"}><Input value={f.name} disabled={!canWrite} onChange={(e) => setP({ name: e.target.value })} /></Field>28 <Field label={fr ? "Révision d'estimation" : "Estimate revision"}><Select value={f.estimateVersionId ?? ""} disabled={!canWrite} onChange={(e) => setP({ estimateVersionId: e.target.value || null })}><option value="">{fr ? "— coût saisi —" : "— entered cost —"}</option>{d.versions.map((v) => <option key={v.id} value={v.id}>r{v.number} — {v.label} ({money(v.total, lang)})</option>)}</Select></Field>29 {!f.estimateVersionId && <Field label={fr ? "Coût à neuf saisi ($)" : "Entered cost new ($)"}><Input type="number" min={0} value={f.costNew} disabled={!canWrite} onChange={(e) => setP({ costNew: Number(e.target.value) })} className="vp-mono" /></Field>}30 <Field label={fr ? "Base" : "Basis"}><Select value={f.basis} disabled={!canWrite} onChange={(e) => setP({ basis: e.target.value as ValuationInput["basis"] })}><option value="replacement">{fr ? "Remplacement (utilité équivalente)" : "Replacement (equivalent utility)"}</option><option value="reproduction">{fr ? "Reproduction (réplique)" : "Reproduction (replica)"}</option></Select></Field>31 <Field label={fr ? "Ajustement de base (%)" : "Basis adjustment (%)"} hint={fr ? "ex. −8 % : matériaux modernes moins coûteux" : "e.g. −8 %: cheaper modern materials"}><Input type="number" step="0.5" min={-50} max={50} value={f.basisAdjustmentPct} disabled={!canWrite} onChange={(e) => setP({ basisAdjustmentPct: Number(e.target.value) })} className="vp-mono" /></Field>32 <Field label={fr ? "Améliorations du site admissibles ($)" : "Eligible site improvements ($)"}><Input type="number" min={0} value={f.siteImprovements} disabled={!canWrite} onChange={(e) => setP({ siteImprovements: Number(e.target.value) })} className="vp-mono" /></Field>33 </div>34 {vLinked && <p className="vp-mono text-[11px] text-ink-2">{fr ? "révision liée" : "linked revision"} : r{vLinked.number} — {vLinked.label} · {fr ? "total" : "total"} {money(vLinked.total, lang)} ({fr ? "le sous-total avant taxes est utilisé" : "subtotal before tax is used"})</p>}35 </Block>3637 <Block num="02" title={fr ? "Dépréciation" : "Depreciation"}>38 <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">39 <Field label={fr ? "Détérioration physique" : "Physical deterioration"}><Select value={f.physical.method} disabled={!canWrite} onChange={(e) => setP({ physical: { ...f.physical, method: e.target.value as "age_life" | "percent" } })}><option value="age_life">{fr ? "âge-vie (âge effectif / vie économique)" : "age-life (effective age / economic life)"}</option><option value="percent">{fr ? "pourcentage direct" : "direct percentage"}</option></Select></Field>40 {f.physical.method === "age_life" ? (<><Field label={fr ? "Âge effectif (ans)" : "Effective age (years)"}><Input type="number" min={0} value={f.physical.effectiveAge ?? ""} disabled={!canWrite} onChange={(e) => setP({ physical: { ...f.physical, effectiveAge: e.target.value === "" ? null : Number(e.target.value) } })} className="vp-mono" /></Field><Field label={fr ? "Vie économique (ans)" : "Economic life (years)"}><Input type="number" min={1} value={f.physical.economicLife} disabled={!canWrite} onChange={(e) => setP({ physical: { ...f.physical, economicLife: Number(e.target.value) } })} className="vp-mono" /></Field></>)41 : <Field label={fr ? "Dépréciation physique (%)" : "Physical depreciation (%)"}><Input type="number" min={0} max={90} value={f.physical.percent ?? ""} disabled={!canWrite} onChange={(e) => setP({ physical: { ...f.physical, percent: e.target.value === "" ? null : Number(e.target.value) } })} className="vp-mono" /></Field>}42 <Field label={fr ? "Obsolescence externe ($)" : "External obsolescence ($)"}><Input type="number" min={0} value={f.external.amount} disabled={!canWrite} onChange={(e) => setP({ external: { ...f.external, amount: Number(e.target.value) } })} className="vp-mono" /></Field>43 </div>44 <Field label={fr ? "Note (externe)" : "Note (external)"}><Input value={f.external.note ?? ""} disabled={!canWrite} onChange={(e) => setP({ external: { ...f.external, note: e.target.value } })} /></Field>45 <div>46 <div className="flex items-center justify-between"><span className="klabel">{fr ? "Obsolescence fonctionnelle" : "Functional obsolescence"}</span>{canWrite && <Btn small onClick={() => setP({ functional: [...f.functional, { id: uuid(), label: "", curable: true, amount: 0, eliminatedByReplacement: false }] })}>+ {fr ? "déficience" : "deficiency"}</Btn>}</div>47 {!f.functional.length ? <p className="mt-1 text-[13px] text-ink-2">—</p> : (48 <div className="src-wrap mt-2"><table className="src-table"><thead><tr><th>{fr ? "Déficience" : "Deficiency"}</th><th>{fr ? "Type" : "Type"}</th><th>{fr ? "Montant ($)" : "Amount ($)"}</th><th>{fr ? "Éliminée par la conception de remplacement" : "Eliminated by replacement design"}</th><th></th></tr></thead><tbody>49 {f.functional.map((x) => (<tr key={x.id}><td><Input value={x.label} disabled={!canWrite} onChange={(e) => setP({ functional: f.functional.map((y) => (y.id === x.id ? { ...y, label: e.target.value } : y)) })} className="!min-h-0 !py-1 text-[13px]" /></td><td><Select value={x.curable ? "curable" : "incurable"} disabled={!canWrite} onChange={(e) => setP({ functional: f.functional.map((y) => (y.id === x.id ? { ...y, curable: e.target.value === "curable" } : y)) })} className="!min-h-0 !py-1 text-[12px]"><option value="curable">{fr ? "curable (coût de correction)" : "curable (cost to cure)"}</option><option value="incurable">{fr ? "incurable (perte de valeur)" : "incurable (value loss)"}</option></Select></td><td><Input type="number" min={0} value={x.amount} disabled={!canWrite} onChange={(e) => setP({ functional: f.functional.map((y) => (y.id === x.id ? { ...y, amount: Number(e.target.value) } : y)) })} className="vp-mono !min-h-0 !py-1 w-[120px] text-[12px]" /></td><td><input type="checkbox" checked={x.eliminatedByReplacement} disabled={!canWrite} onChange={(e) => setP({ functional: f.functional.map((y) => (y.id === x.id ? { ...y, eliminatedByReplacement: e.target.checked } : y)) })} /> <span className="text-[11.5px] text-ink-2">{fr ? "(non déduite en base remplacement)" : "(not deducted on replacement basis)"}</span></td><td>{canWrite && <button type="button" className="vp-mono text-[10px] text-ink-3" onClick={() => setP({ functional: f.functional.filter((y) => y.id !== x.id) })}>✕</button>}</td></tr>))}50 </tbody></table></div>51 )}52 </div>53 </Block>5455 <Block num="03" title={fr ? "Terrain et valeur indiquée" : "Land and indicated value"} aside={canWrite && <Btn small tone="primary" onClick={() => void save()}>{fr ? "Calculer et enregistrer" : "Compute and save"}</Btn>}>56 <div className="grid gap-4 sm:grid-cols-3">57 <Field label={fr ? "Valeur du terrain ($)" : "Land value ($)"}><Input type="number" min={0} value={f.land.value} disabled={!canWrite} onChange={(e) => setP({ land: { ...f.land, value: Number(e.target.value) } })} className="vp-mono" /></Field>58 <Field label={fr ? "Source" : "Source"}><Select value={f.land.source} disabled={!canWrite} onChange={(e) => setP({ land: { ...f.land, source: e.target.value as ValuationInput["land"]["source"] } })}><option value="role">{fr ? "rôle d'évaluation (MAMH)" : "assessment roll (MAMH)"}</option><option value="market">{fr ? "ventes de terrains comparables" : "comparable land sales"}</option><option value="residual">{fr ? "technique résiduelle" : "residual technique"}</option><option value="user">{fr ? "saisie / hypothèse" : "entered / assumption"}</option></Select></Field>59 <Field label={fr ? "Note" : "Note"}><Input value={f.land.note ?? ""} disabled={!canWrite} onChange={(e) => setP({ land: { ...f.land, note: e.target.value } })} /></Field>60 </div>61 {d.project.propertyId && <p className="text-[12.5px] text-ink-2">{fr ? "Valeur au rôle disponible via l'atelier" : "Roll value available in the workshop"} <Link href={`/cout?property=${d.project.propertyId}`} className="text-accent">{fr ? "méthode du coût (MAMH)" : "cost approach (MAMH)"}</Link>.</p>}62 <Err msg={err} />63 {res && (64 <div className="grid gap-6 lg:grid-cols-[1fr_1fr]">65 <div className="src-wrap"><table className="src-table"><tbody>{res.steps.map((s, i) => <tr key={i} className={s.label.startsWith("=") ? "bg-surface-2 font-semibold" : ""}><td>{s.label}</td><td className="vp-mono text-right">{money(s.amount, lang)}</td></tr>)}</tbody></table></div>66 <div className="space-y-2">67 <div className="border border-[var(--line-strong)] bg-surface p-4"><p className="kicker">{fr ? "Valeur indiquée par la méthode du coût" : "Cost-approach indicated value"}</p><p className="vp-display mt-1 text-[32px] font-bold">{money(res.indicatedValue, lang)}</p><p className="vp-mono text-[11px] text-ink-2">{fr ? "dépréciation totale" : "total depreciation"} {money(res.totalDepreciation, lang)} ({fr ? "physique" : "physical"} {res.physicalPct} %)</p></div>68 {res.functionalSkipped.length > 0 && <p className="text-[12.5px] text-ink-2">{fr ? "Non déduites (déjà éliminées par le remplacement) :" : "Not deducted (already eliminated by replacement):"} {res.functionalSkipped.map((x) => `${x.label} (${money(x.amount, lang)})`).join(" ; ")}</p>}69 {res.warnings.map((w, i) => <p key={i} className="text-[12.5px] text-[var(--amber)]">⚠ {w}</p>)}70 <Textarea rows={3} readOnly value={fr ? "Réconciliation : comparez cette indication aux lectures du marché (comparables ajustés, modèle hédonique d'UQO Éval) et expliquez les écarts (coût ≠ valeur : marché, obsolescence, terrain)." : "Reconciliation: compare this indication with market readings (adjusted comparables, UQO Éval hedonic model) and explain gaps (cost ≠ value: market, obsolescence, land)."} className="text-[12.5px]" />71 </div>72 </div>73 )}74 </Block>75 </div>76 );77}78