Skip to content

Commit 7d87d12

Browse files
authored
feat: get_aged_balances, get_open_items_for_agent, get_discharge_candidates (#268)
## Summary Adds `LedgerClient` methods for the AR/AP aging reads, matching the TypeScript client's `getAgedBalances` / `getOpenItemsForAgent` / `getDischargeCandidates`. The server fields shipped in robosystems #1886 and #1887, both merged and not yet in a release (v1.19.2 predates them). Against an older server these queries fail validation. ## Changes **Hand-written** - `operations/ledger/`: `AgedReceivables.graphql`, `AgedPayables.graphql`, `OpenItemsForAgent.graphql`, `DischargeCandidates.graphql`. Each is self-contained, with no cross-file fragments, since every document is validated on its own against `schema.graphql`. - `LedgerClient`: - `get_aged_balances(graph_id, kind, *, as_of, entity_id, scope)` routes to `agedReceivables` or `agedPayables`; both return the same fields. - `get_open_items_for_agent(graph_id, agent_id, kind, *, as_of, entity_id)`. - `get_discharge_candidates(graph_id, event_id)`, which returns `None` for no such event. - Recording a settlement needs no new method: `update_event_block` already takes `discharge_allocations` since the last regen. **Generated:** `graphql/generated/` (ariadne-codegen, hermetic from the checked-in `schema.graphql`): the four operation modules and the `operations.py` / `client.py` / `__init__.py` entries. ## Compatibility ADDITIVE: three new methods and generated modules. No signature, export or existing model changed. ## Testing - `just test-all`: 707 passed, 17 skipped; ruff check and format clean; basedpyright 0 errors. - 4 new facade tests, plus the schema validation of the four new documents. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_013UXnatpnbiixt1ZVHfdZAP
2 parents 9e76617 + 7270867 commit 7d87d12

13 files changed

Lines changed: 986 additions & 1 deletion

‎robosystems_client/clients/ledger_client.py‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from dataclasses import dataclass
3030
from http import HTTPStatus
3131
from pathlib import Path
32-
from typing import Any
32+
from typing import Any, Literal
3333

3434
import httpx
3535

@@ -468,6 +468,19 @@
468468
ClassificationStats,
469469
ClassificationStatsClassificationStats,
470470
)
471+
from ..graphql.generated.aged_payables import AgedPayables, AgedPayablesAgedPayables
472+
from ..graphql.generated.aged_receivables import (
473+
AgedReceivables,
474+
AgedReceivablesAgedReceivables,
475+
)
476+
from ..graphql.generated.discharge_candidates import (
477+
DischargeCandidates,
478+
DischargeCandidatesDischargeCandidates,
479+
)
480+
from ..graphql.generated.open_items_for_agent import (
481+
OpenItemsForAgent,
482+
OpenItemsForAgentOpenItemsForAgent,
483+
)
471484
from ..graphql.generated.operations import (
472485
GET_INFORMATION_BLOCK_GQL,
473486
GET_LEDGER_ACCOUNT_ROLLUPS_GQL,
@@ -513,6 +526,10 @@
513526
MAPPING_CANDIDATES_GQL,
514527
FLOW_CANDIDATES_GQL,
515528
CLASSIFICATION_STATS_GQL,
529+
AGED_RECEIVABLES_GQL,
530+
AGED_PAYABLES_GQL,
531+
OPEN_ITEMS_FOR_AGENT_GQL,
532+
DISCHARGE_CANDIDATES_GQL,
516533
)
517534
from ..models.add_publish_list_members_operation import AddPublishListMembersOperation
518535
from ..models.auto_map_elements_operation import AutoMapElementsOperation
@@ -2424,6 +2441,68 @@ def get_classification_stats(
24242441
)
24252442
return ClassificationStats.model_validate(data).classification_stats
24262443

