T9: Excel as the model authoring surface

- ExcelRepository: canonical columns with ru/en aliases, date/number normalization
  (Excel dates, DD.MM.YYYY, '1 234,56', currency symbols, parentheses), account
  name->UUID resolution, per-row errors with sheet!row, warnings for formulas
  without cached values, duplicate names and default exchange rates
- ExcelTemplate: skeleton generator (number/date formats, dropdowns for currency,
  frequency en/ru and account, 'Инструкция' sheet); ship data/model_template.xlsx
- CLI: cf template-xlsx [--force]; cf import-xlsx --dry-run with rich error table,
  import writes nothing when the file has errors
- RecurringCashflow.account (empty = all accounts) + name/UUID matching in forecast;
  cf config recurring-add --account, account column in info/recurring-list
- Forecast: runs are idempotent, 'Итог: Баланс' sums all accounts of the last month
- README: 'Сборка модели в Excel' section
- ruff check . clean (9 pre-existing import/line-length findings fixed)
- MetaAgent: T9 archived, req-T9 approved, manifest/checkpoints/state synced

118 tests pass
This commit is contained in:
oqyude committed 2026-11-11 15:40:37 +03:00
1 parent b90ca4c282
commit cdda703b37
39 files changed
+2181 -198

No files matched your search

+2
View File
@@ -8,6 +8,7 @@ from infrastructure.cli.commands import (
info,
init,
scenario,
template_xlsx,
whatif,
)
@@ -21,5 +22,6 @@ __all__ = [
"info",
"init",
"scenario",
"template_xlsx",
"whatif",
]
+16 -4
View File
@@ -13,7 +13,7 @@ from domain import (
Transaction,
)
from infrastructure.cli.i18n import t
from infrastructure.cli.services import load_model, save_model
from infrastructure.cli.services import account_label, load_model, save_model
app = typer.Typer(name="config", help=t("config.help"))
console = Console()
@@ -242,12 +242,19 @@ def recurring_add(
frequency: str = typer.Option("monthly", "--frequency", "-f", help=t("cmd.config.recurring_add.opt.frequency")),
start_date: str = typer.Option("", "--start", "-s", help=t("cmd.config.recurring_add.opt.start")),
end_date: str = typer.Option("", "--end", "-e", help=t("cmd.config.recurring_add.opt.end")),
account: str = typer.Option("", "--account", "-a", help=t("cmd.config.recurring_add.opt.account")),
) -> None:
"""Добавить регулярный платёж"""
model = load_model()
account_id = ""
if account:
account_id = _resolve_account_id(model, account) or ""
if not account_id:
console.print(f"[red]{t('cmd.config.recurring_add.err', account=account)}[/red]")
raise typer.Exit(1)
rc = RecurringCashflow(
start_date=start_date or "", end_date=end_date or "",
frequency=frequency, amount=amount, category=category,
frequency=frequency, amount=amount, category=category, account=account_id,
)
model.recurring.append(rc)
save_model(model)
@@ -266,6 +273,7 @@ def recurring_list() -> None:
return
table = Table(title=t("table.recurring.title"))
table.add_column(t("table.recurring.col.id"), style="dim")
table.add_column(t("table.recurring.col.account"))
table.add_column(t("table.recurring.col.start"))
table.add_column(t("table.recurring.col.end"))
table.add_column(t("table.recurring.col.period"))
@@ -273,7 +281,8 @@ def recurring_list() -> None:
table.add_column(t("table.recurring.col.category"))
for r in model.recurring:
amount_str = CurrencyConverter.format_money(r.amount, model.base_currency)
row = (str(r.id)[:8], r.start_date, r.end_date, r.frequency, amount_str, r.category)
owner = account_label(model, r.account, empty=t("table.recurring.all_accounts"))
row = (str(r.id)[:8], owner, r.start_date, r.end_date, r.frequency, amount_str, r.category)
table.add_row(*row)
console.print(table)
@@ -410,8 +419,11 @@ def liability_remove(
def _resolve_account_id(model, identifier: str) -> str | None:
value = (identifier or "").strip()
if not value:
return None
for a in model.accounts:
if str(a.id) == identifier or a.name == identifier:
if value == str(a.id) or value.casefold() == a.name.strip().casefold():
return str(a.id)
return None
+57 -11
View File
@@ -1,29 +1,75 @@
"""cf import-xlsx — импорт данных из Excel."""
import typer
from rich.markup import escape
from rich.table import Table
from infrastructure.cli.i18n import t
from infrastructure.cli.services import console, save_model
from infrastructure.repositories import ExcelRepository, ExcelRepositoryError
from infrastructure.repositories import ExcelRepository, ExcelRepositoryError, ExcelRowError
app = typer.Typer()
_repo = ExcelRepository()
def _counts(model) -> dict[str, int]:
return {
"accounts": len(model.accounts),
"transactions": len(model.transactions),
"recurring": len(model.recurring),
"assets": len(model.assets),
"liabilities": len(model.liabilities),
}
def _print_warnings(warnings: list[str]) -> None:
for warning in warnings:
console.print(f"[yellow]{t('cmd.import.warn', warning=escape(warning))}[/yellow]")
def _print_errors(errors: list[ExcelRowError]) -> None:
table = Table(title=t("cmd.import.errors.title", count=len(errors)))
table.add_column(t("cmd.import.errors.col.sheet"))
table.add_column(t("cmd.import.errors.col.row"), justify="right")
table.add_column(t("cmd.import.errors.col.field"))
table.add_column(t("cmd.import.errors.col.message"))
for error in errors:
table.add_row(
escape(error.sheet),
str(error.row),
escape(error.field) or "—",
escape(error.message),
)
console.print(table)
console.print(f"[red]{t('cmd.import.errors.hint', first=escape(str(errors[0])))}[/red]")
@app.command(name="import-xlsx")
def import_xlsx(
path: str = typer.Argument(..., help=t("cmd.import.arg.path")),
dry_run: bool = typer.Option(False, "--dry-run", help=t("cmd.import.opt.dry_run")),
) -> None:
"""Импорт данных из Excel"""
try:
model = _repo.load(path)
save_model(model)
console.print(t("cmd.import.ok",
accounts=len(model.accounts),
transactions=len(model.transactions),
recurring=len(model.recurring),
assets=len(model.assets),
liabilities=len(model.liabilities)))
console.print(t("cmd.import.base_currency", currency=model.base_currency))
report = _repo.load_report(path)
except ExcelRepositoryError as e:
console.print(f"[red]{t('cmd.import.err', error=str(e))}[/red]")
console.print(f"[red]{t('cmd.import.err', error=escape(str(e)))}[/red]")
raise typer.Exit(1)
_print_warnings(report.warnings)
if report.errors:
_print_errors(report.errors)
raise typer.Exit(1)
counts = _counts(report.model)
if dry_run:
console.print(t("cmd.import.dry_run.title"))
console.print(t("cmd.import.dry_run.ok", **counts))
console.print(t("cmd.import.dry_run.base_currency", currency=report.model.base_currency))
console.print(f"[yellow]{t('cmd.import.dry_run.not_saved')}[/yellow]")
return
save_model(report.model)
console.print(t("cmd.import.ok", **counts))
console.print(t("cmd.import.base_currency", currency=report.model.base_currency))
+4 -2
View File
@@ -4,7 +4,7 @@ from rich.table import Table
from domain import CurrencyConverter
from infrastructure.cli.i18n import t
from infrastructure.cli.services import console, load_model
from infrastructure.cli.services import account_label, console, load_model
app = typer.Typer()
@@ -51,12 +51,14 @@ def info() -> None:
if model.recurring:
tbl = Table(title=t("table.recurring.title"))
tbl.add_column(t("table.recurring.col.account"))
tbl.add_column(t("table.recurring.col.period"))
tbl.add_column(t("table.recurring.col.amount"), justify="right")
tbl.add_column(t("table.recurring.col.category"))
for r in model.recurring:
amount_str = CurrencyConverter.format_money(r.amount, model.base_currency)
tbl.add_row(r.frequency, amount_str, r.category)
owner = account_label(model, r.account, empty=t("table.recurring.all_accounts"))
tbl.add_row(owner, r.frequency, amount_str, r.category)
console.print(tbl)
if model.assets:
+1 -1
View File
@@ -1,10 +1,10 @@
"""cf init — создать пустую модель."""
import typer
from domain import FinancialModel
from infrastructure.cli.i18n import t
from infrastructure.cli.paths import MODEL_PATH
from infrastructure.cli.services import console, save_model
from domain import FinancialModel
app = typer.Typer()
@@ -0,0 +1,32 @@
"""cf template-xlsx — создать Excel-шаблон модели для ручного заполнения."""
from pathlib import Path
import typer
from rich.markup import escape
from infrastructure.cli.i18n import t
from infrastructure.cli.paths import TEMPLATE_PATH
from infrastructure.cli.services import console
from infrastructure.repositories import ExcelRepositoryError, write_template
app = typer.Typer()
@app.command(name="template-xlsx")
def template_xlsx(
path: Path = typer.Argument(TEMPLATE_PATH, help=t("cmd.template.arg.path")),
force: bool = typer.Option(False, "--force", "-f", help=t("cmd.template.opt.force")),
) -> None:
"""Создать Excel-шаблон для заполнения модели"""
if path.exists() and not force:
console.print(f"[red]{t('cmd.template.err.exists', path=escape(str(path)))}[/red]")
raise typer.Exit(1)
try:
target = write_template(path, force=force)
except ExcelRepositoryError as e:
console.print(f"[red]{t('cmd.template.err', error=escape(str(e)))}[/red]")
raise typer.Exit(1)
console.print(t("cmd.template.ok", path=escape(str(target))))
console.print(t("cmd.template.hint", path=escape(str(target))))