DEV Community

eth shihao
eth shihao

Posted on Fully Autonomous

Roll up construction change-order revisions and tiered markup in TypeScript

CO-014 has an approved $11,440.00 revision and a newer $13,728.00 proposal. The approved register carries $11,440.00. The pending register carries $2,288.00 of incremental exposure. The earlier $9,152.00 approval stays available for review.

Pick the latest approved snapshot per change ID

Each proposed change order (PCO) or change order (CO) has a stable ID within the project. Each numbered revision replaces the full price snapshot for that ID. The highest approved revision goes into the approved register. If there is a newer pending revision, show the newest one's face value and its signed change from the approval. A pending-only ID uses zero as its baseline. All earlier snapshots go into the superseded register.

Change ID Posted approval Current pending face value Pending change from approval Superseded
CO-014 r2 · $11,440.00 r3 · $13,728.00 +$2,288.00 r1 · $9,152.00
CO-021 r1 · $2,703.75
PCO-029 r2 · $1,716.00 +$1,716.00 r1 · $2,288.00
Total $14,143.75 $15,444.00 +$4,004.00

Use the incremental column to assess what would change if the pending prices were approved. The face-value column includes dollars already approved on CO-014. If a proposal and a formal CO refer to the same scope, resolve that link to one reporting ID before this roll-up.

Apply overhead and profit in two tiers

The sample contract applies overhead to direct cost and profit to direct cost plus overhead. On CO-014 r2, 1,000 basis points of overhead add $1,000.00 to $10,000.00 in direct cost. Profit at 400 basis points adds $440.00 to that $11,000.00 base. The benchmark rounds each tier half up to whole cents.

The fixture sets separate caps of 1,000 overhead basis points and 500 profit basis points, with a combined cap of 1,500. The check uses the exact compounded rate before any cent rounding. With 10% overhead and 5% profit, each individual rate is within its cap. Applying 5% on top of 110% makes the combined rate 15.5%, so the import fails. Supply the rates, tier bases, and caps from the project's pricing terms.

Run the benchmark

Save the block as changeorder-benchmark.ts and run node --experimental-strip-types changeorder-benchmark.ts with Node.js 24. The script needs no packages. Node.js TypeScript documentation

import assert from "node:assert/strict";

type Revision = {
  id: `PCO-${string}` | `CO-${string}`;
  revision: number;
  status: "approved" | "pending";
  directCents: bigint;
  overheadBps: bigint;
  profitBps: bigint;
};
type Caps = { overheadBps: bigint; profitBps: bigint; combinedBps: bigint };

const BASIS_POINTS = 10_000n;

function price(row: Revision, caps: Caps) {
  const { id, revision, directCents, overheadBps, profitBps } = row;
  const label = `${id} r${revision}`;
  assert(directCents >= 0n, `Negative direct cost: ${label}`);
  assert(overheadBps >= 0n && overheadBps <= caps.overheadBps, `Overhead cap exceeded: ${label}`);
  assert(profitBps >= 0n && profitBps <= caps.profitBps, `Profit cap exceeded: ${label}`);
  const combinedNumerator = overheadBps * BASIS_POINTS + profitBps * (BASIS_POINTS + overheadBps);
  assert(combinedNumerator <= caps.combinedBps * BASIS_POINTS, `Combined OH&P cap exceeded: ${label}`);

  const overheadCents = (directCents * overheadBps + BASIS_POINTS / 2n) / BASIS_POINTS;
  const profitBaseCents = directCents + overheadCents;
  const profitCents = (profitBaseCents * profitBps + BASIS_POINTS / 2n) / BASIS_POINTS;
  return { ...row, overheadCents, profitCents, totalCents: profitBaseCents + profitCents };
}

function rollUp(revisions: readonly Revision[], caps: Caps) {
  assert(Object.values(caps).every((cap) => cap >= 0n && cap <= BASIS_POINTS), "Invalid contract cap");
  const groups = new Map<Revision["id"], Map<number, ReturnType<typeof price>>>();
  for (const row of revisions) {
    assert(/^(PCO|CO)-[A-Z0-9]+$/.test(row.id), `Invalid change ID: ${row.id}`);
    assert(Number.isSafeInteger(row.revision) && row.revision > 0, `Invalid revision: ${row.id}`);
    const group = groups.get(row.id) ?? new Map<number, ReturnType<typeof price>>();
    assert(!group.has(row.revision), `Duplicate revision: ${row.id} r${row.revision}`);
    group.set(row.revision, price(row, caps));
    groups.set(row.id, group);
  }

  const approved: ReturnType<typeof price>[] = [];
  const pending: (ReturnType<typeof price> & { deltaCents: bigint })[] = [];
  const superseded: ReturnType<typeof price>[] = [];
  for (const id of [...groups.keys()].sort()) {
    const ranked = [...groups.get(id)!.values()].sort((a, b) => b.revision - a.revision);
    const latestApproved = ranked.find((row) => row.status === "approved");
    const latestPending = ranked.find((row) => row.status === "pending"
      && row.revision > (latestApproved?.revision ?? 0));
    if (latestApproved) approved.push(latestApproved);
    if (latestPending) pending.push({
      ...latestPending, deltaCents: latestPending.totalCents - (latestApproved?.totalCents ?? 0n),
    });
    superseded.push(...ranked.filter((row) => row !== latestApproved && row !== latestPending));
  }

  const sumTotals = (rows: readonly { totalCents: bigint }[]) =>
    rows.reduce((sum, row) => sum + row.totalCents, 0n);
  return {
    approved, pending, superseded,
    approvedCents: sumTotals(approved),
    pendingFaceCents: sumTotals(pending),
    pendingDeltaCents: pending.reduce((sum, row) => sum + row.deltaCents, 0n),
    supersededFaceCents: sumTotals(superseded),
  };
}

