@davoodmehraban89 / Aram-2 Mirror · read-only / commits / b2750b8
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 4, 15:30 UTC

fix(controls): controls read the same figure as the statement engine

build_statements treats a reclassified row's statement_balance as authoritative and already presentation-signed (ADR-013/018). The R2 context builder used raw closing, so C-06/C-07 would have compared two different rules and invented differences on exactly those rows. BalanceRow now carries statement_balance and one display(sign) method; the context uses it. C-01/C-02 still audit the raw debit/credit columns. Witness: TestReclassifiedRowsAgreeWithTheEngine — engine line total, context note total and context line total all 777 on a row whose CLOSING is 0. ADR-031 §5. Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>

davoodmehraban89 committed Dec 4, 2026 at 10:26 UTC b2750b8ac71035beec9ddd460b39c30803fd6338
4 files changed +79 -4
backend/src/aram_backend/domain/controls.py
+14 -1
@@ -21,7 +21,13 @@ _ZERO = Decimal(0)
21
22 @dataclass(frozen=True)
23 class BalanceRow:
24 - """One ledger balance row (all amounts exact Decimal rial)."""
24 + """One ledger balance row (all amounts exact Decimal rial).
25 +
26 + ``statement_balance`` is the reclassified presentation figure some source rows
27 + carry instead of CLOSING. It is already presentation-signed, so controls that
28 + feed statement comparisons must use it exactly as ``build_statements`` does
29 + (ADR-013/018); C-01/C-02 still audit the raw debit/credit columns.
30 + """
31
32 row_number: int
33 account_code: str
@@ -29,6 +35,13 @@ class BalanceRow:
35 debit: Decimal
36 credit: Decimal
37 closing: Decimal
38 + statement_balance: Decimal | None = None
39 +
40 + def display(self, sign: Decimal) -> Decimal:
41 + """The figure a statement line would show for this row under ``sign``."""
42 + if self.statement_balance is not None:
43 + return self.statement_balance
44 + return self.closing * sign
45
46
47 @dataclass(frozen=True)
backend/src/aram_backend/services/control_context.py
+4 -1
@@ -60,6 +60,7 @@ def load_rows(session: Session, year_id: int) -> list[BalanceRow]:
60 debit=b.debit,
61 credit=b.credit,
62 closing=b.closing,
63 + statement_balance=b.statement_balance,
64 )
65 for b in balances
66 ]
@@ -138,9 +139,11 @@ def _signed_account_totals(
139 sign = Decimal(policy.get(section, 1))
140 key = f"{statement}:{note.note_number}"
141 bucket = totals.setdefault(key, {"opening": _ZERO, "changes": _ZERO, "closing": _ZERO})
142 + # Same rule as build_statements: a reclassified row's presentation
143 + # figure is authoritative and already signed (ADR-013/018).
144 bucket["opening"] += row.opening * sign
145 bucket["changes"] += (row.debit - row.credit) * sign
143 - bucket["closing"] += row.closing * sign
146 + bucket["closing"] += row.display(sign)
147 sections[key] = section
148 return totals, sections
149
backend/tests/test_controls_r2.py
+47 -1
@@ -30,7 +30,7 @@ from aram_backend.domain.controls import (
30 )
31 from aram_backend.services.approval import approve_year
32 from aram_backend.services.control_context import build_control_context
33 -from test_approval_gate import _balanced
33 +from test_approval_gate import _balanced, _carry
34 from test_statement_build import _setup
35
36 D = Decimal
@@ -346,3 +346,49 @@ class TestCompleteYearThroughApi:
346 },
347 )
348 assert bad.status_code == 422
349 +
350 +
351 +class TestReclassifiedRowsAgreeWithTheEngine:
352 + """C-06/C-07 must use the same figure `build_statements` uses.
353 +
354 + A reclassified source row carries only `statement_balance` (already
355 + presentation-signed) and leaves CLOSING empty. Using raw closing in the
356 + control context would compare two different rules and invent a difference.
357 + """
358 +
359 + def test_display_prefers_the_pre_signed_statement_balance(self):
360 + row = BalanceRow(1, "148348", D(0), D(0), D(0), D(0), statement_balance=D(-250))
361 + assert row.display(D(1)) == D(-250)
362 + assert row.display(D(-1)) == D(-250)
363 +
364 + def test_display_signs_ordinary_closing(self):
365 + row = BalanceRow(1, "241001", D(0), D(0), D(60), D(-60))
366 + assert row.display(D(-1)) == D(60)
367 +
368 + def test_context_and_engine_agree_on_a_reclassified_row(self, session):
369 + from sqlalchemy import select
370 +
371 + from aram_backend.models import LedgerBalance, StatementLine
372 + from aram_backend.services.statement_build import build_statements
373 + from test_statement_build import _map, _note, _setup
374 +
375 + year, source = _setup(session)
376 + _carry(session, year, source, "148348", D(0), D(0), 3)
377 + row = session.scalar(
378 + select(LedgerBalance).where(
379 + LedgerBalance.year_id == year.id, LedgerBalance.account_full_code == "148348"
380 + )
381 + )
382 + row.statement_balance = D(777)
383 + note = _note(session, year, "6", "نقد", "BS", "Assets")
384 + _map(session, year, "148348", note)
385 + session.flush()
386 + build_statements(session, year.id)
387 + session.flush()
388 +
389 + line = session.scalar(select(StatementLine).where(StatementLine.target_note_id == note.id))
390 + ctx = build_control_context(session, year.id)
391 + assert line.mapped_total == D(777)
392 + assert ctx.note_totals["BS:6"] == D(777)
393 + assert ctx.line_totals["BS:6"] == D(777)
394 + assert check_c07(ctx.note_totals, ctx.line_totals).passed
docs/adr/ADR-031-r2-full-control-set.md
+14 -1
@@ -52,7 +52,20 @@ hash to prove what was committed to. This reverses the earlier
52 "blocked approval writes no audit" expectation; the old test was updated, not
53 deleted.
54
55 -## Decision 5 — the API must be able to reach an approvable year
55 +## Decision 5 — controls and the engine must read the same figure
56 +
57 +`build_statements` treats a reclassified row's `statement_balance` as authoritative
58 +and already presentation-signed, falling back to `sign × closing` otherwise
59 +(ADR-013/018). The first R2 context builder used raw `closing`, which would have
60 +made C-06/C-07 compare two different rules and invent differences on exactly those
61 +rows. `BalanceRow` now carries `statement_balance` and a single `display(sign)`
62 +method implementing that rule, and the context uses it. C-01/C-02 still audit the
63 +raw debit/credit columns, because that is what they are for.
64 +
65 +Witness: `TestReclassifiedRowsAgreeWithTheEngine` — the engine's line total, the
66 +context's note total and its line total are all 777 on a row whose CLOSING is 0.
67 +
68 +## Decision 6 — the API must be able to reach an approvable year
69
70 Requiring C-05 without a way to load a prior year would make approval impossible
71 forever. `POST /companies/{company_id}/years` adds a fiscal year to an existing