2444+
# ── Receivables and payables ──────────────────────────────────────────
2445+
2446+
def get_aged_balances(
2447+
self,
2448+
graph_id: str,
2449+
kind: Literal["receivables", "payables"],
2450+
*,
2451+
as_of: str | None = None,
2452+
entity_id: str | None = None,
2453+
scope: Literal["entity", "group"] = "entity",
2454+
) -> AgedReceivablesAgedReceivables | AgedPayablesAgedPayables:
2455+
"""Receivables or payables aged at a date (``as_of``, ``YYYY-MM-DD``,
2456+
default today on the server): totals by days past due, a row per
2457+
counterparty, and each entity's open items tied out to its receivables
2458+
or payables account (``difference_cents``, never plugged). ``scope`` is
2459+
``entity`` (one entity's books, ``entity_id`` default the group parent)
2460+
or ``group`` (the parent and its members consolidated in full, with what
2461+
they owe each other in ``intercompany`` and ``pairs``). An item with no
2462+
due date of its own is due on receipt and counted in
2463+
``defaulted_due_date_count``. Both kinds return the same fields.
2464+
"""
2465+
variables = {"asOf": as_of, "entityId": entity_id, "scope": scope}
2466+
if kind == "receivables":
2467+
data = self._query(graph_id, AGED_RECEIVABLES_GQL, variables)
2468+
return AgedReceivables.model_validate(data).aged_receivables
2469+
data = self._query(graph_id, AGED_PAYABLES_GQL, variables)
2470+
return AgedPayables.model_validate(data).aged_payables
2471+
2472+
def get_open_items_for_agent(
2473+
self,
2474+
graph_id: str,
2475+
agent_id: str,
2476+
kind: Literal["receivables", "payables"],
2477+
*,
2478+
as_of: str | None = None,
2479+
entity_id: str | None = None,
2480+
) -> list[OpenItemsForAgentOpenItemsForAgent]:
2481+
"""One counterparty's open invoices (``receivables``) or bills
2482+
(``payables``) at a date, oldest first, each with its due date, days
2483+
past due and bucket: the drill from an aged row into its documents.
2484+
"""
2485+
data = self._query(
2486+
graph_id,
2487+
OPEN_ITEMS_FOR_AGENT_GQL,
2488+
{"agentId": agent_id, "kind": kind, "asOf": as_of, "entityId": entity_id},
2489+
)
2490+
return list(OpenItemsForAgent.model_validate(data).open_items_for_agent)
2491+
2492+
def get_discharge_candidates(
2493+
self, graph_id: str, event_id: str
2494+
) -> DischargeCandidatesDischargeCandidates | None:
2495+
"""The open invoices or bills a payment or bank line may settle: its own
2496+
entity's and counterparty's, open at its date, an exact amount match
2497+
first, then the oldest due date. ``classified_to_control`` says the line
2498+
went to a receivables or payables account, where a pick is expected.
2499+
Record the choice with ``discharge_allocations`` on
2500+
``update_event_block``; nothing settles without one. ``None`` when there
2501+
is no such event.
2502+
"""
2503+
data = self._query(graph_id, DISCHARGE_CANDIDATES_GQL, {"eventId": event_id})
2504+
return DischargeCandidates.model_validate(data).discharge_candidates
2505+
24272506
# ── Event handlers (DSL handler registry) ────────────────────────────
24282507