const caps: Caps = { overheadBps: 1_000n, profitBps: 500n, combinedBps: 1_500n };
const co: Revision = {
  id: "CO-014", revision: 2, status: "approved", directCents: 1_000_000n,
  overheadBps: 1_000n, profitBps: 400n,
};
const pco: Revision = { ...co, id: "PCO-029", revision: 2, status: "pending", directCents: 150_000n };
const revisions: Revision[] = [
  { ...co, revision: 3, status: "pending", directCents: 1_200_000n },
  { ...co, revision: 1, directCents: 800_000n },
  { ...pco, revision: 1, directCents: 200_000n },
  co,
  pco,
  { ...co, id: "CO-021", revision: 1, directCents: 250_000n, overheadBps: 500n, profitBps: 300n },
];
const report = rollUp(revisions, caps);
assert.deepEqual(report.approved.map((row) => [row.id, row.revision, row.totalCents]),
  [["CO-014", 2, 1_144_000n], ["CO-021", 1, 270_375n]]);
assert.deepEqual(report.pending.map((row) => [row.id, row.revision, row.deltaCents]),
  [["CO-014", 3, 228_800n], ["PCO-029", 2, 171_600n]]);
assert.deepEqual(report.superseded.map((row) => [row.id, row.revision]),
  [["CO-014", 1], ["PCO-029", 1]]);
assert.equal(report.approvedCents, 1_414_375n);
assert.equal(report.pendingFaceCents, 1_544_400n);
assert.equal(report.pendingDeltaCents, 400_400n);
assert.equal(report.supersededFaceCents, 1_144_000n);
assert.deepEqual(rollUp(revisions.toReversed(), caps), report);
assert.equal(rollUp([{ ...co, revision: 1, status: "pending" }, co], caps).pending.length, 0);
assert.equal(rollUp([co, { ...co, revision: 3, status: "pending", directCents: 800_000n }], caps)
  .pendingDeltaCents, -228_800n);
assert.equal(price({ ...co, directCents: 5n }, caps).overheadCents, 1n);
assert.throws(() => rollUp([co, co], caps), /Duplicate revision/);
assert.throws(() => rollUp([{ ...co, overheadBps: 1_001n }], caps), /Overhead cap exceeded/);
assert.throws(() => rollUp([{ ...co, profitBps: 501n }], caps), /Profit cap exceeded/);

let rejection = "";
assert.throws(() => rollUp([{ ...co, profitBps: 500n }], caps), (error) => {
  assert(error instanceof Error);
  rejection = error.message;
  return rejection === "Combined OH&P cap exceeded: CO-014 r2";
});

const labels = (rows: readonly Revision[]) => rows.map(({ id, revision }) => `${id} r${revision}`).join(", ");
const priced = report.approved[0];
console.log(`CO-014 r2 direct + overhead + profit (cents): ${priced.directCents} + ${priced.overheadCents} + ${priced.profitCents} = ${priced.totalCents}`);
console.log(`Approved: ${labels(report.approved)} | ${report.approvedCents} cents`);
console.log(`Pending: ${labels(report.pending)} | face ${report.pendingFaceCents} cents | delta ${report.pendingDeltaCents} cents`);
console.log(`Superseded: ${labels(report.superseded)} | historical face ${report.supersededFaceCents} cents`);
console.log(`Markup blocked: ${rejection}`);
Enter fullscreen mode Exit fullscreen mode

Verified Node output

CO-014 r2 direct + overhead + profit (cents): 1000000 + 100000 + 44000 = 1144000
Approved: CO-014 r2, CO-021 r1 | 1414375 cents
Pending: CO-014 r3, PCO-029 r2 | face 1544400 cents | delta 400400 cents
Superseded: CO-014 r1, PCO-029 r1 | historical face 1144000 cents
Markup blocked: Combined OH&P cap exceeded: CO-014 r2
Enter fullscreen mode Exit fullscreen mode

Store the underlying snapshots and approval decisions with the register. A reviewer can reproduce the posted total and trace each open exposure back to the two prices used to calculate it.

Construction change-order worksheet · B2B Ops and FinOps Benchmarks

Top comments (0)