← Files VeraARCHIVED FILE
modules/business-valuation/scripts/valuation_plan.py
10.3 KB · Dec 1, 2026 · 18:14 UTC
"""Replay the existing v3 plan and build an explicit cash-tax FCFF bridge."""
from __future__ import annotations
import shutil
import sys
from calendar import monthrange
from datetime import date, timedelta
from decimal import Decimal, localcontext
from pathlib import Path
from tempfile import TemporaryDirectory
from valuation_case import fields, indexed, read_json, require, source_paths
from valuation_engine import decimal
__all__ = ["bridge_plan"]
def bridge_plan(case: dict, source_root: Path, replay_parent: Path) -> dict:
"""Verify mapped source bytes via the shared compiler, not a parallel forecast.
Original source paths are reconstructed only below the bound run's scratch
directory. The final bridge retains hashes, periods and upstream calculation IDs.
"""
binding = case["plan_binding"]
fields(
binding,
{
"source_id",
"source_map",
"scenario_id",
"cash_operating_taxes",
"opening_operating_nwc",
"operating_classification",
"tax_refund_basis",
},
{"annual_input_ids", "monthly_input_ids", "flow_frequency", "selected_periods"},
)
require(
bool(binding["operating_classification"]),
"Review which current balances are operating and exclude debt-like items",
)
sources = indexed(case["sources"])
paths = dict(zip(sources, source_paths(case, source_root)))
require(binding["source_id"] in sources, "Plan source must be registered")
plan = read_json(paths[binding["source_id"]])
require(
plan["schema_version"] == "mparanza.business_planning_plan.v3",
"Use the finalized shared v3 plan artifact",
)
upstream = plan["case"]
require(
upstream["reporting_currency"] == case["currency"],
"Plan currency differs from valuation",
)
require(
upstream["audience"] == case["audience"], "Plan audience differs from valuation"
)
require(
plan["status"] == "ready_for_professional_review",
"Plan must pass its existing completeness and calculation gates",
)
mapping = binding["source_map"]
require(
isinstance(mapping, dict)
and set(mapping) == {row["id"] for row in upstream["sources"]},
"Map every original plan source to an exact valuation receipt",
)
module = Path(__file__).resolve().parents[2] / "business-planning/scripts"
require(module.is_dir(), "Shared Business Planning component is required")
if str(module) not in sys.path:
sys.path.insert(0, str(module))
from planning_workflow import validate_plan
replay_parent.mkdir(parents=True, exist_ok=True)
with TemporaryDirectory(prefix="plan-replay-", dir=replay_parent) as scratch:
root = Path(scratch)
for row in upstream["sources"]:
target = root / row["path"]
require(
not Path(row["path"]).is_absolute()
and ".." not in Path(row["path"]).parts,
"Invalid original source path",
)
mapped = mapping[row["id"]]
require(
mapped in sources and sources[mapped]["sha256"] == row["sha256"],
"Mapped plan source digest differs",
)
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(paths[mapped], target)
validate_plan(plan, source_root=root)
available = upstream["periods"]
periods = binding.get("selected_periods", available)
require(
bool(periods) and set(periods) <= set(available),
"Select existing plan periods; never extend the forecast",
)
first = available.index(periods[0])
require(
periods == available[first : first + len(periods)],
"Selected plan periods must be contiguous and ordered",
)
frequency = binding.get("flow_frequency", "annual")
if frequency == "annual":
require(
periods[0].endswith("-01") and len(periods) % 12 == 0,
"Annual valuation bridge requires complete January-December years; no invented stub conversion",
)
start = date.fromisoformat(periods[0] + "-01")
require(start > date.min, "Plan start must have a preceding valuation date")
require(
case["mandate"]["valuation_date"] == (start - timedelta(days=1)).isoformat(),
"Plan bridge requires valuation at the month end before the first selected period",
)
flow_ids = binding[f"{frequency}_input_ids"]
require(
not set(flow_ids)
& (
set(binding["cash_operating_taxes"].values())
| {binding["opening_operating_nwc"]}
),
"Derived plan flow inputs must differ from tax and opening working-capital inputs",
)
scenarios = [
row
for row in upstream["financial"]["scenarios"]
if row["id"] == binding["scenario_id"]
]
require(len(scenarios) == 1, "Select one existing plan scenario")
inputs = indexed(case["inputs"])
taxes = binding["cash_operating_taxes"]
require(
isinstance(taxes, dict) and set(taxes) == set(periods),
"Explicit cash operating taxes required for every month",
)
def amount(ref: str) -> Decimal:
require(
ref in inputs and inputs[ref]["unit"] == case["currency"],
"Missing or incompatible bridge input",
)
return decimal(inputs[ref]["value"])
previous = amount(binding["opening_operating_nwc"])
monthly, annual = [], []
with localcontext() as ctx:
ctx.prec = 40
running = Decimal(0)
annual_ids = []
annual_periods: list[str] = []
for row in scenarios[0]["schedule"]:
period = row["period"]
if period not in periods:
continue
values = {
key: decimal(row[key])
for key in (
"revenue",
"cogs",
"operating_expenses",
"depreciation_amortization",
"capital_expenditure",
"ending_accounts_receivable",
"ending_inventory",
"ending_other_current_assets",
"ending_accounts_payable",
"ending_other_liabilities",
)
}
tax = amount(taxes[period])
require(
tax >= 0 or bool(binding["tax_refund_basis"]),
"Negative cash operating tax requires an explicit refund basis",
)
nwc = (
values["ending_accounts_receivable"]
+ values["ending_inventory"]
+ values["ending_other_current_assets"]
- values["ending_accounts_payable"]
- values["ending_other_liabilities"]
)
ebit = (
values["revenue"]
- values["cogs"]
- values["operating_expenses"]
- values["depreciation_amortization"]
)
delta = nwc - previous
fcff = (
ebit
- tax
+ values["depreciation_amortization"]
- values["capital_expenditure"]
- delta
)
ids = [
f"{binding['scenario_id']}/{period}/{metric}"
for metric in ("ebit", "working_capital")
]
require(
all(identifier in plan["calculations"] for identifier in ids),
"Missing shared calculation lineage",
)
monthly.append(
{
"period": period,
"ebit": str(ebit),
"cash_operating_taxes": str(tax),
"depreciation_amortization": str(
values["depreciation_amortization"]
),
"capital_expenditure": str(values["capital_expenditure"]),
"opening_nwc": str(previous),
"delta_nwc": str(delta),
"ending_nwc": str(nwc),
"fcff": str(fcff),
"plan_calculation_ids": ids,
"tax_input_id": taxes[period],
}
)
previous = nwc
running += fcff
annual_ids.extend(ids)
annual_periods.append(period)
if period.endswith("-12"):
# A partial calendar year stays monthly; never relabel it annual.
if len(annual_periods) == 12 and annual_periods[0].endswith("-01"):
annual.append(
{
"year": period[:4],
"fcff": str(running),
"ending_nwc": str(nwc),
"plan_calculation_ids": annual_ids,
}
)
running, annual_ids, annual_periods = Decimal(0), [], []
bound_rows = monthly if frequency == "monthly" else annual
require(
len(flow_ids) == len(bound_rows),
"Bind one FCFF input per selected month or complete year",
)
for ref, row in zip(flow_ids, bound_rows):
require(
amount(ref) == decimal(row["fcff"]),
"Valuation flow differs from replayed plan bridge",
)
require(
binding["source_id"] in inputs[ref]["source_ids"],
"Plan flow must reference its plan artifact",
)
require(
inputs[ref].get("plan_calculation_ids") == row["plan_calculation_ids"],
"Plan flow must retain exact upstream calculation IDs",
)
return {
"plan_sha256": sources[binding["source_id"]]["sha256"],
"plan_case_sha256": plan["case_sha256"],
"scenario_id": binding["scenario_id"],
"flow_frequency": frequency,
"selected_periods": periods,
"flow_input_ids": flow_ids,
"period_end_dates": (
[
f"{period}-{monthrange(int(period[:4]), int(period[5:]))[1]:02}"
for period in periods
]
if frequency == "monthly"
else [f"{row['year']}-12-31" for row in annual]
),
"monthly": monthly,
"annual": annual,
"binding": binding,
}
SHA-256: afb23f84018f15fa16196ced2b8e39c1e2029d858493c0a167c10c434311760a