Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/dualentry_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,15 @@ def paginate(
result["next_offset"] = start_offset + len(all_items)
return result

def post(self, path: str, json: dict[str, Any] | None = None) -> dict:
def post(
self,
path: str,
json: dict[str, Any] | None = None,
*,
files: list[tuple[str, tuple[str, bytes]]] | None = None,
) -> dict:
if files is not None:
return self._request("POST", path, files=files)
return self._request("POST", path, json=json)

def put(self, path: str, json: dict[str, Any] | None = None) -> dict:
Expand Down
166 changes: 166 additions & 0 deletions src/dualentry_cli/commands/statistical_journals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"""Statistical journal commands (custom list filters and attachment upload)."""

from __future__ import annotations

from pathlib import Path
from typing import Annotated

import typer

from dualentry_cli.cli import HelpfulGroup
from dualentry_cli.commands import (
AllPages,
Format,
Limit,
Offset,
Search,
Status,
_load_json_file,
_strip_record_prefix,
)
from dualentry_cli.commands.actions import run_list
from dualentry_cli.output import format_output

app = typer.Typer(help="Manage statistical journals", no_args_is_help=True, cls=HelpfulGroup)


def _parse_csv_ints(value: str) -> list[int] | None:
"""Parse a comma-separated list of integers for list filters."""
raw = value.strip()
if not raw:
return None
result: list[int] = []
for part in raw.split(","):
token = part.strip()
if not token:
continue
try:
result.append(int(token))
except ValueError:
raise typer.BadParameter(f"expected comma-separated integers, got {token!r}") from None
return result or None


def _csv_ints_option(flag: str, *, help: str) -> typer.Option:
return typer.Option(flag, parser=_parse_csv_ints, help=help)


def _csv_strings(value: str | None) -> list[str] | None:
if not value:
return None
return [part.strip() for part in value.split(",") if part.strip()]


@app.command("list")
def list_cmd(
limit: int = Limit,
offset: int = Offset,
all_pages: bool = AllPages,
search: str | None = Search,
status: str | None = Status,
company: str | None = typer.Option(None, "--company", "-c", help="Filter by company ID"),
date_start: str | None = typer.Option(None, "--date-start", help="Filter from journal date (YYYY-MM-DD)"),
date_end: str | None = typer.Option(None, "--date-end", help="Filter to journal date (YYYY-MM-DD)"),
period_start: str | None = typer.Option(None, "--period-start", help="Filter from period start (YYYY-MM-DD)"),
period_end: str | None = typer.Option(None, "--period-end", help="Filter to period end (YYYY-MM-DD)"),
number: Annotated[
list[int] | None,
_csv_ints_option("--number", help="Filter by record number(s), comma-separated"),
] = None,
account_id: Annotated[
list[int] | None,
_csv_ints_option("--account-id", help="Filter by account ID(s), comma-separated"),
] = None,
account_number: str | None = typer.Option(None, "--account-number", help="Filter by account number(s), comma-separated"),
line_account_id: Annotated[
list[int] | None,
_csv_ints_option("--line-account-id", help="Filter by line account ID(s), comma-separated"),
] = None,
line_account_number: str | None = typer.Option(None, "--line-account-number", help="Filter by line account number(s), comma-separated"),
updated_after: str | None = typer.Option(None, "--updated-after", help="Updated after timestamp (ISO 8601)"),
updated_before: str | None = typer.Option(None, "--updated-before", help="Updated before timestamp (ISO 8601)"),
ordering: str | None = typer.Option(None, "--ordering", help="Sort order (API field name, optional leading -)"),
output: str = Format,
):
"""List statistical journals."""
run_list(
"statistical-journals",
resource="statistical-journal",
limit=limit,
offset=offset,
all_pages=all_pages,
output=output,
search=search,
status=status,
company_id=company or None,
date_start=date_start or None,
date_end=date_end or None,
period_start=period_start or None,
period_end=period_end or None,
number=number,
account_id=account_id,
account_number=_csv_strings(account_number),
line_account_id=line_account_id,
line_account_number=_csv_strings(line_account_number),
updated_after=updated_after or None,
updated_before=updated_before or None,
ordering=ordering or None,
)


@app.command("get")
def get_cmd(
value: str = typer.Argument(help="Record number (#) or prefixed number (e.g. SJ-3)"),
output: str = Format,
):
"""Get a statistical journal by number."""
from dualentry_cli.main import get_client

client = get_client()
data = client.get(f"/statistical-journals/{_strip_record_prefix(value)}/")
format_output(data, resource="statistical-journal", fmt=output)


@app.command("create")
def create_cmd(
file: Path = typer.Option(..., "--file", "-f", help="JSON file with record data"),
output: str = Format,
):
"""Create a statistical journal from a JSON file."""
from dualentry_cli.main import get_client

payload = _load_json_file(file)
client = get_client()
data = client.post("/statistical-journals/", json=payload)
format_output(data, resource="statistical-journal", fmt=output)


@app.command("update")
def update_cmd(
number: str = typer.Argument(help="Record number of the journal to update"),
file: Path = typer.Option(..., "--file", "-f", help="JSON file with update data"),
output: str = Format,
):
"""Update a statistical journal."""
from dualentry_cli.main import get_client

payload = _load_json_file(file)
client = get_client()
data = client.put(f"/statistical-journals/{_strip_record_prefix(number)}/", json=payload)
format_output(data, resource="statistical-journal", fmt=output)


@app.command("add-attachments")
def add_attachments_cmd(
number: str = typer.Argument(help="Record number of the journal"),
file: list[Path] = typer.Option(..., "--file", "-f", help="Attachment file(s) to upload", exists=True, readable=True),
output: str = Format,
):
"""Upload attachment file(s) to a statistical journal."""
from dualentry_cli.main import get_client

client = get_client()
stripped = _strip_record_prefix(number)
multipart = [("files", (path.name, path.read_bytes())) for path in file]
data = client.post(f"/statistical-journals/{stripped}/attachments/", files=multipart)
format_output(data, resource="statistical-journal", fmt=output)
8 changes: 8 additions & 0 deletions src/dualentry_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dualentry_cli.commands.bank_match import app as bank_match_app
from dualentry_cli.commands.export_jobs import app as export_jobs_app
from dualentry_cli.commands.ije_extras import IJE_CHECKS, IJE_ONLINE_EXTRA_CHECKS, IJE_TEMPLATE
from dualentry_cli.commands.statistical_journals import app as statistical_journals_app
from dualentry_cli.config import Config

app = typer.Typer(name="dualentry", help="DualEntry accounting CLI", no_args_is_help=True, cls=HelpfulGroup)
Expand Down Expand Up @@ -59,6 +60,7 @@

# Accounting
app.add_typer(make_resource_app("journal entries", "journal-entry", "journal-entries", has_number=True, filters=TXN_ALL_PARTIES), name="journal-entries")
app.add_typer(statistical_journals_app, name="statistical-journals")
app.add_typer(make_resource_app("bank transfers", "bank-transfer", "bank-transfers", has_number=True, filters=TXN), name="bank-transfers")
app.add_typer(
make_resource_app("fixed assets", "fixed-asset", "fixed-assets", has_number=True, filters={"search", "status", "company", "customer", "vendor"}),
Expand All @@ -72,6 +74,12 @@
app.add_typer(make_resource_app("items", "item", "items", filters={"search", "status"}), name="items")
app.add_typer(make_resource_app("companies", "company", "companies", has_create=False, has_update=False, filters={"search"}), name="companies")
app.add_typer(make_resource_app("classifications", "classification", "classifications", filters={"search"}), name="classifications")
app.add_typer(make_resource_app("custom fields", "custom-field", "custom-fields", has_delete=True, filters={"search"}), name="custom-fields")
app.add_typer(make_resource_app("product tax codes", "product-tax-code", "product-tax-codes", has_delete=True, filters={"search"}), name="product-tax-codes")
app.add_typer(
make_resource_app("VAT rates", "vat-rate", "vat-rates", has_create=False, has_update=False, filters=set()),
name="vat-rates",
)

# Recurring
recurring_app = typer.Typer(help="Manage recurring records", no_args_is_help=True, cls=HelpfulGroup)
Expand Down
Loading
Loading