Files

71 lines
2.9 KiB
Python

"""`cf config recurring-*` — регулярные платежи: добавление, список, удаление."""
import typer
from rich.table import Table
from domain import CurrencyConverter, RecurringCashflow
from infrastructure.cli.commands.config._app import app
from infrastructure.cli.commands.config._helpers import print_ok, print_warning, remove_items
from infrastructure.cli.services import console, load_model, save_model
from infrastructure.i18n import t
@app.command()
def recurring_add(
amount: float = typer.Option(..., "--amount", "-m", help=t("cmd.config.recurring_add.opt.amount")),
category: str = typer.Option("", "--category", "-c", help=t("cmd.config.recurring_add.opt.category")),
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")),
) -> None:
"""Добавить регулярный платёж"""
model = load_model()
rc = RecurringCashflow(
start_date=start_date or "",
end_date=end_date or "",
frequency=frequency,
amount=amount,
category=category,
)
model.recurring.append(rc)
save_model(model)
amount_str = CurrencyConverter.format_money(abs(amount), model.base_currency)
print_ok(t("cmd.config.recurring_add.ok", amount_str=amount_str, frequency=frequency))
@app.command(name="recurring-list")
def recurring_list() -> None:
"""Список регулярных платежей"""
model = load_model()
if not model.recurring:
print_warning(t("cmd.config.recurring_list.empty"))
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.start"))
table.add_column(t("table.recurring.col.end"))
table.add_column(t("table.recurring.col.period"))
table.add_column(t("table.recurring.col.amount"), justify="right")
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)
table.add_row(*row)
console.print(table)
@app.command(name="recurring-remove")
def recurring_remove(
identifier: str = typer.Argument(..., help=t("cmd.config.recurring_remove.arg")),
) -> None:
"""Удалить регулярный платёж по ID"""
model = load_model()
remove_items(
model,
"recurring",
lambda r: str(r.id) == identifier,
identifier=identifier,
ok_key="cmd.config.recurring_remove.ok",
err_key="cmd.config.recurring_remove.err",
)