Files
jarca-ledger-mcp/jarca_ledger_mcp/adapters/streamlit/settlement_gateway.py
T

109 lines
6.1 KiB
Python

from __future__ import annotations
import inspect
from ...service import (
approve_settlement_draft as approve_settlement_draft,
cancel_settlement_draft as cancel_settlement_draft,
correct_account_valuation as correct_account_valuation,
create_settlement_draft as create_settlement_draft,
finalize_settlement_draft as finalize_settlement_draft,
get_settlement_draft as get_settlement_draft,
get_monthly_settlement_snapshot as get_monthly_settlement_snapshot,
list_monthly_settlement_history as list_monthly_settlement_history,
get_monthly_settlement_history_record as get_monthly_settlement_history_record,
get_monthly_settlement_record as get_monthly_settlement_record,
list_account_snapshots_for_settlement as list_account_snapshots_for_settlement,
link_account_snapshot as link_account_snapshot,
unlink_account_snapshot as unlink_account_snapshot,
update_account_snapshot_category as update_account_snapshot_category,
list_account_valuations as list_account_valuations,
list_settlement_drafts as list_settlement_drafts,
refresh_settlement_draft_targets as refresh_settlement_draft_targets,
register_account_valuation as register_account_valuation,
review_settlement_draft as review_settlement_draft,
set_settlement_draft_item as set_settlement_draft_item,
set_settlement_cashflow_policy as set_settlement_cashflow_policy,
update_settlement_draft as update_settlement_draft,
)
from ...application import settlement_draft_service as _draft_service
from ...application import monthly_settlement_service as _monthly_service
from ...application.transaction_report_services import get_monthly_cashflow_report as get_monthly_cashflow_report
from ...application.transaction_report_services import get_settlement_cashflow_snapshot as get_settlement_cashflow_snapshot
from ... import service as _service
def _resolve_db_path() -> str:
from ...db import resolve_db_path
return str(resolve_db_path())
def _guard(db_path: str | None) -> str:
return db_path or _resolve_db_path()
def create_monthly_settlement(month: str, created_by: str = "dashboard-admin", *, db_path=None):
return _monthly_service.create_monthly_settlement(month, created_by=created_by, db_path=_guard(db_path))
def get_monthly_cashflow(month: str, *, settlement_id=None, closed=False, db_path=None):
if closed:
return get_settlement_cashflow_snapshot(str(settlement_id), db_path=_guard(db_path))
return get_monthly_cashflow_report(month, db_path=_guard(db_path))
def review_monthly_settlement(settlement_id: str, *, db_path=None):
return _monthly_service.review_monthly_settlement(settlement_id, db_path=_guard(db_path))
def approve_monthly_settlement(settlement_id: str, expected_version: int, review_fingerprint: str, approved_by: str = "dashboard-admin", *, db_path=None):
return _monthly_service.approve_monthly_settlement(settlement_id, expected_version, review_fingerprint, approved_by=approved_by, db_path=_guard(db_path))
def close_monthly_settlement(settlement_id: str, expected_version: int, closed_by: str = "dashboard-admin", *, db_path=None):
return _monthly_service.close_monthly_settlement(settlement_id, expected_version, closed_by=closed_by, db_path=_guard(db_path))
def cancel_monthly_settlement_approval(settlement_id: str, expected_version: int, approved_by: str = "dashboard-admin", *, db_path=None):
return _monthly_service.cancel_monthly_settlement_approval(settlement_id, expected_version, approved_by=approved_by, db_path=_guard(db_path))
def _wrap(fn):
def call(*args, db_path=None, **kwargs):
if fn.__name__ == "set_settlement_cashflow_policy":
target = _draft_service
elif fn.__name__ in {"get_monthly_settlement_snapshot", "list_monthly_settlement_history", "get_monthly_settlement_history_record", "get_monthly_settlement_record", "list_account_snapshots_for_settlement", "link_account_snapshot", "unlink_account_snapshot"}:
target = _monthly_service
else:
target = _draft_service if fn.__name__ in {"create_settlement_draft", "list_settlement_drafts", "get_settlement_draft", "update_settlement_draft", "refresh_settlement_draft_targets", "set_settlement_draft_item", "review_settlement_draft", "approve_settlement_draft", "finalize_settlement_draft", "cancel_settlement_draft"} else _service
return getattr(target, fn.__name__)(*args, db_path=_guard(db_path), **kwargs)
call.__name__ = fn.__name__
call.__doc__ = fn.__doc__
call.__signature__ = inspect.signature(fn)
return call
# Keep the service signatures unchanged; path resolution/auth is adapter policy.
for _name in (
"create_settlement_draft", "list_settlement_drafts", "get_settlement_draft",
"update_settlement_draft", "refresh_settlement_draft_targets",
"set_settlement_draft_item", "review_settlement_draft",
"approve_settlement_draft", "finalize_settlement_draft", "cancel_settlement_draft",
"cancel_monthly_settlement_approval",
"register_account_valuation", "list_account_valuations", "correct_account_valuation",
"set_settlement_cashflow_policy", "get_monthly_settlement_snapshot",
"list_monthly_settlement_history", "get_monthly_settlement_history_record", "get_monthly_settlement_record", "list_account_snapshots_for_settlement", "link_account_snapshot", "unlink_account_snapshot", "update_account_snapshot_category",
):
globals()[_name] = _wrap(getattr(_service, _name))
__all__ = [
"create_settlement_draft", "list_settlement_drafts", "get_settlement_draft",
"update_settlement_draft", "refresh_settlement_draft_targets",
"set_settlement_draft_item", "review_settlement_draft",
"approve_settlement_draft", "finalize_settlement_draft", "cancel_settlement_draft",
"cancel_monthly_settlement_approval",
"register_account_valuation", "list_account_valuations", "correct_account_valuation",
"set_settlement_cashflow_policy", "get_monthly_settlement_snapshot",
"list_monthly_settlement_history", "get_monthly_settlement_history_record", "get_monthly_settlement_record", "list_account_snapshots_for_settlement",
"get_monthly_cashflow",
]