Files
nifodea/infrastructure/cli/commands/compare.py
T

54 lines
2.0 KiB
Python

"""cf compare — сравнить все сценарии."""
import typer
from pydantic import ValidationError
from rich.table import Table
from infrastructure.cli.services import (
build_services,
console,
convert_value,
fail_validation,
fmt_amount,
load_model,
resolve_currency,
)
from infrastructure.i18n import t
CURRENCY_OPTION = typer.Option(None, "--currency", "-c", help=t("cmd.forecast.opt.currency"))
def compare(
months: int = typer.Option(12, "--months", "-m", help=t("cmd.compare.opt.months")),
currency: str | None = CURRENCY_OPTION,
) -> None:
"""Сравнить все сценарии"""
model = load_model()
target_curr, symbol = resolve_currency(model, currency)
_, scenario_service, _, converter = build_services(model)
base_curr = model.base_currency
try:
results = scenario_service.compare(months)
except ValidationError as e:
# Сценарий может обнулить суммы (множитель 0) — модель это
# отклоняет (валидация при присваивании, D17).
fail_validation(e)
table = Table(title=t("cmd.compare.title", currency=target_curr))
table.add_column(t("cmd.compare.col.scenario"), style="cyan")
table.add_column(t("cmd.forecast.col.balance"), justify="right")
table.add_column(t("cmd.forecast.col.income"), justify="right")
table.add_column(t("cmd.forecast.col.expenses"), justify="right")
for name, r in results.items():
b = convert_value(converter, r["total_balance"], base_curr, target_curr)
i = convert_value(converter, r["total_income"], base_curr, target_curr)
e = convert_value(converter, r["total_expenses"], base_curr, target_curr)
table.add_row(
name,
fmt_amount(b, target_curr, symbol),
fmt_amount(i, target_curr, symbol),
fmt_amount(e, target_curr, symbol),
)
console.print(table)