24292508
def create_event_handler(

‎robosystems_client/graphql/generated/__init__.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
from .aged_payables import (
2+
AgedPayables,
3+
AgedPayablesAgedPayables,
4+
AgedPayablesAgedPayablesAgents,
5+
AgedPayablesAgedPayablesAgentsBuckets,
6+
AgedPayablesAgedPayablesBuckets,
7+
AgedPayablesAgedPayablesIntercompany,
8+
AgedPayablesAgedPayablesPairs,
9+
AgedPayablesAgedPayablesTieOuts,
10+
AgedPayablesAgedPayablesTieOutsAccounts,
11+
)
12+
from .aged_receivables import (
13+
AgedReceivables,
14+
AgedReceivablesAgedReceivables,
15+
AgedReceivablesAgedReceivablesAgents,
16+
AgedReceivablesAgedReceivablesAgentsBuckets,
17+
AgedReceivablesAgedReceivablesBuckets,
18+
AgedReceivablesAgedReceivablesIntercompany,
19+
AgedReceivablesAgedReceivablesPairs,
20+
AgedReceivablesAgedReceivablesTieOuts,
21+
AgedReceivablesAgedReceivablesTieOutsAccounts,
22+
)
123
from .base_client import BaseClient
224
from .base_model import BaseModel, Upload
325
from .classification_stats import (
@@ -6,6 +28,11 @@
628
ClassificationStatsClassificationStatsBySource,
729
)
830
from .client import Client
31+
from .discharge_candidates import (
32+
DischargeCandidates,
33+
DischargeCandidatesDischargeCandidates,
34+
DischargeCandidatesDischargeCandidatesCandidates,
35+
)
936
from .enums import ReportDownloadFormat, ReportLifecycle
1037
from .exceptions import (
1138
GraphQLClientError,
@@ -351,8 +378,12 @@
351378
ListLibraryTaxonomyArcsLibraryTaxonomyArcs,
352379
)
353380
from .mapping_candidates import MappingCandidates, MappingCandidatesMappingCandidates
381+
from .open_items_for_agent import OpenItemsForAgent, OpenItemsForAgentOpenItemsForAgent
354382
from .operations import (
383+
AGED_PAYABLES_GQL,
384+
AGED_RECEIVABLES_GQL,
355385
CLASSIFICATION_STATS_GQL,
386+
DISCHARGE_CANDIDATES_GQL,
356387
FLOW_CANDIDATES_GQL,
357388
GET_INFORMATION_BLOCK_GQL,
358389
GET_INVESTOR_HOLDINGS_GQL,
@@ -412,6 +443,7 @@
412443
LIST_LIBRARY_TAXONOMIES_GQL,
413444
LIST_LIBRARY_TAXONOMY_ARCS_GQL,
414445
MAPPING_CANDIDATES_GQL,
446+
OPEN_ITEMS_FOR_AGENT_GQL,
415447
SEARCH_LIBRARY_ELEMENTS_GQL,
416448
)
417449
from .search_library_elements import (
@@ -422,13 +454,37 @@
422454
)
423455

424456
__all__ = [
457+
"AGED_PAYABLES_GQL",
458+
"AGED_RECEIVABLES_GQL",
459+
"AgedPayables",
460+
"AgedPayablesAgedPayables",
461+
"AgedPayablesAgedPayablesAgents",
462+
"AgedPayablesAgedPayablesAgentsBuckets",
463+
"AgedPayablesAgedPayablesBuckets",
464+
"AgedPayablesAgedPayablesIntercompany",
465+
"AgedPayablesAgedPayablesPairs",
466+
"AgedPayablesAgedPayablesTieOuts",
467+
"AgedPayablesAgedPayablesTieOutsAccounts",
468+
"AgedReceivables",
469+
"AgedReceivablesAgedReceivables",
470+
"AgedReceivablesAgedReceivablesAgents",
471+
"AgedReceivablesAgedReceivablesAgentsBuckets",
472+
"AgedReceivablesAgedReceivablesBuckets",
473+
"AgedReceivablesAgedReceivablesIntercompany",
474+
"AgedReceivablesAgedReceivablesPairs",
475+
"AgedReceivablesAgedReceivablesTieOuts",
476+
"AgedReceivablesAgedReceivablesTieOutsAccounts",
425477
"BaseClient",
426478
"BaseModel",
427479
"CLASSIFICATION_STATS_GQL",
428480
"ClassificationStats",
429481
"ClassificationStatsClassificationStats",
430482
"ClassificationStatsClassificationStatsBySource",
431483
"Client",
484+
"DISCHARGE_CANDIDATES_GQL",
485+
"DischargeCandidates",
486+
"DischargeCandidatesDischargeCandidates",
487+
"DischargeCandidatesDischargeCandidatesCandidates",
432488
"FLOW_CANDIDATES_GQL",
433489
"FlowCandidates",
434490
"FlowCandidatesFlowCandidates",
@@ -742,6 +798,9 @@
742798
"MAPPING_CANDIDATES_GQL",
743799
"MappingCandidates",
744800
"MappingCandidatesMappingCandidates",
801+
"OPEN_ITEMS_FOR_AGENT_GQL",
802+
"OpenItemsForAgent",
803+
"OpenItemsForAgentOpenItemsForAgent",
745804
"ReportDownloadFormat",
746805
"ReportLifecycle",
747806
"SEARCH_LIBRARY_ELEMENTS_GQL",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from typing import Optional
2+
3+
from pydantic import Field
4+
5+
from .base_model import BaseModel
6+
7+
8+
class AgedPayables(BaseModel):
9+
aged_payables: "AgedPayablesAgedPayables" = Field(alias="agedPayables")
10+
11+
12+
class AgedPayablesAgedPayables(BaseModel):
13+
kind: str
14+
as_of: str = Field(alias="asOf")
15+
scope: str
16+
entity_id: Optional[str] = Field(alias="entityId")
17+
member_entity_ids: list[str] = Field(alias="memberEntityIds")
18+
buckets: "AgedPayablesAgedPayablesBuckets"
19+
agents: list["AgedPayablesAgedPayablesAgents"]
20+
defaulted_due_date_count: int = Field(alias="defaultedDueDateCount")
21+
intercompany: list["AgedPayablesAgedPayablesIntercompany"]
22+
pairs: list["AgedPayablesAgedPayablesPairs"]
23+
tie_outs: list["AgedPayablesAgedPayablesTieOuts"] = Field(alias="tieOuts")
24+
25+
26+
class AgedPayablesAgedPayablesBuckets(BaseModel):
27+
current_cents: int = Field(alias="currentCents")
28+
one_to_thirty_cents: int = Field(alias="oneToThirtyCents")
29+
thirty_one_to_sixty_cents: int = Field(alias="thirtyOneToSixtyCents")
30+
sixty_one_to_ninety_cents: int = Field(alias="sixtyOneToNinetyCents")
31+
over_ninety_cents: int = Field(alias="overNinetyCents")
32+
unapplied_credit_cents: int = Field(alias="unappliedCreditCents")
33+
total_cents: int = Field(alias="totalCents")
34+
35+
36+
class AgedPayablesAgedPayablesAgents(BaseModel):
37+
agent_id: Optional[str] = Field(alias="agentId")
38+
agent_name: Optional[str] = Field(alias="agentName")
39+
open_item_count: int = Field(alias="openItemCount")
40+
oldest_due_date: Optional[str] = Field(alias="oldestDueDate")
41+
buckets: "AgedPayablesAgedPayablesAgentsBuckets"
42+
43+
44+
class AgedPayablesAgedPayablesAgentsBuckets(BaseModel):
45+
current_cents: int = Field(alias="currentCents")
46+
one_to_thirty_cents: int = Field(alias="oneToThirtyCents")
47+
thirty_one_to_sixty_cents: int = Field(alias="thirtyOneToSixtyCents")
48+
sixty_one_to_ninety_cents: int = Field(alias="sixtyOneToNinetyCents")
49+
over_ninety_cents: int = Field(alias="overNinetyCents")
50+
unapplied_credit_cents: int = Field(alias="unappliedCreditCents")
51+
total_cents: int = Field(alias="totalCents")
52+
53+
54+
class AgedPayablesAgedPayablesIntercompany(BaseModel):
55+
event_id: str = Field(alias="eventId")
56+
entity_id: str = Field(alias="entityId")
57+
agent_id: Optional[str] = Field(alias="agentId")
58+
occurred_on: str = Field(alias="occurredOn")
59+
document_number: Optional[str] = Field(alias="documentNumber")
60+
description: Optional[str]
61+
original_cents: int = Field(alias="originalCents")
62+
settled_cents: int = Field(alias="settledCents")
63+
open_cents: int = Field(alias="openCents")
64+
currency: str
65+
due_date: str = Field(alias="dueDate")
66+
due_date_basis: str = Field(alias="dueDateBasis")
67+
days_past_due: int = Field(alias="daysPastDue")
68+
bucket: str
69+
70+
71+
class AgedPayablesAgedPayablesPairs(BaseModel):
72+
creditor_entity_id: str = Field(alias="creditorEntityId")
73+
debtor_entity_id: str = Field(alias="debtorEntityId")
74+
receivable_cents: int = Field(alias="receivableCents")
75+
payable_cents: int = Field(alias="payableCents")
76+
difference_cents: int = Field(alias="differenceCents")
77+
78+
79+
class AgedPayablesAgedPayablesTieOuts(BaseModel):
80+
entity_id: str = Field(alias="entityId")
81+
accounts: list["AgedPayablesAgedPayablesTieOutsAccounts"]
82+
control_balance_cents: Optional[int] = Field(alias="controlBalanceCents")
83+
open_items_cents: int = Field(alias="openItemsCents")
84+
difference_cents: Optional[int] = Field(alias="differenceCents")
85+
86+
87+
class AgedPayablesAgedPayablesTieOutsAccounts(BaseModel):
88+
element_id: str = Field(alias="elementId")
89+
code: Optional[str]
90+
name: str
91+
92+
93+
AgedPayables.model_rebuild()
94+
AgedPayablesAgedPayables.model_rebuild()
95+
AgedPayablesAgedPayablesAgents.model_rebuild()
96+
AgedPayablesAgedPayablesTieOuts.model_rebuild()

0 commit comments

Comments
 (0)