"""`cf config account-*` — счета: добавление, список, удаление.""" import typer from pydantic import ValidationError from rich.table import Table from domain import Account, CurrencyConverter from infrastructure.cli.commands.config._app import app from infrastructure.cli.commands.config._helpers import print_error, print_ok, print_warning, remove_items from infrastructure.cli.services import console, fail_validation, load_model, save_model from infrastructure.i18n import t @app.command() def account_add( name: str = typer.Option(..., "--name", "-n", help=t("cmd.config.account_add.opt.name")), balance: float = typer.Option(0.0, "--balance", "-b", help=t("cmd.config.account_add.opt.balance")), currency: str = typer.Option("", "--currency", "-c", help=t("cmd.config.account_add.opt.currency")), ) -> None: """Добавить счёт""" model = load_model() curr = currency.upper() if currency else model.base_currency try: account = Account(name=name, balance=balance, currency=curr) except ValidationError as e: fail_validation(e) model.accounts.append(account) save_model(model) print_ok(t("cmd.config.account_add.ok", name=name, balance=balance, currency=curr)) @app.command(name="account-list") def account_list() -> None: """Список счетов""" model = load_model() if not model.accounts: print_warning(t("cmd.config.account_list.empty")) return table = Table(title=t("table.accounts_config.title")) table.add_column(t("table.accounts_config.col.id"), style="dim") table.add_column(t("table.accounts_config.col.name"), style="cyan") table.add_column(t("table.accounts_config.col.currency")) table.add_column(t("table.accounts_config.col.balance"), justify="right") for a in model.accounts: balance_str = CurrencyConverter.format_money(a.balance, a.currency) table.add_row(str(a.id)[:8], a.name, a.currency, balance_str) console.print(table) @app.command(name="account-remove") def account_remove( identifier: str = typer.Argument(..., help=t("cmd.config.account_remove.arg")), ) -> None: """Удалить счёт по ID или названию""" model = load_model() account = next((a for a in model.accounts if str(a.id) == identifier or a.name == identifier), None) if account is not None: # M1: удаление счёта оставляло транзакции и регулярные платежи # висячими ссылками — прогноз молча пропускал их. Отказываем, пока # пользователь не удалит или не переназначит ссылки. transactions = sum(1 for t in model.transactions if _refers_to_account(account, t.account)) recurring = sum(1 for r in model.recurring if _refers_to_account(account, r.account)) if transactions or recurring: print_error( t( "cmd.config.account_remove.in_use", id=identifier, transactions=transactions, recurring=recurring, ) ) raise typer.Exit(1) remove_items( model, "accounts", lambda a: str(a.id) == identifier or a.name == identifier, identifier=identifier, ok_key="cmd.config.account_remove.ok", err_key="cmd.config.account_remove.err", ) def _refers_to_account(account: Account, reference: str) -> bool: """Ссылается ли строка на счёт — по UUID или имени (как в прогнозе).""" value = (reference or "").strip() if not value: return False return value == str(account.id) or value.casefold() == account.name.strip().casefold()