47 lines
2.2 KiB
Python
47 lines
2.2 KiB
Python
"""cf whatif — what-if анализ с произвольными множителями."""
|
|
|
|
import typer
|
|
from pydantic import ValidationError
|
|
|
|
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 whatif(
|
|
income: float = typer.Option(1.0, "--income", "-i", help=t("cmd.whatif.opt.income")),
|
|
expense: float = typer.Option(1.0, "--expense", "-e", help=t("cmd.whatif.opt.expense")),
|
|
growth: float = typer.Option(1.0, "--growth", "-g", help=t("cmd.whatif.opt.growth")),
|
|
months: int = typer.Option(12, "--months", "-m", help=t("cmd.whatif.opt.months")),
|
|
currency: str | None = CURRENCY_OPTION,
|
|
) -> None:
|
|
"""What-if анализ с произвольными множителями"""
|
|
model = load_model()
|
|
target_curr, symbol = resolve_currency(model, currency)
|
|
_, scenario_service, _, converter = build_services(model)
|
|
base_curr = model.base_currency
|
|
try:
|
|
result = scenario_service.what_if(income, expense, growth, months)
|
|
except ValidationError as e:
|
|
# Множитель 0 обнуляет суммы транзакций — модель это отклоняет
|
|
# (валидация при присваивании, D17). Показываем сообщение, а не traceback.
|
|
fail_validation(e)
|
|
|
|
balance = convert_value(converter, result["total_balance"], base_curr, target_curr)
|
|
income_v = convert_value(converter, result["total_income"], base_curr, target_curr)
|
|
expenses = convert_value(converter, result["total_expenses"], base_curr, target_curr)
|
|
console.print(f"[bold]{t('cmd.whatif.title')}[/bold]")
|
|
console.print(t("cmd.whatif.params", income=income, expense=expense, growth=growth))
|
|
console.print(f"{t('cmd.scenario.label.balance')}: {fmt_amount(balance, target_curr, symbol)}")
|
|
console.print(f"{t('cmd.scenario.label.income')}: {fmt_amount(income_v, target_curr, symbol)}")
|
|
console.print(f"{t('cmd.scenario.label.expenses')}: {fmt_amount(expenses, target_curr, symbol)}")
|